Repository: matthewwithanm/django-imagekit

Issue:
Feature Request: Make it possible to specify the format for the thumbnail template tag
I want that all my thumbnails use WEBP. Currently I have to create a lot of own generators. It would be nice if the `thumbnail` template tag would accept a format param or/and that I can specify a default format (I always want WEBP). 

Discussion:

Patch:
diff --git a/docs/configuration.rst b/docs/configuration.rst
index 84636ce..f6b6028 100644
--- a/docs/configuration.rst
+++ b/docs/configuration.rst
@@ -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'``
diff --git a/imagekit/generatorlibrary.py b/imagekit/generatorlibrary.py
index 59f71ef..dd5466f 100644
--- a/imagekit/generatorlibrary.py
+++ b/imagekit/generatorlibrary.py
@@ -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)
diff --git a/imagekit/templatetags/imagekit.py b/imagekit/templatetags/imagekit.py
index b2bbfe7..0722909 100644
--- a/imagekit/templatetags/imagekit.py
+++ b/imagekit/templatetags/imagekit.py
@@ -97,6 +97,8 @@ class ThumbnailAssignmentNode(template.Node):
             # 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)
@@ -124,6 +126,8 @@ class ThumbnailImageTagNode(template.Node):
             # 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)
@@ -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)::
 


Test patch:
diff --git a/tests/test_thumbnail_tag.py b/tests/test_thumbnail_tag.py
index f3bdfda..3cedf9b 100644
--- a/tests/test_thumbnail_tag.py
+++ b/tests/test_thumbnail_tag.py
@@ -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')