Skip to content

Commit

Permalink
Merge pull request #623 from gonzalodelgado/master
Browse files Browse the repository at this point in the history
Fix #622 address ZeroDivisionError in scale_and_crop.
  • Loading branch information
jrief committed Sep 11, 2024
2 parents acb666f + 9a7adcc commit 4bdef07
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
10 changes: 8 additions & 2 deletions easy_thumbnails/processors.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,9 +215,15 @@ def scale_and_crop(im, size, crop=False, upscale=False, zoom=None, target=None,
target_x, target_y = [int(v) for v in size]

if crop or not target_x or not target_y:
scale = max(target_x / source_x, target_y / source_y)
scale = max(
1.0 if source_x == 0 else target_x / source_x,
1.0 if source_y == 0 else target_y / source_y,
)
else:
scale = min(target_x / source_x, target_y / source_y)
scale = min(
1.0 if source_x == 0 else target_x / source_x,
1.0 if source_y == 0 else target_y / source_y,
)

# Handle one-dimensional targets.
if not target_x:
Expand Down
10 changes: 10 additions & 0 deletions easy_thumbnails/tests/test_svg_processors.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ def test_scale(self):
self.assertEqual(upscaled.size, (1000, 750))
self.assertEqual(upscaled.getbbox(), (0, 0, 800, 600))

empty = processors.scale_and_crop(create_image(size=(0, 0)), (1000, 1000))
self.assertEqual(empty.size, (0, 0))
self.assertEqual(empty.getbbox(), (0, 0, 0, 0))

def test_crop(self):
image = create_image()

Expand All @@ -49,3 +53,9 @@ def test_crop(self):
upscaled = processors.scale_and_crop(image, (1000, 1000), crop=True, upscale=True)
self.assertEqual(upscaled.size, (1000, 1000))
self.assertEqual(upscaled.getbbox(), (100, 0, 600, 600))

empty = processors.scale_and_crop(
create_image(size=(0, 0)), (1000, 1000), crop=True
)
self.assertEqual(empty.size, (0, 0))
self.assertEqual(empty.getbbox(), (0, 0, 0, 0))

0 comments on commit 4bdef07

Please sign in to comment.