From 19309b02e73cfdfe9d908882c8b37d8c1a7a00c2 Mon Sep 17 00:00:00 2001 From: Karl Hobley Date: Mon, 3 Nov 2014 13:37:30 +0000 Subject: [PATCH] Added get_vary and get_vary_key to Filter This allows a filter operation to specify which strings of data it used inside the image to perform its calculations. This can be used to determine whether an image has changed in such a way that requires new renditions to be made. --- wagtail/wagtailimages/image_operations.py | 15 +++++++++++++++ wagtail/wagtailimages/models.py | 17 +++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/wagtail/wagtailimages/image_operations.py b/wagtail/wagtailimages/image_operations.py index 2681e63fc7..8699bfcedb 100644 --- a/wagtail/wagtailimages/image_operations.py +++ b/wagtail/wagtailimages/image_operations.py @@ -147,6 +147,21 @@ class FillOperation(object): willow.resize(width, height) + def get_vary(self, image): + focal_point = image.get_focal_point() + + if focal_point is not None: + focal_point_key = "%(x)d-%(y)d-%(width)dx%(height)d" % { + 'x': int(focal_point.centroid_x), + 'y': int(focal_point.centroid_y), + 'width': int(focal_point.width), + 'height': int(focal_point.height), + } + else: + focal_point_key = '' + + return [focal_point_key] + class MinMaxOperation(object): def __init__(self, method, size): diff --git a/wagtail/wagtailimages/models.py b/wagtail/wagtailimages/models.py index 6209ad98f1..26236c4a03 100644 --- a/wagtail/wagtailimages/models.py +++ b/wagtail/wagtailimages/models.py @@ -1,4 +1,5 @@ import os.path +import hashlib import re from six import BytesIO, text_type @@ -428,6 +429,22 @@ class Filter(models.Model): return output + def get_vary(self, image): + vary = [] + + for operation in self.operations: + if hasattr(operation, 'get_vary'): + vary.extend(operation.get_vary(image)) + + return vary + + def get_vary_key(self, image): + vary_string = '-'.join(self.get_vary(image)) + vary_key = hashlib.sha1(vary_string.encode('utf-8')).hexdigest() + + return vary_key[:8] + + _registered_operations = None @classmethod