1

I found more topics from this web site about quality with Imagick but nothing help me... I have to save all images as JPG. I created this script:

$image_url = 'http://limuzynamercedes.pl/wp-content/uploads/2014/06/3.png';
$image_code = file_get_contents($image_url);

$img = new Imagick();
$img -> readImageBlob($image_code);
$img->setResolution(300, 300);
$d = $img->getImageGeometry(); 
$img->cropImage($d['width'],($d['height']-120), 0,0);
$img->setImageFormat('jpeg');
$img->setImageCompression(imagick::COMPRESSION_JPEG); 
$img->setCompressionQuality(100); 
$img->writeImage('read.jpg');
$img->clear();

echo '<img src="read.jpg?'.time().'">';exit;

Here is original image: http://limuzynamercedes.pl/wp-content/uploads/2014/06/3.png and here is image which was converted by my script: http://s5.ifotos.pl/img/demo1jpg_saeaxqx.jpg

Where is a problem? Why this image is always convert in bad quality?

Thanks.

Majkson
  • 227
  • 4
  • 15
  • the image is not in "bad quality" (there is no blurry areas found), but the difference between 2 images is caused by transparent PNG to JPG conversion. – Raptor May 23 '16 at 09:01
  • So how save this image as JPG with all efects (blurry area etc)? I have to save images from all formats to JPG and these images should look the same as original :/ – Majkson May 23 '16 at 09:03
  • When the png is converted to jpg, the background of the original image which was made blank, is still there, it's just no longer hidden. So if you want, check the png file in an image editor first cs photoshop is good and check the layers of the image, then crop the car out. Which makes less sense than just saving it as a jpg and uploading it directly like that. – Jonathan Yaniv Ben Avraham May 23 '16 at 09:03
  • 2
    Possible duplicate of [Convert PNG to JPG and set transparent background to white with ImageMagick and PHP](http://stackoverflow.com/questions/5280118/convert-png-to-jpg-and-set-transparent-background-to-white-with-imagemagick-and) – Raptor May 23 '16 at 09:04

1 Answers1

5

The image is not in "bad quality" (there is no blurry areas found), but the difference between 2 images is caused by transparent PNG to JPG conversion.

Before you crop the image, add these two lines:

// set background to white (Imagick doesn't know how to deal with transparent background if you don't instruct it)
$img->setImageBackgroundColor(new ImagickPixel('white'));

// flattens multiple layers
$img = $img->mergeImageLayers(Imagick::LAYERMETHOD_FLATTEN);
Raptor
  • 53,206
  • 45
  • 230
  • 366
  • glad it helps you. – Raptor May 23 '16 at 09:05
  • 1
    I could honestly kiss you right now :) Ok maybe exaggerating a bit... finally after hours of struggling this was the answer. While I knew that flatternimages() will return a new object, documentation of `mergeImageLayers` doesn't specify that, so I was struggling for a very long time, till I came across this. I wish I could give you more upvotes! – Emil Borconi Jun 20 '19 at 21:47