diff --git a/src/wagtail_bynder/models.py b/src/wagtail_bynder/models.py index 61f603e..2fa43cd 100644 --- a/src/wagtail_bynder/models.py +++ b/src/wagtail_bynder/models.py @@ -242,7 +242,7 @@ def set_focal_area_from_focus_point( self, x: int, y: int, original_height: int, original_width: int ) -> None: """ - Using the provided center-point coordinates, generate a + Using the provided focus point coordinates, generate a 2D focal area for the downloaded image. """ if x < 0 or y < 0 or x > original_width or y > original_height: @@ -261,11 +261,21 @@ def set_focal_area_from_focus_point( self.focal_point_x = x self.focal_point_y = y + # Draw a rectangle around the centre point # For the width, span outwards until we hit the left or right bounds - self.focal_point_width = min(x, self.width - x) * 2 + rect_width = min(x, self.width - x) * 2 + # Restrict rectangle width to 40% of the image height + rect_width = min(rect_width, math.floor(self.width * 0.4)) # For the height, span outwards until we hit the top or bottom bounds - self.focal_point_height = min(y, self.height - y) * 2 + rect_height = min(y, self.height - y) * 2 + # Restrict rectangle height to 40% of the image height + rect_height = min(rect_height, math.floor(self.height * 0.4)) + + # Use the shortest side to make a square + width = min(rect_width, rect_height) + self.focal_point_width = width + self.focal_point_height = width @staticmethod def extract_file_source(asset_data: dict[str, Any]) -> str: diff --git a/tests/test_models.py b/tests/test_models.py index 829952c..df01d64 100644 --- a/tests/test_models.py +++ b/tests/test_models.py @@ -216,8 +216,8 @@ def test_update_from_asset_data(self): self.assertEqual(self.obj.is_public, self.asset_data["isPublic"] == 1) self.assertEqual(self.obj.focal_point_x, 13) self.assertEqual(self.obj.focal_point_y, 13) - self.assertEqual(self.obj.focal_point_height, 26) - self.assertEqual(self.obj.focal_point_width, 26) + self.assertEqual(self.obj.focal_point_height, 20) + self.assertEqual(self.obj.focal_point_width, 20) self.assertTrue(self.obj._focal_point_changed) def test_update_from_asset_data_with_focal_point_change(self): @@ -246,8 +246,8 @@ def test_update_from_asset_data_without_focal_point_change(self): # Set the focal point values to reflect the typical test outcome self.obj.focal_point_x = 13 self.obj.focal_point_y = 13 - self.obj.focal_point_height = 26 - self.obj.focal_point_width = 26 + self.obj.focal_point_height = 20 + self.obj.focal_point_width = 20 current_focal_point = self.obj.get_focal_point()