Skip to content

Commit

Permalink
Fix to the way crop rects are rounded
Browse files Browse the repository at this point in the history
Eight and bottom were previously rounded down which sometimes led to images having a width/height of 0

This fixes a ZeroDivisionError and potential "tile cannot extend outside image" errors when resizing JPEGs
  • Loading branch information
kaedroho committed Aug 27, 2015
1 parent 4901f5d commit 217e962
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
3 changes: 2 additions & 1 deletion wagtail/wagtailimages/image_operations.py
@@ -1,6 +1,7 @@
from __future__ import division

import inspect
import math

from wagtail.wagtailimages.exceptions import InvalidFilterSpecError

Expand Down Expand Up @@ -158,7 +159,7 @@ def run(self, willow, image):
bottom = image_height

# Crop!
willow.crop((int(left), int(top), int(right), int(bottom)))
willow.crop((math.floor(left), math.floor(top), math.ceil(right), math.ceil(bottom)))

# Get scale for resizing
# The scale should be the same for both the horizontal and
Expand Down
29 changes: 27 additions & 2 deletions wagtail/wagtailimages/tests/test_image_operations.py
Expand Up @@ -154,7 +154,7 @@ class TestFillOperation(ImageOperationTestCase):
# Basic usage with an oddly-sized original image
# This checks for a rounding precision issue (#968)
('fill-200x200', Image(width=539, height=720), [
('crop', ((0, 90, 539, 629), ), {}),
('crop', ((0, 90, 539, 630), ), {}),
('resize', ((200, 200), ), {}),
]),

Expand Down Expand Up @@ -258,7 +258,32 @@ class TestFillOperation(ImageOperationTestCase):
), [
# This operation could probably be optimised out
('crop', ((0, 0, 1500, 1500), ), {}),
])
]),


# A few tests for single pixel images

('fill-100x100', Image(
width=1,
height=1,
), [
('crop', ((0, 0, 1, 1), ), {}),
]),

# This one once gave a ZeroDivisionError
('fill-100x150', Image(
width=1,
height=1,
), [
('crop', ((0, 0, 1, 1), ), {}),
]),

('fill-150x100', Image(
width=1,
height=1,
), [
('crop', ((0, 0, 1, 1), ), {}),
]),
]

TestFillOperation.setup_test_methods()
Expand Down

0 comments on commit 217e962

Please sign in to comment.