Changes in ow/views/user.py [bddf042:f0e64eb] in OpenWorkouts-current


Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • ow/views/user.py

    rbddf042 rf0e64eb  
    33from datetime import datetime, timezone, timedelta
    44from decimal import Decimal
     5from io import BytesIO
    56
    67from pyramid.httpexceptions import HTTPFound, HTTPNotFound
     
    1112from pyramid_simpleform import Form, State
    1213from pytz import common_timezones
     14from PIL import Image
    1315
    1416from ..models.user import User
     
    212214    permission='view')
    213215def profile_picture(context, request):
    214     return Response(
    215         content_type='image',
    216         body_file=context.picture.open())
     216    if context.picture is None:
     217        return HTTPNotFound()
     218
     219    size = request.GET.get('size', 0)
     220    # we will need a tuple, it does not matter if both values are the same,
     221    # Pillow will keep aspect ratio
     222    size = (int(size), int(size))
     223
     224    image = Image.open(context.picture.open())
     225
     226    if size > (0, 0) and size < image.size:
     227        # resize only if they are asking for smaller size, prevent
     228        # someone asking for a "too big" image
     229        image.thumbnail(size)
     230
     231    body_file = BytesIO()
     232    image.save(body_file, format=image.format)
     233    return Response(content_type='image', body=body_file.getvalue())
    217234
    218235
Note: See TracChangeset for help on using the changeset viewer.