我已经通过pecl在MAC上安装了ImageMagick PHP扩展。它显示在phpinfo()下,并列出PDF作为支持的格式。
我正在尝试阅读PDF并将其转换为图像。但是,当源是PDF时,构造函数会抛出异常。
$im = new imagick('TestDoc.pdf[0]'); // Throws Exception when PDF specified.
//$im = new imagick('TestImage.png'); // Succeeds.
$im->setImageFormat('jpg');
header('Content-Type: image/jpeg');
echo $im;
Fatal error: Uncaught exception 'ImagickException' with message 'FailedToExecuteCommand
`"gs" -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -
dAlignToPixels=0 -dGridFitTT=2 "-sDEVICE=pngalpha" -dTextAlphaBits=4 -dGraphicsAlphaBits=4
"-r72x72" -dFirstPage=1 -dLastPage=1 "-sOutputFile=/var/tmp/magick-43594XlaRxeGWg1ps%d" "-
f/var/tmp/magick-43594O_WVqnAJTgzr" "-f/var/tmp/magick-43594ivJ_pKBcF3s7"' (-1) @
error/utility.c/SystemCommand/2029' in
/Users/garys/Documents/Projects/accrivia/code/test/test.php:8 Stack trace: #0
/Users/garys/Documents/Projects/accrivia/code/test/test.php(8): Imagick-
>__construct('../TestDoc.pdf[...') #1 {main} thrown in
/Users/garys/Documents/Projects/accrivia/code/test/test.php on line 8
创建了输出中提到的tmp文件,但它是0字节。如果我在命令行上执行消息中的完整'gs'命令(使用实际的文件),它会成功运行。
ImageMagick和GhostScript是和brew一起安装的。
PHP中,system('echo $PATH');给工作:/bin:/usr/sbin/sbin'gs'在/usr/bin/
有人有什么建议吗?在web上搜索异常消息没有找到任何特定的内容。
多谢加里。
我从未使用过imageMagick,但文档说它需要GhostScript来读取PDF文档
PDF support : Requires Ghostscript to read.
EDIT:您是否检查了GhostScript正在工作?
我在MacOS High Sierra上有同样的问题,PHP 7.0是从Homebrew安装的。问题是FPM $PATH
包含(与<php echo getenv('PATH'); ?>
呼应)/usr/bin:/bin:/usr/sbin:/sbin
,不包括Brew的/usr/local/bin/
安装路径。
env[PATH] = /usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
到/usr/local/etc/php/7.0/php-fpm.d/www.conf
,重新启动php70并工作!在Homebrew的论坛上@comes建议添加相同的.env
文件