Skip to content

Commit 2f7adb5

Browse files
committed
Add bestfit option
1 parent 6059525 commit 2f7adb5

2 files changed

Lines changed: 37 additions & 1 deletion

File tree

src/Image.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ public static function resize(\Imagick $source, int $boxWidth, int $boxHeight, a
3030
*
3131
* @throws \InvalidArgumentException if $options["color"] was not a string
3232
* @throws \InvalidArgumentException if $options["upsize"] was not a bool
33+
* @throws \InvalidArgumentException if $options["bestfit"] was not a bool
3334
* @throws \InvalidArgumentException if $options["maxWidth"] was not an int
3435
* @throws \InvalidArgumentException if $options["maxHeight"] was not an int
3536
* @throws \InvalidArgumentException if a width in a $boxSizes value was not an int
@@ -60,6 +61,14 @@ public static function resizeMulti(\Imagick $source, array $boxSizes, array $opt
6061
}
6162
}
6263

64+
$bestfit = false;
65+
if (isset($options['bestfit'])) {
66+
$bestfit = $options['bestfit'];
67+
if ($bestfit !== true && $bestfit !== false) {
68+
throw new \InvalidArgumentException('$options["bestfit"] was not a bool');
69+
}
70+
}
71+
6372
$maxWidth = 10000;
6473
if (isset($options['maxWidth'])) {
6574
$maxWidth = $options['maxWidth'];
@@ -198,7 +207,7 @@ public static function resizeMulti(\Imagick $source, array $boxSizes, array $opt
198207
}
199208

200209
if ($upsize && ($width < $targetWidth || $height < $targetHeight)) {
201-
if ($clone->resizeImage($targetWidth, $targetHeight, \Imagick::FILTER_CUBIC, 1.0) !== true) {
210+
if ($clone->resizeImage($targetWidth, $targetHeight, \Imagick::FILTER_CUBIC, 1.0, $bestfit) !== true) {
202211
//cumbersome to test
203212
throw new \Exception('Imagick::resizeImage() did not return true');//@codeCoverageIgnore
204213
}

tests/ImageTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,21 @@ public function resizeUpsizeToMoreVerticalAspect()
215215
$this->assertLessThan(0.1, $imageBottom['luminosity']);
216216
}
217217

218+
/**
219+
* @test
220+
* @covers ::resize
221+
* @covers ::resizeMulti
222+
*/
223+
public function resizeWithUpsizeAndBestFit()
224+
{
225+
$source = new \Imagick('pattern:gray0');
226+
$source->scaleImage(85, 45);
227+
$notBestFit = Image::resize($source, 300, 300, ['upsize' => true, 'bestfit' => false]);
228+
$this->assertSame('srgb(0,0,0)', $notBestFit->getImagePixelColor(299, 100)->getColorAsString());
229+
$bestFit = Image::resize($source, 300, 300, ['upsize' => true, 'bestfit' => true]);
230+
$this->assertSame('srgb(255,255,255)', $bestFit->getImagePixelColor(299, 100)->getColorAsString());
231+
}
232+
218233
/**
219234
* @test
220235
* @covers ::resize
@@ -311,6 +326,18 @@ public function resizeNonBoolUpsize()
311326
Image::resize(new \Imagick(), 10, 10, ['upsize' => 'not bool']);
312327
}
313328

329+
/**
330+
* @test
331+
* @covers ::resize
332+
* @covers ::resizeMulti
333+
* @expectedException \InvalidArgumentException
334+
* @expectedExceptionMessage $options["bestfit"] was not a bool
335+
*/
336+
public function resizeNonBoolBestFit()
337+
{
338+
Image::resize(new \Imagick(), 10, 10, ['bestfit' => 'not bool']);
339+
}
340+
314341
/**
315342
* Verify images are rotated according to EXIF header
316343
* @test

0 commit comments

Comments
 (0)