Skip to content

Commit

Permalink
Merge pull request #574 from autoantwort/default-thumbnail-format
Browse files Browse the repository at this point in the history
Add way to specify thumbnail format
  • Loading branch information
vstoykov authored Jul 22, 2024
2 parents 92dff00 + 3cdeba8 commit c4ed0bb
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 2 deletions.
7 changes: 7 additions & 0 deletions docs/configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@ Settings
.. currentmodule:: django.conf.settings


.. attribute:: IMAGEKIT_DEFAULT_THUMBNAIL_FORMAT

:default: ``None``

The output format of the images generated by the ``thumbnail`` template tag.


.. attribute:: IMAGEKIT_CACHEFILE_DIR

:default: ``'CACHE/images'``
Expand Down
5 changes: 4 additions & 1 deletion imagekit/generatorlibrary.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
from .processors import Thumbnail as ThumbnailProcessor
from .registry import register
from .specs import ImageSpec
from django.conf import settings

default_thumbnail_format = getattr(settings, 'IMAGEKIT_DEFAULT_THUMBNAIL_FORMAT', None)

class Thumbnail(ImageSpec):
def __init__(self, width=None, height=None, anchor=None, crop=None, upscale=None, **kwargs):
def __init__(self, width=None, height=None, anchor=None, crop=None, upscale=None, format=default_thumbnail_format, **kwargs):
self.processors = [ThumbnailProcessor(width, height, anchor=anchor,
crop=crop, upscale=upscale)]
super().__init__(**kwargs)
self.format = format


register.generator('imagekit:thumbnail', Thumbnail)
6 changes: 5 additions & 1 deletion imagekit/templatetags/imagekit.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ def render(self, context):
# recursion errors when anchor is set to a SafeString instance.
# This converts the SafeString into a str instance.
kwargs['anchor'] = kwargs['anchor'][:]
if kwargs.get('format'):
kwargs['format'] = kwargs['format'][:]
generator = generator_registry.get(generator_id, **kwargs)

context[variable_name] = ImageCacheFile(generator)
Expand Down Expand Up @@ -124,6 +126,8 @@ def render(self, context):
# recursion errors when anchor is set to a SafeString instance.
# This converts the SafeString into a str instance.
kwargs['anchor'] = kwargs['anchor'][:]
if kwargs.get('format'):
kwargs['format'] = kwargs['format'][:]
generator = generator_registry.get(generator_id, **kwargs)

file = ImageCacheFile(generator)
Expand Down Expand Up @@ -241,7 +245,7 @@ def thumbnail(parser, token):
The thumbnail tag supports the "--" and "as" bits for adding html
attributes and assigning to a variable, respectively. It also accepts the
kwargs "anchor", and "crop".
kwargs "format", "anchor", and "crop".
To use "smart cropping" (the ``SmartResize`` processor)::
Expand Down
7 changes: 7 additions & 0 deletions tests/test_thumbnail_tag.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,10 @@ def test_alternate_generator():
clear_imagekit_cache()
html = render_tag(ttag)
assert html == '1'


def test_alternate_format():
ttag = r"""{% thumbnail '100x' img format='webp' as th %}{{ th.url }}"""
clear_imagekit_cache()
html = render_tag(ttag)
assert html.endswith('webp')

0 comments on commit c4ed0bb

Please sign in to comment.