简体   繁体   English

PHP imagick - 将 eps 转换为 jpg 但质量很差

[英]PHP imagick - Convert eps to jpg but poor quality

I'm trying to convert and resize eps files into jpg.我正在尝试将 eps 文件转换并调整大小为 jpg。 I use php imagick for this.我为此使用 php imagick。 After converting the quality is very bad.转换后质量很差。

my eps you can download here: https://www.file-upload.net/download-14285439/icon.eps.html我的 eps 你可以在这里下载: https : //www.file-upload.net/download-14285439/icon.eps.html

my jpg-img我的 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:我将此版本与 php 7.2.33 一起使用:

phpinfo php信息

What is wrong?怎么了?

As you obviously know ImageMagick uses Ghostscript to render EPS files to JPEG.众所周知,ImageMagick 使用 Ghostscript 将 EPS 文件渲染为 JPEG。 I would suggest that, rather than use ImageMagick you use Ghostscript directly.我建议您直接使用 Ghostscript,而不是使用 ImageMagick。 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.与使用 ImageMagick 相比,这将使您对过程有更多的控制,这意味着您可以发布 Ghostscript 命令行而不是 IM 命令行。

I'm afraid that I have no idea what ImageMagick sends to Ghostscript which makes it rather difficult to offer any suggestions.恐怕我不知道 ImageMagick 向 Ghostscript 发送了什么,这使得提供任何建议变得相当困难。

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.您发布的图像与我所看到的不太一样,但由于我不知道使用什么命令来驱动 Ghostscript,可能只是因为我没有完全复制您的设置。

First note;第一个注意事项; the nature of your EPS is not really suitable for JPEG compression. EPS 的性质并不适合 JPEG 压缩。 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. JPEG 在应用于平滑变化的图像时表现最佳,例如照片(JPEG = 联合图像专家组),它不适用于大面积的带有锐利边缘的平面颜色(这正是您在这里拥有的),锋利的边缘会产生“振铃”或“边缘”效应。

When using Ghostscript directly it is possible to alter the JPEG quality.直接使用 Ghostscript 时,可以更改 JPEG 质量。 Setting -dJPEGQ=100 will produce the highest quality, trading off the compression (ie the output file will be larger).设置-dJPEGQ=100将产生最高质量,折衷压缩(即输出文件将更大)。

In addition your EPS gives its BoundingBox as 20x20 points.此外,您的 EPS 将其 BoundingBox 作为 20x20 点。 So that's 20/72 inch in each dimension.所以每个维度都是 20/72 英寸。 Even at 300 dpi that's going to result in an image which is 84x84 pixels.即使在 300 dpi 下,也会产生 84x84 像素的图像。 Pretty small.相当小。 At 72 dpi you'll get an image which is 20x20 pixels,在 72 dpi 时,您将获得一个 20x20 像素的图像,

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.在我看来,您使用默认的 JPEGQ 值以 72 dpi 呈现 EPS,“质量差”似乎只不过是 JPEG 压缩产生的众所周知的人工制品。 Using that setup with Ghostscript produces something not entirely unlike your posted image (though without the sharp edged corner artefacts).在 Ghostscript 中使用该设置会产生与您发布的图像完全不同的东西(尽管没有锋利的边角伪影)。 Setting JPEGQ to 100 produces something more sensible.将 JPEGQ 设置为 100 会产生更合理的结果。 The file produced by the default settings is 3,564 bytes, while the higher quality file is 4,485 bytes.默认设置生成的文件为 3,564 字节,而更高质量的文件为 4,485 字节。

If it were me I would render to a TIFF file at a decent resolution, say 1200 dpi to give an image 333x333 pixels.如果是我,我会以合适的分辨率渲染 TIFF 文件,比如 1200 dpi 以提供 333x333 像素的图像。 Then load that into ImageMagick and resize it to your desired dimensions.然后将其加载到 ImageMagick 并将其调整为您想要的尺寸。 Finally export as a JPEG if you require it that way for some reason.如果出于某种原因需要以这种方式导出,最后导出为 JPEG。

here's a comparison of the output from Ghostscript.这是 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.左边是 1200 dpi 生成的 JPEG,中间是 72 dpi 的默认质量渲染,右边是 72 dpi 渲染,JPEGQ 设置为 100。

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM