diff --git a/wagtail/wagtailimages/backends/base.py b/wagtail/wagtailimages/backends/base.py index afaf550703..865b405f74 100644 --- a/wagtail/wagtailimages/backends/base.py +++ b/wagtail/wagtailimages/backends/base.py @@ -1,5 +1,7 @@ from __future__ import division +import math + from django.conf import settings from wagtail.wagtailimages.rect import Rect @@ -242,7 +244,9 @@ class BaseImageBackend(object): bottom = im_height # Crop! - return self.resize_to_min(self.crop(image, Rect(left, top, right, bottom)), size) + return self.resize_to_min(self.crop(image, + Rect(math.floor(left), math.floor(top), math.ceil(right), math.ceil(bottom)) + ), size) def no_operation(self, image, param, focal_point=None): """Return the image unchanged""" diff --git a/wagtail/wagtailimages/tests/test_models.py b/wagtail/wagtailimages/tests/test_models.py index b867117c68..19c7975364 100644 --- a/wagtail/wagtailimages/tests/test_models.py +++ b/wagtail/wagtailimages/tests/test_models.py @@ -164,6 +164,28 @@ class TestRenditions(TestCase): self.assertEqual(first_rendition, second_rendition) +class TestSinglePixelRenditions(TestCase): + def setUp(self): + # Create an image for running tests on + self.image = Image.objects.create( + title="Test image", + file=get_test_image_file(width=1, height=1), + ) + + def test_resize(self): + rendition = self.image.get_rendition('fill-100x100') + self.assertEqual(rendition.width, 1) + self.assertEqual(rendition.height, 1) + + rendition = self.image.get_rendition('fill-100x150') + self.assertEqual(rendition.width, 1) + self.assertEqual(rendition.height, 1) + + rendition = self.image.get_rendition('fill-150x100') + self.assertEqual(rendition.width, 1) + self.assertEqual(rendition.height, 1) + + class TestRenditionsWand(TestCase): def setUp(self): try: diff --git a/wagtail/wagtailimages/tests/utils.py b/wagtail/wagtailimages/tests/utils.py index 62b88126f8..e5eb3db311 100644 --- a/wagtail/wagtailimages/tests/utils.py +++ b/wagtail/wagtailimages/tests/utils.py @@ -9,8 +9,8 @@ from wagtail.wagtailimages.models import get_image_model Image = get_image_model() -def get_test_image_file(filename='test.png'): +def get_test_image_file(filename='test.png', width=640, height=480): f = BytesIO() - image = PIL.Image.new('RGB', (640, 480), 'white') + image = PIL.Image.new('RGB', (width, height), 'white') image.save(f, 'PNG') return ImageFile(f, name=filename)