From 5152af4126fb611f1e4c9d9c35e57324d79de630 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anders=20Engl=C3=B6f=20Ytterstr=C3=B6m?= Date: Tue, 3 Sep 2024 00:52:21 +0200 Subject: [PATCH] Create placeany flask project --- .gitignore | 4 +++ LICENSE | 5 +++ requirements.txt | 12 +++++++ templates/bookmarklet.html | 64 ++++++++++++++++++++++++++++++++++ templates/index.html | 70 ++++++++++++++++++++++++++++++++++++++ wsgi.py | 66 +++++++++++++++++++++++++++++++++++ 6 files changed, 221 insertions(+) create mode 100644 .gitignore create mode 100644 LICENSE create mode 100644 requirements.txt create mode 100644 templates/bookmarklet.html create mode 100644 templates/index.html create mode 100644 wsgi.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b4627f8 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +venv +__pycache__ +.ropeproject +images diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..65628d4 --- /dev/null +++ b/LICENSE @@ -0,0 +1,5 @@ +"THE FADEOEL LICENSE" (Revision 4): +Anders Englöf Ytterström wrote this file. As long as you retain this notice +you can do whatever you want with this stuff, but do know that any +problem is YOUR problem, not hers/his/theirs. If you meet some day, and you +think this stuff is worth it, you must buy her/him/them a beer in return. diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..9c6b9af --- /dev/null +++ b/requirements.txt @@ -0,0 +1,12 @@ +blinker==1.8.2 +cachelib==0.9.0 +click==8.1.7 +Flask==3.0.3 +Flask-Cache==0.13.1 +Flask-Caching==2.3.0 +itsdangerous==2.2.0 +Jinja2==3.1.4 +MarkupSafe==2.1.5 +pillow==10.4.0 +waitress==3.0.0 +Werkzeug==3.0.4 diff --git a/templates/bookmarklet.html b/templates/bookmarklet.html new file mode 100644 index 0000000..db26364 --- /dev/null +++ b/templates/bookmarklet.html @@ -0,0 +1,64 @@ + + + + + + placeany bookmarklet + + + + +
+
+

placeany bookmarklet

+

A bookmarklet which add your flavor of choice to any website by replacing images with placeholder images from + this placeany instance.

+ +
+
+

Instructions

+
    +
  1. Drag bookmarklet to your bookmarks bar.
    (You may need to go to your browser's view menu and make + sure the + Bookmarks toolbar is displayed.)
  2. +
  3. Navigate to any website.
    (The more images, the better.)
  4. +
  5. Click the bookmark.
  6. +
+
+
+
+ + + + \ No newline at end of file diff --git a/templates/index.html b/templates/index.html new file mode 100644 index 0000000..1db0a68 --- /dev/null +++ b/templates/index.html @@ -0,0 +1,70 @@ + + + + + + placeany + + + + +
+
+

placeany

+

A quick and simple service for getting pictures for use as placeholders in your designs or code. + Just put your image size (width & height) after our URL and you'll get a placeholder.

+
Examples:
+          {{ url }}/200/300
+          {{ url }}/g/200/300
+        
+
+
+ +
+
+
+
+ +
+
+
+ +
+
+ +
+ +
+
+
+ + + + \ No newline at end of file diff --git a/wsgi.py b/wsgi.py new file mode 100644 index 0000000..77e7cb1 --- /dev/null +++ b/wsgi.py @@ -0,0 +1,66 @@ +import os +import random +from io import BytesIO + +from flask import Flask, render_template, request, send_file +from flask_caching import Cache +from PIL import Image, ImageOps + +app = Flask(__name__) + +GREY = "G" +COLOR = "RGB" + +cache = Cache(config={"CACHE_TYPE": "SimpleCache"}) + +app = Flask(__name__) +cache.init_app(app) + + +def get_cropped_image(x, y, grey=False): + """crops a random image from collection""" + im_src = random.choice(os.listdir("./images")) + im = Image.open(f"images/{im_src}") + out = BytesIO() + max_x, max_y = im.size + if x < max_x or y < max_y: + im = ImageOps.fit(im, (x, y)) + if grey: + im = ImageOps.grayscale(im) + im.save(out, "WEBP", quality=50) + out.seek(0) + return out + + +def make_response(x, y, color_mode=COLOR): + im = get_cropped_image(x, y, color_mode == GREY) + return send_file(im, mimetype="image/webp") + + +@app.route("/") +def hello(): + u = request.host + return render_template("index.html", url=u) + + +@app.route("/bookmarklet") +def bookmarklet(): + u = request.host + return render_template("bookmarklet.html", url=u) + + +@app.route("//") +@cache.cached(10) +def generate(x, y): + return make_response(x, y, COLOR) + + +@app.route("/g//") +@cache.cached(10) +def generate_grey(x, y): + return make_response(x, y, GREY) + + +if __name__ == "__main__": + port = int(os.environ.get("PORT", 5000)) + app.run(host="0.0.0.0", port=port)