Page 1 of 1
php exec() returns "Invalid Parameter - 400"
Posted: 2010-06-10T09:32:51-07:00
by frumpus
I have the following code with which I am trying to create a preview image from the first page of a pdf file. It returns an error: "Invalid Parameter - 400" when run from php's 'exec()' command. But if I copy the output of the 'echo' statement and paste it into a command window on the server it works just fine. Can anyone see the problem here?
Code: Select all
<?php
$filename = "some file.pdf";
$command = "convert -density 400 \"C:\\Inetpub\\vhosts\\mydomain.net\\httpdocs\\lit_request\\upload";
$command .= "\\" . $filename . "[0]\"";
$command .= " -resize 25% \"C:\\Inetpub\\vhosts\\mydomain.net\\httpdocs\\lit_request\\upload";
$filename = str_replace(".pdf",".png",$filename);
$command .= "\\$filename\"";
echo $command . "<br/>" ;
$result = exec($command);
echo $result;
?>
The output of the echo statement is:
Code: Select all
convert -density 400 "C:\Inetpub\vhosts\mydomain.net\httpdocs\lit_request\upload\some file.pdf[0]" -resize 25% "C:\Inetpub\vhosts\pbigordon.net\httpdocs\lit_request\upload\some file.png"
and again, i can paste that into the command line and it works just fine.
note: I have included a space in 'some file.pdf' here because most of the files i am working with have spaces in them and I want to be sure that isn't the problem.
edit: fwiw, the local web user has read/execute on the image magick folder and full control of the \upload directory.
Re: php exec() returns "Invalid Parameter - 400"
Posted: 2010-06-10T11:15:43-07:00
by snibgo
What is PHP error 400? Perhaps IM can't find Ghostscript under PHP. Within PHP, are other conversions (eg from PNG) okay? If so, check the PHP configurarion.
Re: php exec() returns "Invalid Parameter - 400"
Posted: 2010-06-10T11:28:50-07:00
by Bonzo
I pressume the php code is in the localhost folder - try putting the pdf file there as well
Your code is
very hard to read try something like this:
Code: Select all
<?php
$filename = "some file.pdf[0]";
$filename_new = str_replace(".pdf[0]",".png",$filename);
$command = "convert -density 400 $filename".
" -resize 25% ".
" $filename_new";
echo $command . "<br/>" ;
$result = exec($command);
echo $result;
?>
I do not think this line is correct ( you did this on two lines ):
$command = "convert -density 400 \"C:\\Inetpub\\vhosts\\mydomain.net\\httpdocs\\lit_request\\upload";
Should be:
$command = "convert -density 400 \"C:\\Inetpub\\vhosts\\mydomain.net\\httpdocs\\lit_request\\upload\" ";
Re: php exec() returns "Invalid Parameter - 400"
Posted: 2010-06-10T11:42:51-07:00
by frumpus
@Bonzo, yeah it is a bit hard on the eyes. I broke it into segments like that because it took a lot of tweaking to get the output right.
this line: $command = "convert -density 400 \"C:\\Inetpub\\vhosts\\mydomain.net\\httpdocs\\lit_request\\upload";
gets the trailing slash and filename appended to it by the command that follows it. The quote needs to come after the filename.
the php code is in lit_request, one level up from the upload directory. I was assuming exec() would require the full path but I'll try a relative path and see what happens. Still, now that my non-pdf conversion is working, I suspect my problem lies with ghost script.
Re: php exec() returns "Invalid Parameter - 400"
Posted: 2010-06-10T11:56:42-07:00
by frumpus
I granted the local web user permission on the ghost script install directory and added that directory to the 'path' environment variable and everything works fine now. Issue resolved.
Thanks!
Re: php exec() returns "Invalid Parameter - 400"
Posted: 2010-06-10T11:59:33-07:00
by Bonzo
Thats good - the problem is you assume the command prompt and php have the same settings but it has been proved in the past that they do not !
Re: php exec() returns "Invalid Parameter - 400"
Posted: 2010-06-15T21:45:34-07:00
by wjykk
I have a similar problem, and it appears to be returning the second parameter after the filename as invalid. E.g, this is my command:
Code: Select all
$init = "convert -background black -transparent white -font Calibri -pointsize 60 -distort Arc ".$randarc." label:".$randpos." ".$fout.".tmp.gif";
echo $init;
print exec($init);
It always returns something like the following:
Code: Select all
convert -background black -transparent white -font Calibri -pointsize 60 -distort Arc 9 label:83F13ED C:/Users/tmp/wjykk-captcha-1276663443-192.168.1.103.gif.tmp.gifInvalid Parameter - black
I honestly don't get it. It works fine if I copy it to CMD. Does it have to do with magic_quotes?
E: never mind, I just had to specify the full path for convert, so instead of convert I put 'C:/Program Files/ImageMagick-6.6.2-Q16/convert'.
Re: php exec() returns "Invalid Parameter - 400"
Posted: 2010-06-15T21:55:49-07:00
by fmw42
What is tmp.gif and what is $fout? Is tmp.gif an input or do you have no input?
If no input then -background black -transparent white has nothing to work on. You are then creating text with no input image. if you want a transparent background just use -background none.
IM processes mostly sequentially. The typical command syntax is
convert input options output
where input may be optional. see
http://www.imagemagick.org/Usage/basics/#cmdline
You are also issuing the arc command before you even have text drawn. You need to draw your text with the label: command before you distort it.
If possible, it is a good idea to try your command in command line mode in a terminal window, before porting to PHP exec.
PS It is also not generally a good idea to tack a new topic onto an old one. In the future, start a new topic, if you don't mind, unless directly related to the earlier topic.
Re: php exec() returns "Invalid Parameter - 400"
Posted: 2010-06-15T21:56:59-07:00
by wjykk
Never mind, PATH just doesn't work outside the server. See edited post.
Re: php exec() returns "Invalid Parameter - 400"
Posted: 2010-06-15T21:59:20-07:00
by fmw42
That is often a typical problem. But I recommend you read the page I referenced and use proper IM syntax to ensure you have the best chance of success. IM can be forgiving now, but in future releases it may not be so forgiving.