Page 1 of 1

There are gray stains on the image after converting to another format after use ImageMagick php

Posted: 2017-01-24T08:06:52-07:00
by valentinn.rusanov
I have the following problem. I use ImageMagick for the reformat PDF to JPG. Everything goes well but on the picture appear the mark in the form of triangles. Here is an example code and images before and after.

Code: Select all

   $pdf_file = $name[0] . '.pdf';

    $im = new imagick($pdf_file);

    $i = 0;

    foreach ($im as $_img) {

        $i++;

        $_img->setResolution(300, 300);

        $im->setImageColorspace(255);

        $im->setCompression(Imagick::COMPRESSION_JPEG);

        $im->setCompressionQuality(60);

        $_img->setImageFormat('jpg');

        $_img->writeImage(__DIR__ .'/'.$main_name[0] . '.jpg');

    }


    $im->destroy();

    $new_name = DIR .'/'. $main_name[0] . '.jpg';

    $new_name1 = $main_name[0] . '.jpg';

    //$new_name1 = preg_replace('/[^A-Za-z0-9\-]/', '', $new_name1);


    $size = getimagesize($new_name1);
    $ratio = $size[0]/$size[1]; // width/height
    if( $ratio > 1) {
        $width = 700;
        $height = 700/$ratio;
    }
    else {
        $width = 700*$ratio;
        $height = 700;
    }
    $src = imagecreatefromstring(file_get_contents($new_name1));
    $dst = imagecreatetruecolor($width,$height);
    imagecopyresampled($dst,$src,0,0,0,0,$width,$height,$size[0],$size[1]);
    imagedestroy($src);
    imagepng($dst,$new_name1); // adjust format as needed
    imagedestroy($dst);


[1]: https://i.stack.imgur.com/Du1f8.jpg

Re: There are gray stains on the image after converting to another format after use ImageMagick php

Posted: 2017-01-24T08:12:33-07:00
by snibgo
The image is a graphic image, with solid colours. But you are compressing it with a method for photographs, JPG (P=Photography). JPEG compression changes pixels in a way that doesn't show much in photos, but does in graphics.

And you have an aggressive compression, 60.

I suggest you don't use JPEG. If you really must, then use quality=100.

Re: There are gray stains on the image after converting to another format after use ImageMagick php

Posted: 2017-01-27T12:38:57-07:00
by glennrp
I saw this same question on Stackexchange about 4 days ago and gave the same answer as snibgo's, but apparently it didn't help the person, who said changing quality to 100 did no help. I'm wondering if this is the old-ghostscript-version problem. Valentinn, please try converting to PNG or to PPM instead of JPEG. If the stains are still there then we'll know whether to blame the JPG encoding or the PDF decoding.