Page 1 of 2

IM With PHP exec ()

Posted: 2007-11-18T03:14:55-07:00
by ngkong
i have been crazy for a day now...

via command prompt, works fine OK

Code: Select all

/usr/local/bin/convert /home/xxxxx/public_html/pdf2img/images/pdf/temp/abcde.pdf[0] -colorspace RGB -geometry 200 /home/xxxxx/public_html/pdf2img/images/pdf/thumb/abcde.jpg
then i create a php file, test.php

Code: Select all

<?php
system("/usr/local/bin/convert /home/xxxxx/public_html/pdf2img/images/pdf/temp/abcde.pdf[0] -colorspace RGB -geometry 200 /home/xxxxx/public_html/pdf2img/images/pdf/thumb/abcde.jpg");
?>

it works fine when i execute it via command prompt:

Code: Select all

php test.php
but when i execute it via browser, nothing happen. i have tried 0777 the folders, chmod 0777 abcde.pdf, chgrp apache folders & file, chown apache folders & file

no luck

but the version reporting works fine

Code: Select all

<?php system("/usr/local/bin/convert --version"); ?>
thanks...

Re: IM With PHP exec ()

Posted: 2007-11-18T07:32:28-07:00
by Bonzo
This should fail as you have --version rather than -version
<?php system("/usr/local/bin/convert --version"); ?>
Where did you get your path from ? Confirm it is correct with:

Code: Select all

<?php
echo "<pre>";
system("type convert"); 
echo "</pre>";
?> 
I use relative paths as they are easer than absoulte. Try something simple first to confirm the install is OK.
Put your pdf in the same folder as the code and run ( changing convert to your path ):

Code: Select all

<?php
$array=array();
echo "<pre>";
exec("convert abcde.pdf[0] abcde.jpg 2>&1", $array); 
echo "<br>".print_r($array)."<br>"; 
echo "</pre>";
?> 
This should also display any errors.

Re: IM With PHP exec ()

Posted: 2007-11-18T09:07:10-07:00
by ngkong
convert is here: /usr/local/bin/convert

Code: Select all

<?php
$array=array();
echo "<pre>";
exec("/usr/local/bin/convert abcde.pdf[0] abcde.jpg 2>&1", $array);
echo "<br>".print_r($array)."<br>";
echo "</pre>";
?>
it produce:
Array
(
[0] => convert: missing an image filename `abcde.jpg'.
)

1
what does it mean?

touch abcde.jpg && chmod abcde.jpg

still no success...

thanks very much...

Re: IM With PHP exec ()

Posted: 2007-11-18T09:26:35-07:00
by Bonzo
The error could be lots of things as it is not that detailed.

Do any other ImageMagic comands work with php e.g convert a jpg to a png ?

You need ghostscript installed to read pdf files. I wonder if there is a problem there, although you said it worked OK from the command line.

Otherwise I can not think of why it is not working.

Re: IM With PHP exec ()

Posted: 2007-11-18T09:40:29-07:00
by el_supremo
I think that the problem is with the [0]
This tells IM to get the first page of the PDF.
BUT to PHP it is the index of an array and that is what PHP is complaining about.
You need to "escape" the brackets so that PHP doesn't interpret them.
From a previous post (viewtopic.php?f=1&t=9929) I think that quoting the filename will work:

exec("/usr/local/bin/convert 'abcde.pdf[0]' abcde.jpg 2>&1", $array);

Pete

Re: IM With PHP exec ()

Posted: 2007-11-19T09:40:38-07:00
by ngkong
Bonzo wrote: You need ghostscript installed to read pdf files. I wonder if there is a problem there, although you said it worked OK from the command line.
ghostscript is installed, i have recently converted hundreds of pdf files to jpg by php, called it via command prompt: php file.php
el_supremo wrote: exec("/usr/local/bin/convert 'abcde.pdf[0]' abcde.jpg 2>&1", $array);
Pete
quote doesnt change anything :(

here abc.php:

Code: Select all

<?php
system("/usr/local/bin/convert 'abcde.pdf[0]' 'abcde.jpg' 2>&1");
?>
execute via command prompt, works perfect:
# php abc.php

**** Warning: Fonts with Subtype = /TrueType should be embedded.
The following fonts were not embedded:
TimesNewRomanPS-BoldMT
TimesNewRomanPSMT

**** This file had errors that were repaired or ignored.
**** The file was produced by:
**** >>>> Acrobat Distiller 6.0 (Windows) <<<<
**** Please notify the author of the software that produced this
**** file that it does not conform to Adobe's published PDF
**** specification.
call it via browser the same abc.php, no jpg produce, report:
sh: gs: command not found convert: Postscript delegate failed `abcde.pdf': No such file or directory. convert: missing an image filename `abcde.jpg'.
/usr/local/bin/gs is executable (0755)
active directory is writable (0777)

info:
Centos 5
Apache 2.2.3
PHP 5.1.6
Ghostscript 8.60
ImageMagick 6.3.7
Bonzo wrote:This should fail as you have --version rather than -version
it's works! really...
# convert --version
Version: ImageMagick 6.3.7 11/17/07 Q16 http://www.imagemagick.org
Copyright: Copyright (C) 1999-2007 ImageMagick Studio LLC
thanks you all...

Re: IM With PHP exec ()

Posted: 2007-11-19T10:12:55-07:00
by ngkong
well, as i can see it must be caused by the system doesnt know the location of gs command location while the command is executed by other user then root.

so how to configure the path of commands for a user in linux?

im a linux n00b and i have been googling and found nothing...

thanks

Re: IM With PHP exec ()

Posted: 2007-11-19T21:41:38-07:00
by anthony
set PATH environment variable in the PHP. or tell your administartors that the PHP command path is not set right for IM and GS

If the web server has the right path. PHP usally inherits it and then it can find things properly!

Re: IM With PHP exec ()

Posted: 2007-11-20T07:47:43-07:00
by ngkong
finally, problem solved!

<?php
system ("PATH=\$PATH:/usr/local/bin/");
system ("convert 'abcde.pdf[0]' 'abcde.jpg' 2>&1");
?>

i cant fine a way to set the path permanently. so i think setting the path each time the php file is executed is ok, because variable will be reseted each time the php file execution done...

thanks all you guys...

btw what '2>&1' means? why adding this command will cause echoing the report?

Re: IM With PHP exec ()

Posted: 2007-11-20T17:32:14-07:00
by anthony
Actually I am supprised that worked!!! The two system calls in a normal API would run two separate shells and as such the environment change in first sub-shell in not in the second. Usally the API itself provides methods to set environment variables within the API itself and not just in the sub-shell. That way the environment will be inherited from the parent API to the child sub-shell run commands.

I suggest you refer to a good PHP tutoral book on correct handling of PATH and other environment variables within PHP.

Re: IM With PHP exec ()

Posted: 2007-11-22T13:41:27-07:00
by ngkong
anthony wrote:Actually I am supprised that worked!!! The two system calls in a normal API would run two separate shells and as such the environment change in first sub-shell in not in the second. Usally the API itself provides methods to set environment variables within the API itself and not just in the sub-shell. That way the environment will be inherited from the parent API to the child sub-shell run commands.
you are absolutely right! sorry my fault.

i'm actually using:

Code: Select all

system("PATH=\$PATH:/usr/local/bin/ && convert 'abcde.pdf[0]' 'abcde.jpg' 2>&1");
but posted as

Code: Select all

<?php
system ("PATH=\$PATH:/usr/local/bin/");
system ("convert 'abcde.pdf[0]' 'abcde.jpg' 2>&1");
?>
i test those codes, not work...

Re: IM With PHP exec ()

Posted: 2007-11-22T17:06:21-07:00
by anthony
It is probably better to find out how a PHP program can set its command execution PATH environment variable properly. That way programs such as convert and gs do not need to have it hard coded thoughtout the program.

You can fix the path in one place, at the top, and then just use the programs directly.

Perhaps bonzo, the forums PHP IM example expert can figure it out and put it up on his Rubbleweb site http://www.rubblewebs.co.uk/imagemagick/

Re: IM With PHP exec ()

Posted: 2007-11-23T06:05:51-07:00
by Bonzo
Not quite sure what you want Anthony.

This will list all the enviromental variables:

Code: Select all

<?php
echo"<pre>";
echo $HTTP_ENV_VARS['PATH'];
echo"</pre>";
?>
My path is listed as: [PATH] => /bin:/usr/bin

To just put the /usr/bin into a variable and use that with the convert:

Code: Select all

<?php
$env_vars = explode( ':', $HTTP_ENV_VARS['PATH']);
$path = $env_vars[1]."/convert";
?>
This sets the $path variable to: /usr/bin/convert

This can be used as ?:

Code: Select all

<?php
$env_vars = explode( ':', $HTTP_ENV_VARS['PATH']);
$env_path = $env_vars[1]."/convert";

system ("PATH=$PATH:$env_path");
?>
Or would you us it like:

Code: Select all

<?php
$env_vars = explode( ':', $HTTP_ENV_VARS['PATH']);
$env_path = $env_vars[1]."/convert";

exec("$env_path image.jpg -resize 100x100 output.jpg");
?>
But where would GS get its path?

Re: IM With PHP exec ()

Posted: 2007-11-23T07:22:46-07:00
by ngkong
i think the simplest way to solve gs path problem is create file links for gs (and convert) execution files in the php execution PATH directory...
have not tried, but it should work..

Re: IM With PHP exec ()

Posted: 2007-11-24T05:38:02-07:00
by anthony
What I mean is that their must be some way to set the PATH environment variable within the current PHP script, so that all system and exec calls will use that environment variable, It is after all how environment variables are suposed to be used. Setting or modifying the current (PHP script) environment!!!

I am not a PHP programmer, but I am sure it is quite posible. I figured you would know.