diff --git a/bookwyrm/static/images/med.jpg b/bookwyrm/static/images/med.jpg deleted file mode 100644 index c275cd1c..00000000 Binary files a/bookwyrm/static/images/med.jpg and /dev/null differ diff --git a/bookwyrm/static/images/profile.jpg b/bookwyrm/static/images/profile.jpg deleted file mode 100644 index f150ceab..00000000 Binary files a/bookwyrm/static/images/profile.jpg and /dev/null differ diff --git a/bookwyrm/static/images/small.jpg b/bookwyrm/static/images/small.jpg deleted file mode 100644 index 158163b6..00000000 Binary files a/bookwyrm/static/images/small.jpg and /dev/null differ diff --git a/bookwyrm/tests/views/test_user.py b/bookwyrm/tests/views/test_user.py index 0e2ad904..6a51aeda 100644 --- a/bookwyrm/tests/views/test_user.py +++ b/bookwyrm/tests/views/test_user.py @@ -1,5 +1,9 @@ ''' test for app action functionality ''' +import pathlib from unittest.mock import patch +from PIL import Image + +from django.core.files.base import ContentFile from django.template.response import TemplateResponse from django.test import TestCase from django.test.client import RequestFactory @@ -97,3 +101,15 @@ class UserViews(TestCase): with patch('bookwyrm.broadcast.broadcast_task.delay'): view(request) self.assertEqual(self.local_user.name, 'New Name') + + + def test_crop_avatar(self): + ''' reduce that image size ''' + image_file = pathlib.Path(__file__).parent.joinpath( + '../../static/images/no_cover.jpg') + image = Image.open(image_file) + + result = views.user.crop_avatar(image) + self.assertIsInstance(result, ContentFile) + image_result = Image.open(result) + self.assertEqual(image_result.size, (120, 120)) diff --git a/bookwyrm/views/user.py b/bookwyrm/views/user.py index 6f7873d5..b65fb48f 100644 --- a/bookwyrm/views/user.py +++ b/bookwyrm/views/user.py @@ -159,30 +159,35 @@ class EditUser(View): if 'avatar' in form.files: # crop and resize avatar upload image = Image.open(form.files['avatar']) - target_size = 120 - width, height = image.size - thumbnail_scale = height / (width / target_size) if height > width \ - else width / (height / target_size) - image.thumbnail([thumbnail_scale, thumbnail_scale]) - width, height = image.size - - width_diff = width - target_size - height_diff = height - target_size - cropped = image.crop(( - int(width_diff / 2), - int(height_diff / 2), - int(width - (width_diff / 2)), - int(height - (height_diff / 2)) - )) - output = BytesIO() - cropped.save(output, format=image.format) - ContentFile(output.getvalue()) + image = crop_avatar(image) # set the name to a hash extension = form.files['avatar'].name.split('.')[-1] filename = '%s.%s' % (uuid4(), extension) - user.avatar.save(filename, ContentFile(output.getvalue())) + user.avatar.save(filename, image) user.save() broadcast(user, user.to_update_activity(user)) return redirect(user.local_path) + + +def crop_avatar(image): + ''' reduce the size and make an avatar square ''' + target_size = 120 + width, height = image.size + thumbnail_scale = height / (width / target_size) if height > width \ + else width / (height / target_size) + image.thumbnail([thumbnail_scale, thumbnail_scale]) + width, height = image.size + + width_diff = width - target_size + height_diff = height - target_size + cropped = image.crop(( + int(width_diff / 2), + int(height_diff / 2), + int(width - (width_diff / 2)), + int(height - (height_diff / 2)) + )) + output = BytesIO() + cropped.save(output, format=image.format) + return ContentFile(output.getvalue())