1
0
mirror of https://github.com/Intervention/image.git synced 2025-01-17 04:08:14 +01:00

Fix blending problem in GD's PngEncoder

This commit is contained in:
Oliver Vogel 2024-08-03 17:04:44 +02:00
parent 63990a8fb3
commit 2adc3b45de
No known key found for this signature in database
GPG Key ID: 1B19D214C02D69BB

View File

@ -5,6 +5,7 @@ declare(strict_types=1);
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;
@ -49,19 +50,32 @@ class PngEncoder extends GenericPngEncoder implements SpecializedInterface
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());
// fill with blending color
imagefill($output, 0, 0, $blendingColor);
/**
* Decode configured blending color
*
* @var Color
*/
$blendingColor = $this->driver()->handleInput($this->driver()->config()->blendingColor);
// set transparency
imagecolortransparent($output, $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, $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));