Skip to content

Commit

Permalink
[#2] Fixes black pixels on alpha layer for PNG
Browse files Browse the repository at this point in the history
  • Loading branch information
jaymoulin committed Jun 26, 2017
1 parent 908f747 commit e01840c
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 20 deletions.
8 changes: 3 additions & 5 deletions src/ResizeEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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);
}
}
Expand Down
15 changes: 0 additions & 15 deletions tests/ResizeEngineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -509,8 +509,6 @@ public function testPrepareImageResizedPng()
'imagecolorstotal',
'imagecolorsforindex',
'imagealphablending',
'imagecolorallocatealpha',
'imagefill',
'imagesavealpha',
)
)
Expand All @@ -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));
Expand Down

0 comments on commit e01840c

Please sign in to comment.