feat: always pad thumbnails to a square shape

This commit is contained in:
s3lph 2024-12-11 01:18:56 +01:00
parent 54086dea39
commit 5a0c0cf148
Signed by: s3lph
GPG key ID: 0AA29A52FB33CFB5
2 changed files with 10 additions and 4 deletions

View file

@ -7,7 +7,7 @@ import magic
from PIL import Image
def upload_thumbnail(image_data: bytes, filename: str, size: int = 150) -> bool:
def upload_thumbnail(image_data: bytes, filename: str, size: int = 300) -> bool:
# Only process the image, if its size is more than zero. Zero size means no new image was uploaded
if image_data is None or len(image_data) == 0:
return False
@ -25,7 +25,13 @@ def upload_thumbnail(image_data: bytes, filename: str, size: int = 150) -> bool:
except IOError:
raise ValueError(f'Unsupported file type: {filemagic.mime_type}')
# Resize the image to 150x150
image.thumbnail((150, 150), Image.LANCZOS)
image.thumbnail((size, size), Image.LANCZOS)
# Create a new 150x150 transparent image and paste the thumbnail in the center
thumb: Image = Image.new('RGBA', (size, size), (0, 0, 0, 0))
x = (thumb.width - image.width) // 2
y = (thumb.height - image.height) // 2
thumb.paste(image, (x, y))
# Write the image to the file
image.save(filename, 'PNG')
thumb.save(filename, 'PNG')
return True

View file

@ -49,7 +49,7 @@
<input class="form-control" id="moduser-account-balance-reason" type="text" name="reason" placeholder="Shows up on receipt" /><br/>
<label class="form-label" for="moduser-account-avatar">
<img src="/static/upload/thumbnails/users/{{ user.id }}.png?cacheBuster={{ now }}" alt="Avatar of {{ user.name }}" />
<img height="150" src="/static/upload/thumbnails/users/{{ user.id }}.png?cacheBuster={{ now }}" alt="Avatar of {{ user.name }}" />
</label><br/>
<input class="form-control" id="moduser-account-avatar" type="file" name="avatar" accept="image/*" /><br/>