Page 1 of 1

PHP produces emty mvg file

Posted: 2007-08-21T11:07:45-07:00
by hknight
Hello,

I would like to create a .mvg file use shell_exec() and ImageMagick.

My php code produces an empty .mvg file. However if I use print() then copy/paste it through a command line, the correct .mvg file is created!

I realize that this is more of a php question than an ImageMagick question but I thought this was the best place for it.

Thanks!

Code: Select all

<?php
function make_button_mvg      ($text, $fontSize, $fontColor, $bgcolor, $borderColor)

{
      print      ("convert -background transparent -fill transparent -pointsize " . escapeshellarg($fontSize + 2) . " label:" . escapeshellarg($text) . " -format " . escapeshellarg('viewbox 0 0 %[fx:w+7] %[fx:h+7] fill red roundRectangle 1,1 %[fx:w+5] %[fx:h+5] 5,5') . " info: > rounded_corner.mvg");
      shell_exec ("convert -background transparent -fill transparent -pointsize " . escapeshellarg($fontSize + 2) . " label:" . escapeshellarg($text) . " -format " . escapeshellarg('viewbox 0 0 %[fx:w+7] %[fx:h+7] fill red roundRectangle 1,1 %[fx:w+5] %[fx:h+5] 5,5') . " info: > rounded_corner.mvg");
}
?>

Re: PHP produces emty mvg file

Posted: 2007-08-21T13:00:36-07:00
by Bonzo
At a guess I would say most of your problem is escapeshellarg as that will be putting extra " around your values. I would say you do not need escapeshellarg here escapeshellarg('viewbox 0 0 %[fx:w+7] %[fx:h+7] fill red roundRectangle 1,1 %[fx:w+5] %[fx:h+5] 5,5') anyway as the values are generated within the code.
I would also validate text before passing it to the code as any ' or " inside the $text would probably mess it up.

I would not have ($fontSize + 2) inside the code as you may need to escape the ( like \( It just adds to confusion. Put $fontSize = $fontSize + 2; outside the code and just put $fontSize inside it.

Do you need to use shell_exec ? I just use exec.

Try something like this first to get the code to do what you want then add the validation in when the code is actualy working.

Code: Select all

$fontSize = $fontSize + 2;

exec ("convert -background transparent -fill transparent -pointsize $fontSize label:$text -format 'viewbox 0 0 %[fx:w+7] %[fx:h+7] fill red roundRectangle 1,1 %[fx:w+5] %[fx:h+5] 5,5' info: > rounded_corner.mvg");
You also need to be getting your size for w & h from somewhere; although I can not coment on this as I have not really used fx. I also have no idea what viewbox is !

Re: PHP produces emty mvg file

Posted: 2007-08-21T14:19:16-07:00
by hknight
Thanks!

But it does not fix the issue. Your code also creates a 0 size file.

Please try the code through the command line AND from PHP and you will see what I mean.

Re: PHP produces emty mvg file

Posted: 2007-08-22T21:34:56-07:00
by anthony
Check if the PHP has any error output. You could try adding 2>&1 in the
system command to redirect errors to either the web page generated or the file redirect (depending on its position relative to that file redirect).

Basically there may be an error you can not see. even something like 'command not found'. Remember PHP runs in a different environment to command line and may have a different PATH, LD_LIBRARY_PATH, and even directory, to that of the command line. This can cause problems.

Re: PHP produces emty mvg file

Posted: 2007-08-23T18:20:20-07:00
by hknight
Wow, Anthony, you did it again!

Adding 2>&1 pointed me quickly to the problems.

1. I had to include the FULL absolute path to convert (/usr/local/bin/convert)
2. I had to add a few extra slashes.

Seeing the errors let me fix them quickly instead of going blind.

Thanks!