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

Post any defects you find in the released or beta versions of the ImageMagick software here. Include the ImageMagick version, OS, and any command-line required to reproduce the problem. Got a patch for a bug? Post it here.
Post Reply
valentinn.rusanov
Posts: 1
Joined: 2017-01-24T07:57:30-07:00
Authentication code: 1151

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

Post 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
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

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

Post 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.
snibgo's IM pages: im.snibgo.com
User avatar
glennrp
Posts: 1147
Joined: 2006-04-01T08:16:32-07:00
Location: Maryland 39.26.30N 76.16.01W

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

Post 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.
Post Reply