ImageMagick/Imagick 使用原生 PHP API 将 PDF 转换为 JPG
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/588918/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ImageMagick/Imagick convert PDF to JPG using native PHP API
提问by matt
I'm attempting to convert PDF files into PNGs. It works great from the command line (I do have GhostScript 8.64 installed). But from PHP I'm having a problem:
我正在尝试将 PDF 文件转换为 PNG。它从命令行运行得很好(我确实安装了 GhostScript 8.64)。但是从 PHP 我遇到了一个问题:
code:
代码:
$im = new Imagick($pdf_file); // this is where it throws the exception below
output:
输出:
Fatal error: Uncaught exception ‘ImagickException' with message ‘Postscript delegate failed `23_1235606503.pdf': No such file or directory @ pdf.c/ReadPDFImage/612′ in get_thumbnail.php:93
Stack trace:
\#0 get_thumbnail.php(93): Imagick->__construct('…')
etc. etc.
等等等等
I'm not sure what I'm doing wrong here, but I suspect it has something to do with my server configuration somewhere. I'm running: Apache 2.2.11 PHP 5.2.8 ImageMagick 6.4.8-9 GhostScript 8.64
我不确定我在这里做错了什么,但我怀疑这与我的服务器配置有关。我正在运行:Apache 2.2.11 PHP 5.2.8 ImageMagick 6.4.8-9 GhostScript 8.64
回答by matt
Finally figured this out. The GhostScript executable (gs) wasn't in Apache's environment path. It was in /usr/local/bin. Though I tried several ways to add /usr/local/binto the path, I did not succeed. I ended up putting a symlink for gsin the /usr/bin directory. Now everything works perfectly.
终于想通了这一点。GhostScript 可执行文件 ( gs) 不在 Apache 的环境路径中。它在/usr/local/bin. 虽然我尝试了几种方法来添加/usr/local/bin到路径中,但我没有成功。我最终gs在/usr/bin directory. 现在一切正常。
回答by user3071502
I don't have the "reputation" on Stackoverflow to add a comment inline above, but there is an extra step I had to perform to get this working on my Mac with the latest Sierra update.
我没有在 Stackoverflow 上的“声誉”来添加上面的内联评论,但是我必须执行一个额外的步骤才能在我的 Mac 上使用最新的 Sierra 更新来运行它。
When you enter the command:
当你输入命令时:
sudo ln -s /usr/local/bin/gs /usr/bin/gs
On the Mac, you may get the error, "Operation not Permitted".
在 Mac 上,您可能会收到错误消息“不允许操作”。
Apparently Apple made a change that the "bin" directory is not editable, unless you disable SIP (System Integrity Protection).
显然 Apple 做出了更改,即“bin”目录不可编辑,除非您禁用 SIP(系统完整性保护)。
So here are the steps to do that:
因此,以下是执行此操作的步骤:
- Reboot your Mac into Recorvery Mode by restarting your computer and holding down "Command + R" until the Apple logo appears on your screen.
- Click Utilities > Terminal
- In the Terminal window, type in
crutil disableand press "Enter" - Restart your Mac.
- 通过重新启动计算机并按住“Command + R”直到 Apple 标志出现在屏幕上,将 Mac 重新启动到恢复模式。
- 单击实用程序 > 终端
- 在终端窗口中,输入
crutil disable并按“Enter” - 重新启动您的 Mac。
I just went through these steps and now my Ghostscript works great and I successfully converted a PDF to JPG.
我刚刚完成了这些步骤,现在我的 Ghostscript 工作得很好,我成功地将 PDF 转换为 JPG。
回答by Mark
I am successfully doing this. Here is the code that I am using to do the conversion. We are using this solution commercially. I know this question has been out there for awhile, but it may still help you.
我成功地做到了这一点。这是我用来进行转换的代码。我们正在商业上使用这种解决方案。我知道这个问题已经存在一段时间了,但它可能仍然对你有帮助。
//Convert PDF contract to image using ImageMagik and Ghostscript
// NOTE: This will need to be change if running on Linux
$source = $appDir."\".$clientID."\".$clientID.".pdf";
$dest = $appDir."\".$clientID."\".$clientID.".jpg";
//print("c:\IM\convert.exe $source $dest ");
exec("c:\IM\convert.exe $source $dest ");

