You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ImageMagick与Ghostscript在CMD可用但PHP中失效的问题求助

解决方案:Windows 2019下PHP Imagick调用Ghostscript转PDF失败问题

问题背景

从Windows Server 2012迁移至2019后,原有的PHP Imagick+ImageMagick+Ghostscript转PDF为JPG的功能失效,报错如下:

Fatal error: Uncaught ImagickException: FailedToExecuteCommand `"gs" -sstdout=%stderr -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 "-sDEVICE=pngalpha" -dTextAlphaBits=4 -dGraphicsAlphaBits=4 "-r300x300" -dPrinted=false -dFirstPage=1 -dLastPage=1 "-sOutputFile=C:/Windows/TEMP/magick-mkMmFi8vXckWEAzjPQWNNyAeLCmErcUI%d" "-fC:/Windows/TEMP/magick-RHvJ0K3gv-gkEMBSrS8cft5IBz4k5e3f" "-fC:/Windows/TEMP/magick-YHH7fNU9o9vL3dni8YuaJxpvgDWi04hK"' (The system cannot find the file specified. ) @ error/delegate.c/ExternalDelegateCommand/516 in C:\inetpub\vhosts\example.com\httpdocs\crons\test.php:1214 Stack trace: #0 C:\inetpub\vhosts\example.com\httpdocs\crons\test.php(1214): Imagick->readImage() #1 {main} thrown in C:\inetpub\vhosts\example.com\httpdocs\crons\test.php on line 1214

已完成以下操作但无效:

  • 将ImageMagick与Ghostscript路径添加至环境变量
  • 修改ImageMagick的Delegates.xml配置Ghostscript路径
  • 修改Policy.xml开放PDF读写权限及临时路径
  • 验证Ghostscript注册表键及目录权限
  • 命令行执行magick test.pdf test.png正常,PHP的exec("gswin64c --help")能正常获取信息,但Imagick扩展无法调用Ghostscript

环境配置:PHP 7.4.3,所有组件均为64位(Windows、IIS、PHP、ImageMagick 7.1.0、Ghostscript 10)


针对性解决方案

1. 修正Delegates.xml中的Ghostscript绝对路径

找到ImageMagick安装目录下的delegates.xml,定位到处理PDF的delegate节点(通常包含decode="ps:alpha"或pdf的条目),将其中的"gs"替换为Ghostscript可执行文件的完整绝对路径,示例:

<delegate decode="ps:alpha" encode="pngalpha" mode="bi" command="&quot;C:/Program Files/gs/gs10.02.1/bin/gswin64c.exe&quot; -sstdout=%stderr -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 &quot;-sDEVICE=pngalpha&quot; -dTextAlphaBits=4 -dGraphicsAlphaBits=4 &quot;-r%s&quot; %s &quot;-sOutputFile=%s&quot; &quot;-f%s&quot; &quot;-f%s&quot;"/>

注意:路径使用正斜杠/或双反斜杠\\,确保版本号与你安装的Ghostscript一致。

2. 强制重启IIS及应用池

环境变量和配置文件修改后,IIS应用池不会自动加载新配置,需执行以下操作:

  • 打开命令提示符,运行iisreset重启整个IIS服务
  • 或在IIS管理器中找到对应应用池,右键选择“回收”

3. 确认应用池身份对临时目录的权限

确保IIS应用池的运行身份(默认是IIS AppPool\你的应用池名称)对C:\Windows\TEMP目录具备读、写、修改权限:

  1. 右键TEMP目录→属性→安全→编辑
  2. 添加应用池身份,赋予对应的权限

4. 验证Imagick扩展与ImageMagick版本兼容性

在phpinfo()中查看Imagick模块的信息,确认Imagick version与安装的ImageMagick 7.1.0版本匹配,若不匹配需下载对应版本的64位Imagick扩展替换。

5. 检查ImageMagick的Policy.xml限制

确保Policy.xml中没有额外限制PDF处理的规则,相关配置应如下:

<policy domain="coder" rights="read|write" pattern="PDF" />
<policy domain="resource" name="temporary-path" value="C:/Windows/TEMP"/>

内容的提问来源于stack exchange,提问作者Fred Langemark

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.18 16:01:10