imagick将pdf转换为jpg会引发错误 [英] imagick converting pdf to jpg throws an error

查看:227
本文介绍了imagick将pdf转换为jpg会引发错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

for ($i = 0; $i < $int; $i++) {
    $im = new imagick();
    // $im->readimage($soubory."[".$i."]");
    $im->setResolution(300, 300);
    $im->readImage($soubory . "[" . $i . "]");
    $im->resampleImage(150, 150, imagick::FILTER_UNDEFINED, 1);
    $im->resizeImage(512, 700, Imagick::FILTER_LANCZOS, 0);
    $im->setImageFormat('jpeg');
    $im->setImageUnits(imagick::RESOLUTION_PIXELSPERINCH);
    $im->setImageFormat('jpg');
    $i = sprintf("%03s", $i);
    $im->writeImage('../pdf/publisher/' . $select . '/x-' . $i . '.jpg');
    $im->clear();
    $im->destroy();

    if ($i == 000) {
        $preview = '../pdf/publisher/' . $select . '/x-' . $i . '.jpg';
    }
}

如果我的文件大小小于4 MB,则可以正常工作.但是某些文件无法上传.我不知道为什么.

If I have a file with size less than 4 MB it works perfectly. But some files do not upload. I don't know why.

我收到此错误:

致命错误:未捕获的异常'ImagickException'和消息'Postscript委托失败'/data/web/virtuals/69845/virtual/www/domains/nabytek-novydomov.cz/pdf/9.pdf':无此类文件或/data/web/virtuals/69845/virtual/www/domains/nabytek-novydomov.cz/funkce.php:92中的目录@ pdf.c/ReadPDFImage/611':堆栈跟踪:#0/data/web/virtuals/69845 /virtual/www/domains/nabytek-novydomov.cz/funkce.php(92):Imagick-> readimage('/data/web/virtu ...')#1/data/web/virtuals/69845/virtual/www/domains/nabytek-novydomov.cz/adm/includes/pdf_publisher.php(4):uploadpdf(NULL)#2/data/web/virtuals/69845/virtual/www/domains/nabytek-novydomov.cz/adm/include/container.php(17):include('/data/web/virtu ...')#3/data/web/virtuals/69845/virtual/www/domains/nabytek-novydomov.cz/adm/index. php(155):include('/data/web/virtu ...')#4 {main}放在/data/web/virtuals/69845/virtual/www/domains/nabytek-novydomov.cz/funkce.php中在第92行

Fatal error: Uncaught exception 'ImagickException' with message 'Postscript delegate failed `/data/web/virtuals/69845/virtual/www/domains/nabytek-novydomov.cz/pdf/9.pdf': No such file or directory @ pdf.c/ReadPDFImage/611' in /data/web/virtuals/69845/virtual/www/domains/nabytek-novydomov.cz/funkce.php:92 Stack trace: #0 /data/web/virtuals/69845/virtual/www/domains/nabytek-novydomov.cz/funkce.php(92): Imagick->readimage('/data/web/virtu...') #1 /data/web/virtuals/69845/virtual/www/domains/nabytek-novydomov.cz/adm/includes/pdf_publisher.php(4): uploadpdf(NULL) #2 /data/web/virtuals/69845/virtual/www/domains/nabytek-novydomov.cz/adm/includes/container.php(17): include('/data/web/virtu...') #3 /data/web/virtuals/69845/virtual/www/domains/nabytek-novydomov.cz/adm/index.php(155): include('/data/web/virtu...') #4 {main} thrown in /data/web/virtuals/69845/virtual/www/domains/nabytek-novydomov.cz/funkce.php on line 92

推荐答案

感谢提供示例-很大的pdf.使用ImageMagick打开它不会给我带来错误,但是PDF也无法正确呈现.

Thanks for providing the example - that is a very large pdf. Opening it with ImageMagick doesn't give an error for me, however the PDF isn't rendered correctly either.

ImageMagick实际上是通过将呈现委托给GhostScript程序来呈现PDF的.我认为您最好直接调用GhostScript,将PDF渲染为PNG,然后以合理的格式(例如,p.p.)处理图像.

ImageMagick actually renders PDFs by delegating the rendering to the GhostScript program. I think you would be better off calling GhostScript directly, to render the PDFs to PNGs and then be able manipulate the images once they are in sensible format e.g.

要生成所有页面的PNG文件,您应该能够从命令行调用Ghostscript,只要在您的路径中将其设置为:

To generate PNG files of all the pages, you should be able to call Ghostscript from the command line, so long as it is setup in your path, as:

gs \
-sDEVICE=png16m \
-o %03d.png \
-r300 \
casopis.pdf

或仅选择某些页面.

gs \
-sDEVICE=png16m \
-o %03d.png \
-dFirstPage=10 \
-dLastPage=13 \
-r300 \
casopis.pdf

如果您确实要输出Jpeg,则可以将其配置为输出设备.

If you really want Jpeg output, you can configure that as the output device.

我无法复制您看到的错误.完全有可能是由于ImageMagick或GhostScript无法创建一些临时文件,或者只是用尽了内存.

I haven't been able to replicate the error you are seeing. It's entirely possible that it's due to either ImageMagick or GhostScript being unable to create some temporary files, or just running out of memory.

无论如何,答案是直接调用ghostscript,而不必跳过两层抽象来处理非常大的文件.

Regardless, the answer is to call ghostscript directly, rather than have to jump through two layers of abstraction to process very large files.

关于如何从命令行调用GS的明确说明

Explicit instructions for how to invoke GS from the command line

  1. 打开命令提示符.
  2. 转到PDF所在的目录.
  3. 运行命令

如果要从PHP调用它,则需要使用 exec 或以下之一其他在命令行上调用东西的功能.

If you wanted to invoke it from PHP, you would need to use exec or one of the other functions for calling stuff on the command line.

这篇关于imagick将pdf转换为jpg会引发错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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