Skip to content

Commit 904bd07

Browse files
authored
Merge pull request #776 from mlocati/gd-resize-filter
Make the $filter parameter of the resize method invariant
2 parents c442f5e + 2856a84 commit 904bd07

File tree

4 files changed

+34
-8
lines changed

4 files changed

+34
-8
lines changed

src/Gd/Image.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,12 +184,14 @@ final public function paste(ImageInterface $image, PointInterface $start, $alpha
184184
/**
185185
* {@inheritdoc}
186186
*
187+
* Please remark that GD doesn't support different filters, so the $filter argument is ignored.
188+
*
187189
* @see \Imagine\Image\ManipulatorInterface::resize()
188190
*/
189191
final public function resize(BoxInterface $size, $filter = ImageInterface::FILTER_UNDEFINED)
190192
{
191-
if ($filter !== ImageInterface::FILTER_UNDEFINED) {
192-
throw new InvalidArgumentException('Unsupported filter type, GD only supports ImageInterface::FILTER_UNDEFINED filter');
193+
if (!in_array($filter, static::getAllFilterValues(), true)) {
194+
throw new InvalidArgumentException('Unsupported filter type');
193195
}
194196

195197
$width = $size->getWidth();

src/Gmagick/Image.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,9 @@ public function paste(ImageInterface $image, PointInterface $start, $alpha = 100
222222
*/
223223
public function resize(BoxInterface $size, $filter = ImageInterface::FILTER_UNDEFINED)
224224
{
225+
if (!in_array($filter, static::getAllFilterValues(), true)) {
226+
throw new InvalidArgumentException('Unsupported filter type');
227+
}
225228
static $supportedFilters = array(
226229
ImageInterface::FILTER_UNDEFINED => \Gmagick::FILTER_UNDEFINED,
227230
ImageInterface::FILTER_BESSEL => \Gmagick::FILTER_BESSEL,
@@ -242,7 +245,7 @@ public function resize(BoxInterface $size, $filter = ImageInterface::FILTER_UNDE
242245
);
243246

244247
if (!array_key_exists($filter, $supportedFilters)) {
245-
throw new InvalidArgumentException('Unsupported filter type');
248+
$filter = ImageInterface::FILTER_UNDEFINED;
246249
}
247250

248251
try {

src/Image/AbstractImage.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,28 @@ protected function updateSaveOptions(array $options)
168168
return $options;
169169
}
170170

171+
/**
172+
* Get all the available filter defined in ImageInterface.
173+
*
174+
* @return string[]
175+
*/
176+
protected static function getAllFilterValues()
177+
{
178+
static $result;
179+
if (!is_array($result)) {
180+
$values = array();
181+
$interface = new \ReflectionClass('Imagine\Image\ImageInterface');
182+
foreach ($interface->getConstants() as $name => $value) {
183+
if (strpos($name, 'FILTER_') === 0) {
184+
$values[] = $value;
185+
}
186+
}
187+
$result = $values;
188+
}
189+
190+
return $result;
191+
}
192+
171193
/**
172194
* {@inheritdoc}
173195
*

src/Imagick/Image.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1095,12 +1095,11 @@ private function getFilter($filter = ImageInterface::FILTER_UNDEFINED)
10951095
ImageInterface::FILTER_SINC => \Imagick::FILTER_SINC,
10961096
ImageInterface::FILTER_TRIANGLE => \Imagick::FILTER_TRIANGLE,
10971097
);
1098-
1098+
if (!in_array($filter, static::getAllFilterValues(), true)) {
1099+
throw new InvalidArgumentException('Unsupported filter type');
1100+
}
10991101
if (!array_key_exists($filter, $supportedFilters)) {
1100-
throw new InvalidArgumentException(sprintf(
1101-
'The resampling filter "%s" is not supported by Imagick driver.',
1102-
$filter
1103-
));
1102+
$filter = ImageInterface::FILTER_UNDEFINED;
11041103
}
11051104

11061105
return $supportedFilters[$filter];

0 commit comments

Comments
 (0)