Add webp support

This commit is contained in:
frmdstryr 2019-03-04 14:12:35 -05:00 committed by Matt Westcott
parent 3024e02481
commit 8862dbdd80
3 changed files with 11 additions and 4 deletions

View file

@ -7,8 +7,8 @@ from django.forms.fields import ImageField
from django.template.defaultfilters import filesizeformat
from django.utils.translation import ugettext_lazy as _
ALLOWED_EXTENSIONS = ['gif', 'jpg', 'jpeg', 'png']
SUPPORTED_FORMATS_TEXT = _("GIF, JPEG, PNG")
ALLOWED_EXTENSIONS = ['gif', 'jpg', 'jpeg', 'png', 'webp']
SUPPORTED_FORMATS_TEXT = _("GIF, JPEG, PNG, WEBP")
class WagtailImageField(ImageField):

View file

@ -297,6 +297,7 @@ class AbstractImage(CollectionMember, index.Indexed, models.Model):
'jpeg': '.jpg',
'png': '.png',
'gif': '.gif',
'webp': '.webp',
}
output_extension = filter.spec.replace('|', '.') + FORMAT_EXTENSIONS[generated_image.format_name]
@ -414,6 +415,10 @@ class Filter:
if original_format == 'gif' and not willow.has_animation():
output_format = 'png'
# Convert WEBP files to PNG
if original_format == 'webp':
output_format = 'png'
if output_format == 'jpeg':
# Allow changing of JPEG compression quality
if 'jpeg-quality' in env:
@ -432,6 +437,8 @@ class Filter:
return willow.save_as_png(output, optimize=True)
elif output_format == 'gif':
return willow.save_as_gif(output)
elif output_format == 'webp':
return willow.save_as_webp(output)
def get_cache_key(self, image):
vary_parts = []

View file

@ -853,7 +853,7 @@ class TestImageChooserUploadView(TestCase, WagtailTestUtils):
self.assertEqual(response.status_code, 200)
self.assertTemplateUsed(response, 'wagtailimages/chooser/chooser.html')
self.assertFormError(response, 'uploadform', 'file', "Not a supported image format. Supported formats: GIF, JPEG, PNG.")
self.assertFormError(response, 'uploadform', 'file', "Not a supported image format. Supported formats: GIF, JPEG, PNG, WEBP.")
# the action URL of the re-rendered form should include the select_format=true parameter
# (NB the HTML in the response is embedded in a JS string, so need to escape accordingly)
@ -1050,7 +1050,7 @@ class TestMultipleImageUploader(TestCase, WagtailTestUtils):
self.assertIn('error_message', response_json)
self.assertFalse(response_json['success'])
self.assertEqual(
response_json['error_message'], "Not a supported image format. Supported formats: GIF, JPEG, PNG."
response_json['error_message'], "Not a supported image format. Supported formats: GIF, JPEG, PNG, WEBP."
)
def test_edit_get(self):