aboutsummaryrefslogtreecommitdiff
path: root/app/views/thumbnails.py
diff options
context:
space:
mode:
authorrubenwardy <rw@rubenwardy.com>2018-12-21 21:22:15 +0000
committerrubenwardy <rw@rubenwardy.com>2018-12-22 21:20:25 +0000
commit31f57e1f1238a5e410f6304b72de7584a4f6d9b5 (patch)
tree6a5a4634a2e8023702c516bdcd97ee78c2cb90f1 /app/views/thumbnails.py
parent89cae279cd0b31cd257e3983a4f2b285beb719e1 (diff)
downloadcheatdb-31f57e1f1238a5e410f6304b72de7584a4f6d9b5.tar.xz
Add multi-level thumbnails
Diffstat (limited to 'app/views/thumbnails.py')
-rw-r--r--app/views/thumbnails.py16
1 files changed, 9 insertions, 7 deletions
diff --git a/app/views/thumbnails.py b/app/views/thumbnails.py
index 19ab0be..8303067 100644
--- a/app/views/thumbnails.py
+++ b/app/views/thumbnails.py
@@ -21,7 +21,7 @@ from app import app
import os
from PIL import Image
-ALLOWED_RESOLUTIONS=[(350,233)]
+ALLOWED_RESOLUTIONS=[(100,67), (270,180), (350,233)]
def mkdir(path):
if not os.path.isdir(path):
@@ -56,15 +56,17 @@ def resize_and_crop(img_path, modified_path, size):
img.save(modified_path)
-@app.route("/thumbnails/<img>")
-@app.route("/thumbnails/<int:w>x<int:h>/<img>")
-def make_thumbnail(img, w=350, h=233):
- if not (w, h) in ALLOWED_RESOLUTIONS:
+
+@app.route("/thumbnails/<int:level>/<img>")
+def make_thumbnail(img, level):
+ if level > len(ALLOWED_RESOLUTIONS) or level <= 0:
abort(403)
- mkdir("app/public/thumbnails/{}x{}/".format(w, h))
+ w, h = ALLOWED_RESOLUTIONS[level - 1]
+
+ mkdir("app/public/thumbnails/{:d}/".format(level))
- cache_filepath = "public/thumbnails/{}x{}/{}".format(w, h, img)
+ cache_filepath = "public/thumbnails/{:d}/{}".format(level, img)
source_filepath = "public/uploads/" + img
resize_and_crop("app/" + source_filepath, "app/" + cache_filepath, (w, h))