ImageMagick/Imagick 使用本机 PHP API 将 PDF 转换为 JPG
我正在尝试将 PDF 文件转换为 PNG。 它在命令行中运行得很好(我安装了 GhostScript 8.64)。 但是从 PHP 中我遇到了一个问题:
代码:
$im = new Imagick($pdf_file); // this is where it throws the exception below
输出:
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(’…’)
等等。
我不确定我在这里做错了什么,但我怀疑它与我的服务器配置有关。 我在跑: 阿帕奇2.2.11 PHP 5.2.8 ImageMagick 6.4.8-9 GhostScript 8.64
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:
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
终于明白了这一点。 GhostScript 可执行文件 (
gs
) 不在 Apache 的环境路径中。 它位于/usr/local/bin
中。 尽管我尝试了多种方法将/usr/local/bin
添加到路径中,但没有成功。 我最终在/usr/bin 目录
中放置了gs
的符号链接。 现在一切都很完美。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/bin
to the path, I did not succeed. I ended up putting a symlink forgs
in the/usr/bin directory
. Now everything works perfectly.我在 Stackoverflow 上没有“声誉”来添加上面的内嵌评论,但我必须执行一个额外的步骤才能在我的 Mac 上使用最新的 Sierra 更新来实现此功能。
当您输入命令时:
在 Mac 上,您可能会收到错误“不允许操作”。
显然苹果做了一个改变,“bin”目录不可编辑,除非你禁用SIP(系统完整性保护)。
因此,执行此操作的步骤如下:
crutildisable
并按“Enter”我刚刚完成了这些步骤,现在我的 Ghostscript 工作得很好,我成功地将 PDF 转换为 JPG。
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.
When you enter the command:
On the Mac, you may get the error, "Operation not Permitted".
Apparently Apple made a change that the "bin" directory is not editable, unless you disable SIP (System Integrity Protection).
So here are the steps to do that:
crutil disable
and press "Enter"I just went through these steps and now my Ghostscript works great and I successfully converted a PDF to JPG.
我成功地做到了这一点。 这是我用来进行转换的代码。 我们正在商业上使用这个解决方案。 我知道这个问题已经存在了一段时间,但它仍然可能对您有所帮助。
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.