PHP produces emty mvg file

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
hknight
Posts: 32
Joined: 2007-08-02T10:16:15-07:00

PHP produces emty mvg file

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

Re: PHP produces emty mvg file

Post 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 !
hknight
Posts: 32
Joined: 2007-08-02T10:16:15-07:00

Re: PHP produces emty mvg file

Post 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.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: PHP produces emty mvg file

Post 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.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
hknight
Posts: 32
Joined: 2007-08-02T10:16:15-07:00

Re: PHP produces emty mvg file

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