0

Environment: Using brew installed ghostscript and brew installed imagemagick on Mac with Mamp. Don't ask why... or suggest alternate means of executing

Issue: Running php exec through mamp php page with imagemagick's convert on a pdf file to output an image does nothing. No errors returned. When running the same command in console it works. When running gs with php exec it works on a pdf file to extract a single page. When running convert with php exec to convert an image to another image it works. Even when running convert pdf to image with php -r in command line works.


Doesn't Work:

PHP Exec convert pdf: (not works) :(

$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.

All below Works:

Command Line: (works)

convert input.pdf[0] output.jpg

Command Line: (works)

php -r 'exec("convert input.pdf[0] output.jpg");'

PHP Exec gs: (works)

$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 convert image: (works)

$im_command = "convert input.jpg output.jpg";
exec($im_command, $output);
3
  • Have you tried to pass an absolute path for input.pdf? Why do you think that an empty command output means an error? You should check the exit status instead (exec's 3rd argument). Commented Nov 5, 2016 at 13:10
  • It is -debug rather than --debug, by the way. Commented Nov 5, 2016 at 13:52
  • Try setting the PATH before calling convert and include /usr/local/bin where homebrew puts all its binaries. Commented Nov 5, 2016 at 13:53

2 Answers 2

1

Turns out Apache does not have path to 'gs'.

Two solutions:

  1. Make sure Apache has EnvironmentVariables Path to location of gs. Example: '/usr/local/bin'
  2. Update delegate to call ghostscript with absolute path.
    • Update {{imagemagick-install-dir}}/delegates.xml
    • command=""gs" to command=""/usr/local/bin/gs"

Reference: http://www.imagemagick.org/discourse-server/viewtopic.php?t=29096#p129955

Sign up to request clarification or add additional context in comments.

Comments

0

Only with image magick convert command, if you try to create an image using a pdf file, you must to remove all parameters, first convert it, in a second step you can do any kind of extra convertion

  1. convert input.pdf[0] output.jpg

  2. convert output.jpg -resize 100x129 output_thumb.jpg

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.