kopia lustrzana https://github.com/wagtail/wagtail
Added 'format' template filter
This allows the developer to change the output format of an image placementpull/3053/head
rodzic
b20cce01aa
commit
f58eb6ef28
|
@ -227,3 +227,14 @@ class JPEGQualityOperation(Operation):
|
|||
|
||||
def run(self, willow, image, env):
|
||||
env['jpeg-quality'] = self.quality
|
||||
|
||||
|
||||
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'")
|
||||
|
||||
def run(self, willow, image, env):
|
||||
env['output-format'] = self.format
|
||||
|
|
|
@ -435,7 +435,23 @@ class Filter(models.Model):
|
|||
else:
|
||||
willow = operation.run(willow, image) or willow
|
||||
|
||||
if original_format == 'jpeg':
|
||||
# Find the output format to use
|
||||
if 'output-format' in env:
|
||||
# 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 unanimated GIFs to PNG as well
|
||||
if original_format == 'gif' and not willow.has_animation():
|
||||
output_format = 'png'
|
||||
|
||||
if output_format == 'jpeg':
|
||||
# Allow changing of JPEG compression quality
|
||||
if 'jpeg-quality' in env:
|
||||
quality = env['jpeg-quality']
|
||||
|
@ -445,17 +461,10 @@ class Filter(models.Model):
|
|||
quality = 85
|
||||
|
||||
return willow.save_as_jpeg(output, quality=quality, progressive=True, optimize=True)
|
||||
elif original_format == 'gif':
|
||||
# Convert image to PNG if it's not animated
|
||||
if not willow.has_animation():
|
||||
return willow.save_as_png(output)
|
||||
else:
|
||||
return willow.save_as_gif(output)
|
||||
elif original_format == 'bmp':
|
||||
# Convert to PNG
|
||||
elif output_format == 'png':
|
||||
return willow.save_as_png(output)
|
||||
else:
|
||||
return willow.save(original_format, output)
|
||||
elif output_format == 'gif':
|
||||
return willow.save_as_gif(output)
|
||||
|
||||
def get_cache_key(self, image):
|
||||
vary_parts = []
|
||||
|
|
|
@ -77,6 +77,7 @@ def register_image_operations():
|
|||
('width', image_operations.WidthHeightOperation),
|
||||
('height', image_operations.WidthHeightOperation),
|
||||
('jpegquality', image_operations.JPEGQualityOperation),
|
||||
('format', image_operations.FormatOperation),
|
||||
]
|
||||
|
||||
|
||||
|
|
Ładowanie…
Reference in New Issue