This commit is contained in:
Aevann1 2021-07-30 12:43:25 +02:00
parent f8744da4c2
commit c604bd0388
3 changed files with 16 additions and 17 deletions

View file

@ -1,6 +1,6 @@
import requests import requests
from os import environ from os import environ
from PIL import Image as IImage from PIL import Image as IImage, ImageSequence
import base64 import base64
import io import io
from drama.classes.images import * from drama.classes.images import *
@ -10,28 +10,27 @@ CF_ZONE = environ.get("CLOUDFLARE_ZONE").strip()
imgurkey = environ.get("imgurkey").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") if file: file.save("image.gif")
i = IImage.open("image.gif")
if resize: if resize:
org_ratio = i.width / i.height i = IImage.open("image.gif")
new_ratio = resize[0] / resize[1] frames = ImageSequence.Iterator(i)
if new_ratio > org_ratio: def thumbnails(frames):
crop_height = int(i.width / new_ratio) for frame in frames:
box = (0, (i.height // 2) - (crop_height // 2), thumbnail = frame.copy()
i.width, (i.height // 2) + (crop_height // 2)) thumbnail.thumbnail(100, 100, IImage.ANTIALIAS)
else: yield thumbnail
crop_width = int(new_ratio * i.height)
box = ((i.width // 2) - (crop_width // 2), 0,
(i.width // 2) + (crop_width // 2), i.height)
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() img = io.BytesIO()
i.save(img, format='GIF') 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())}) req = requests.post('https://api.imgur.com/3/upload.json', headers = {"Authorization": f"Client-ID {imgurkey}"}, data = {'image': base64.b64encode(img.getvalue())})

View file

@ -535,7 +535,7 @@ def thumbs(new_post):
for chunk in image_req.iter_content(1024): for chunk in image_req.iter_content(1024):
file.write(chunk) file.write(chunk)
post.thumburl = upload_file(resize=(100, 100)) post.thumburl = upload_file(resize=True)
g.db.add(post) g.db.add(post)
g.db.commit() g.db.commit()

View file

@ -331,7 +331,7 @@ def settings_images_profile(v):
g.db.rollback() g.db.rollback()
abort(413) abort(413)
imageurl = upload_file(request.files["profile"], (100,100)) imageurl = upload_file(request.files["profile"], True)
if not imageurl: abort(400) if not imageurl: abort(400)
highres = upload_file() highres = upload_file()
if not highres: abort(400) if not highres: abort(400)