22
33namespace TraderInteractive \Util ;
44
5+ use PHPUnit \Framework \Constraint \GreaterThan ;
56use PHPUnit \Framework \TestCase ;
67
78/**
@@ -231,13 +232,10 @@ public function resizeWithUpsizeAndBestFit()
231232 */
232233 public function resizeWithColorOfBlur ()
233234 {
234- $ source = new \Imagick ();
235- $ source ->readImage (__DIR__ . '/_files/portrait.jpg ' );
235+ $ source = $ this ->getTestImage ('portrait.jpg ' );
236236 $ actual = Image::resize ($ source , 1024 , 768 , ['upsize ' => true , 'bestfit ' => false , 'color ' => 'blur ' ]);
237- $ expected = new \Imagick ();
238- $ expected ->readImage (__DIR__ . '/_files/blur.jpg ' );
239- $ comparison = $ expected ->compareImages ($ actual , \Imagick::METRIC_UNDEFINED );
240- $ this ->assertGreaterThanOrEqual (.99 , $ comparison [1 ]);
237+ $ expected = $ this ->getTestImage ('blur.jpg ' );
238+ $ this ->assertSameImage ($ expected , $ actual );
241239 }
242240
243241 /**
@@ -246,14 +244,10 @@ public function resizeWithColorOfBlur()
246244 */
247245 public function resizeWithBlurBackground ()
248246 {
249- $ source = new \Imagick ();
250- $ source ->readImage (__DIR__ . '/_files/portrait.jpg ' );
247+ $ source = $ this ->getTestImage ('portrait.jpg ' );
251248 $ actual = Image::resize ($ source , 1024 , 768 , ['upsize ' => true , 'bestfit ' => false , 'blurBackground ' => true ]);
252-
253- $ expected = new \Imagick ();
254- $ expected ->readImage (__DIR__ . '/_files/blur.jpg ' );
255- $ comparison = $ expected ->compareImages ($ actual , \Imagick::METRIC_UNDEFINED );
256- $ this ->assertGreaterThanOrEqual (.99 , $ comparison [1 ]);
249+ $ expected = $ this ->getTestImage ('blur.jpg ' );
250+ $ this ->assertSameImage ($ expected , $ actual );
257251 }
258252
259253 /**
@@ -262,15 +256,11 @@ public function resizeWithBlurBackground()
262256 */
263257 public function resizeWithBurredBackgroundWithCustomBlurValue ()
264258 {
265- $ source = new \Imagick ();
266- $ source ->readImage (__DIR__ . '/_files/portrait.jpg ' );
259+ $ source = $ this ->getTestImage ('portrait.jpg ' );
267260 $ options = ['upsize ' => true , 'bestfit ' => false , 'blurBackground ' => true , 'blurValue ' => 30.0 ];
268261 $ actual = Image::resize ($ source , 1024 , 768 , $ options );
269-
270- $ expected = new \Imagick ();
271- $ expected ->readImage (__DIR__ . '/_files/blur-30.jpg ' );
272- $ comparison = $ expected ->compareImages ($ actual , \Imagick::METRIC_UNDEFINED );
273- $ this ->assertGreaterThanOrEqual (.999 , $ comparison [1 ]);
262+ $ expected = $ this ->getTestImage ('blur-30.jpg ' );
263+ $ this ->assertSameImage ($ expected , $ actual );
274264 }
275265
276266 /**
@@ -539,6 +529,66 @@ public function resizeMultiNonIntHeight()
539529 Image::resizeMulti (new \Imagick (), [['width ' => 10 , 'height ' => true ]]);
540530 }
541531
532+ /**
533+ * @test
534+ * @covers ::resize
535+ */
536+ public function resizeTransparentImageWithTransparentBackground ()
537+ {
538+ $ source = $ this ->getTestImage ('transparent.png ' );
539+ $ actual = Image::resize ($ source , 128 , 128 , ['color ' => 'transparent ' ]);
540+ $ expected = $ this ->getTestImage ('transparent-resize.png ' );
541+ $ this ->assertSameImage ($ expected , $ actual );
542+ }
543+
544+ /**
545+ * @test
546+ * @covers ::resize
547+ */
548+ public function resizeTransparentImageWithColorBackground ()
549+ {
550+ $ source = $ this ->getTestImage ('transparent.png ' );
551+ $ actual = Image::resize ($ source , 128 , 128 , ['color ' => 'green ' ]);
552+ $ expected = $ this ->getTestImage ('transparent-resize-color.png ' );
553+ $ this ->assertSameImage ($ expected , $ actual );
554+ }
555+
556+ /**
557+ * @test
558+ * @covers ::resize
559+ */
560+ public function resizeTransparentImageWithUpsize ()
561+ {
562+ $ source = $ this ->getTestImage ('transparent.png ' );
563+ $ actual = Image::resize ($ source , 128 , 128 , ['upsize ' => true ]);
564+ $ expected = $ this ->getTestImage ('transparent-upsize.png ' );
565+ $ this ->assertSameImage ($ expected , $ actual );
566+ }
567+
568+ /**
569+ * @test
570+ * @covers ::resize
571+ */
572+ public function resizeTransparentImageWithColorOfBlur ()
573+ {
574+ $ source = $ this ->getTestImage ('transparent.png ' );
575+ $ actual = Image::resize ($ source , 128 , 128 , ['color ' => 'blur ' ]);
576+ $ expected = $ this ->getTestImage ('transparent-blur.png ' );
577+ $ this ->assertSameImage ($ expected , $ actual );
578+ }
579+
580+ /**
581+ * @test
582+ * @covers ::resize
583+ */
584+ public function resizeTransparentImageWithBlurBackground ()
585+ {
586+ $ source = $ this ->getTestImage ('transparent.png ' );
587+ $ actual = Image::resize ($ source , 128 , 128 , ['blurBackground ' => true ]);
588+ $ expected = $ this ->getTestImage ('transparent-blur.png ' );
589+ $ this ->assertSameImage ($ expected , $ actual );
590+ }
591+
542592 /**
543593 * @test
544594 * @covers ::write
@@ -642,4 +692,17 @@ public function stripHeadersMissingImage()
642692 {
643693 Image::stripHeaders ("{$ this ->tempDir }/doesnotexist.jpg " );
644694 }
695+
696+ private function assertSameImage (\Imagick $ expected , \Imagick $ actual )
697+ {
698+ $ comparison = $ expected ->compareImages ($ actual , \Imagick::METRIC_UNDEFINED );
699+ $ this ->assertThat ($ comparison [1 ], new GreaterThan (.99 ));
700+ }
701+
702+ private function getTestImage (string $ filename ) : \Imagick
703+ {
704+ $ image = new \Imagick ();
705+ $ image ->readImage ("{$ this ->sourceFilesDir }/ {$ filename }" );
706+ return $ image ;
707+ }
645708}
0 commit comments