Skip to content
Advertisement

PHP imagick – Convert eps to jpg but poor quality

I’m trying to convert and resize eps files into jpg. I use php imagick for this. After converting the quality is very bad.

my eps you can download here: https://www.file-upload.net/download-14285439/icon.eps.html

my jpg-img

i use this code:

if ( extension_loaded('imagick') ) {

    $imagePath = 'icon.eps';

    $imagick = new Imagick();
    $imagick->setResolution(300, 300);        
    $imagick->setColorspace(Imagick::COLORSPACE_SRGB);
    $imagick->readImage($imagePath);
    $imagick->resizeImage(0, 1000, Imagick::FILTER_LANCZOS, 1);
    $imagick->setImageResolution(72, 72);
    $imagick->setImageCompressionQuality(70);
    $imagick->setImageCompression(Imagick::COMPRESSION_JPEG);
    $imagick->setCompressionQuality(70);
    $imagick->setImageFormat('jpeg');
    $imagick->writeImage('test.jpg');
} else {
    echo 'not found';
}

same result with this settings without resize/only convert, but the quality is still bad:

if ( extension_loaded('imagick') ) {

    $imagePath = 'icon.eps';

    $imagick = new Imagick();
    $imagick->setResolution(300, 300);        
    $imagick->setColorspace(Imagick::COLORSPACE_SRGB);
    $imagick->readImage($imagePath);        
    $imagick->setImageFormat('jpeg');
    $imagick->writeImage('test.jpg');
} else {
    echo 'not found';
}

i use this version with php 7.2.33:

phpinfo

What is wrong?

Advertisement

Answer

As you obviously know ImageMagick uses Ghostscript to render EPS files to JPEG. I would suggest that, rather than use ImageMagick you use Ghostscript directly. This will give you more control over the process than using ImageMagick and will mean that you can post the Ghostscript command line instead of an IM one.

I’m afraid that I have no idea what ImageMagick sends to Ghostscript which makes it rather difficult to offer any suggestions.

In addition you really need to be much more explicit about your problem. What do you actually mean by ‘the quality is very bad’. Is this purely subjective or is there some objective criteria you are using ?

The image you’ve posted doesn’t look much like what I see, but since I don’t know what command is being used to drive Ghostscript, it may simply be that I am not reproducing your setup exactly.

First note; the nature of your EPS is not really suitable for JPEG compression. JPEG performs best when applied to smoothly varying images, like photographs (JPEG = Joint Photographic Expert Group), it does not work well with large areas of flat colour with sharp edges (which is exactly what you have here), the high frequency component of the sharp edges gives rise to ‘ringing’ or ‘fringing’ effects.

When using Ghostscript directly it is possible to alter the JPEG quality. Setting -dJPEGQ=100 will produce the highest quality, trading off the compression (ie the output file will be larger).

In addition your EPS gives its BoundingBox as 20×20 points. So that’s 20/72 inch in each dimension. Even at 300 dpi that’s going to result in an image which is 84×84 pixels. Pretty small. At 72 dpi you’ll get an image which is 20×20 pixels,

It looks to me like you have rendered the EPS at 72 dpi with the default JPEGQ value, the ‘poor quality’ appears to be nothing more than the well known artefacts produced by JPEG compression. Using that setup with Ghostscript produces something not entirely unlike your posted image (though without the sharp edged corner artefacts). Setting JPEGQ to 100 produces something more sensible. The file produced by the default settings is 3,564 bytes, while the higher quality file is 4,485 bytes.

If it were me I would render to a TIFF file at a decent resolution, say 1200 dpi to give an image 333×333 pixels. Then load that into ImageMagick and resize it to your desired dimensions. Finally export as a JPEG if you require it that way for some reason.

here’s a comparison of the output from Ghostscript. On the left is a JPEG produced at 1200 dpi, in the middle is the default quality rendering at 72 dpi and on the right the 72 dpi rendering with JPEGQ set to 100.

enter image description here

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement