From 4757c1633c2680d057a6cbee7b122f8ebf492103 Mon Sep 17 00:00:00 2001 From: Karl Hobley Date: Tue, 16 Sep 2014 15:10:40 +0100 Subject: [PATCH] Make sure the entire focal point is in the crop --- wagtail/wagtailimages/backends/base.py | 40 ++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/wagtail/wagtailimages/backends/base.py b/wagtail/wagtailimages/backends/base.py index 401733ae93..3d7d4957a6 100644 --- a/wagtail/wagtailimages/backends/base.py +++ b/wagtail/wagtailimages/backends/base.py @@ -202,6 +202,46 @@ class BaseImageBackend(object): right = crop_x + crop_width / 2 bottom = crop_y + crop_height / 2 + # Make sure the entire focal point is in the crop box + if focal_point is not None: + focal_point_left = focal_point.x - focal_point.width / 2 + focal_point_top = focal_point.y - focal_point.height / 2 + focal_point_right = focal_point.x + focal_point.width / 2 + focal_point_bottom = focal_point.y + focal_point.height / 2 + + if left > focal_point_left: + right -= left - focal_point_left + left = focal_point_left + + if top > focal_point_top: + bottom -= top - focal_point_top + top = focal_point_top + + if right < focal_point_right: + left += focal_point_right - right; + right = focal_point_right + + if bottom < focal_point_bottom: + top += focal_point_bottom - bottom; + bottom = focal_point_bottom + + # Don't allow the crop box to go over the image boundary + if left < 0: + right -= left + left = 0 + + if top < 0: + bottom -= top + top = 0 + + if right > im_width: + left -= right - im_width + right = im_width + + if bottom > im_height: + top -= bottom - im_height + bottom = im_height + # Crop! return self.resize_to_min(self.crop(image, Rect(left, top, right, bottom)), size)