mirror of
https://github.com/zulip/zulip.git
synced 2025-11-06 23:13:25 +00:00
Resize uploaded avatar images
(imported from commit 47ff86fcd47871916631baa34f99bbddb67f87d0)
This commit is contained in:
@@ -11,6 +11,8 @@ from mimetypes import guess_type, guess_extension
|
|||||||
|
|
||||||
import base64
|
import base64
|
||||||
import os
|
import os
|
||||||
|
from PIL import Image, ImageOps
|
||||||
|
from StringIO import StringIO
|
||||||
|
|
||||||
# Performance Note:
|
# Performance Note:
|
||||||
#
|
#
|
||||||
@@ -102,16 +104,35 @@ def get_signed_upload_url(path):
|
|||||||
conn = S3Connection(settings.S3_KEY, settings.S3_SECRET_KEY)
|
conn = S3Connection(settings.S3_KEY, settings.S3_SECRET_KEY)
|
||||||
return conn.generate_url(15, 'GET', bucket=settings.S3_AUTH_UPLOADS_BUCKET, key=path)
|
return conn.generate_url(15, 'GET', bucket=settings.S3_AUTH_UPLOADS_BUCKET, key=path)
|
||||||
|
|
||||||
|
def resize_avatar(image_data):
|
||||||
|
AVATAR_SIZE = 100
|
||||||
|
im = Image.open(StringIO(image_data))
|
||||||
|
im = ImageOps.fit(im, (AVATAR_SIZE, AVATAR_SIZE), Image.ANTIALIAS)
|
||||||
|
out = StringIO()
|
||||||
|
im.save(out, format='png')
|
||||||
|
return out.getvalue()
|
||||||
|
|
||||||
def upload_avatar_image(user_file, user_profile, email):
|
def upload_avatar_image(user_file, user_profile, email):
|
||||||
content_type = guess_type(user_file.name)[0]
|
content_type = guess_type(user_file.name)[0]
|
||||||
bucket_name = settings.S3_AVATAR_BUCKET
|
bucket_name = settings.S3_AVATAR_BUCKET
|
||||||
s3_file_name = user_avatar_hash(email)
|
s3_file_name = user_avatar_hash(email)
|
||||||
|
|
||||||
|
image_data = user_file.read()
|
||||||
|
upload_image_to_s3(
|
||||||
|
bucket_name,
|
||||||
|
s3_file_name + ".original",
|
||||||
|
content_type,
|
||||||
|
user_profile,
|
||||||
|
image_data,
|
||||||
|
)
|
||||||
|
|
||||||
|
resized_data = resize_avatar(image_data)
|
||||||
upload_image_to_s3(
|
upload_image_to_s3(
|
||||||
bucket_name,
|
bucket_name,
|
||||||
s3_file_name,
|
s3_file_name,
|
||||||
content_type,
|
'image/png',
|
||||||
user_profile,
|
user_profile,
|
||||||
user_file.read(),
|
resized_data,
|
||||||
)
|
)
|
||||||
# See avatar_url in avatar.py for URL. (That code also handles the case
|
# See avatar_url in avatar.py for URL. (That code also handles the case
|
||||||
# that users use gravatar.)
|
# that users use gravatar.)
|
||||||
|
|||||||
Reference in New Issue
Block a user