]> littlesong.place Git - littlesongplace.git/commitdiff
Crop PFP on upload
authorChris Fulljames <christianfulljames@gmail.com>
Thu, 6 Feb 2025 13:01:43 +0000 (08:01 -0500)
committerChris Fulljames <christianfulljames@gmail.com>
Thu, 6 Feb 2025 13:01:43 +0000 (08:01 -0500)
main.py
requirements.txt

diff --git a/main.py b/main.py
index a3497a6ad6956d2b4f52db80a1d0bb30d6a7cca6..b89d5f9615f0200704cc7a46277c1224e56367ee 100644 (file)
--- a/main.py
+++ b/main.py
@@ -19,6 +19,7 @@ import click
 from bleach.css_sanitizer import CSSSanitizer
 from flask import Flask, render_template, request, redirect, g, session, abort, \
         send_from_directory, flash, get_flashed_messages
+from PIL import Image, UnidentifiedImageError
 from werkzeug.utils import secure_filename
 from werkzeug.middleware.proxy_fix import ProxyFix
 
@@ -176,8 +177,32 @@ def edit_profile():
     get_db().commit()
 
     if request.files["pfp"]:
-        pfp_path = get_user_images_path(session["userid"]) / "pfp"
-        request.files["pfp"].save(pfp_path)
+        pfp_path = get_user_images_path(session["userid"]) / "pfp.png"
+
+        try:
+            with Image.open(request.files["pfp"]) as im:
+                target_size = 256  # Square (same width/height)
+                # Resize
+                if im.width >= im.height:
+                    scale = 256 / im.height
+                else:
+                    scale = 256 / im.width
+
+                im = im.resize((round(im.width*scale), round(im.height*scale)))
+
+                # Crop to square
+                center_h = im.width / 2
+                center_v = im.height / 2
+                left = center_h - (target_size // 2)
+                right = center_h + (target_size // 2)
+                top = center_v - (target_size // 2)
+                bottom = center_v + (target_size // 2)
+                im = im.crop((left, top, right, bottom))
+
+                # Save to permanent location
+                im.save(pfp_path)
+        except UnidentifiedImageError:
+            abort(400)  # Invalid image
 
     flash("Profile updated successfully")
 
@@ -187,7 +212,7 @@ def edit_profile():
 
 @app.get("/pfp/<int:userid>")
 def pfp(userid):
-    return send_from_directory(DATA_DIR / "images" / str(userid), "pfp")
+    return send_from_directory(DATA_DIR / "images" / str(userid), "pfp.png")
 
 @app.get("/edit-song")
 def edit_song():
index 6d89524c5bf95b25fafa060dfaba5f17280fc117..3d48ce7ddd60b64076825bc8c0a68d0cbb93344e 100644 (file)
@@ -2,4 +2,5 @@ bcrypt
 bleach[css]
 flask
 gunicorn
+pillow