From e01840cc02873dafebe8175c4254b6a509433a48 Mon Sep 17 00:00:00 2001 From: Jay MOULIN Date: Sat, 24 Jun 2017 17:25:19 +0200 Subject: [PATCH] [#2] Fixes black pixels on alpha layer for PNG --- src/ResizeEngine.php | 8 +++----- tests/ResizeEngineTest.php | 15 --------------- 2 files changed, 3 insertions(+), 20 deletions(-) diff --git a/src/ResizeEngine.php b/src/ResizeEngine.php index 6df0f05..e0dde84 100644 --- a/src/ResizeEngine.php +++ b/src/ResizeEngine.php @@ -147,9 +147,9 @@ protected function prepareImageResized($width, $height, $type, $image) $imageResized = $libGd->imagecreatetruecolor($width, $height); if (($type == IMAGETYPE_GIF) || ($type == IMAGETYPE_PNG)) { $transparency = $libGd->imagecolortransparent($image); - $palletsize = $libGd->imagecolorstotal($image); + $palletSize = $libGd->imagecolorstotal($image); - if ($transparency >= 0 && $transparency < $palletsize) { + if ($type !== IMAGETYPE_PNG && $transparency >= 0 && $transparency < $palletSize) { $transparentColor = $libGd->imagecolorsforindex($image, $transparency); $transparency = $libGd->imagecolorallocate( $imageResized, @@ -159,10 +159,8 @@ protected function prepareImageResized($width, $height, $type, $image) ); $libGd->imagefill($imageResized, 0, 0, $transparency); $libGd->imagecolortransparent($imageResized, $transparency); - } elseif ($type == IMAGETYPE_PNG) { + } elseif ($type === IMAGETYPE_PNG) { $libGd->imagealphablending($imageResized, false); - $color = $libGd->imagecolorallocatealpha($imageResized, 0, 0, 0, 127); - $libGd->imagefill($imageResized, 0, 0, $color); $libGd->imagesavealpha($imageResized, true); } } diff --git a/tests/ResizeEngineTest.php b/tests/ResizeEngineTest.php index 78f1e5c..b6ae879 100644 --- a/tests/ResizeEngineTest.php +++ b/tests/ResizeEngineTest.php @@ -509,8 +509,6 @@ public function testPrepareImageResizedPng() 'imagecolorstotal', 'imagecolorsforindex', 'imagealphablending', - 'imagecolorallocatealpha', - 'imagefill', 'imagesavealpha', ) ) @@ -529,19 +527,6 @@ public function testPrepareImageResizedPng() $mockGd->expects($this->once()) ->method('imagealphablending') ->with($this->equalTo($resource2), $this->equalTo(false)); - $mockGd->expects($this->once()) - ->method('imagecolorallocatealpha') - ->with( - $this->equalTo($resource2), - $this->equalTo(0), - $this->equalTo(0), - $this->equalTo(0), - $this->equalTo(127) - ) - ->willReturn(5000); - $mockGd->expects($this->once()) - ->method('imagefill') - ->with($this->equalTo($resource2), $this->equalTo(0), $this->equalTo(0), $this->equalTo(5000)); $mockGd->expects($this->once()) ->method('imagesavealpha') ->with($this->equalTo($resource2), $this->equalTo(true));