Converting a multi-page pdf to multiple JPG images with Imagick and PHP
Posted: 2017-08-16T11:47:33-07:00
I have a small script for creating jpg images from an uploaded multi-paged pdf file ($uploadfile) with Imagick:
This works fine for a 1 or 2 paged pdf-document, but gives an error with 3 or more pages:
Line 152 mentioned in the error log is:
Thank you in advance!
Code: Select all
$imagick = new imagick();
$imagick->setResolution(600, 600);
$imagick->readImage($uploadfile);
$imagick->setImageFormat('jpg');
$pages = (int)$imagick->getNumberImages();
foreach($imagick as $i=>$imagick) {
$imagick->writeImage($uploadfile. " page ". ($i+1) ." of ". $pages.".jpg");
}
$imagick->clear();
unlink ($uploadfile);
Code: Select all
[lsapi:error] Backend fatal error: PHP Fatal error: Uncaught exception 'ImagickException' with message 'Failed to read the file' in /index.php:152\nStack trace:\n#0 /index.php(152): Imagick->readimage('...')\n#1 {main}\n thrown in /index.php on line 152\n
Code: Select all
$imagick->readImage($uploadfile);