Changeset f0e64eb in OpenWorkouts-current for ow/views/user.py


Ignore:
Timestamp:
Feb 15, 2019, 11:00:35 AM (5 years ago)
Author:
Borja Lopez <borja@…>
Branches:
current, feature/docs, master
Children:
0fa21cd
Parents:
eb20dc8
Message:

(#51) Fixed profile images were too big:

  • Added new dependency (Pillow)
  • Modified the user profile picture view. Now it accepts a GET parameter (size) telling the size of the image we want. Only one value is needed, the code will scale the image appropiately.
  • Modified the dashboard, profile and edit_profile templates to ask for a smaller version (200px) of the user profile picture.

IMPORTANT: Ensure you install Pillow in any existing environments after pulling:

pip install Pillow

File:
1 edited

Legend:

Unmodified
Added
Removed
  • ow/views/user.py

    reb20dc8 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.