Skip to content

Commit

Permalink
Fix blending problem in GD's PngEncoder
Browse files Browse the repository at this point in the history
  • Loading branch information
olivervogel committed Aug 3, 2024
1 parent 63990a8 commit 2adc3b4
Showing 1 changed file with 22 additions and 8 deletions.
30 changes: 22 additions & 8 deletions src/Drivers/Gd/Encoders/PngEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Intervention\Image\Drivers\Gd\Encoders;

use GdImage;
use Intervention\Image\Colors\Rgb\Color;
use Intervention\Image\Drivers\Gd\Cloner;
use Intervention\Image\EncodedImage;
use Intervention\Image\Encoders\PngEncoder as GenericPngEncoder;
Expand Down Expand Up @@ -49,19 +50,32 @@ private function prepareOutput(ImageInterface $image): GdImage
return Cloner::clone($image->core()->native());
}

// get blending color
$blendingColor = $this->driver()->colorProcessor($image->colorspace())->colorToNative(
$this->driver()->handleInput($this->driver()->config()->blendingColor)
);

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

/**
* Decode configured blending color
*
* @var Color
*/
$blendingColor = $this->driver()->handleInput($this->driver()->config()->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->red()->value(),
$blendingColor->green()->value(),
$blendingColor->blue()->value(),
1,
);

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

// set transparency
imagecolortransparent($output, $blendingColor);
// 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));
Expand Down

0 comments on commit 2adc3b4

Please sign in to comment.