6

I'm using imagick to convert a pdf to a jpg in a script I have...it works fine if I give the direct path to the uploaded pdf without the page specified, but when I add the [0] onto the end of the file path to specify that I only want to read the first page, it bombs out with the following error:

"Fatal error: Uncaught exception 'ImagickException' with message 'Invalid filename provided' Imagick->readImage()"

I've also tried using '/path/to/file.pdf[0]' directly in the constructor with no luck, but without the page specifier, that also works fine.

According to the documentation...this should work. What am I doing wrong here?

$doc_preview = new Imagick();
$doc_preview->setResolution(180,180);
$doc_preview->readImage('/path/to/file.pdf[0]');
$doc_preview->setImageFormat('jpeg');
$doc_preview->writeImage('/path/to/file.jpg');
$doc_preview->clear();
$doc_preview->destroy();

UPDATE: I should have mentioned I am using HHVM. Not sure this would make a difference in this case...but I am.

UPDATE2: I have opened an issue over on the HHVM github repo. Hopefully they will fix this bug...until then, the answer I have marked correct below is a decent (albeit hacky) workaround.

kevindeleon
  • 1,914
  • 3
  • 18
  • 30
  • It's a bug. The feature is not supported, but repeated all over the PHP manual. Duplicate: http://stackoverflow.com/q/11114747/1163786 Solution using setIteratorIndex() over at: http://stackoverflow.com/a/22673725/1163786 – Jens A. Koch Feb 02 '15 at 21:25
  • Good find...I figured it out by reading through all the supported methods...but still...it's unintuitive at best. I also don't consider my solution a duplicate solution as I am not trying to iterate and produce multiple images...and therefore was unable to find that solution when searching earlier. – kevindeleon Feb 02 '15 at 21:30
  • Sorry, Kevin - i didn't notice that this issue relates to the HHVM extension. – Jens A. Koch Feb 03 '15 at 16:27

3 Answers3

4

OK...so the (kind of hacky) way to fix this in my situation is to use fopen() and then use setIteratorIndex(0) which is HIGLY unintuitive. But for those having the same problem...there you go!

$pdf_handle = fopen('/path/to/file.pdf', 'rb');

$doc_preview = new Imagick();
$doc_preview->setResolution(180,180);
$doc_preview->readImageFile($pdf_handle);
$doc_preview->setIteratorIndex(0);
$doc_preview->setImageFormat('jpeg');
$doc_preview->writeImage('/path/to/file.jpg');
$doc_preview->clear();
$doc_preview->destroy();
kevindeleon
  • 1,914
  • 3
  • 18
  • 30
1

I don't have imagick in my computer but I can guess what the problem is. The problem is that when you open a PDF file with the page part you cannot include directory path string that includes the forward slash. I think that because I read the readImage function page from php.net here http://php.net/manual/en/imagick.readimage.php and they there don't include directory path so I suppose that that is a glich but continuing. You should try to change directory to the PDF file using chdir()(http://php.net/manual/en/function.chdir.php) and then try the same function like this $doc_preview->readImage('file.pdf[0]'); without the directory path. Most probably it will work. Some API's have gliches and you have to work around them.

  • I appreciate the guess, but I'm not sure using `chdir()` is a good option. If this really is the issue...that's a disappointing bug. – kevindeleon Feb 02 '15 at 21:02
1

readImage() requires that $filename is an absolute path.

If you want to use a relative path, then use realpath() on it, which turns the relative path into an absolute file path.

Instead of

$imagick->readImage('/path/to/file.pdf[0]');

try

$imagick->readImage(realpath("/path/to/file.pdf")."[0]");
Jens A. Koch
  • 39,862
  • 13
  • 113
  • 141
  • Except I'm already using absolute server paths...not relative paths...which is why it works if I don't use the page specifier on the end. The problem is with PHP/Imagemagick not accepting a filename with a page specifier if the path is also given...which seems to be a bug. It should accept the filename with page specifier or full path to file with page specifier, but it doesn't. I'll test to be certain though. – kevindeleon Feb 03 '15 at 14:45