This commit is contained in:
Aevann1 2021-07-30 11:34:00 +02:00
parent f4064461d5
commit be442491f7
4 changed files with 32 additions and 69 deletions

View file

@ -9,80 +9,43 @@ CF_KEY = environ.get("CLOUDFLARE_KEY").strip()
CF_ZONE = environ.get("CLOUDFLARE_ZONE").strip() CF_ZONE = environ.get("CLOUDFLARE_ZONE").strip()
imgurkey = environ.get("imgurkey").strip() imgurkey = environ.get("imgurkey").strip()
def crop_and_resize(img, resize):
i = img def upload_file(file=None, resize=None):
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):
if resize: if file: file.save("image.gif")
file.save("image.gif") i = IImage.open("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
else:
req = requests.post('https://api.imgur.com/3/upload.json', headers = {"Authorization": f"Client-ID {imgurkey}"}, data = {'image': base64.b64encode(file.read())}) if resize:
try: resp = req.json()['data'] org_ratio = i.width / i.height
except: new_ratio = resize[0] / resize[1]
print(req.text)
return 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" try: url = resp['link'].replace(".png", "_d.png").replace(".jpg", "_d.jpg").replace(".jpeg", "_d.jpeg") + "?maxwidth=9999"
except Exception as e: except Exception as e:
print(req.text) print(req.text)
return return
new_image = Image( new_image = Image(text=url, deletehash=resp["deletehash"])
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"],
)
g.db.add(new_image) g.db.add(new_image)
return(url) return(url)

View file

@ -1146,7 +1146,7 @@ def multiple_plots(**kwargs):
posts_chart.legend(loc='upper left', frameon=True) posts_chart.legend(loc='upper left', frameon=True)
comments_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() plt.clf()
return upload_from_file("multiplot.png") return upload_file()

View file

@ -533,7 +533,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_from_file("image.gif", (100, 100)) post.thumburl = upload_file(resize=(100, 100))
g.db.add(post) g.db.add(post)
g.db.commit() g.db.commit()

View file

@ -330,7 +330,7 @@ def settings_images_profile(v):
highres = upload_file(request.files["profile"], (100,100)) highres = upload_file(request.files["profile"], (100,100))
if not highres: abort(400) if not highres: abort(400)
imageurl = upload_from_file("image.gif") imageurl = upload_file()
if not imageurl: abort(400) if not imageurl: abort(400)
v.highres = highres v.highres = highres
v.profileurl = imageurl v.profileurl = imageurl