Commandline Working. PHP Exec not.

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
JRozee

Commandline Working. PHP Exec not.

Post by JRozee »

In relation to this users post: http://studio.imagemagick.org/discourse ... f=1&t=8898

I can happily convert a PDF to PNG/JPEG via the commandline but when it comes to doing this via PHP I dont get any success.

I'm lost for what to do now, Google hasnt given me much help apart from unresolved posts to various forums.

Thanks in advance!
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Commandline Working. PHP Exec not.

Post by Bonzo »

What comand are you using?

You may need to give the full path to Imagemagick - run this and use the output in your code:

Code: Select all

<?php 
echo "<pre>"; 
system("type convert");  
echo "</pre>"; 
?> 
You should get something like; convert is /usr/local/bin/convert
JRozee

Re: Commandline Working. PHP Exec not.

Post by JRozee »

Bonzo wrote:What comand are you using?

You may need to give the full path to Imagemagick - run this and use the output in your code:

Code: Select all

<?php 
echo "<pre>"; 
system("type convert");  
echo "</pre>"; 
?> 
You should get something like; convert is /usr/local/bin/convert

Okay, I ran the code you suggested and I got no response.

This is what im currently running inbid to get the PDF converted:

Code: Select all

    exec( "convert -quality 80 \"{$fileName}.pdf\" \"{$fileName}.png\"", $out, $ret );
    // or convert -quality 80 "../pdfs/pdf-b9b4a0174c7c01.pdf" "../pdfs/pdf-b9b4a0174c7c01.png"
    echo $ret; print_r( $out );
I run "type convert" into SSH, I get:

Code: Select all

convert is hashed (/usr/local/bin/convert)
The line im running which works on Commandline is

Code: Select all

convert "../pdfs/pdf-b9b4a0174c7c01.pdf" "../pdfs/pdf-b9b4a0174c7c01.png"
This is run within the PUBLIC_HTML directory like this:

Code: Select all

root@atlantic [/home/whosout/public_html]# convert "../pdfs/pdf-b9b4a0174c7c01.pdf" "../pdfs/pdf-b9b4a0174c7c01.png"
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Commandline Working. PHP Exec not.

Post by Bonzo »

You may have a path problem; try I would try something simple first to prove IM is working

Code: Select all

<?php
  exec( "/usr/local/bin/convert rose: -resize 200x200 output.jpg );
?>
<img src="output.jpg"
NOTE: rose: is an inbuilt imagemagick image so you do not need to worry about the path etc.
JRozee

Re: Commandline Working. PHP Exec not.

Post by JRozee »

Bonzo wrote:You may have a path problem; try I would try something simple first to prove IM is working

Code: Select all

<?php
  exec( "/usr/local/bin/convert rose: -resize 200x200 output.jpg );
?>
<img src="output.jpg"
NOTE: rose: is an inbuilt imagemagick image so you do not need to worry about the path etc.
http://uploadsafe.com/~whosout/_css/playground.php

playground.php

Code: Select all

<?php
  exec( "/usr/local/bin/convert rose: -resize 200x200 output.jpg" );
?>
<img src="output.jpg">
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Commandline Working. PHP Exec not.

Post by Bonzo »

I would contact your hosts and get them to check the setup; IM can work from the command line and not from php if the paths are incorrect.

I assume that you have CHMOD the folder you are running the code in to either 755 or 777
JRozee

Re: Commandline Working. PHP Exec not.

Post by JRozee »

Bonzo wrote:I would contact your hosts and get them to check the setup; IM can work from the command line and not from php if the paths are incorrect.

I assume that you have CHMOD the folder you are running the code in to either 755 or 777
To your first question, I installed both ImageMagick and Ghostscript. Its not a managed server, so I would be the guy who is "supposed" to know whats wrong!

It was 755, Ive now changed it to 777 and it works.

However its not resizing the image to 200x200
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Commandline Working. PHP Exec not.

Post by Bonzo »

To your first question, I installed both ImageMagick and Ghostscript. Its not a managed server, so I would be the guy who is "supposed" to know whats wrong!
Woops

I has resized it but it keeps the aspect ratio so the logest side is 200.

What happens to your original code now ?
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Commandline Working. PHP Exec not.

Post by Bonzo »

Some coments on your code:

1/ Read the image in first unless yoy want to set the density
2/ Th -quality wants to go just before the save filename
3/ I do not think you need the \" or { in this case.

I would try:

Code: Select all

exec( "convert  -density 400 $fileName.pdf -quality 80 $fileName.png", $out, $ret );
JRozee

Re: Commandline Working. PHP Exec not.

Post by JRozee »

Bonzo wrote:
To your first question, I installed both ImageMagick and Ghostscript. Its not a managed server, so I would be the guy who is "supposed" to know whats wrong!
Woops

I has resized it but it keeps the aspect ratio so the logest side is 200.

What happens to your original code now ?
<?php
exec( '/usr/local/bin/convert testpdf.pdf testing.jpg' );
?>

Does not work, nor does it work with quotes around the filenames. Its crazy yet still works in command!
JRozee

Re: Commandline Working. PHP Exec not.

Post by JRozee »

Bonzo wrote:Some coments on your code:

1/ Read the image in first unless yoy want to set the density
2/ Th -quality wants to go just before the save filename
3/ I do not think you need the \" or { in this case.

I would try:

Code: Select all

exec( "convert  -density 400 $fileName.pdf -quality 80 $fileName.png", $out, $ret );

Doesnt work :(

Also:

Code: Select all

<?php
exec( "/usr/local/bin/convert /home/whosout/public_html/_css/testpdf.pdf /home/whosout/public_html/_css/output.png", $output );

print_r( $output );
?>
Response:
Array ( [0] => CRIT: rangecheck in .setuserparams [1] => Operand stack: [2] => --nostringval-- --nostringval-- --nostringval-- )
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Commandline Working. PHP Exec not.

Post by Bonzo »

Try adding this error reporting - IM is not great at error reporting but hopefuly you should get something:

Code: Select all

<?php 
$array=array(); 
echo "<pre>"; 
exec("convert -density 400 $fileName.pdf -quality 80 $fileName.png 2>&1", $array);  
echo "<br>".print_r($array)."<br>";  
echo "</pre>"; 
?> 
I think you need 777 for a normal php setup but 755 will work for phpsusx or whatever it is.
JRozee

Re: Commandline Working. PHP Exec not.

Post by JRozee »

Code: Select all

Array
(
    [0] => CRIT: rangecheck in .setuserparams
    [1] => Operand stack:
    [2] =>     --nostringval--  --nostringval--  --nostringval--
    [3] => convert: Postscript delegate failed `testpdf.pdf':  @ error/pdf.c/ReadPDFImage/645.
    [4] => convert: missing an image filename `output.png' @ error/convert.c/ConvertImageCommand/2949.
)

1
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Commandline Working. PHP Exec not.

Post by Bonzo »

I am afraid we are now at the limit of my knowledge.
IM is working OK and the image can be saved and so it is looking like a ghostscript problem?

Hopefuly somebody else will be online later and can offer some advice.
Last edited by Bonzo on 2010-11-15T11:43:16-07:00, edited 1 time in total.
JRozee

Re: Commandline Working. PHP Exec not.

Post by JRozee »

I think, but my guess is that GhostScript doesn't have the privileges to execute but on SSH as im logged in as root, it does. I do not know how to get around that though

I've reinstalled GhostScript and IM both ways to see if that was the problem. Its currently installed first and IM afterwards.
Post Reply