Skip to content

Commit bdb60c7

Browse files
committed
Add blur option
1 parent 3c7deaa commit bdb60c7

4 files changed

Lines changed: 35 additions & 6 deletions

File tree

src/Image.php

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace TraderInteractive\Util;
44

5+
use TraderInteractive\Util;
6+
57
final class Image
68
{
79
/**
@@ -214,12 +216,7 @@ public static function resizeMulti(\Imagick $source, array $boxSizes, array $opt
214216
}
215217

216218
//put image in box
217-
$canvas = new \Imagick();
218-
if ($canvas->newImage($boxWidth, $boxHeight, $color) !== true) {
219-
//cumbersome to test
220-
throw new \Exception('Imagick::newImage() did not return true');//@codeCoverageIgnore
221-
}
222-
219+
$canvas = self::getBackgroundCanvas($clone, $color, $boxWidth, $boxHeight);
223220
if ($canvas->compositeImage($clone, \Imagick::COMPOSITE_ATOP, $targetX, $targetY) !== true) {
224221
//cumbersome to test
225222
throw new \Exception('Imagick::compositeImage() did not return true');//@codeCoverageIgnore
@@ -234,6 +231,21 @@ public static function resizeMulti(\Imagick $source, array $boxSizes, array $opt
234231
return $results;
235232
}
236233

234+
private static function getBackgroundCanvas(\Imagick $source, string $color, int $boxWidth, $boxHeight)
235+
{
236+
if ($color === 'blur') {
237+
$canvas = new \Imagick();
238+
$canvas->readImageBlob($source->getImageBlob());
239+
$canvas->resizeImage($boxWidth, $boxHeight, \Imagick::FILTER_BOX, 15.0, false);
240+
return $canvas;
241+
}
242+
243+
$canvas = new \Imagick();
244+
$imageCreated = $canvas->newImage($boxWidth, $boxHeight, $color);
245+
Util::ensure(true, $imageCreated, 'Imagick::newImage() did not return true');
246+
return $canvas;
247+
}
248+
237249
/**
238250
* write $source to $destPath with $options applied
239251
*

tests/ImageTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,23 @@ public function resizeWithUpsizeAndBestFit()
230230
$this->assertSame('srgb(255,255,255)', $bestFit->getImagePixelColor(299, 100)->getColorAsString());
231231
}
232232

233+
/**
234+
* @test
235+
* @covers ::resize
236+
* @covers ::resizeMulti
237+
*/
238+
public function resizeWithBurredBackground()
239+
{
240+
$source = new \Imagick();
241+
$source->readImage(__DIR__ . '/_files/portrait.jpg');
242+
$actual = Image::resize($source, 1024, 768, ['upsize' => true, 'bestfit' => false, 'color' => 'blur']);
243+
244+
$expected = new \Imagick();
245+
$expected->readImage(__DIR__ . '/_files/blur.jpg');
246+
$comparison = $expected->compareImages($actual, \Imagick::METRIC_UNDEFINED);
247+
$this->assertGreaterThanOrEqual(.999, $comparison[1]);
248+
}
249+
233250
/**
234251
* @test
235252
* @covers ::resize

tests/_files/blur.jpg

57.2 KB
Loading

tests/_files/portrait.jpg

39.1 KB
Loading

0 commit comments

Comments
 (0)