Dynamic buttons
Posted: Tue Aug 26, 2003 1:53 pm
Needed a way to make a link look like a form button so I came up a script/function that will make a button on the fly and center the text in it. Would be nice to be able to change the font. Maybe some one can show me how.
BP
The function:
The script:
Usage:
BP
The function:
Code: Select all
function print_button($text, $size, $background, $border, $foreground) {
preg_match("/(.*),(.*)/", $size, $match);
$width = $matchї'1'];
$height = $matchї'2'];
preg_match("/(.*),(.*),(.*)/", $background, $match);
$background_red = $matchї'1'];
$background_green = $matchї'2'];
$background_blue = $matchї'3'];
preg_match("/(.*),(.*),(.*)/", $border, $match);
$border_red = $matchї'1'];
$border_green = $matchї'2'];
$border_blue = $matchї'3'];
preg_match("/(.*),(.*),(.*)/", $foreground, $match);
$foreground_red = $matchї'1'];
$foreground_green = $matchї'2'];
$foreground_blue = $matchї'3'];
$font = "2";
$width = floor(ImageFontWidth($font) * strlen($text ) + 10);
$border_x2 = ($width - 2);
$border_y2 = ($height - 2);
Header("Content-type: image/jpeg");
$image = ImageCreate($width, $height);
$bg = ImageColorAllocate($image ,$background_red, $background_green, $background_blue);
$fg = ImageColorAllocate($image ,$foreground_red, $foreground_green, $foreground_blue);
$bc = ImageColorAllocate($image ,$border_red, $border_green, $border_blue);
ImageFilledRectangle($image ,0 , 0, $width, $height, $bc);
ImageFilledRectangle($image ,1 , 1, $border_x2, $border_y2, $bg);
$x = floor((ImageSX($image) / 2) - ((ImageFontWidth($font) * strlen($text)) / 2));
$y = floor((ImageSY($image) / 2) - (ImageFontHeight($font) / 2));
ImageString($image,$font ,$x ,$y ,$text, $fg);
ImageJPEG($image);
ImageDestroy($image);
}Code: Select all
<?php
include 'libroster.php'; <- where the function lives
if (empty($_GETї'size'])){
$size = $roster_configї'html_default_button_size'];
} else {
$size = $_GETї'size'];
}
if (empty($_GETї'background'])){
$background = $roster_configї'html_default_button_background'];
} else {
$background = $_GETї'background'];
}
if (empty($_GETї'border'])){
$border = $roster_configї'html_default_button_border'];
} else {
$border = $_GETї'border'];
}
if (empty($_GETї'foreground'])){
$foreground = $roster_configї'html_default_button_foreground'];
} else {
$foreground = $_GETї'foreground'];
}
print_button($_GETї'text'], $size, $background, $border, $foreground);
?>Code: Select all
echo "<a href="#" onclick="window.close()"><img src="$roster_configїrelative_path]/button.php?text=Close"></a>
";