PHP call to readImage Exception ReadPDFImage/664 Failed

Post any defects you find in the released or beta versions of the ImageMagick software here. Include the ImageMagick version, OS, and any command-line required to reproduce the problem. Got a patch for a bug? Post it here.
Post Reply
harold
Posts: 4
Joined: 2018-08-25T09:18:04-07:00
Authentication code: 1152

PHP call to readImage Exception ReadPDFImage/664 Failed

Post by harold » 2018-08-25T09:29:18-07:00

My goal is to extract about 30 JPG or PNG images from a PDF using PHP. I'm using ImageMagick 6.9.4 from PHP 7.1.8 in a shared hosting account using this code to extract one specific image:

$imagick = new Imagick();
$imagick->readImage(bound.pdf[0]);
$imagick->setImageFormat("jpeg");
$imagick->writeImage("image1.jpg");

It works perfect for the first 10 or 15 images, but gives an exception "ReadPDFImage/664 Failed" during the readImage command if I try to read bound.pdf[18] for example. I have tried several source PDFs having from 20 to 30 images each. It always works fine for the first 10 images, but starting at index X and thereafter it always gets an exception.

I tried the same test on another hosting account with PHP 7.2.9 and ImageMagick 6.7.2-7 with similar results. It always gives an exception in the readImage command when index is around 15 or higher depending on the source PDF.

Any suggestions?

snibgo
Posts: 12011
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: PHP call to readImage Exception ReadPDFImage/664 Failed

Post by snibgo » 2018-08-25T10:11:51-07:00

harold wrote:My goal is to extract about 30 JPG or PNG images from a PDF using PHP.
IM doesn't do that. IM will rasterize each page, no matter how many images each page has. I suspect you are trying to extract more pages than are in the document. Extracting embedded images from a PDF is a different task, which "pdfimages" does fairly well.
snibgo's IM pages: im.snibgo.com

harold
Posts: 4
Joined: 2018-08-25T09:18:04-07:00
Authentication code: 1152

Re: PHP call to readImage Exception ReadPDFImage/664 Failed

Post by harold » 2018-08-25T14:01:33-07:00

Perhaps my goal description was unclear. I have a 20 page PDF file and I want to extract out 20 individual JPG files (one for each page). Each page is essentially a big image. The code above works for the first X pages, then fails with an exception. I'm trying to extract pages 0 to 19.

snibgo
Posts: 12011
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: PHP call to readImage Exception ReadPDFImage/664 Failed

Post by snibgo » 2018-08-25T15:19:56-07:00

I don't use PHP or IMagick so can't advise, but are you freeing memory correctly?
snibgo's IM pages: im.snibgo.com

harold
Posts: 4
Joined: 2018-08-25T09:18:04-07:00
Authentication code: 1152

Re: PHP call to readImage Exception ReadPDFImage/664 Failed

Post by harold » 2018-08-25T16:36:20-07:00

Just these two line of code alone will cause the exception so there is never a chance to free any memory. I tried changing the memory_limit assigned to PHP, but that doesn't seem to have an affect. It seems that when you are reading a page that is far enough into the PDF file, it gets the exception.

$imagick = new Imagick();
$imagick->readImage(bound.pdf[15]);

User avatar
fmw42
Posts: 25408
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: PHP call to readImage Exception ReadPDFImage/664 Failed

Post by fmw42 » 2018-08-25T17:01:11-07:00

Are you sure that pdf file has 16 or more pages? The first page is [0] and you are requesting page [15] which would be the 16 page.

The Imagemagick developers have nothing to do with Imagick. Does it work with a simple convert command line?

Code: Select all

convert bound.pdf[15] result.jpg
If it does, then you will need to report the issue to the Imagick developers. See the Imagick forum.

Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: PHP call to readImage Exception ReadPDFImage/664 Failed

Post by Bonzo » 2018-08-26T00:22:04-07:00

You could try working with the first 10 pages and then the second 10 and see what happens " Each page is essentially a big image. " could be the problem as fmw42 suggested.

If it still fails try reading 5 pages at a time which will hopefully isolate the problem.

harold
Posts: 4
Joined: 2018-08-25T09:18:04-07:00
Authentication code: 1152

Re: PHP call to readImage Exception ReadPDFImage/664 Failed

Post by harold » 2018-08-28T16:42:00-07:00

I have tested multiple PDF files each with over 20 pages. They all fail on the readImage[X] command. Bear in mind this is running in PHP from a shared hosting account so I do not have a Command line environment to run a convert command.

To clarify, the entire script consists of only two lines. It is not running a loop and finally getting the exception on the 16th page.

$imagick = new Imagick();
$imagick->readImage(bound.pdf[15]);

My "guess" and results of testing thus far seems to indicate that the implementation of the readImage[X] command exceeds some kind of memory limitation or calculation limitation starting at page X so the exception will occur at every index of X or above. The value of X depends on the specific PDF but appears consist for a specific PDF when running this tiny PHP script from different hosting environments.

User avatar
fmw42
Posts: 25408
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: PHP call to readImage Exception ReadPDFImage/664 Failed

Post by fmw42 » 2018-08-28T17:07:14-07:00

It could be a limitation within PHP or Imagick and not ImageMagick.

Do you have access to PHP exec(), so that you could run the command line and see if that fails?

Post Reply