php exec() returns "Invalid Parameter - 400"

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?".
Post Reply
frumpus

php exec() returns "Invalid Parameter - 400"

Post 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.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: php exec() returns "Invalid Parameter - 400"

Post 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.
snibgo's IM pages: im.snibgo.com
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: php exec() returns "Invalid Parameter - 400"

Post 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\" ";
frumpus

Re: php exec() returns "Invalid Parameter - 400"

Post 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.
frumpus

Re: php exec() returns "Invalid Parameter - 400"

Post 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!
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: php exec() returns "Invalid Parameter - 400"

Post 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 !
wjykk

Re: php exec() returns "Invalid Parameter - 400"

Post 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'.
Last edited by wjykk on 2010-06-15T21:56:02-07:00, edited 1 time in total.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: php exec() returns "Invalid Parameter - 400"

Post 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.
Last edited by fmw42 on 2010-06-15T21:57:20-07:00, edited 1 time in total.
wjykk

Re: php exec() returns "Invalid Parameter - 400"

Post by wjykk »

Never mind, PATH just doesn't work outside the server. See edited post.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: php exec() returns "Invalid Parameter - 400"

Post 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.
Post Reply