Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add way to specify thumbnail format #574

Merged
merged 2 commits into from
Jul 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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')
Loading