【问题标题】:Imagick/Ghostscript error with PDF inputPDF 输入的 Imagick/Ghostscript 错误
【发布时间】:2015-02-19 23:24:24
【问题描述】:

我已经在 Windows 上使用 imagick PHP 的 DLL 安装了 ImageMagick。

我需要将 PDF 转换为 JPEG。所以我也安装了 Ghostcript。

如果我运行这个命令:

 convert rose.pdf rose.jpg 

效果很好。但如果我尝试在我的网络环境中使用 PHP 重现同样的功能:

$im = new imagick('test_pdf.pdf[0]' ); 

$im->setImageColorspace(255);
$im->setCompression(Imagick::COMPRESSION_JPEG);
$im->setCompressionQuality(100);
$im->setImageFormat('pdf');
$im->writeImage('thumb.jpg');

然后我的页面就挂了。

我假设 Imagick PHP 无法使用 PDF 转为 JPEG,因为它不知道如何使用 Ghostscript 库 gslib

但是,使用此代码,我可以将 PNG 转换为 JPEG。

我尝试在delegates.xml 中进行编辑,但我不确定应该如何进行。

以下是我的delegates.xml 中的相关行:

<delegate decode="pdf" encode="eps" mode="bi" \
    command="&quot;@PSDelegate@&quot; \
   -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 \
   -dEPSCrop \
   -sDEVICE=epswrite &quot;-sOutputFile=%o&quot; -- &quot;%i&quot;"/>

<delegate decode="pdf" encode="ps" mode="bi" \
    command="&quot;@PSDelegate@&quot; \
   -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 \
   -dEPSCrop -dAlignToPixels=0 -dGridFitTT=2 \
   -sDEVICE=pswrite &quot;-sOutputFile=%o&quot; -- &quot;%i&quot;"/>

更新

我尝试使用 -verbose 运行 ImageMagick 以获得更多提示:

convert -verbose 2.pdf 1.jpg

结果:

[ghostscript library] Files/gs/gs9.15/bin/gswin64c.exe" -q -dQUIET -dSAFER -dBATCH 
-dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dEPSCrop -dAlignToPixels=0 -dGridFitTT=2
 "-sDEVICE=pngalpha" -dTextAlphaBits=4 -dGraphicsAlphaBits=4 "-r72x72"
 "-sOutputFile=C:/Users/Usuario/AppData/Local/Temp/magick-PjagmlB-%08d" 
 "-fC:/Users/Usuario/AppData/Local/Temp/magick-1l5fdY8X"
 "-fC:/Users/Usuario/AppData/Local/Temp/magick-Fr-GsA3"
  C:/Users/Usuario/AppData/Local/Temp/magick-_PjagmlB-000000 
 1 PNG 609x791 609x791+0+0 8-bit DirectClass 47.8KB 0.016u 0:00.031
 2.pdf PDF 609x791 609x791+0+0 16-bit DirectClass 47.8KB 0.000u 0:00.031
 2.pdf=>1.jpg PDF 609x791 609x791+0+0 16-bit DirectClass 131KB 0.031u 0:00.046 –

Apache 错误日志的结果

[2014 年 12 月 22 日星期一 09:11:59.022854] [mpm_winnt:notice] [pid 6600:tid 520] AH00428:父级:子进程 6552 以状态 255 退出 -- 正在重新启动。 [2014 年 12 月 22 日星期一 09:11:59.170587] [mpm_winnt:notice] [pid 6600:tid 520] AH00455: Apache/2.4.9 (Win64) PHP/5.5.12 已配置 -- 恢复正常操作 -- PHP/5.5。 12已配置——恢复正常操作 [2014 年 12 月 22 日星期一 09:11:59.170587] [mpm_winnt:notice] [pid 6600:tid 520] AH00456:Apache Lounge VC11 服务器建成时间:2014 年 3 月 16 日 12:42:59 [2014 年 12 月 22 日星期一 09:11:59.170587] [core:notice] [pid 6600:tid 520] AH00094:命令行:'c:\\wamp\\bin\\apache\\apache2.4.9\\bin\\ httpd.exe -d C:/wamp/bin/apache/apache2.4.9' [Mon Dec 22 09:11:59.170587 2014] [mpm_winnt:notice] [pid 6600:tid 520] AH00418: Parent: Created child process 4484 [2014 年 12 月 22 日星期一 09:11:59.807339] [mpm_winnt:notice] [pid 4484:tid 436] AH00354:子级:启动 64 个工作线程。

我在 PDF 加载时创建了一个 var_dump

$im = new imagick( __DIR__ . DIRECTORY_SEPARATOR .'test_pdf.pdf' ); 
 var_dump($im);die;

但是在进入var_dump之前页面就关闭了。

测试场景

  • 在命令行上使用 PHP 将 PDF 转换为 JPG > 工作
  • 在浏览器中使用 PHP-FPM 将 PDF 转换为 JPG > 不起作用(上述错误)
  • 在浏览器中使用 PHP-FPM 将 JPG 转为 PDF > 有效

【问题讨论】:

  • 您能否将-verbose 添加到您成功的PDF 转换命令中?运行convert -verbose rose.pdf rose.jpg。您将看到 ImageMagick 调用的 Ghostscript 命令。也许这会提示您在 Imagick 设置中需要更改的内容...
  • 奇怪...我觉得[ghostscript library] Files/gs/gs9.15/bin/gswin64c.exe" 部分有点奇怪。你确定你复制粘贴正确吗?!? -- 我猜这应该是"C:/Program Files/gs/gs9.15/bin/gswin64c.exe"
  • 是的,它的复制粘贴包括 [ghostscript 库] 我有同样的疑问,我尝试使用 Files/gs/gs9.15/bin/gswin64c.ex 但我没有成功,我现在将尝试使用 C:/Program 文件
  • “我假设...”不要假设。您的日志文件中的实际错误是什么?
  • 我在运行 convert -verbose 2.pdf 1.jpg 时没有错误,但是当我使用 php 代码转换 PDF 时,页面会关闭。

标签: php imagemagick ghostscript imagick


【解决方案1】:

以下是来自 my delegates.xml 的相关行。然而,我不使用 Imagick 或 PHP,所以我不确定它是否是您需要进行的唯一更改才能使其正常工作。

首先,一种无需水平滚动即可阅读的表单。您必须删除我插入的换行符以及行继续字符 \

<delegate decode="pdf" encode="eps" mode="bi" \
  command="&quot;/opt/local/bin/gsx&quot; \
 -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 \
 -dAlignToPixels=0 -dGridFitTT=2 &quot;-sDEVICE=epswrite&quot; \
  &quot;-sOutputFile=%o&quot; &quot;-f%i&quot;"/>

<delegate decode="pdf" encode="ps" mode="bi" \
  command="&quot;/opt/local/bin/gsx&quot; \
 -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 \
 -dAlignToPixels=0 -dGridFitTT=2 &quot;-sDEVICE=ps2write&quot; \
  &quot;-sOutputFile=%o&quot; &quot;-f%i&quot;"/>

现在是原始行。您需要水平滚动才能完整查看其原始形状:

<delegate decode="pdf" encode="eps" mode="bi" command="&quot;/opt/local/bin/gsx&quot; -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 &quot;-sDEVICE=epswrite&quot; &quot;-sOutputFile=%o&quot; &quot;-f%i&quot;"/>
<delegate decode="pdf" encode="ps" mode="bi" command="&quot;/opt/local/bin/gsx&quot; -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 &quot;-sDEVICE=ps2write&quot; &quot;-sOutputFile=%o&quot; &quot;-f%i&quot;"/>

这些行来自我的 Mac OS X 版本的 ImageMagick,6.9.0-0 Q16 x86_64 2014-12-06

如您所见,它不使用@PSDelegate-syntax。相反,它包含 Ghostscript 可执行文件的完整路径,在 my 的情况下为 /opt/local/bin/gsx

对于您的情况,它应该是您的 Ghostscript 可执行文件的完整 Windows 路径,通常命名为 gswin32c.exegswin64c.exe

您对奇怪的@PSDelegate 的问题可能来自您安装 ImageMagick/Imagick 在安装 Ghostscript 之前,然后才安装 Ghostscript....

更新

也许您应该尝试在您的delegates.xml 文件中为您的 Windows/Ghostscript 安装以下行:

<delegate decode="pdf" encode="eps" mode="bi" command="&quot;C:/Program Files/gs/gs9.15/bin/gswin64c.exe&quot; -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 &quot;-sDEVICE=epswrite&quot; &quot;-sOutputFile=%o&quot; &quot;-f%i&quot;"/>
<delegate decode="pdf" encode="ps" mode="bi" command="&quot;C:/Program Files/gs/gs9.15/bin/gswin64c.exe&quot; -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 &quot;-sDEVICE=ps2write&quot; &quot;-sOutputFile=%o&quot; &quot;-f%i&quot;"/>

注意,您不需要使用反斜杠 ('\') -- Ghostscript 和 ImageMagick 可以使用正斜杠 ('/' ) 在 Windows 上。

【讨论】:

  • 当我运行 PHP 代码将 PDF 转换为 JPG 或本地主机上的任何页面出现故障时,编辑的委托仍然没有运气
  • 现在它与 php 函数中的 exec 语句一起工作
  • 仅仅更新delegates.xml 中的这两行是不够的。我必须替换文件中@PSDelegate@ 的所有实例才能正常工作。
【解决方案2】:

它对我有用:)。我已经在那里尝试了几个月的安装,但没有成功。

我只是关注了那个话题:Where to find php_imagick.dll for php 5.5.12 for Windows wampserver 2.5?

之后,我将 C:/Path/to/ImageMagick/delegates.xml 复制/粘贴到 C:/wamp/bin/php/php.5.5.12/ext/imagick 并编辑委托标签文件:

<delegate decode="eps" encode="pdf" mode="bi" command="&quot;C:\Program Files (x86)\gs\gs9.15\bin\gswin32c.exe&quot; -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -sDEVICE=pdfwrite &quot;-sOutputFile=%o&quot; -- &quot;%i&quot;"/>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-10-18
    • 1970-01-01
    • 2019-05-02
    • 2018-05-28
    • 1970-01-01
    • 2016-11-05
    • 2014-09-12
    • 2013-12-22
    相关资源
    最近更新 更多