diff --git a/app/routes.py b/app/routes.py index 5e053b4..7a75378 100644 --- a/app/routes.py +++ b/app/routes.py @@ -1,5 +1,6 @@ import csv from io import StringIO +import io from flask import Blueprint, render_template, redirect, request, send_file, url_for, flash from werkzeug.security import generate_password_hash, check_password_hash from app.models import Profile, Reading, db, User @@ -7,6 +8,7 @@ from app.forms import DeleteForm, LoginForm, ProfileForm, ReadingForm, SignupFor from flask_login import login_user, login_required, current_user, logout_user import base64 from datetime import datetime, timedelta +from PIL import Image main = Blueprint('main', __name__) auth = Blueprint('auth', __name__) @@ -206,7 +208,23 @@ def profile(): # Handle profile picture upload if form.profile_pic.data: file_data = form.profile_pic.data.read() - profile.profile_pic = base64.b64encode(file_data).decode('utf-8') + + # Resize and compress the image + try: + image = Image.open(io.BytesIO(file_data)) + image = image.convert("RGB") # Ensure it's in RGB format + image.thumbnail((300, 300)) # Resize to a maximum of 300x300 pixels + + # Save the resized image to a buffer + buffer = io.BytesIO() + image.save(buffer, format="JPEG", quality=80) # Compress with quality=80 + buffer.seek(0) + + # Encode the compressed image as base64 + profile.profile_pic = base64.b64encode(buffer.read()).decode('utf-8') + except Exception as e: + flash(f"Error processing profile picture: {e}", 'danger') + db.session.add(profile) db.session.commit() diff --git a/requirements.txt b/requirements.txt index c0d390b..8a4c275 100644 --- a/requirements.txt +++ b/requirements.txt @@ -18,6 +18,7 @@ itsdangerous==2.2.0 jinja2==3.1.5 Mako==1.3.8 MarkupSafe==3.0.2 +pillow==11.0.0 psycopg2==2.9.10 psycopg2-binary==2.9.10 SQLAlchemy==2.0.36 @@ -25,4 +26,3 @@ typing-extensions==4.12.2 werkzeug==3.1.3 wtforms==3.2.1 zipp==3.21.0 -gunicorn==20.1.0