diff --git a/drama/helpers/images.py b/drama/helpers/images.py index 763c4530af..aa7909972e 100644 --- a/drama/helpers/images.py +++ b/drama/helpers/images.py @@ -1,6 +1,6 @@ import requests from os import environ -from PIL import Image as IImage +from PIL import Image as IImage, ImageSequence import base64 import io from drama.classes.images import * @@ -10,28 +10,27 @@ CF_ZONE = environ.get("CLOUDFLARE_ZONE").strip() imgurkey = environ.get("imgurkey").strip() -def upload_file(file=None, resize=None): +def upload_file(file=None, resize=False): if file: file.save("image.gif") - i = IImage.open("image.gif") - if resize: - org_ratio = i.width / i.height - new_ratio = resize[0] / resize[1] + i = IImage.open("image.gif") + frames = ImageSequence.Iterator(i) - if new_ratio > org_ratio: - crop_height = int(i.width / new_ratio) - box = (0, (i.height // 2) - (crop_height // 2), - i.width, (i.height // 2) + (crop_height // 2)) - else: - crop_width = int(new_ratio * i.height) - box = ((i.width // 2) - (crop_width // 2), 0, - (i.width // 2) + (crop_width // 2), i.height) + def thumbnails(frames): + for frame in frames: + thumbnail = frame.copy() + thumbnail.thumbnail(100, 100, IImage.ANTIALIAS) + yield thumbnail - i = i.resize(resize, box=box) + frames = thumbnails(frames) + om = next(frames) + om.info = i.info + om.save("image.gif", save_all=True, append_images=list(frames)) + i = IImage.open("image.gif") img = io.BytesIO() i.save(img, format='GIF') req = requests.post('https://api.imgur.com/3/upload.json', headers = {"Authorization": f"Client-ID {imgurkey}"}, data = {'image': base64.b64encode(img.getvalue())}) diff --git a/drama/routes/posts.py b/drama/routes/posts.py index cf953f2d4a..131e8827e1 100644 --- a/drama/routes/posts.py +++ b/drama/routes/posts.py @@ -535,7 +535,7 @@ def thumbs(new_post): for chunk in image_req.iter_content(1024): file.write(chunk) - post.thumburl = upload_file(resize=(100, 100)) + post.thumburl = upload_file(resize=True) g.db.add(post) g.db.commit() diff --git a/drama/routes/settings.py b/drama/routes/settings.py index 47b4b66764..1eca418ef6 100644 --- a/drama/routes/settings.py +++ b/drama/routes/settings.py @@ -331,7 +331,7 @@ def settings_images_profile(v): g.db.rollback() abort(413) - imageurl = upload_file(request.files["profile"], (100,100)) + imageurl = upload_file(request.files["profile"], True) if not imageurl: abort(400) highres = upload_file() if not highres: abort(400)