为什么PHP exec('convert input.pdf output.jpg')不起作用?

时间:2016-11-05 12:57:26

标签: php macos pdf imagemagick ghostscript

环境:在Mamp上使用brew安装的ghostscript和brew安装的imagemagick。 不要问为什么......或建议其他方式执行

问题:通过mamp php页面运行php exec,使用imagemagick在pdf文件上转换输出图像什么都不做。没有错误返回。在控制台中运行相同的命令时,它可以工作。当使用php exec运行gs时,它适用于pdf文件以提取单个页面。当使用php exec运行转换以将图像转换为另一个图像时,它可以工作。即使在命令行中使用php -r运行convert pdf to image。

不起作用:

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执行:(工作)

$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);

2 个答案:

答案 0 :(得分:0)

原来Apache没有'gs'的路径。

两个解决方案:

  1. 确保Apache具有指向gs位置的EnvironmentVariables路径。示例:'/ usr / local / bin'
  2. 更新委托以使用绝对路径调用ghostscript。
    • 更新{{imagemagick-install-dir}} / delegates.xml
    • command=""gs"command=""/usr/local/bin/gs"
  3. 参考:http://www.imagemagick.org/discourse-server/viewtopic.php?t=29096#p129955

答案 1 :(得分:0)

仅使用image magick convert命令,如果您尝试使用pdf文件创建图像,则必须删除所有参数,首先将其转换,然后在第二步中可以进行任何其他形式的转换

  1. 转换input.pdf [0] output.jpg

  2. 转换output.jpg-调整大小100x129 output_thumb.jpg