Button Maker

July 23rd, 2007 Comments Off

There haven’t been any updates to this site for a while, but this little tool of mine probably warrants an entry.

Ever wanted to make one of these small web buttons that appear on web pages and promote other links (like the ones on the bottom of this site’s sidebar)? Well, after some googling about you might stumble upon a generator by Adam Kalsey. Since I couldn’t find any source code for it, I wondered if it’s a lot of effort to make one of my own.

Within two evenings, I did and the end result is available here (also on the navigation menu just below the site logo). It only supports basic textual buttons (or, as some call them, stickers or badges). You can however adjust the text, colors, borders and the like. It utilizes the free silkscreen font, so basically you may use the buttons generated by it for any purpose.

The generator’s source code follows (a simple GET query interpreter):

<?php
header('Content-type: image/png');
 
function get_color($img, $color) {
    if ($color[0] == '#')
        $color = substr($color,1,6);
    if (strlen($color) == 3)
        $color = $color[0].$color[0].$color[1].$color[1].$color[2].$color[2];
    $R = hexdec(substr($color,0,2));
    $G = hexdec(substr($color,2,2));
    $B = hexdec(substr($color,4,2));
    return imagecolorallocate($img,$R,$G,$B);
}
 
$gWidth = 80;
$gHeight = 15;
 
$btn = imagecreate($gWidth,$gHeight);
 
$outerBorder = get_color($btn, $_GET['outerBorder']);
imagefilledrectangle($btn, 0, 0, $gWidth, $gHeight-1, $outerBorder);
 
$innerBorder = get_color($btn, $_GET['innerBorder']);
imagefilledrectangle($btn, 1, 1, $gWidth-2, $gHeight-2, $innerBorder);
 
$leftBg = get_color($btn, $_GET['leftBg']);
imagefilledrectangle($btn, 2, 2, $_GET['leftWidth'], $gHeight-3, $leftBg);
 
$leftColor = get_color($btn, $_GET['leftColor']);
imagettftext($btn, 6, 0, 1+$_GET['leftOffset'], 10, $leftColor, '/button/slkscr.ttf', $_GET['leftText']);
 
$rightBg = get_color($btn, $_GET['rightBg']);
imagefilledrectangle($btn, 2+$_GET['leftWidth'], 2, $gWidth-3, $gHeight-3, $rightBg);
 
$rightColor = get_color($btn, $_GET['rightColor']);
imagettftext($btn, 6, 0, 1+$_GET['leftWidth']+$_GET['rightOffset'], 10, $rightColor, '/button/slkscr.ttf', $_GET['rightText']);
 
imagepng($btn);
?>

Have fun! =)

Comments are closed.

Switch to our mobile site