forked from s3lph/matemat
feat: always pad thumbnails to a square shape
This commit is contained in:
parent
54086dea39
commit
5a0c0cf148
2 changed files with 10 additions and 4 deletions
|
@ -7,7 +7,7 @@ import magic
|
||||||
from PIL import Image
|
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
|
# 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:
|
if image_data is None or len(image_data) == 0:
|
||||||
return False
|
return False
|
||||||
|
@ -25,7 +25,13 @@ def upload_thumbnail(image_data: bytes, filename: str, size: int = 150) -> bool:
|
||||||
except IOError:
|
except IOError:
|
||||||
raise ValueError(f'Unsupported file type: {filemagic.mime_type}')
|
raise ValueError(f'Unsupported file type: {filemagic.mime_type}')
|
||||||
# Resize the image to 150x150
|
# 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
|
# Write the image to the file
|
||||||
image.save(filename, 'PNG')
|
thumb.save(filename, 'PNG')
|
||||||
return True
|
return True
|
||||||
|
|
|
@ -49,7 +49,7 @@
|
||||||
<input class="form-control" id="moduser-account-balance-reason" type="text" name="reason" placeholder="Shows up on receipt" /><br/>
|
<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">
|
<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/>
|
</label><br/>
|
||||||
<input class="form-control" id="moduser-account-avatar" type="file" name="avatar" accept="image/*" /><br/>
|
<input class="form-control" id="moduser-account-avatar" type="file" name="avatar" accept="image/*" /><br/>
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue