为啥 PHP exec('convert input.pdf output.jpg') 不起作用?
Posted
技术标签:
【中文标题】为啥 PHP exec(\'convert input.pdf output.jpg\') 不起作用?【英文标题】:Why does PHP exec('convert input.pdf output.jpg') not work?为什么 PHP exec('convert input.pdf output.jpg') 不起作用? 【发布时间】:2016-11-05 12:57:26 【问题描述】:环境:在带有 Mamp 的 Mac 上使用 brew 安装 ghostscript 和 brew 安装 imagemagick。 不要问为什么......或建议其他执行方式
问题:通过 mamp php 页面运行 php exec 并在 pdf 文件上使用 imagemagick 的转换来输出图像没有任何作用。没有返回错误。在控制台中运行相同的命令时,它可以工作。当使用 php exec 运行 gs 时,它可以在 pdf 文件上提取单个页面。当使用 php exec 运行转换以将图像转换为另一个图像时,它可以工作。即使在命令行中使用 php -r 运行将 pdf 转换为图像也可以。
不起作用:
PHP Exec 转换 pdf:(不起作用):(
$pdf_command = "convert input.pdf[0] output.jpg --debug all 2>&1";
exec($pdf_command, $output);
print_r($output);
[0] => 2016-11-05T09:24:54-04:00 0:00.000 0.000u 6.9.3 Configure convert[95804]: utility.c/ExpandFilenames/940/Configure
[1] => Command line: convert test_pdf.pdf[0] outimage.jpg -debug all
[2] => 2016-11-05T09:24:54-04:00 0:00.000 0.000u 6.9.3 Configure convert[95804]: configure.c/GetConfigureOptions/679/Configure
[3] => Searching for configure file: "/usr/local/Cellar/imagemagick/6.9.3-7/share/ImageMagick-6/coder.xml"
[4] => 2016-11-05T09:24:54-04:00 0:00.000 0.000u 6.9.3 Configure convert[95804]: configure.c/GetConfigureOptions/679/Configure
[5] => Searching for configure file: "/usr/local/Cellar/imagemagick/6.9.3-7/lib/ImageMagick//config-Q16/coder.xml"
[6] => 2016-11-05T09:24:54-04:00 0:00.000 0.000u 6.9.3 Configure convert[95804]: configure.c/GetConfigureOptions/679/Configure
[7] => Searching for configure file: "/usr/local/Cellar/imagemagick/6.9.3-7/etc/ImageMagick-6/coder.xml"
[8] => 2016-11-05T09:24:54-04:00 0:00.000 0.000u 6.9.3 Configure convert[95804]: configure.c/GetConfigureOptions/679/Configure
[9] => Searching for configure file: "/usr/local/Cellar/imagemagick/6.9.3-7/share/doc/ImageMagick-6/coder.xml"
[10] => 2016-11-05T09:24:54-04:00 0:00.000 0.000u 6.9.3 Configure convert[95804]: coder.c/LoadCoderCache/823/Configure
[11] => Loading coder configuration file "/usr/local/Cellar/imagemagick/6.9.3-7/etc/ImageMagick-6/coder.xml" ...
[12] => 2016-11-05T09:24:54-04:00 0:00.000 0.000u 6.9.3 Module convert[95804]: module.c/OpenModule/1286/Module
[13] => Searching for module "PDF" using filename "pdf.la"
[14] => 2016-11-05T09:24:54-04:00 0:00.000 0.000u 6.9.3 Module convert[95804]: module.c/GetMagickModulePath/556/Module
[15] => Searching for coder module file "pdf.la" ...
[16] => 2016-11-05T09:24:54-04:00 0:00.000 0.000u 6.9.3 Module convert[95804]: module.c/OpenModule/1295/Module
[17] => Opening module at path "/usr/local/Cellar/imagemagick/6.9.3-7/lib/ImageMagick//modules-Q16/coders/pdf.la"
[18] => 2016-11-05T09:24:54-04:00 0:00.000 0.000u 6.9.3 Module convert[95804]: module.c/OpenModule/1322/Module
[19] => Method "RegisterPDFImage" in module "PDF" at address 0x10afcb268
[20] => 2016-11-05T09:24:54-04:00 0:00.000 0.000u 6.9.3 Module convert[95804]: module.c/OpenModule/1336/Module
[21] => Method "UnregisterPDFImage" in module "PDF" at address 0x10afd144b
[22] => 2016-11-05T09:24:54-04:00 0:00.000 0.000u 6.9.3 Policy convert[95804]: policy.c/IsRightsAuthorized/574/Policy
[23] => Domain: Path; rights=Read; pattern="test_pdf.pdf" ...
[24] => 2016-11-05T09:24:54-04:00 0:00.000 0.000u 6.9.3 Blob convert[95804]: blob.c/OpenBlob/2594/Blob
[25] => read 3 magic header bytes
[26] => 2016-11-05T09:24:54-04:00 0:00.000 0.000u 6.9.3 Cache convert[95804]: cache.c/DestroyPixelCache/999/Cache
[27] => destroy
[28] => 2016-11-05T09:24:54-04:00 0:00.000 0.000u 6.9.3 Configure convert[95804]: configure.c/GetConfigureOptions/679/Configure
[29] => Searching for configure file: "/usr/local/Cellar/imagemagick/6.9.3-7/share/ImageMagick-6/magic.xml"
[30] => 2016-11-05T09:24:54-04:00 0:00.000 0.000u 6.9.3 Configure convert[95804]: configure.c/GetConfigureOptions/679/Configure
[31] => Searching for configure file: "/usr/local/Cellar/imagemagick/6.9.3-7/lib/ImageMagick//config-Q16/magic.xml"
[32] => 2016-11-05T09:24:54-04:00 0:00.000 0.000u 6.9.3 Configure convert[95804]: configure.c/GetConfigureOptions/679/Configure
[33] => Searching for configure file: "/usr/local/Cellar/imagemagick/6.9.3-7/etc/ImageMagick-6/magic.xml"
[34] => 2016-11-05T09:24:54-04:00 0:00.000 0.000u 6.9.3 Configure convert[95804]: configure.c/GetConfigureOptions/679/Configure
[35] => Searching for configure file: "/usr/local/Cellar/imagemagick/6.9.3-7/share/doc/ImageMagick-6/magic.xml"
[36] => 2016-11-05T09:24:54-04:00 0:00.000 0.000u 6.9.3 Configure convert[95804]: magic.c/LoadMagicCache/789/Configure
[37] => Loading magic configure file "/usr/local/Cellar/imagemagick/6.9.3-7/etc/ImageMagick-6/magic.xml" ...
[38] => 2016-11-05T09:24:54-04:00 0:00.000 0.000u 6.9.3 Policy convert[95804]: policy.c/IsRightsAuthorized/574/Policy
[39] => Domain: Coder; rights=Read; pattern="PDF" ...
[40] => 2016-11-05T09:24:54-04:00 0:00.000 0.000u 6.9.3 Policy convert[95804]: policy.c/IsRightsAuthorized/574/Policy
[41] => Domain: Path; rights=Read; pattern="test_pdf.pdf" ...
[42] => 2016-11-05T09:24:54-04:00 0:00.000 0.000u 6.9.3 Blob convert[95804]: blob.c/OpenBlob/2594/Blob
[43] => read 3 magic header bytes
[44] => 2016-11-05T09:24:54-04:00 0:00.000 0.000u 6.9.3 Cache convert[95804]: cache.c/DestroyPixelCache/999/Cache
[45] => destroy
[46] => 2016-11-05T09:24:54-04:00 0:00.000 0.000u 6.9.3 Policy convert[95804]: policy.c/IsRightsAuthorized/574/Policy
[47] => Domain: Path; rights=Read; pattern="test_pdf.pdf" ...
[48] => 2016-11-05T09:24:54-04:00 0:00.000 0.000u 6.9.3 Blob convert[95804]: blob.c/OpenBlob/2594/Blob
[49] => read 3 magic header bytes
[50] => 2016-11-05T09:24:54-04:00 0:00.000 0.000u 6.9.3 Resource convert[95804]: resource.c/AcquireUniqueFileResource/502/Resource
[51] => ...
[52] => 2016-11-05T09:24:54-04:00 0:00.000 0.000u 6.9.3 Resource convert[95804]: resource.c/AcquireUniqueFileResource/553/Resource
[53] => Acquire /var/tmp/magick-95804YtWGdJXpcmM8
[54] => 2016-11-05T09:24:54-04:00 0:00.000 0.000u 6.9.3 Resource convert[95804]: resource.c/RelinquishUniqueFileResource/1042/Resource
[55] => Relinquish /var/tmp/magick-95804YtWGdJXpcmM8
[56] => 2016-11-05T09:24:54-04:00 0:00.000 0.000u 6.9.3 Exception convert[95804]: utility.c/ShredFile/1822/Exception
[57] => Failed to remove: /var/tmp/magick-95804YtWGdJXpcmM8.cache
[58] => 2016-11-05T09:24:54-04:00 0:00.120 0.110u 6.9.3 Resource convert[95804]: resource.c/AcquireUniqueFileResource/502/Resource
[59] => ...
[60] => 2016-11-05T09:24:54-04:00 0:00.120 0.110u 6.9.3 Resource convert[95804]: resource.c/AcquireUniqueFileResource/553/Resource
[61] => Acquire /var/tmp/magick-95804xlBtBwymf2E7
[62] => 2016-11-05T09:24:54-04:00 0:00.120 0.110u 6.9.3 Configure convert[95804]: configure.c/GetConfigureOptions/679/Configure
[63] => Searching for configure file: "/usr/local/Cellar/imagemagick/6.9.3-7/share/ImageMagick-6/delegates.xml"
[64] => 2016-11-05T09:24:54-04:00 0:00.120 0.110u 6.9.3 Configure convert[95804]: configure.c/GetConfigureOptions/679/Configure
[65] => Searching for configure file: "/usr/local/Cellar/imagemagick/6.9.3-7/lib/ImageMagick//config-Q16/delegates.xml"
[66] => 2016-11-05T09:24:54-04:00 0:00.120 0.110u 6.9.3 Configure convert[95804]: configure.c/GetConfigureOptions/679/Configure
[67] => Searching for configure file: "/usr/local/Cellar/imagemagick/6.9.3-7/etc/ImageMagick-6/delegates.xml"
[68] => 2016-11-05T09:24:54-04:00 0:00.120 0.110u 6.9.3 Configure convert[95804]: configure.c/GetConfigureOptions/679/Configure
[69] => Searching for configure file: "/usr/local/Cellar/imagemagick/6.9.3-7/share/doc/ImageMagick-6/delegates.xml"
[70] => 2016-11-05T09:24:54-04:00 0:00.120 0.110u 6.9.3 Configure convert[95804]: delegate.c/LoadDelegateCache/1498/Configure
[71] => Loading delegate configuration file "/usr/local/Cellar/imagemagick/6.9.3-7/etc/ImageMagick-6/delegates.xml" ...
[72] => 2016-11-05T09:24:54-04:00 0:00.120 0.110u 6.9.3 Resource convert[95804]: resource.c/AcquireUniqueFileResource/502/Resource
[73] => ...
[74] => 2016-11-05T09:24:54-04:00 0:00.120 0.110u 6.9.3 Resource convert[95804]: resource.c/AcquireUniqueFileResource/553/Resource
[75] => Acquire /var/tmp/magick-95804sJWpHcGjyE2T
[76] => 2016-11-05T09:24:54-04:00 0:00.120 0.110u 6.9.3 Resource convert[95804]: resource.c/RelinquishUniqueFileResource/1042/Resource
[77] => Relinquish /var/tmp/magick-95804sJWpHcGjyE2T
[78] => 2016-11-05T09:24:54-04:00 0:00.120 0.110u 6.9.3 Exception convert[95804]: utility.c/ShredFile/1822/Exception
[79] => Failed to remove: /var/tmp/magick-95804sJWpHcGjyE2T.cache
[80] => 2016-11-05T09:24:54-04:00 0:00.120 0.110u 6.9.3 Policy convert[95804]: policy.c/IsRightsAuthorized/574/Policy
[81] => Domain: Delegate; rights=Execute; pattern="gs" ...
[82] => 2016-11-05T09:24:54-04:00 0:00.120 0.110u 6.9.3 Resource convert[95804]: resource.c/RelinquishUniqueFileResource/1042/Resource
[83] => Relinquish /var/tmp/magick-95804xlBtBwymf2E7
[84] => 2016-11-05T09:24:54-04:00 0:00.120 0.110u 6.9.3 Exception convert[95804]: utility.c/ShredFile/1822/Exception
[85] => Failed to remove: /var/tmp/magick-95804xlBtBwymf2E7.cache
[86] => 2016-11-05T09:24:54-04:00 0:00.120 0.110u 6.9.3 Resource convert[95804]: resource.c/RelinquishUniqueFileResource/1042/Resource
[87] => Relinquish /var/tmp/magick-95804YtWGdJXpcmM8
[88] => 2016-11-05T09:24:54-04:00 0:00.120 0.110u 6.9.3 Exception convert[95804]: utility.c/ShredFile/1822/Exception
[89] => Failed to remove: /var/tmp/magick-95804YtWGdJXpcmM8.cache
[90] => 2016-11-05T09:24:54-04:00 0:00.120 0.110u 6.9.3 Cache convert[95804]: cache.c/DestroyPixelCache/999/Cache
[91] => destroy
[92] => 2016-11-05T09:24:54-04:00 0:00.120 0.110u 6.9.3 Module convert[95804]: module.c/OpenModule/1286/Module
[93] => Searching for module "JPEG" using filename "jpeg.la"
[94] => 2016-11-05T09:24:54-04:00 0:00.120 0.110u 6.9.3 Module convert[95804]: module.c/GetMagickModulePath/556/Module
[95] => Searching for coder module file "jpeg.la" ...
[96] => 2016-11-05T09:24:54-04:00 0:00.120 0.110u 6.9.3 Module convert[95804]: module.c/OpenModule/1295/Module
[97] => Opening module at path "/usr/local/Cellar/imagemagick/6.9.3-7/lib/ImageMagick//modules-Q16/coders/jpeg.la"
[98] => 2016-11-05T09:24:54-04:00 0:00.120 0.110u 6.9.3 Module convert[95804]: module.c/OpenModule/1322/Module
[99] => Method "RegisterJPEGImage" in module "JPEG" at address 0x10b00ce20
[100] => 2016-11-05T09:24:54-04:00 0:00.120 0.110u 6.9.3 Module convert[95804]: module.c/OpenModule/1336/Module
[101] => Method "UnregisterJPEGImage" in module "JPEG" at address 0x10b0107f7
[102] => 2016-11-05T09:24:54-04:00 0:00.120 0.110u 6.9.3 Policy convert[95804]: policy.c/IsRightsAuthorized/574/Policy
[103] => Domain: Path; rights=Read; pattern="outimage.jpg" ...
[104] => 2016-11-05T09:24:54-04:00 0:00.120 0.110u 6.9.3 Locale convert[95804]: locale.c/GetLocaleOptions/825/Locale
[105] => Searching for locale file: "/usr/local/Cellar/imagemagick/6.9.3-7/share/ImageMagick-6/locale.xml"
[106] => 2016-11-05T09:24:54-04:00 0:00.120 0.110u 6.9.3 Locale convert[95804]: locale.c/GetLocaleOptions/825/Locale
[107] => Searching for locale file: "/usr/local/Cellar/imagemagick/6.9.3-7/lib/ImageMagick//config-Q16/locale.xml"
[108] => 2016-11-05T09:24:54-04:00 0:00.120 0.110u 6.9.3 Locale convert[95804]: locale.c/GetLocaleOptions/825/Locale
[109] => Searching for locale file: "/usr/local/Cellar/imagemagick/6.9.3-7/etc/ImageMagick-6/locale.xml"
[110] => 2016-11-05T09:24:54-04:00 0:00.120 0.110u 6.9.3 Locale convert[95804]: locale.c/GetLocaleOptions/825/Locale
[111] => Searching for locale file: "/usr/local/Cellar/imagemagick/6.9.3-7/share/doc/ImageMagick-6/locale.xml"
[112] => 2016-11-05T09:24:54-04:00 0:00.120 0.110u 6.9.3 Configure convert[95804]: locale.c/LoadLocaleCache/1176/Configure
[113] => Loading locale configure file "/usr/local/Cellar/imagemagick/6.9.3-7/share/ImageMagick-6/locale.xml" ...
[114] => 2016-11-05T09:24:54-04:00 0:00.120 0.110u 6.9.3 Locale convert[95804]: locale.c/GetLocaleOptions/825/Locale
[115] => Searching for locale file: "/usr/local/Cellar/imagemagick/6.9.3-7/share/ImageMagick-6/english.xml"
[116] => 2016-11-05T09:24:54-04:00 0:00.120 0.110u 6.9.3 Locale convert[95804]: locale.c/GetLocaleOptions/825/Locale
[117] => Searching for locale file: "/usr/local/Cellar/imagemagick/6.9.3-7/lib/ImageMagick//config-Q16/english.xml"
[118] => 2016-11-05T09:24:54-04:00 0:00.120 0.110u 6.9.3 Locale convert[95804]: locale.c/GetLocaleOptions/825/Locale
[119] => Searching for locale file: "/usr/local/Cellar/imagemagick/6.9.3-7/etc/ImageMagick-6/english.xml"
[120] => 2016-11-05T09:24:54-04:00 0:00.120 0.110u 6.9.3 Locale convert[95804]: locale.c/GetLocaleOptions/825/Locale
[121] => Searching for locale file: "/usr/local/Cellar/imagemagick/6.9.3-7/share/doc/ImageMagick-6/english.xml"
[122] => 2016-11-05T09:24:54-04:00 0:00.120 0.110u 6.9.3 Configure convert[95804]: locale.c/LoadLocaleCache/1176/Configure
[123] => Loading locale configure file "/usr/local/Cellar/imagemagick/6.9.3-7/share/ImageMagick-6/english.xml" ...
[124] => 2016-11-05T09:24:54-04:00 0:00.120 0.110u 6.9.3 Exception convert[95804]: blob.c/OpenBlob/2702/Exception
[125] => unable to open image `outimage.jpg': No such file or directory
[126] => 2016-11-05T09:24:54-04:00 0:00.120 0.110u 6.9.3 Cache convert[95804]: cache.c/DestroyPixelCache/999/Cache
[127] => destroy
[128] => 2016-11-05T09:24:54-04:00 0:00.120 0.110u 6.9.3 Policy convert[95804]: policy.c/IsRightsAuthorized/574/Policy
[129] => Domain: Coder; rights=Read; pattern="JPG" ...
[130] => 2016-11-05T09:24:54-04:00 0:00.120 0.110u 6.9.3 Policy convert[95804]: policy.c/IsRightsAuthorized/574/Policy
[131] => Domain: Path; rights=Read; pattern="outimage.jpg" ...
[132] => 2016-11-05T09:24:54-04:00 0:00.120 0.110u 6.9.3 Exception convert[95804]: blob.c/OpenBlob/2702/Exception
[133] => unable to open image `outimage.jpg': No such file or directory
[134] => 2016-11-05T09:24:54-04:00 0:00.120 0.110u 6.9.3 Cache convert[95804]: cache.c/DestroyPixelCache/999/Cache
[135] => destroy
[136] => 2016-11-05T09:24:54-04:00 0:00.120 0.110u 6.9.3 Exception convert[95804]: convert.c/ConvertImageCommand/3249/Exception
[137] => missing an image filename `all'
[138] => convert: unable to open image `outimage.jpg': No such file or directory @ error/blob.c/OpenBlob/2702.
[139] => convert: missing an image filename `all' @ error/convert.c/ConvertImageCommand/3249.
以下所有作品:
命令行:(有效)
convert input.pdf[0] output.jpg
命令行:(有效)
php -r 'exec("convert input.pdf[0] output.jpg");'
PHP Exec gs:(有效)
$gs_command = "gs -sDEVICE=pdfwrite -dNOPAUSE -dBATCH -dSAFER ";
$gs_command .= "-dFirstPage=1 -dLastPage=1 ";
$gs_command .= "-sOutputFile=output.pdf input.pdf ";
exec($gs_command, $output);
PHP Exec 转换图片:(有效)
$im_command = "convert input.jpg output.jpg";
exec($im_command, $output);
【问题讨论】:
您是否尝试为input.pdf
传递绝对路径?为什么你认为空的命令输出意味着错误?您应该检查退出状态(exec
的第三个参数)。
顺便说一下,它是-debug
而不是--debug
。
尝试在调用 convert
之前设置 PATH
并包括 /usr/local/bin
其中 homebrew
放置其所有二进制文件。
【参考方案1】:
原来 Apache 没有到 'gs' 的路径。
两种解决方案:
-
确保 Apache 具有指向 gs 位置的 EnvironmentVariables 路径。示例:'/usr/local/bin'
更新委托以使用绝对路径调用 ghostscript。
更新 imagemagick-install-dir/delegates.xml
command=""gs"
到 command=""/usr/local/bin/gs"
参考:http://www.imagemagick.org/discourse-server/viewtopic.php?t=29096#p129955
【讨论】:
【参考方案2】:仅使用 image magick convert 命令,如果您尝试使用 pdf 文件创建图像,则必须删除所有参数,首先将其转换,在第二步中您可以进行任何类型的额外转换
转换 input.pdf[0] output.jpg
转换 output.jpg -resize 100x129 output_thumb.jpg
【讨论】:
以上是关于为啥 PHP exec('convert input.pdf output.jpg') 不起作用?的主要内容,如果未能解决你的问题,请参考以下文章
为啥这个带有 shell_exec 调用的 PHP 脚本从 Windows 10 的命令行运行,而不是浏览器/本地主机?
PHP exec():为啥我可以访问共享内存,但不能访问命名信号量? (错误号 = 13)