Skip to content

Commit

Permalink
Correct thumbnail transposing for Safari, resolves #207
Browse files Browse the repository at this point in the history
  • Loading branch information
johanneswilm committed Aug 16, 2022
1 parent 8475d37 commit 0e09d40
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 23 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Changelog
* Made ``True`` the default value of ``AVATAR_CLEANUP_DELETED``. (Set to ``False`` to obtain previous behavior).
* Fix invalidate_cache for on-the-fly created thumbnails.
* New setting ``AVATAR_ALLOWED_MIMETYPES``. If enabled, it checks mimetypes of uploaded files using ``python-magic``. Default is ``None``.
* Fix thumbnail transposing for Safari.

* 6.0.1 (August 12, 2022)
* Exclude tests folder from distribution.
Expand Down
28 changes: 5 additions & 23 deletions avatar/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from django.utils.module_loading import import_string
from django.utils.timezone import now
from django.utils.translation import gettext_lazy as _
from PIL import Image
from PIL import Image, ImageOps

from avatar.conf import settings
from avatar.utils import force_bytes, get_username, invalidate_cache
Expand Down Expand Up @@ -122,28 +122,10 @@ def thumbnail_exists(self, width, height=None):
return self.avatar.storage.exists(self.avatar_name(width, height))

def transpose_image(self, image):
"""
Transpose based on EXIF information.
Borrowed from django-imagekit:
imagekit.processors.Transpose
"""
EXIF_ORIENTATION_STEPS = {
1: [],
2: ["FLIP_LEFT_RIGHT"],
3: ["ROTATE_180"],
4: ["FLIP_TOP_BOTTOM"],
5: ["ROTATE_270", "FLIP_LEFT_RIGHT"],
6: ["ROTATE_270"],
7: ["ROTATE_90", "FLIP_LEFT_RIGHT"],
8: ["ROTATE_90"],
}
try:
orientation = image._getexif()[0x0112]
ops = EXIF_ORIENTATION_STEPS[orientation]
except (AttributeError, TypeError):
ops = []
for method in ops:
image = image.transpose(getattr(Image, method))
EXIF_ORIENTATION = 0x0112
exif_code = image.getexif().get(EXIF_ORIENTATION, 1)
if exif_code and exif_code != 1:
image = ImageOps.exif_transpose(image)
return image

def create_thumbnail(self, width, height=None, quality=None):
Expand Down

0 comments on commit 0e09d40

Please sign in to comment.