Page 1 of 2

Commandline Working. PHP Exec not.

Posted: 2010-11-15T09:47:30-07:00
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!

Re: Commandline Working. PHP Exec not.

Posted: 2010-11-15T09:50:58-07:00
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

Re: Commandline Working. PHP Exec not.

Posted: 2010-11-15T10:12:05-07:00
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"

Re: Commandline Working. PHP Exec not.

Posted: 2010-11-15T10:44:56-07:00
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.

Re: Commandline Working. PHP Exec not.

Posted: 2010-11-15T10:58:33-07:00
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">

Re: Commandline Working. PHP Exec not.

Posted: 2010-11-15T11:04:38-07:00
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

Re: Commandline Working. PHP Exec not.

Posted: 2010-11-15T11:15:45-07:00
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

Re: Commandline Working. PHP Exec not.

Posted: 2010-11-15T11:23:20-07:00
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 ?

Re: Commandline Working. PHP Exec not.

Posted: 2010-11-15T11:26:51-07:00
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 );

Re: Commandline Working. PHP Exec not.

Posted: 2010-11-15T11:28:06-07:00
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!

Re: Commandline Working. PHP Exec not.

Posted: 2010-11-15T11:29:46-07:00
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-- )

Re: Commandline Working. PHP Exec not.

Posted: 2010-11-15T11:34:28-07:00
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.

Re: Commandline Working. PHP Exec not.

Posted: 2010-11-15T11:38:33-07:00
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

Re: Commandline Working. PHP Exec not.

Posted: 2010-11-15T11:42:15-07:00
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.

Re: Commandline Working. PHP Exec not.

Posted: 2010-11-15T11:43:06-07:00
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.