Gracefully handle out-of-bounds crops #92
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
When passed a crop rectangle that extends outside an image's boundaries, Pillow gracefully returns an image of the requested size (padding with background / transparent pixels as required), but Wand throws a value error. If the calling app is sloppy about dimensions (as is apparently the case in wagtail/wagtail#7766), this will result in errors that only surface when Wand is in use (i.e. for animated gifs).
This PR gives the two backends a consistent "middle ground" behaviour that hopefully makes intuitive sense: the crop rectangle is clamped to the image boundary so that if it overlaps, the final image is smaller than requested. Totally out of bounds or nonsensical parameters (e.g. left > right) will raise a BadImageOperationError.
Fixes wagtail/wagtail#7766, although we'll also want to fix the rounding error that leads to the out-of-bounds condition in the first place.