make sure all images with resize parameter are under 500KB

remotes/1693176582716663532/tmp_refs/heads/watchparty
Aevann1 2022-10-22 17:24:07 +02:00
parent 49b12f220c
commit 2dcf6bcb0f
3 changed files with 24 additions and 28 deletions

View File

@ -154,36 +154,32 @@ def process_image(filename=None, resize=0, trim=False, uploader=None, patron=Fal
i.save(filename, format="WEBP", method=6, quality=88)
if resize in (300,400,1200):
if resize:
if os.stat(filename).st_size > MAX_IMAGE_SIZE_BANNER_RESIZED_KB * 1024:
os.remove(filename)
abort(413, f"Max size for banners, sidebars, and badges is {MAX_IMAGE_SIZE_BANNER_RESIZED_KB} KB")
if resize == 1200:
path = f'files/assets/images/{SITE_NAME}/banners'
elif resize == 400:
path = f'files/assets/images/{SITE_NAME}/sidebar'
else:
path = f'files/assets/images/badges'
if filename.startswith('files/assets/images/'):
path = filename.rsplit('/', 1)[0]
hashes = {}
hashes = {}
for img in os.listdir(path):
if resize == 400 and img in ('256.webp','585.webp'): continue
img_path = f'{path}/{img}'
if img_path == filename: continue
img = Image.open(img_path)
i_hash = str(imagehash.phash(img))
for img in os.listdir(path):
if resize == 400 and img in ('256.webp','585.webp'): continue
img_path = f'{path}/{img}'
if img_path == filename: continue
img = Image.open(img_path)
i_hash = str(imagehash.phash(img))
if i_hash in hashes.keys():
print(hashes[i_hash], flush=True)
print(img_path, flush=True)
else: hashes[i_hash] = img_path
i = Image.open(filename)
i_hash = str(imagehash.phash(i))
if i_hash in hashes.keys():
print(hashes[i_hash], flush=True)
print(img_path, flush=True)
else: hashes[i_hash] = img_path
i = Image.open(filename)
i_hash = str(imagehash.phash(i))
if i_hash in hashes.keys():
os.remove(filename)
abort(409, "Image already exists!")
os.remove(filename)
abort(409, "Image already exists!")
db = db or g.db

View File

@ -244,7 +244,7 @@ if SITE not in ('pcmemes.net', 'watchpeopledie.tv'):
filename = f'/asset_submissions/hats/{name}.webp'
copyfile(highquality, filename)
process_image(filename)
process_image(filename, resize=100)
hat = HatDef(name=name, author_id=author.id, description=description, price=price, submitter_id=v.id)
g.db.add(hat)
@ -451,7 +451,7 @@ if SITE not in ('pcmemes.net', 'watchpeopledie.tv'):
filename = f"files/assets/images/hats/{name}.webp"
copyfile(new_path, filename)
process_image(filename)
process_image(filename, resize=100)
purge_files_in_cache([f"https://{SITE}/i/hats/{name}.webp", f"https://{SITE}/assets/images/hats/{name}.webp", f"https://{SITE}/asset_submissions/hats/original/{name}.{format}"])
ma = ModAction(
kind="update_hat",

View File

@ -480,7 +480,7 @@ def sub_banner(v, sub):
name = f'/images/{time.time()}'.replace('.','') + '.webp'
file.save(name)
bannerurl = process_image(name, patron=v.patron)
bannerurl = process_image(name, patron=v.patron, resize=1200)
if bannerurl:
if sub.bannerurl and '/images/' in sub.bannerurl:
@ -512,7 +512,7 @@ def sub_sidebar(v, sub):
file = request.files["sidebar"]
name = f'/images/{time.time()}'.replace('.','') + '.webp'
file.save(name)
sidebarurl = process_image(name, patron=v.patron)
sidebarurl = process_image(name, patron=v.patron, resize=400)
if sidebarurl:
if sub.sidebarurl and '/images/' in sub.sidebarurl:
@ -544,7 +544,7 @@ def sub_marsey(v, sub):
file = request.files["marsey"]
name = f'/images/{time.time()}'.replace('.','') + '.webp'
file.save(name)
marseyurl = process_image(name, patron=v.patron)
marseyurl = process_image(name, patron=v.patron, resize=200)
if marseyurl:
if sub.marseyurl and '/images/' in sub.marseyurl: