$IM = new imagick();
$IM->setResolution(200, 200);
$IM->setCompressionQuality(100);
$IM->readImage($pdfPath);
使用imagick将PDF转换成图片时报错如下:
Fatal error: Uncaught exception 'ImagickException' with message 'FailedToExecuteCommand `'gs' -sstdout=%stderr -dQUIET
-dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 '-sDEVICE=pngalpha'
-dTextAlphaBits=4 -dGraphicsAlphaBits=4 '-r200x200' '-sOutputFile=/var/tmp/magick-71725ioOTNJteonfW%d' '
-f/var/tmp/magick-71725laJDl-aBaQPX' '-f/var/tmp/magick-71725EuMqNHYhE98Q'' (1) @ error/pdf.c/InvokePDFDelegate/291'
in /Users/nick/www/html/t.php on line 5
参考https://stackoverflow.com/questions/24298815/php-uncaught-exception-imagickexception-with-message-failedtoexecutecommand 这个文章发现是环境变量的问题。 在命令行中运行:
php56 -r echo getenv('PATH');
/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Applications/VMware Fusion.app/Contents/Public:/usr/local/opt/go/libexec/bin:/Users/nick/.composer/vendor/bin:/Users/nick/gocode/bin
通过web方式访问是返回false;
由此可确认是未能读取到环境变量,所以不能执行gs命令,于是添加环境变量:
vim /PATH/php-fpm.conf
再次通过获取环境变量,发现成功返回了环境变量,运行程序,成功将pdf转成图片。
转载请注明:MitNick » 使用imagick将PDF转换成图片时报Fatal error: Uncaught exception ‘ImagickException’ with message ‘FailedToExecuteCommand `’gs’