使用ImageMagick将PDF转换为PNG或JPEG非常慢 [英] Conversion PDF to PNG or JPEG is very very slow using ImageMagick

查看:1471
本文介绍了使用ImageMagick将PDF转换为PNG或JPEG非常慢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用PHP和ImageMagick工作的PDF到PNG转换脚本,但我遇到了转换速度的问题。

I have a working PDF to PNG conversion script using PHP and ImageMagick but I am having a problem with the speed of the conversion.

我知道它有效,因为一个非常小的PDF转换所用的时间并不是那么好,但是有一个250kb的文件(实际上还不是那么大)转换需要超过20分钟。

I know it works because with a very small PDF the time taken to convert is not that great, but with a 250kb file (still not that large really) it takes in excess of 20 minutes to convert.

这是PHP:

//***** GET PATH TO IMAGEMAGICK *****
$path_to_imagemagick = trim(`which convert`);

//***** PATH TO PDF TO CONVERT *****
$path_to_pdf = getcwd() . "/pdf/myfile.pdf[0]";

//***** PATH TO OUTPUT TO *****
$output_path = getcwd() . "/pdfimage/test_converted.png";

@exec($path_to_imagemagick . " -density 72 -quality 60 -resize 150x " . $path_to_pdf . " " . $output_path);

我可以更改任何设置以加快速度吗?

Are there any settings I can change to make this quicker?

如果有帮助,图像不需要是PNG。如果JPEG更快,我很乐意继续使用。

If it helps, the image does not need to be a PNG. If JPEG is going to be quicker I'm happy to go with that.

推荐答案

ImageMagick无法将PDF转换为光栅图像根本

ImageMagick cannot convert PDF to raster images by itself at all.

ImageMagick为此作业使用委托:该委托是 Ghostscript 。如果您没有在与ImageMagick相同的系统上安装Ghostscript,则 convert 的PDF转换将无效。

ImageMagick uses a delegate for this job: that delegate is Ghostscript. If you hadn't installed Ghostscript on the same system as ImageMagick, the PDF conversion by convert wouldn't work.

要获得速度,请不要将ImageMagick用于PDF - >光栅图像转换。而是直接使用Ghostscript(也可以通过PHP)。

To gain speed, don't use ImageMagick for PDF -> raster image conversion. Instead, rather use Ghostscript directly (also possible via PHP).

JPEG输出的命令行:

Command line for JPEG output:

gs                                 \
  -o ./pdfimage/test_converted.jpg \
  -sDEVICE=jpeg                    \
  -dJPEGQ=60                       \
  -r72                             \
  -dLastPage=1                     \
   pdf/myfile.pdf

PNG输出的命令行:

Command line for PNG output:

gs                                 \
  -o ./pdfimage/test_converted.png \
  -sDEVICE=pngalpha                \
  -dLastPage=1                     \
  -r72                             \
   pdf/myfile.pdf 

这两个命令都会给你提供未缩放的输出。

Both of these commands will give you unscaled output.

要缩小输出,你可以使用类似

To scale the output down, you may use something like

gs                                 \
  -o ./pdfimage/test_converted.png \
  -sDEVICE=pngalpha                \
  -dLastPage=1                     \
  -r72                             \
  -dDEVICEWIDTHPOINTS=150          \
  -dDEVICEHEIGHTPOINTS=150         \
  -dPDFFitPage                     \
   pdf/myfile.pdf 

另请注意:您为PNG输出命令使用了 -quality 60 设置。但JPEG的 -quality 和PNG输出的 -quality 确实与ImageMagick有完全不同的含义(你可能不会要注意它)。另请参阅 此答案 ,了解有关此问题的一些详细信息。

Also please note: You used a -quality 60 setting for your PNG outputting command. But -quality for JPEG and -quality for PNG output do have a completely different meaning with ImageMagick (and you may not be aware of it). See also this answer for some details about this.

这篇关于使用ImageMagick将PDF转换为PNG或JPEG非常慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆