Compress profile image before saving
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
import csv
|
import csv
|
||||||
from io import StringIO
|
from io import StringIO
|
||||||
|
import io
|
||||||
from flask import Blueprint, render_template, redirect, request, send_file, url_for, flash
|
from flask import Blueprint, render_template, redirect, request, send_file, url_for, flash
|
||||||
from werkzeug.security import generate_password_hash, check_password_hash
|
from werkzeug.security import generate_password_hash, check_password_hash
|
||||||
from app.models import Profile, Reading, db, User
|
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
|
from flask_login import login_user, login_required, current_user, logout_user
|
||||||
import base64
|
import base64
|
||||||
from datetime import datetime, timedelta
|
from datetime import datetime, timedelta
|
||||||
|
from PIL import Image
|
||||||
|
|
||||||
main = Blueprint('main', __name__)
|
main = Blueprint('main', __name__)
|
||||||
auth = Blueprint('auth', __name__)
|
auth = Blueprint('auth', __name__)
|
||||||
@@ -206,7 +208,23 @@ def profile():
|
|||||||
# Handle profile picture upload
|
# Handle profile picture upload
|
||||||
if form.profile_pic.data:
|
if form.profile_pic.data:
|
||||||
file_data = form.profile_pic.data.read()
|
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.add(profile)
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
|
|||||||
@@ -18,6 +18,7 @@ itsdangerous==2.2.0
|
|||||||
jinja2==3.1.5
|
jinja2==3.1.5
|
||||||
Mako==1.3.8
|
Mako==1.3.8
|
||||||
MarkupSafe==3.0.2
|
MarkupSafe==3.0.2
|
||||||
|
pillow==11.0.0
|
||||||
psycopg2==2.9.10
|
psycopg2==2.9.10
|
||||||
psycopg2-binary==2.9.10
|
psycopg2-binary==2.9.10
|
||||||
SQLAlchemy==2.0.36
|
SQLAlchemy==2.0.36
|
||||||
@@ -25,4 +26,3 @@ typing-extensions==4.12.2
|
|||||||
werkzeug==3.1.3
|
werkzeug==3.1.3
|
||||||
wtforms==3.2.1
|
wtforms==3.2.1
|
||||||
zipp==3.21.0
|
zipp==3.21.0
|
||||||
gunicorn==20.1.0
|
|
||||||
|
|||||||
Reference in New Issue
Block a user