Page 1 of 2

convert works from command line but not php shell PDF

Posted: 2016-04-12T15:31:00-07:00
by Jeremy
Hey guys I'm working with Imagick version Version: ImageMagick 6.9.3-6 Q16 x86_64 2016-04-12

From the command line I can type: convert test.pdf test.png and it works flawlessly.

From a php script if I type exec('convert test.pdf test.png') I get the error convert: no images defined `test.jpg' @ error/convert.c/ConvertImageCommand/3252.

I can also do exec('convert test.png test.jpg') and it works. It seems to just dislike PDF's from exec.

The file exists. Any clues to what's going on?

Thanks.

Re: convert works from command line but not php shell PDF

Posted: 2016-04-12T18:24:00-07:00
by fmw42
From a php script if I type exec('convert test.pdf test.png') I get the error convert: no images defined `test.jpg' @ error/convert.c/ConvertImageCommand/3252.
Are you sure you have the correct message? How can you have a message about JPG, when your input and output are not even JPG?


Best guess - It would appear that your Imagick cannot find where ghostscript is located

Re: convert works from command line but not php shell PDF

Posted: 2016-04-13T07:20:03-07:00
by Jeremy
Ok I ran
$cmd = "convert -list delegate | grep -Ei '(PDF|PS|EPS|JPG|PNG)'";
exec($cmd, $out);

(from a php script) and got:


array(30) {
[0]=>
string(96) " blender => "blender" -b "%i" -F PNG -o "%o""\n"convert" -concatenate "%o*.png" "%o""
[1]=>
string(86) " bpg => "@BPGDecodeDelegate@" -b 16 -o "%o.png" "%i"; mv "%o.png" "%o""
[2]=>
string(116) " dng:decode => "ufraw-batch" --silent --create-id=also --out-type=png --out-depth=16 "--output=%u.png" "%i""
[3]=>
string(116) " doc => "soffice" --headless --convert-to pdf --outdir `dirname "%i"` "%i" 2> "%Z"; mv "%i.pdf" "%o""
[4]=>
string(116) " docx => "soffice" --headless --convert-to pdf --outdir `dirname "%i"` "%i" 2> "%Z"; mv "%i.pdf" "%o""
[5]=>
string(47) " dvi => "dvips" -q -o "%o" "%i""
[6]=>
string(175) " eps<=>ps "gs" -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 "-sDEVICE=ps2write" "-sOutputFile=%o" "-f%i""
[7]=>
string(143) " eps<=>pdf "gs" -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 "-sDEVICE=pdfwrite" "-sOutputFile=%o" "-f%i""
[8]=>
string(97) " hpg => "hp2xx" -q -m eps -f `basename "%o"` "%i"; mv -f `basename "%o"` "%o""
[9]=>
string(97) " hpgl => "hp2xx" -q -m eps -f `basename "%o"` "%i"; mv -f `basename "%o"` "%o""
[10]=>
string(49) " htm => "html2ps" -U -o "%o" "%i""
[11]=>
string(49) " html => "html2ps" -U -o "%o" "%i""
[12]=>
string(58) " https => "curl" -s -k -L -o "%o" "https:%M""
[13]=>
string(53) " man => "groff" -man -Tps "%i" > "%o""
[14]=>
string(116) " odt => "soffice" --headless --convert-to pdf --outdir `dirname "%i"` "%i" 2> "%Z"; mv "%i.pdf" "%o""
[15]=>
string(176) " pdf<=>eps "gs" -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 "-sDEVICE=eps2write" "-sOutputFile=%o" "-f%i""
[16]=>
string(175) " pdf<=>ps "gs" -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 "-sDEVICE=ps2write" "-sOutputFile=%o" "-f%i""
[17]=>
string(57) " png<= webp "cwebp" -quiet -q %Q "%i" -o "%o""
[18]=>
string(83) " png<= bpg "@BPGEncodeDelegate@" -b 12 -q %[fx:quality/2] -o "%o" "%i""
[19]=>
string(129) " pov => "povray" "+i%i" -D0 "+o%o" +fn%q +w%w +h%h +a -q9 "-kfi%s" "-kff%n";"convert" -concatenate "%o*.png" "%o""
[20]=>
string(116) " ppt => "soffice" --headless --convert-to pdf --outdir `dirname "%i"` "%i" 2> "%Z"; mv "%i.pdf" "%o""
[21]=>
string(116) " pptx => "soffice" --headless --convert-to pdf --outdir `dirname "%i"` "%i" 2> "%Z"; mv "%i.pdf" "%o""
[22]=>
string(175) " ps<=>pdf "gs" -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 "-sDEVICE=pdfwrite" "-sOutputFile=%o" "-f%i""
[23]=>
string(176) " ps<=>eps "gs" -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 "-sDEVICE=eps2write" "-sOutputFile=%o" "-f%i""
[24]=>
string(33) " ps<= print "lpr "%i""
[25]=>
string(49) " shtml => "html2ps" -U -o "%o" "%i""
[26]=>
string(47) " txt<=>ps "enscript" -o "%o" "%i""
[27]=>
string(46) " wmf => "wmf2eps" -o "%o" "%i""
[28]=>
string(116) " xls => "soffice" --headless --convert-to pdf --outdir `dirname "%i"` "%i" 2> "%Z"; mv "%i.pdf" "%o""
[29]=>
string(116) " xlsx => "soffice" --headless --convert-to pdf --outdir `dirname "%i"` "%i" 2> "%Z"; mv "%i.pdf" "%o""
}

Re: convert works from command line but not php shell PDF

Posted: 2016-04-13T09:24:17-07:00
by fmw42
I believe that list is the IM defaults and not what delegates you actually have registered with IM. I think this problem has been answered before. I suggest you search the archives and see if you can find the issue. You could try putting the full path to gs in your delegates.xml file. Also what do you get via PHP from

convert -version

Does it list gs or gslib?

Sorry I am not an expert on PHP or Imagick.

Re: convert works from command line but not php shell PDF

Posted: 2016-04-13T09:36:03-07:00
by Jeremy
It lists:

array(5) {
[0]=>
string(77) "Version: ImageMagick 6.9.3-6 Q16 x86_64 2016-04-12 http://www.imagemagick.org"
[1]=>
string(57) "Copyright: Copyright (C) 1999-2016 ImageMagick Studio LLC"
[2]=>
string(54) "License: http://www.imagemagick.org/script/license.php"
[3]=>
string(28) "Features: Cipher DPC Modules"
[4]=>
string(73) "Delegates (built-in): bzlib freetype jng jpeg ltdl lzma png tiff xml zlib"
}

Re: convert works from command line but not php shell PDF

Posted: 2016-04-13T10:44:05-07:00
by fmw42
It does not list gs. So IM cannot find GS under PHP. If you do the same in a terminal (convert -version) do you get gs listed?

Find were Ghostscript resides?

find / | grep "gs"

see if it is at /usr/local/bin/gs or /usr/bin/gs or somewhere else.

Then in a terminal

echo $path

Then do the same via PHP exec().

Is gs in the $PATH for PHP?

Re: convert works from command line but not php shell PDF

Posted: 2016-04-13T13:00:09-07:00
by Jeremy
The problem is though it lists the same from the command line when I run

convert -version

and the command line works fine.

My gs path is:
/usr/local/bin/gs

Re: convert works from command line but not php shell PDF

Posted: 2016-04-13T14:20:12-07:00
by fmw42
What platform? I am told that gs is not listed from Windows from convert -version.

On my Mac I get:
convert -version
Version: ImageMagick 6.9.3-8 Q16 x86_64 2016-04-11 http://www.imagemagick.org
Copyright: Copyright (C) 1999-2016 ImageMagick Studio LLC
License: http://www.imagemagick.org/script/license.php
Features: Cipher DPC Modules OpenMP
Delegates (built-in): bzlib cairo fftw fontconfig freetype gslib jbig jng jp2 jpeg lcms lqr ltdl lzma openexr png ps rsvg tiff webp x xml zlib

and

Code: Select all

convert -list delegates
...
DELEGATES bzlib mpeg fftw fontconfig freetype gslib jbig jng jpeg lcms lqr lzma openexr openjp2 png ps rsvg tiff webp x xml zlib
...


try editing your delegates.xml file to add the full path to gs


<delegate decode="eps" encode="pdf" mode="bi" command=""gs" -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 "-sDEVICE=pdfwrite" "-sOutputFile=%o" "-f%i""/>
<delegate decode="eps" encode="ps" mode="bi" command=""gs" -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 "-sDEVICE=ps2write" "-sOutputFile=%o" "-f%i""/>

Re: convert works from command line but not php shell PDF

Posted: 2016-04-14T12:44:25-07:00
by Jeremy
Heres the contents of my Delegates file:

Where would I specify the full path?

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE delegatemap [
<!ELEMENT delegatemap (delegate)+>
<!ELEMENT delegate (#PCDATA)>
<!ATTLIST delegate decode CDATA #IMPLIED>
<!ATTLIST delegate encode CDATA #IMPLIED>
<!ATTLIST delegate mode CDATA #IMPLIED>
<!ATTLIST delegate spawn CDATA #IMPLIED>
<!ATTLIST delegate stealth CDATA #IMPLIED>
<!ATTLIST delegate thread-support CDATA #IMPLIED>
<!ATTLIST delegate command CDATA #REQUIRED>
]>
<!--
Delegate command file.

Commands which specify

decode="in_format" encode="out_format"

specify the rules for converting from in_format to out_format These
rules may be used to translate directly between formats.

Commands which specify only

decode="in_format"

specify the rules for converting from in_format to some format that
ImageMagick will automatically recognize. These rules are used to
decode formats.

Commands which specify only

encode="out_format"

specify the rules for an "encoder" which may accept any input format.

For delegates other than ps:*, pcl:*, and mpeg:* the substitution rules are
as follows:

%i input image filename
%o output image filename
%u unique temporary filename
%Z unique temporary filename
%# input image signature
%b image file size
%c input image comment
%g image geometry
%h image rows (height)
%k input image number colors
%l image label
%m input image format
%p page number
%q input image depth
%s scene number
%w image columns (width)
%x input image x resolution
%y input image y resolution

Set option delegate:bimodal=true to process bimodal delegates otherwise they
are ignored.
-->
<delegatemap>
<delegate decode="autotrace" stealth="True" command=""convert" "%i" "pnm:%u"\n"autotrace" -input-format pnm -output-format svg -output-file "%o" "%u""/>
<delegate decode="blender" command=""blender" -b "%i" -F PNG -o "%o""\n"convert" -concatenate "%o*.png" "%o""/>
<delegate decode="browse" stealth="True" spawn="True" command=""xdg-open" http://www.imagemagick.org/"/>
<delegate decode="cdr" command=""uniconvertor" "%i" "%o.svg"; mv "%o.svg" "%o""/>
<delegate decode="cgm" thread-support="False" command=""ralcgm" -d ps -oC < "%i" > "%o" 2> "%Z""/>
<delegate decode="dvi" command=""dvips" -q -o "%o" "%i""/>
<delegate decode="dng:decode" command=""ufraw-batch" --silent --create-id=also --out-type=png --out-depth=16 "--output=%u.png" "%i""/>
<delegate decode="edit" stealth="True" command=""xterm" -title "Edit Image Comment" -e vi "%o""/>
<delegate decode="eps" encode="pdf" mode="bi" command=""gs" -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 "-sDEVICE=pdfwrite" "-sOutputFile=%o" "-f%i""/>
<delegate decode="eps" encode="ps" mode="bi" command=""gs" -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 "-sDEVICE=pswrite" "-sOutputFile=%o" "-f%i""/>
<delegate decode="fig" command=""fig2dev" -L ps "%i" "%o""/>
<delegate decode="plt" command=""echo" "set size 1.25,0.62; set terminal postscript portrait color solid; set output \'%o\'; load \'%i\'" > "%u";"gnuplot" "%u""/>
<delegate decode="hpg" command=""hp2xx" -q -m eps -f `basename "%o"` "%i"; mv -f `basename "%o"` "%o""/>
<delegate decode="hpgl" command="if [ -e hp2xx -o -e /usr/bin/hp2xx ]; then hp2xx -q -m eps -f `basename "%o"` "%i"; mv -f `basename "%o"` "%o"; else echo "You need to install hp2xx to use HPGL files with ImageMagick."; exit 1; fi"/>
<delegate decode="htm" command=""html2ps" -U -o "%o" "%i""/>
<delegate decode="html" command=""html2ps" -U -o "%o" "%i""/>
<delegate decode="https" command=""curl" -s -k -o "%o" "https:%M""/>
<delegate decode="ilbm" command=""ilbmtoppm" "%i" > "%o""/>
<delegate decode="man" command=""groff" -man -Tps "%i" > "%o""/>
<delegate decode="mpeg:decode" command=""ffmpeg" -v -1 -vframes %S -i "%i" -vcodec pam -an -f rawvideo -y "%u.pam" 2> "%Z""/>
<delegate encode="mpeg:encode" stealth="True" command=""ffmpeg" -v -1 -mbd rd -trellis 2 -cmp 2 -subcmp 2 -g 300 -i "%M%%d.jpg" "%u.%m" 2> "%Z""/>
<delegate decode="sid" command=""mrsidgeodecode" -if sid -i "%i" -of tif -o "%o" > "%u""/>
<delegate decode="pcl:color" stealth="True" command=""pcl6" -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 "-sDEVICE=ppmraw" -dTextAlphaBits=%u -dGraphicsAlphaBits=%u "-r%s" %s "-sOutputFile=%s" "%s""/>
<delegate decode="pcl:cmyk" stealth="True" command=""pcl6" -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 "-sDEVICE=pamcmyk32" -dTextAlphaBits=%u -dGraphicsAlphaBits=%u "-r%s" %s "-sOutputFile=%s" "%s""/>
<delegate decode="pcl:mono" stealth="True" command=""pcl6" -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 "-sDEVICE=pbmraw" -dTextAlphaBits=%u -dGraphicsAlphaBits=%u "-r%s" %s "-sOutputFile=%s" "%s""/>
<delegate decode="pdf" encode="eps" mode="bi" command=""gs" -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 "-sDEVICE=epswrite" "-sOutputFile=%o" "-f%i""/>
<delegate decode="pdf" encode="ps" mode="bi" command=""gs" -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 "-sDEVICE=pswrite" "-sOutputFile=%o" "-f%i""/>
<delegate decode="tiff" encode="launch" mode="encode" command=""gimp" "%i""/>
<delegate decode="pnm" encode="ilbm" mode="encode" command=""ppmtoilbm" -24if "%i" > "%o""/>
<delegate decode="pov" command=""povray" "+i%i" -D0 +o"%o" +fn%q +w%w +h%h +a -q9 -kfi"%s" -kff"%n"\n"convert" -concatenate "%o*.png" "%o""/>
<delegate decode="ps" encode="eps" mode="bi" command=""gs" -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 "-sDEVICE=epswrite" "-sOutputFile=%o" "-f%i""/>
<delegate decode="ps" encode="pdf" mode="bi" command=""gs" -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 "-sDEVICE=pdfwrite" "-sOutputFile=%o" "-f%i""/>
<delegate decode="ps" encode="print" mode="encode" command="lpr "%i""/>
<delegate decode="ps:alpha" stealth="True" command=""gs" -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 "-sDEVICE=pngalpha" -dTextAlphaBits=%u -dGraphicsAlphaBits=%u "-r%s" %s "-sOutputFile=%s" "-f%s" "-f%s""/>
<delegate decode="ps:cmyk" stealth="True" command=""gs" -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 "-sDEVICE=pam" -dTextAlphaBits=%u -dGraphicsAlphaBits=%u "-r%s" %s "-sOutputFile=%s" "-f%s" "-f%s""/>
<delegate decode="ps:color" stealth="True" command=""gs" -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 "-sDEVICE=pnmraw" -dTextAlphaBits=%u -dGraphicsAlphaBits=%u "-r%s" %s "-sOutputFile=%s" "-f%s" "-f%s""/>
<delegate decode="ps:mono" stealth="True" command=""gs" -q -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 "-sDEVICE=pbmraw" -dTextAlphaBits=%u -dGraphicsAlphaBits=%u "-r%s" %s "-sOutputFile=%s" "-f%s" "-f%s""/>
<delegate decode="rgba" encode="rle" mode="encode" command=""rawtorle" -o "%o" -v "%i""/>
<delegate decode="scan" command=""scanimage" -d "%i" > "%o""/>
<delegate decode="scanx" command=""scanimage" > "%o""/>
<delegate decode="miff" encode="show" spawn="True" command=""/usr/local/bin/display" -delay 0 -window-group %[group] -title "%l " "ephemeral:%i""/>
<delegate decode="shtml" command=""html2ps" -U -o "%o" "%i""/>
<delegate decode="svg" command=""rsvg" "%i" "%o""/>
<delegate decode="txt" encode="ps" mode="bi" command=""enscript" -o "%o" "%i""/>
<delegate decode="miff" encode="win" stealth="True" spawn="True" command=""/usr/local/bin/display" -immutable -delay 0 -window-group %[group] -title "%l " "ephemeral:%i""/>
<delegate decode="wmf" command=""wmf2eps" -o "%o" "%i""/>
<delegate decode="xps:color" stealth="True" command=""gxps" -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 "-sDEVICE=ppmraw" -dTextAlphaBits=%u -dGraphicsAlphaBits=%u "-r%s" %s "-sOutputFile=%s" "%s""/>
<delegate decode="xps:cmyk" stealth="True" command=""gxps" -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 "-sDEVICE=bmpsep8" -dTextAlphaBits=%u -dGraphicsAlphaBits=%u "-r%s" %s "-sOutputFile=%s" "%s""/>
<delegate decode="xps:mono" stealth="True" command=""gxps" -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 "-sDEVICE=pbmraw" -dTextAlphaBits=%u -dGraphicsAlphaBits=%u "-r%s" %s "-sOutputFile=%s" "%s""/>
</delegatemap>

Re: convert works from command line but not php shell PDF

Posted: 2016-04-14T13:51:58-07:00
by fmw42
try editing it where I marked the blue gs in my previous post.

Re: convert works from command line but not php shell PDF

Posted: 2016-04-14T14:14:12-07:00
by Jeremy
Yeah the only thing that did was break my command line version.

Re: convert works from command line but not php shell PDF

Posted: 2016-04-14T14:19:14-07:00
by fmw42
Did you answer my earlier questions:
Find were Ghostscript resides?

find / | grep "gs"

see if it is at /usr/local/bin/gs or /usr/bin/gs or somewhere else.

Then in a terminal

echo $path

Then do the same via PHP exec().

Is gs in the $PATH for PHP?

What are the above two PATHs?

Your PHP environment probably needs to know where ghostscript resides. But I do not know enough about PHP to tell you where or how.

Did you install the Ghostscript fonts also? Did you install Ghostscript at the same place as where Imagick gets installed? Perhaps you need a copy there (along with the GS fonts).

Have you searched this forum for similar problems? It seems to me that it has come up before.

see for example:
http://stackoverflow.com/questions/2759 ... -pdf-input
http://php.net/manual/en/imagick.construct.php
https://github.com/delphian/drupal-conv ... P-and-MAMP

Sorry, after the above, if it still does not work, then I am out of ideas.

Re: convert works from command line but not php shell PDF

Posted: 2016-04-15T07:02:34-07:00
by Jeremy
Thanks for the research, I believe I've read those but I'll take another look.

I do think it knows where GS is because I can go from something to a PDF via exec.

I.E.

exec('convert test.jpg test.pdf');

Re: convert works from command line but not php shell PDF

Posted: 2016-04-15T07:11:44-07:00
by snibgo
Jeremy wrote:I do think it knows where GS is because I can go from something to a PDF via exec.
This doesn't prove GS is working.

GS is used only to read PDF. It isn't used to write PDF.

[EDIT for clarity.]

Re: convert works from command line but not php shell PDF

Posted: 2016-04-15T08:56:03-07:00
by fmw42
snibgo wrote:GS is used only to read PDF. It isn't used to write PDF.
Snibgo is correct. But to be clear, the original discussion was about conversion from pdf to png, so that would depend upon Ghostscript.

I still believe you need to get the path to ghostscript into your PHP environment PATH and you have not answered my questions about that twice now.