Skip to content

Commit

Permalink
SimpleResizer calculation in round() can get return invalid value f…
Browse files Browse the repository at this point in the history
…or image resizing.
  • Loading branch information
silasjoisten committed Jul 21, 2023
1 parent 670a90f commit 105b852
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/Resizer/SimpleResizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,11 @@ public function getBox(MediaInterface $media, array $settings)
}

if (null === $settings['height']) {
$settings['height'] = (int) round($settings['width'] * $size->getHeight() / $size->getWidth());
$settings['height'] = max((int) round($settings['width'] * $size->getHeight() / $size->getWidth()), 1);
}

if (null === $settings['width']) {
$settings['width'] = (int) round($settings['height'] * $size->getWidth() / $size->getHeight());
$settings['width'] = max((int) round($settings['height'] * $size->getWidth() / $size->getHeight()), 1);
}

return $this->computeBox($media, $settings);
Expand Down
4 changes: 4 additions & 0 deletions tests/Resizer/SimpleResizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,16 @@ public static function getBoxSettings(): array
['inset', ['width' => 90, 'height' => null], new Box(50, 50), new Box(90, 90)],
['inset', ['width' => 90, 'height' => null], new Box(567, 200), new Box(90, 32)],
['inset', ['width' => 100, 'height' => 100], new Box(567, 200), new Box(100, 35)],
['inset', ['width' => 1060, 'height' => 1], new Box(567, 200), new Box(3, 1)],
['inset', ['width' => 1, 'height' => 1060], new Box(567, 200), new Box(1, 1)],

['outbound', ['width' => 90, 'height' => 90], new Box(100, 120), new Box(90, 90)],
['outbound', ['width' => 90, 'height' => 90], new Box(120, 100), new Box(90, 90)],
['outbound', ['width' => 90, 'height' => 90], new Box(50, 50), new Box(90, 90)],
['outbound', ['width' => 90, 'height' => null], new Box(50, 50), new Box(90, 90)],
['outbound', ['width' => 90, 'height' => null], new Box(567, 50), new Box(90, 8)],
['outbound', ['width' => 1060, 'height' => 1], new Box(567, 200), new Box(1060, 1)],
['outbound', ['width' => 1, 'height' => 1060], new Box(567, 200), new Box(1, 1060)],
];
}
}

0 comments on commit 105b852

Please sign in to comment.