From 091ea75de7f95a8d3d539c1bf1722c8d99ad0697 Mon Sep 17 00:00:00 2001 From: Oliver Wilkerson Date: Fri, 23 Mar 2018 07:09:22 -0500 Subject: [PATCH] Added a simple scale filter to image_operations. --- CHANGELOG.txt | 1 + CONTRIBUTORS.rst | 1 + docs/releases/2.3.rst | 3 ++ docs/topics/images.rst | 9 +++++ wagtail/images/image_operations.py | 14 ++++++++ wagtail/images/tests/test_image_operations.py | 34 +++++++++++++++++++ wagtail/images/wagtail_hooks.py | 1 + 7 files changed, 63 insertions(+) diff --git a/CHANGELOG.txt b/CHANGELOG.txt index 76d9eadf23..4419804a51 100644 --- a/CHANGELOG.txt +++ b/CHANGELOG.txt @@ -5,6 +5,7 @@ Changelog ~~~~~~~~~~~~~~~~~~~~ * Added support for Django 2.1 (Ryan Verner, Matt Westcott) + * Added 'scale' image filter (Oliver Wilkerson) 2.2 (xx.xx.xxx) - IN DEVELOPMENT diff --git a/CONTRIBUTORS.rst b/CONTRIBUTORS.rst index 219b68ae81..7ddb27ac5c 100644 --- a/CONTRIBUTORS.rst +++ b/CONTRIBUTORS.rst @@ -312,6 +312,7 @@ Contributors * Aram Dulyan * Kevin Howbrook * Ryan Verner +* Oliver Wilkerson Translators =========== diff --git a/docs/releases/2.3.rst b/docs/releases/2.3.rst index 8c69f3b83e..b4dce5cceb 100644 --- a/docs/releases/2.3.rst +++ b/docs/releases/2.3.rst @@ -22,6 +22,9 @@ Wagtail is now compatible with Django 2.1. Compatibility fixes were contributed Other features ~~~~~~~~~~~~~~ + * Added 'scale' image filter (Oliver Wilkerson) + + Bug fixes ~~~~~~~~~ diff --git a/docs/topics/images.rst b/docs/topics/images.rst index d86a5d7d02..34db8b134c 100644 --- a/docs/topics/images.rst +++ b/docs/topics/images.rst @@ -88,6 +88,15 @@ The available resizing methods are as follows: Resize the height of the image to the dimension specified. + ``scale`` + (takes percentage) + + .. code-block:: html+django + + {% image page.photo scale-50 %} + + Resize the image to the percentage specified. + ``fill`` (takes two dimensions and an optional ``-c`` parameter) diff --git a/wagtail/images/image_operations.py b/wagtail/images/image_operations.py index 5cd49e0615..6caaaef978 100644 --- a/wagtail/images/image_operations.py +++ b/wagtail/images/image_operations.py @@ -217,6 +217,20 @@ class WidthHeightOperation(Operation): return willow.resize((width, height)) +class ScaleOperation(Operation): + def construct(self, percent): + self.percent = float(percent) + + def run(self, willow, image, env): + image_width, image_height = willow.get_size() + + scale = self.percent / 100 + width = float(image_width * scale) + height = float(image_height * scale) + + return willow.resize((width, height)) + + class JPEGQualityOperation(Operation): def construct(self, quality): self.quality = int(quality) diff --git a/wagtail/images/tests/test_image_operations.py b/wagtail/images/tests/test_image_operations.py index da131b69f1..66f01187e8 100644 --- a/wagtail/images/tests/test_image_operations.py +++ b/wagtail/images/tests/test_image_operations.py @@ -362,6 +362,40 @@ class TestWidthHeightOperation(ImageOperationTestCase): TestWidthHeightOperation.setup_test_methods() +class TestScaleOperation(ImageOperationTestCase): + operation_class = image_operations.ScaleOperation + + filter_spec_tests = [ + ('scale-100', dict(method='scale', percent=100)), + ('scale-50', dict(method='scale', percent=50)), + ] + + filter_spec_error_tests = [ + 'scale', + 'scale-800x600', + 'scale-abc', + 'scale-800-c100', + ] + + run_tests = [ + # Basic almost a no-op of scale + ('scale-100', dict(width=1000, height=500), [ + ('resize', ((1000, 500), ), {}), + ]), + # Basic usage of scale + ('scale-50', dict(width=1000, height=500), [ + ('resize', ((500, 250), ), {}), + ]), + # Rounded usage of scale + ('scale-83.0322', dict(width=1000, height=500), [ + ('resize', ((1000 * 0.830322, 500 * 0.830322), ), {}), + ]), + ] + + +TestScaleOperation.setup_test_methods() + + class TestCacheKey(TestCase): def test_cache_key(self): image = Image(width=1000, height=1000) diff --git a/wagtail/images/wagtail_hooks.py b/wagtail/images/wagtail_hooks.py index 7f163f8caf..ce98cc614a 100644 --- a/wagtail/images/wagtail_hooks.py +++ b/wagtail/images/wagtail_hooks.py @@ -113,6 +113,7 @@ def register_image_operations(): ('max', image_operations.MinMaxOperation), ('width', image_operations.WidthHeightOperation), ('height', image_operations.WidthHeightOperation), + ('scale', image_operations.ScaleOperation), ('jpegquality', image_operations.JPEGQualityOperation), ('format', image_operations.FormatOperation), ('bgcolor', image_operations.BackgroundColorOperation),