resize_avatar: Add a size option.

This commit is contained in:
Tim Abbott
2016-10-02 21:28:31 -07:00
parent 87b9017845
commit ac2007dd9b

View File

@@ -32,6 +32,8 @@ import io
import random import random
import logging import logging
DEFAULT_AVATAR_SIZE = 100
# Performance Note: # Performance Note:
# #
# For writing files to S3, the file could either be stored in RAM # For writing files to S3, the file could either be stored in RAM
@@ -80,12 +82,11 @@ def random_name(bytes=60):
class BadImageError(JsonableError): class BadImageError(JsonableError):
pass pass
def resize_avatar(image_data): def resize_avatar(image_data, size=DEFAULT_AVATAR_SIZE):
# type: (binary_type) -> binary_type # type: (binary_type, int) -> binary_type
AVATAR_SIZE = 100
try: try:
im = Image.open(io.BytesIO(image_data)) im = Image.open(io.BytesIO(image_data))
im = ImageOps.fit(im, (AVATAR_SIZE, AVATAR_SIZE), Image.ANTIALIAS) im = ImageOps.fit(im, (size, size), Image.ANTIALIAS)
except IOError: except IOError:
raise BadImageError("Could not decode avatar image; did you upload an image file?") raise BadImageError("Could not decode avatar image; did you upload an image file?")
out = io.BytesIO() out = io.BytesIO()