Skip to content

Make the $filter parameter of the resize method invariant #776

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/Gd/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -184,12 +184,14 @@ final public function paste(ImageInterface $image, PointInterface $start, $alpha
/**
* {@inheritdoc}
*
* Please remark that GD doesn't support different filters, so the $filter argument is ignored.
*
* @see \Imagine\Image\ManipulatorInterface::resize()
*/
final public function resize(BoxInterface $size, $filter = ImageInterface::FILTER_UNDEFINED)
{
if ($filter !== ImageInterface::FILTER_UNDEFINED) {
throw new InvalidArgumentException('Unsupported filter type, GD only supports ImageInterface::FILTER_UNDEFINED filter');
if (!in_array($filter, static::getAllFilterValues(), true)) {
throw new InvalidArgumentException('Unsupported filter type');
}

$width = $size->getWidth();
Expand Down
5 changes: 4 additions & 1 deletion src/Gmagick/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,9 @@ public function paste(ImageInterface $image, PointInterface $start, $alpha = 100
*/
public function resize(BoxInterface $size, $filter = ImageInterface::FILTER_UNDEFINED)
{
if (!in_array($filter, static::getAllFilterValues(), true)) {
throw new InvalidArgumentException('Unsupported filter type');
}
static $supportedFilters = array(
ImageInterface::FILTER_UNDEFINED => \Gmagick::FILTER_UNDEFINED,
ImageInterface::FILTER_BESSEL => \Gmagick::FILTER_BESSEL,
Expand All @@ -242,7 +245,7 @@ public function resize(BoxInterface $size, $filter = ImageInterface::FILTER_UNDE
);

if (!array_key_exists($filter, $supportedFilters)) {
throw new InvalidArgumentException('Unsupported filter type');
$filter = ImageInterface::FILTER_UNDEFINED;
}

try {
Expand Down
22 changes: 22 additions & 0 deletions src/Image/AbstractImage.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,28 @@ protected function updateSaveOptions(array $options)
return $options;
}

/**
* Get all the available filter defined in ImageInterface.
*
* @return string[]
*/
protected static function getAllFilterValues()
{
static $result;
if (!is_array($result)) {
$values = array();
$interface = new \ReflectionClass('Imagine\Image\ImageInterface');
foreach ($interface->getConstants() as $name => $value) {
if (strpos($name, 'FILTER_') === 0) {
$values[] = $value;
}
}
$result = $values;
}

return $result;
}

/**
* {@inheritdoc}
*
Expand Down
9 changes: 4 additions & 5 deletions src/Imagick/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -1086,12 +1086,11 @@ private function getFilter($filter = ImageInterface::FILTER_UNDEFINED)
ImageInterface::FILTER_SINC => \Imagick::FILTER_SINC,
ImageInterface::FILTER_TRIANGLE => \Imagick::FILTER_TRIANGLE,
);

if (!in_array($filter, static::getAllFilterValues(), true)) {
throw new InvalidArgumentException('Unsupported filter type');
}
if (!array_key_exists($filter, $supportedFilters)) {
throw new InvalidArgumentException(sprintf(
'The resampling filter "%s" is not supported by Imagick driver.',
$filter
));
$filter = ImageInterface::FILTER_UNDEFINED;
}

return $supportedFilters[$filter];
Expand Down