Creating complex buttons with MagickWand

MagickWand for PHP is an object-oriented PHP interface to ImageMagick. Use this forum to discuss, make suggestions about, or report bugs concerning MagickWand for PHP.
Post Reply
hknight
Posts: 32
Joined: 2007-08-02T10:16:15-07:00

Creating complex buttons with MagickWand

Post by hknight »

Hello,

How can I do this with MagickWand? It requires multiple commands.

Thanks!

Code: Select all

<?

global $imPath;
$imPath = "/usr/local/bin/convert";

make_button_3D ("Hello World", 11, '#111111', '#C27A08', '#FE9800', '35', '#eeeeee', 5, 0, 'button_3d');
echo '<p><img src="button_3d.gif"></p>';


function make_button_3D ($text, $fontSize, $fontColor, $buttonColor1, $buttonColor2, $angle, $bgColor, $curve, $antialias, $name)
{
// Usage
// $text: The word placed inside of the button
// $fontSize: The size of the text inside of the button.  11 is recommended.
// $fontColor: The color of the text inside of the button. Must be a hexadecimal code such as #123abc
// $buttonColor1: The first color of the button. Must be a hexadecimal code such as #123abc
// $buttonColor2: The second color of the button. Must be a hexadecimal code such as #123abc
// $angle: Rotate from one color to the next at this angle.  Between 1 and 359.
// $bgColor: The color of the background behind the button. Must be a hexadecimal code such as #123abc
// $curve: defines the curve for the rounded corners.  0 is a square, 10 is very round.
// $antialias: Set to 1 if you want the text to be antialiased. Otherwise set to 0. 
// $name: name of the gif file to be created. Do not include the extension.

global $imPath;
$prefix = 'tmp_' . substr(time(), -4,4) . rand(100,999);
$font = 'font.ttf';
$curve = inRange($curve, 0, 9);
$fontSize = inRange($fontSize, 5, 20);
$fontColor = valid_color($fontColor);
$buttonColor1 = valid_color($buttonColor1);
$buttonColor2 = valid_color($buttonColor2);
$angle = inRange($angle, 1, 359);
$bgColor = valid_color($bgColor);
if (!$antialias===1) $a = '+antialias'; else $a = '';
$name = substr(preg_replace('/\W/','', $name),0,22).'.gif';

exec  ($imPath ." -background transparent -fill transparent -font " . escapeshellarg($font) . " -pointsize " . escapeshellarg($fontSize + 2) . " label:".escapeshellarg($text)." -border 7 +repage -format '%wx%w' -write info:" . escapeshellarg($prefix . "size1.txt") . " -format '%wx%h'  -write info:" . escapeshellarg($prefix . "size.txt") . " info: > " . escapeshellarg($prefix . "grad.mvg"  ). ' 2>&1' , $error );
exec  ($imPath ." -background transparent -fill transparent -font " . escapeshellarg($font) . " -pointsize " . escapeshellarg($fontSize + 2) . " label:".escapeshellarg($text)." -format 'viewbox 0 0 %[fx:w+7] %[fx:h+7] \\nfill white roundRectangle 1,1 %[fx:w+5] %[fx:h+5] \\n" . $curve ."," . $curve . "' info: > " . escapeshellarg($prefix . "corner.mvg")  . ' 2>&1' , $error );
exec  ($imPath ." -background black " . $a . " " . $prefix. "corner.mvg " . $prefix ."mask.png"  . ' 2>&1' , $error );
exec  ($imPath ." -size `cat ". escapeshellarg($prefix ."size1.txt") . "` gradient:" . escapeshellarg($buttonColor1) ."-". escapeshellarg($buttonColor2) ." -rotate -". escapeshellarg($angle) ." -gravity center -crop `cat " . escapeshellarg($prefix . "size.txt") ."`+0+0 +repage " . escapeshellarg($prefix . "mask.png"). " +matte  -compose CopyOpacity -composite -pointsize ". escapeshellarg($fontSize) ." -font ". escapeshellarg($font) ." -fill black +antialias -gravity Center -annotate 0 ". escapeshellarg($text) ." " . escapeshellarg($prefix."flat.png")  .  " 2>&1", $error );
exec  ($imPath ." " . escapeshellarg($prefix."flat.png") . " -fx A +matte -blur 0x3  -shade 110x30 -normalize ". escapeshellarg($prefix."flat.png") ." -compose Overlay -composite ". escapeshellarg($prefix."flat.png") ." -matte  -compose Dst_In  -composite -trim +repage ". escapeshellarg($name)  .  " 2>&1", $error );
exec  ("rm -f $prefix*" . ' 2>&1' , $error );
}



function inRange ($number, $minVal, $maxVal){
      $number = (int) $number;
      if($number > $maxVal){ 
            $number = $maxVal;
      }elseif($number < $minVal){ 
            $number = $minVal;
      }
      return $number;
}

function valid_color($color){
      if(preg_match("/^#[0-9abcdef]{6}/i",$color,$match)){
            return $color;
      
      } else{
      if(preg_match("/^[0-9abcdef]{6}/i",$color,$match)){
            return '#'.$color;

      } else{
            return '#aaaaaa';
      }
  }
}

?>
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Creating complex buttons with MagickWand

Post by anthony »

You should be able to do it in one command. Otherwise you will need to use some temporary image files (make sure you clean them up, especially on error).
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply