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

Change logic of encoding indexed PNG format

This commit is contained in:
Oliver Vogel 2024-08-11 11:19:47 +02:00
parent 50feb11dce
commit 568444c939
No known key found for this signature in database
GPG Key ID: 1B19D214C02D69BB

View File

@ -47,36 +47,31 @@ class PngEncoder extends GenericPngEncoder implements SpecializedInterface
*/ */
private function prepareOutput(ImageInterface $image): Imagick private function prepareOutput(ImageInterface $image): Imagick
{ {
if ($this->indexed === false) { $output = clone $image;
$output = clone $image->core()->native();
// ensure to encode PNG image type 6 (true color alpha) if ($this->indexed) {
$output->setFormat('PNG32'); // reduce colors
$output->setImageFormat('PNG32'); $output->reduceColors(256);
$output = $output->core()->native();
$output->setFormat('PNG');
$output->setImageFormat('PNG');
// $output->setImageBackgroundColor(new \ImagickPixel('#00ff00'));
// $output->setImageProperty();
// $output->setType(Imagick::IMGTYPE_PALETTEMATTE);
// $output->setOption('png:bit-depth', '8');
// $output->setOption('png:color-type', '4');
return $output; return $output;
} }
// get blending color // ensure to encode PNG image type 6 (true color alpha)
$blendingColor = $this->driver()->colorProcessor($image->colorspace())->colorToNative( $output = clone $image->core()->native();
$this->driver()->handleInput($this->driver()->config()->blendingColor) $output->setFormat('PNG32');
); $output->setImageFormat('PNG32');
// create new image with blending color as background
$output = new Imagick();
$output->newImage($image->width(), $image->height(), $blendingColor, 'PNG');
// set transparency of original image
$output->compositeImage($image->core()->native(), Imagick::COMPOSITE_DSTIN, 0, 0);
$output->transparentPaintImage('#000000', 0, 0, false);
// copy original and create indexed color palette version
$output->compositeImage($image->core()->native(), Imagick::COMPOSITE_DEFAULT, 0, 0);
$output->quantizeImage(255, $output->getImageColorSpace(), 0, false, false);
// ensure to encode PNG image type 3 (indexed)
$output->setFormat('PNG8');
$output->setImageFormat('PNG8');
return $output; return $output;
} }