6

I have installed Imagemagick extension on my MAMP dev environment and PHP info showing imagemagick installed properly. However, I am receiving the following exception:

PHP Fatal error:  Uncaught exception 'ImagickException' with message 
'Unable to read the file:
 /Applications/MAMP/htdocs/image/demo.pdf' 
in /Applications/MAMP/htdocs/image/index.php:8
Stack trace:
#0 /Applications/MAMP/htdocs/image/index.php(8): Imagick->__construct('/Applications/M...')
#1 {main}
  thrown in /Applications/MAMP/htdocs/image/index.php on line 8

Source code:

$pdf_file   = '/Applications/MAMP/htdocs/image/demo.pdf';

echo $pdf_file;

$save_to    = '/Applications/MAMP/htdocs/image/demo.jpg';

$img = new imagick($pdf_file);

//reduce the dimensions - scaling will lead to black color in transparent regions
$img->scaleImage(800,0);

//set new format
$img->setImageFormat('jpg');

//save image file
$img->writeImages($save_to, false);

Screen short- imagemagick installed on MAMP

Edit 1:

I am using brew for managing packages.

My MAMP configuration:

Imagick extension(php.ini):

[imagick]
extension="/usr/local/Cellar/php55-imagick/3.1.0RC2/imagick.so"

Envvars:

path:

/Applications/MAMP/Library/bin/envvars

Content:

#if test "x$DYLD_LIBRARY_PATH" != "x" ; then
# DYLD_LIBRARY_PATH="/Applications/MAMP/Library/lib:$DYLD_LIBRARY_PATH"
#else
  #DYLD_LIBRARY_PATH="/Applications/MAMP/Library/lib"
#fi
#export DYLD_LIBRARY_PATH


#DYLD_LIBRARY_PATH="/Applications/MAMP/bin/ImageMagick/ImageMagick-6.8.9/lib:/Applications/MAMP/Library/lib:$DYLD_LIBRARY_PATH"
#export DYLD_LIBRARY_PATH

Anam
  • 11,999
  • 9
  • 49
  • 63
  • Are you sure this file exists (it's correct path)? Have you tried to pass not PDF but image to constructor? Were there any errors? It's also quite possible you need to use `$img = new imagick(realpath($pdf_file));` or you don't have ghostscript libraries installed – Marcin Nabiałek Jul 01 '14 at 14:49
  • Also try relative path (`$_SERVER['DOCUMENT_ROOT'] . '/image/demo.pdf'`). – klugerama Jul 01 '14 at 20:52
  • @MarcinNabiałek I have checked the path, its correct. I am not sure about the ghostscript. However, I can covert image via command line. – Anam Jul 02 '14 at 00:20
  • @Anam you mean you can convert this pdf into jpg? In PHP manual there is `For example, Imagemagick requires ghostscript to conduct PDF operations.` so you should make sure you have ghostscript installed – Marcin Nabiałek Jul 02 '14 at 08:40
  • According to this(http://stackoverflow.com/questions/15446687/imagick-unable-to-open-file and http://stackoverflow.com/questions/15955013/php-imagick-with-pdf-issue) answer you've to use the full local system path because Imagick is running within PHP so it doesn't can resolve your path. – GuyT Jul 03 '14 at 08:27
  • @GuyT, I am using full local path. – Anam Jul 04 '14 at 00:27
  • 1
    @MarcinNabiałek, I can convert pdf from command-line. So, Ghostscript is installed. – Anam Jul 04 '14 at 00:29

2 Answers2

5

first check your pdf filepath:

if (! is_readable('/Applications/MAMP/htdocs/image/demo.pdf')) {
    echo 'file not readable';
    exit();
}

if file is readable, check this: https://github.com/delphian/drupal-convert-file/wiki/Installing-ImageMagick-on-Mac-OSX-for-PHP-and-MAMP

Kevin
  • 1,232
  • 10
  • 28
  • 1
    Your answer is correct, +1. Don't forget to mention that the path has to be the **local system path**. When the above function hits the `echo` imagick will never work. – GuyT Jul 03 '14 at 08:35
  • File is readable and I have followed many tutorials including the link you have provided. I can convert pdf to image from commandline but not using php extension. – Anam Jul 04 '14 at 00:40
  • Last try: there are more than one pdf specifications (versions). Perhabs you can try to find out the current pdf version of demo.pdf and convert it to an other. Have you tried other files than demo.pdf? – Kevin Jul 06 '14 at 14:03
1

From http://www.php.net/manual/en/imagick.construct.php

apprently when using pdf files, we may specify which page to use, which may in turn help imageMagick construct correctly when using a pdf file

$pdf_file   = '/Applications/MAMP/htdocs/image/demo.pdf';
$img = new Imagick($pdf_file.'[0]'); 
//[0] indicate the number of the wanted page