imagick converting pdf to jpg throws an error

痴心易碎 提交于 2019-12-25 06:23:59

问题


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';
    }
}

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.

I get this error:

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


回答1:


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

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

Or just select certain pages.

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

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

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.

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


Explicit instructions for how to invoke GS from the command line

  1. Open a command prompt.
  2. Go to the directory where the PDF is.
  3. Run the command from

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.



来源:https://stackoverflow.com/questions/23676548/imagick-converting-pdf-to-jpg-throws-an-error

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!