mirror of
https://github.com/Hopiu/wagtail.git
synced 2026-03-16 22:10:28 +00:00
Support format-webp and allow changing default formatting
This commit is contained in:
parent
8862dbdd80
commit
01e0cd9dac
5 changed files with 73 additions and 13 deletions
45
docs/advanced_topics/images/image_file_formats.rst
Normal file
45
docs/advanced_topics/images/image_file_formats.rst
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
Image file formats
|
||||
==================
|
||||
|
||||
Using the picture element
|
||||
-------------------------
|
||||
|
||||
The `picture element <https://developer.mozilla.org/en-US/docs/Web/HTML/Element/picture>`_
|
||||
can be used with the ``format-<type>`` image operation to specify different
|
||||
image formats and let the browser choose the one it prefers. For example:
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
{% load wagtailimages_tags %}
|
||||
|
||||
<picture>
|
||||
{% image myimage width-1000 format-webp as image_webp %}
|
||||
<source srcset="{{ image_webp.url }}" type="image/webp">
|
||||
|
||||
{% image myimage width-1000 format-png as image_png %}
|
||||
<source srcset="{{ image_png.url }}" type="image/png">
|
||||
|
||||
{{ image_png }}
|
||||
</picture>
|
||||
|
||||
Customizing output formats
|
||||
--------------------------
|
||||
|
||||
By default all ``bmp`` and ``webp`` images are converted to the ``png`` format
|
||||
when no image output format is given.
|
||||
|
||||
The default conversion mapping can be changed by setting the
|
||||
``WAGTAILIMAGES_FORMAT_CONVERSIONS`` to a dictionary which maps the input type
|
||||
to an output type.
|
||||
|
||||
For example:
|
||||
|
||||
.. code-block:: python
|
||||
|
||||
WAGTAILIMAGES_FORMAT_CONVERSIONS = {
|
||||
'bmp': 'jpeg',
|
||||
'webp': 'webp',
|
||||
}
|
||||
|
||||
will convert ``bmp`` images to ``jpeg`` and disable the default ``webp``
|
||||
to ``png`` conversion.
|
||||
|
|
@ -7,6 +7,7 @@ Images
|
|||
|
||||
renditions
|
||||
animated_gifs
|
||||
image_file_formats
|
||||
custom_image_model
|
||||
changing_rich_text_representation
|
||||
feature_detection
|
||||
|
|
|
|||
|
|
@ -246,8 +246,9 @@ class FormatOperation(Operation):
|
|||
def construct(self, fmt):
|
||||
self.format = fmt
|
||||
|
||||
if self.format not in ['jpeg', 'png', 'gif']:
|
||||
raise ValueError("Format must be either 'jpeg', 'png' or 'gif'")
|
||||
if self.format not in ['jpeg', 'png', 'gif', 'webp']:
|
||||
raise ValueError(
|
||||
"Format must be either 'jpeg', 'png', 'gif', or 'webp'")
|
||||
|
||||
def run(self, willow, image, env):
|
||||
env['output-format'] = self.format
|
||||
|
|
|
|||
|
|
@ -404,20 +404,23 @@ class Filter:
|
|||
# Developer specified an output format
|
||||
output_format = env['output-format']
|
||||
else:
|
||||
# Default to outputting in original format
|
||||
output_format = original_format
|
||||
|
||||
# Convert BMP files to PNG
|
||||
if original_format == 'bmp':
|
||||
output_format = 'png'
|
||||
# Convert bmp and webp to png by default
|
||||
default_conversions = {
|
||||
'bmp': 'png',
|
||||
'webp': 'png',
|
||||
}
|
||||
|
||||
# Convert unanimated GIFs to PNG as well
|
||||
if original_format == 'gif' and not willow.has_animation():
|
||||
output_format = 'png'
|
||||
if not willow.has_animation():
|
||||
default_conversions['gif'] = 'png'
|
||||
|
||||
# Convert WEBP files to PNG
|
||||
if original_format == 'webp':
|
||||
output_format = 'png'
|
||||
# Allow the user to override the conversions
|
||||
conversion = getattr(settings, 'WAGTAILIMAGES_FORMAT_CONVERSIONS', {})
|
||||
default_conversions.update(conversion)
|
||||
|
||||
# Get the converted output format falling back to the original
|
||||
output_format = default_conversions.get(
|
||||
original_format, original_format)
|
||||
|
||||
if output_format == 'jpeg':
|
||||
# Allow changing of JPEG compression quality
|
||||
|
|
|
|||
|
|
@ -521,6 +521,16 @@ class TestFormatFilter(TestCase):
|
|||
|
||||
self.assertEqual(out.format_name, 'gif')
|
||||
|
||||
def test_webp(self):
|
||||
fil = Filter(spec='width-400|format-webp')
|
||||
image = Image.objects.create(
|
||||
title="Test image",
|
||||
file=get_test_image_file(),
|
||||
)
|
||||
out = fil.run(image, BytesIO())
|
||||
|
||||
self.assertEqual(out.format_name, 'webp')
|
||||
|
||||
def test_invalid(self):
|
||||
fil = Filter(spec='width-400|format-foo')
|
||||
image = Image.objects.create(
|
||||
|
|
|
|||
Loading…
Reference in a new issue