Search code examples
phppngjpegimagick

Imagick JPEG to PNG conversion


I need to convert a JPEG Imagick image to PNG while maintaining the imageCompressionQuality of the JPEG image. I've tried using compositeimage as well as clone $image in order to achieve this, but both take the original quality (before the image compression of the JPEG file).

$image = new Imagick($image_name);
$image->resizeImage($imageWidth, $imageHeight, Imagick::FILTER_LANCZOS, 1);
$image->setImageFormat("jpeg");
$image->setImageCompression(imagick::COMPRESSION_JPEG);
$image->setImageCompressionQuality(00);
$image->stripImage();

METHOD 1:

$finalImage = new Imagick();
$finalImage->newImage($imageWidth, $imageHeight, "none");
$finalImage->compositeimage($image, Imagick::COMPOSITE_OVER, 0, 0);
$finalImage->setImageFormat("png");
echo $finalImage;

METHOD 2:

$finalImage = clone $image;
etc.

Is there any way to do this?


Solution

    1. "Quality" settings are not part of JPEG. It is merely a shorthand some encoders use for selecting quantization tables.

    2. There is no equivalent to quantization table selection in PNG. PNG is a lossless compression. What goes in is what comes out for all settings.