Skip to content

Commit

Permalink
Support webp as image format (medialibrary recognizes it as image, ad…
Browse files Browse the repository at this point in the history
…min can show thumbnail).

Minor refactoring, still lots of duplicated code here!
  • Loading branch information
Martin J. Laubach committed Apr 15, 2024
1 parent b21fc62 commit 14e655f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
2 changes: 1 addition & 1 deletion feincms/module/medialibrary/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ def delete_mediafile(self, name=None):
"image",
_("Image"),
lambda f: re.compile(
r"\.(bmp|jpe?g|jp2|jxr|gif|png|tiff?)$", re.IGNORECASE
r"\.(bmp|jpe?g|jp2|jxr|gif|png|tiff?|webp)$", re.IGNORECASE
).search(f),
),
(
Expand Down
22 changes: 13 additions & 9 deletions feincms/templatetags/feincms_thumbnail.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
class Thumbnailer:
THUMBNAIL_SIZE_RE = re.compile(r"^(?P<w>\d+)x(?P<h>\d+)$")
MARKER = "_thumb_"
BROWSER_SUPPORTED_FORMATS = ("jpg", "jpeg", "png", "webp") # browser supported image formats
TRANSPARENCY_SUPPORTING_FORMATS = ('png', 'webp') # formats with alpha channel

def __init__(self, filename, size="200x200"):
self.filename = filename
Expand Down Expand Up @@ -85,7 +87,7 @@ def __str__(self):
# storage does NOT support modified_time
generate = False
except OSError:
# Someone might have delete the file
# Someone might have deleted the file
return ""

if generate:
Expand Down Expand Up @@ -116,13 +118,14 @@ def generate(self, storage, original, size, miniature):
# defining the size
w, h = int(size["w"]), int(size["h"])

format = image.format # Save format for the save() call later
image.thumbnail([w, h], Image.LANCZOS)
assert image.format is not None
format = image.format.lower() # Save format for the save() call later
image.thumbnail([w, h], Image.Resampling.LANCZOS)
buf = BytesIO()
if format.lower() not in ("jpg", "jpeg", "png"):
if format not in self.BROWSER_SUPPORTED_FORMATS: # browser supported image formats
format = "jpeg"
if image.mode not in ("RGBA", "RGB", "L"):
if format == "png":
if format in self.TRANSPARENCY_SUPPORTING_FORMATS: # handles transparency?
image = image.convert("RGBA")
else:
image = image.convert("RGB")
Expand Down Expand Up @@ -168,7 +171,8 @@ def generate(self, storage, original, size, miniature):
x_offset = 0
y_offset = int(float(src_height - crop_height) * y / 100)

format = image.format # Save format for the save() call later
assert image.format is not None
format = image.format.lower() # Save format for the save() call later
image = image.crop(
(
x_offset,
Expand All @@ -177,13 +181,13 @@ def generate(self, storage, original, size, miniature):
y_offset + int(crop_height),
)
)
image = image.resize((dst_width, dst_height), Image.LANCZOS)
image = image.resize((dst_width, dst_height), Image.Resampling.LANCZOS)

buf = BytesIO()
if format.lower() not in ("jpg", "jpeg", "png"):
if format not in self.BROWSER_SUPPORTED_FORMATS:
format = "jpeg"
if image.mode not in ("RGBA", "RGB", "L"):
if format == "png":
if format in self.TRANSPARENCY_SUPPORTING_FORMATS:
image = image.convert("RGBA")
else:
image = image.convert("RGB")
Expand Down

0 comments on commit 14e655f

Please sign in to comment.