diff --git a/drama/helpers/images.py b/drama/helpers/images.py index f2a611d02..763c4530a 100644 --- a/drama/helpers/images.py +++ b/drama/helpers/images.py @@ -9,80 +9,43 @@ CF_KEY = environ.get("CLOUDFLARE_KEY").strip() CF_ZONE = environ.get("CLOUDFLARE_ZONE").strip() imgurkey = environ.get("imgurkey").strip() -def crop_and_resize(img, resize): - i = img - - org_ratio = i.width / i.height - new_ratio = resize[0] / resize[1] - - 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) - - return i.resize(resize, box=box) - - -def upload_file(file, resize=None): +def upload_file(file=None, resize=None): - if resize: - file.save("image.gif") - i = IImage.open("image.gif") - i = crop_and_resize(i, resize) - 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())}) - try: resp = req.json()['data'] - except Exception as e: - print(req.text) - return + if file: file.save("image.gif") + i = IImage.open("image.gif") - else: - req = requests.post('https://api.imgur.com/3/upload.json', headers = {"Authorization": f"Client-ID {imgurkey}"}, data = {'image': base64.b64encode(file.read())}) - try: resp = req.json()['data'] - except: - print(req.text) - return + + if resize: + org_ratio = i.width / i.height + new_ratio = resize[0] / resize[1] + + 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) + + i = i.resize(resize, box=box) + + + 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())}) + try: resp = req.json()['data'] + except Exception as e: + print(req.text) + return try: url = resp['link'].replace(".png", "_d.png").replace(".jpg", "_d.jpg").replace(".jpeg", "_d.jpeg") + "?maxwidth=9999" except Exception as e: print(req.text) return - new_image = Image( - text=url, - deletehash=resp["deletehash"], - ) - - g.db.add(new_image) - return(url) - - -def upload_from_file(filename, resize=None): - i = IImage.open(filename) - if resize: i = crop_and_resize(i, resize) - 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())}) - - try: - resp = req.json()['data'] - url = resp['link'].replace(".png", "_d.png").replace(".jpg", "_d.jpg").replace(".jpeg", "_d.jpeg") + "?maxwidth=9999" - except Exception as e: - print(e) - print(req) - print(req.text) - return - - new_image = Image( - text=url, - deletehash=resp["deletehash"], - ) + new_image = Image(text=url, deletehash=resp["deletehash"]) g.db.add(new_image) return(url) \ No newline at end of file diff --git a/drama/routes/admin.py b/drama/routes/admin.py index e55b8d4d4..c1b9d90d9 100644 --- a/drama/routes/admin.py +++ b/drama/routes/admin.py @@ -1146,7 +1146,7 @@ def multiple_plots(**kwargs): posts_chart.legend(loc='upper left', frameon=True) comments_chart.legend(loc='upper left', frameon=True) - plt.savefig("multiplot.png") + plt.savefig("image.gif") plt.clf() - return upload_from_file("multiplot.png") \ No newline at end of file + return upload_file() \ No newline at end of file diff --git a/drama/routes/posts.py b/drama/routes/posts.py index 96006b93b..480f6af63 100644 --- a/drama/routes/posts.py +++ b/drama/routes/posts.py @@ -533,7 +533,7 @@ def thumbs(new_post): for chunk in image_req.iter_content(1024): file.write(chunk) - post.thumburl = upload_from_file("image.gif", (100, 100)) + post.thumburl = upload_file(resize=(100, 100)) g.db.add(post) g.db.commit() diff --git a/drama/routes/settings.py b/drama/routes/settings.py index 03a3a2156..1e85556c3 100644 --- a/drama/routes/settings.py +++ b/drama/routes/settings.py @@ -330,7 +330,7 @@ def settings_images_profile(v): highres = upload_file(request.files["profile"], (100,100)) if not highres: abort(400) - imageurl = upload_from_file("image.gif") + imageurl = upload_file() if not imageurl: abort(400) v.highres = highres v.profileurl = imageurl