Skip to content

Commit

Permalink
Let gd's PngEncoder encode binary transparency in indexed mode
Browse files Browse the repository at this point in the history
  • Loading branch information
olivervogel committed Aug 11, 2024
1 parent 20dae08 commit 78534a3
Showing 1 changed file with 6 additions and 35 deletions.
41 changes: 6 additions & 35 deletions src/Drivers/Gd/Encoders/PngEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@
namespace Intervention\Image\Drivers\Gd\Encoders;

use GdImage;
use Intervention\Image\Colors\Rgb\Channels\Blue;
use Intervention\Image\Colors\Rgb\Channels\Green;
use Intervention\Image\Colors\Rgb\Channels\Red;
use Intervention\Image\Drivers\Gd\Cloner;
use Intervention\Image\EncodedImage;
use Intervention\Image\Encoders\PngEncoder as GenericPngEncoder;
Expand Down Expand Up @@ -48,39 +45,13 @@ public function encode(ImageInterface $image): EncodedImage
*/
private function prepareOutput(ImageInterface $image): GdImage
{
if ($this->indexed === false) {
return Cloner::clone($image->core()->native());
}

// clone output instance
$output = Cloner::cloneEmpty($image->core()->native());

// Decode configured blending color
$blendingColor = $image->blendingColor();

// allocate blending color with slighty different alpha value
// to avoid "overwriting" pixels with the same color in the
// original image with transprency
$blendingIndex = imagecolorallocatealpha(
$output,
$blendingColor->channel(Red::class)->value(),
$blendingColor->channel(Green::class)->value(),
$blendingColor->channel(Blue::class)->value(),
1,
);
if ($this->indexed) {
$output = clone $image;
$output->reduceColors(255);

// fill with blending color
imagefill($output, 0, 0, $blendingIndex);

// define blending index as transparent
imagecolortransparent($output, $blendingIndex);

// copy original into output
imagecopy($output, $image->core()->native(), 0, 0, 0, 0, imagesx($output), imagesy($output));

// reduce to indexed color palette
imagetruecolortopalette($output, true, 255);
return $output->core()->native();
}

return $output;
return Cloner::clone($image->core()->native());
}
}

0 comments on commit 78534a3

Please sign in to comment.