Resize images to max 512px in resolution

This commit is contained in:
Michał Gdula 2023-06-23 13:49:28 +01:00
parent fdac4dc402
commit a5143f9335
5 changed files with 23 additions and 11 deletions

View file

@ -1,6 +1,7 @@
import uuid import uuid
import re import re
import os import os
from PIL import Image
from flask import Blueprint, request, render_template, flash, redirect, url_for from flask import Blueprint, request, render_template, flash, redirect, url_for
from flask_login import login_required, current_user, logout_user from flask_login import login_required, current_user, logout_user
@ -10,9 +11,9 @@ from .config import (
USER_REGEX, USER_REGEX,
USER_EMAIL_REGEX, USER_EMAIL_REGEX,
UPLOAD_EXTENSIONS, UPLOAD_EXTENSIONS,
UPLOAD_RESOLUTION,
UPLOAD_MAX_SIZE, UPLOAD_MAX_SIZE,
UPLOAD_DIR, UPLOAD_DIR,
UPLOAD_RESOLUTION,
) )
from .models import Users, Sessions, Scores, ProfileTags, PasswordReset from .models import Users, Sessions, Scores, ProfileTags, PasswordReset
from .extensions import db from .extensions import db
@ -47,9 +48,14 @@ def settings():
if file_ext not in UPLOAD_EXTENSIONS: if file_ext not in UPLOAD_EXTENSIONS:
error.append("Picture is not a valid image!") error.append("Picture is not a valid image!")
if picture.content_length > UPLOAD_MAX_SIZE: if picture.content_length > UPLOAD_MAX_SIZE:
error.append( error.append(f"Picture must be less than {UPLOAD_EXTENSIONS / 1000000}MB!")
f"Picture is too large! Must be less than {UPLOAD_EXTENSIONS / 1000000}MB!"
) image = Image.open(picture.stream)
image_x, image_y = image.size
image.thumbnail((
min(image_x, UPLOAD_RESOLUTION),
min(image_y, UPLOAD_RESOLUTION)
))
if error: if error:
for err in error: for err in error:
@ -60,7 +66,8 @@ def settings():
os.remove(os.path.join(UPLOAD_DIR, user.picture)) os.remove(os.path.join(UPLOAD_DIR, user.picture))
user.picture = file_name user.picture = file_name
picture.save(os.path.join(UPLOAD_DIR, file_name)) image.save(os.path.join(UPLOAD_DIR, file_name))
image.close()
if username: if username:
if user_regex.match(username): if user_regex.match(username):

View file

@ -3,7 +3,7 @@ from os import getenv
UPLOAD_DIR = "/data/uploads" UPLOAD_DIR = "/data/uploads"
UPLOAD_EXTENSIONS = ["png", "jpg", "jpeg", "gif"] UPLOAD_EXTENSIONS = ["png", "jpg", "jpeg", "gif"]
UPLOAD_RESOLUTION = (512, 512) UPLOAD_RESOLUTION = 512
UPLOAD_MAX_SIZE = 3 * 1024 * 1024 # 3MB UPLOAD_MAX_SIZE = 3 * 1024 * 1024 # 3MB
GAME_VERSION = "alpha" GAME_VERSION = "alpha"

View file

@ -48,7 +48,7 @@
> label > label
padding: 0.5rem 0.7rem padding: 0.5rem 0.7rem
min-width: 6rem min-width: 7rem
text-decoration: none text-decoration: none
text-align: end text-align: end

View file

@ -59,12 +59,14 @@
.other .other
width: 100% width: 100%
height: 100%
display: flex display: flex
flex-direction: column flex-direction: column
justify-content: flex-start justify-content: space-between
gap: 0.5rem gap: 0.5rem
> .text-input
margin: 0 !important
@media (max-width: 621px) @media (max-width: 621px)
.profile-settings .profile-settings
flex-direction: column flex-direction: column

View file

@ -19,9 +19,12 @@
{{ text(id="profile-username", name="username", value=current_user.username) }} {{ text(id="profile-username", name="username", value=current_user.username) }}
{{ text(id="profile-email", name="email") }} {{ text(id="profile-email", name="email") }}
{{ text(id="profile-password", name="password", type="password", required=True, minlength=8) }} {{ text(id="profile-password", name="password", type="password", required=True, minlength=8) }}
<span style="height: 100%"></span>
<button type="submit" class="button primary">Save changes</button>
</div> </div>
</div> </div>
<button type="submit" class="button primary">Save changes</button>
</form> </form>
</div> </div>