<?php

    
# Check for non-empty "text" parameter
    #    
    
$text = $_GET['text'];
    
$huerotation = $_GET['huerotation'];
    
$saturation = $_GET['saturation'];
    
$text or die("no text parameter given");

    
# x and y are the coordinates of the pixel clicked on the image button
    # if the lower button is clicked the specified hue value is used
    # otherwise it's taken from the rainbow button.
    #
    
$y = $_GET['y'];
    if (
$y<24)     { $huerotation = ($_GET['x']-16)/144*360;}

    
$height       = 23;    # how tall is the button
    
$cap_width    = 15;    # how wide are the left and right rounded caps
    
$cap_overlap  = 5;     # how many pixels should the text intrude into each cap
    
$center_width = 20;    # how wide is the stretchable center part
    
    
$left_name   = "button_left.png";
    
$center_name = "button_center.png";
    
$right_name  = "button_right.png";

    
# What font face and size to use
    #
    
$ttfont = "/System/Library/Frameworks/JavaVM.framework/Versions/1.3.1/Home/lib/fonts/LucidaSansRegular.ttf";
    
$ttsize = 8;

    
# Calculate the bounding box of the given string with
    # the above font parameters
    #
    
$ttbbox = imagettfbbox($ttsize, 0, $ttfont, $text);

    
# array index 2 contains lower right X, 0 contains lower left X,
    # difference is our text width
    #
    
$ttwidth = $ttbbox[2] - $ttbbox[0];

    
# The image width is the text width plus the
    # space needed for the two caps on each side
    # minus the intrusion amount
    #
    
$img_width = $ttwidth + 2 * ($cap_width - $cap_overlap);
    
    
$img_out = ImageCreate($img_width, $height) or die("Unable to create new image");

    
# Load in the three images needed to create the button
    #
    
$img_button_left   = ImageCreateFromPNG($left_name) or die("Unable to open $left_name");
    
$img_button_center = ImageCreateFromPNG($center_name) or die("Unable to open $center_name");
    
$img_button_right  = ImageCreateFromPNG($right_name) or die("Unable to open $right_name");

    
# Fill the empty image canvas by tiling the center image
    #
    
for ($i =0; $i < $img_width / $center_width; $i++) {
        
ImageCopy($img_out, $img_button_center, $i * $center_width, 0, 0, 0, $center_width, $height);
    }
    
    
# Now add the left and right cap, this finishes the button
    #
    
ImageCopy($img_out, $img_button_left, 0, 0, 0, 0, $cap_width, $height);
    
ImageCopy($img_out, $img_button_right, $img_width - $cap_width, 0, 0, 0, $cap_width, $height);

    
# Rotate the color hue value by the parameter given
    #
    
for($i = 0; $i < imagecolorstotal($img_out); $i++) {

        
$colors = ImageColorsForIndex($img_out, $i);
        
$hsv = rgb2hsv($colors['red'], $colors['green'], $colors['blue']);
        
$hue = $hsv[0];
        
$sat = $saturation ? $saturation : $hsv[1];
        
$val = $hsv[2];

        
$hue += $huerotation;

        
$rgb = hsv2rgb($hue, $sat, $val);

        
$red   = $rgb[0];
        
$green = $rgb[1];
        
$blue  = $rgb[2];

        
ImageColorSet($img_out, $i, $red, $green, $blue);

    }


    
# Define text color black and render the text onto the button image
    #
    
$text_color = ImageColorAllocate($img_out, 0, 0, 0);
    
ImageTTFText($img_out, $ttsize, 0, $cap_width - $cap_overlap, 15, $text_color, $ttfont, $text);

    
# That's it, output the result
    #
    
header("Content-Type: image/png");
    
ImagePng($img_out);
    
?>

<?php

    
function hsv2rgb($h, $s, $v) {
    
        
# Adapted from C source code found here:
        # http://www.cs.rit.edu/~ncs/color/t_convert.html
    
        
if ($s == 0) {        # achromatic
            
$r = $g = $b = $v;
            return array(
$r * 255, $g * 255, $b * 255);
        }

        
$h %= 360;

        
$h /= 60;
        
$i = floor($h);
        
$f = $h - $i;
        
$p = $v * (1 - $s);
        
$q = $v * (1 - $s * $f);
        
$t = $v * (1 - $s * (1 - $f));
    
        switch(
$i) {
            case
0:
                
$r = $v;
                
$g = $t;
                
$b = $p;
                break;
            case
1:
                
$r = $q;
                
$g = $v;
                
$b = $p;
                break;
            case
2:
                
$r = $p;
                
$g = $v;
                
$b = $t;
                break;
            case
3:
                
$r = $p;
                
$g = $q;
                
$b = $v;
                break;
            case
4:
                
$r = $t;
                
$g = $p;
                
$b = $v;
                break;
            default:        
# and case 5:
                
$r = $v;
                
$g = $p;
                
$b = $q;
                break;
        }
    
        return array(
$r * 255, $g * 255, $b * 255);
    
    
    }
    
    
    function
rgb2hsv($red, $green, $blue) {
    
        
# Adapted from C source code found here:
        # http://www.cs.rit.edu/~ncs/color/t_convert.html
    
    
        # Need values from 0 - 1 instead of 0 - 255
        #
        
$r = $red   / 255;
        
$g = $green / 255;
        
$b = $blue  / 255;
    
        
$min = min($r, $g, $b);    
        
$max = max($r, $g, $b);    
    
        
$v = $max;
    
        
$delta = $max - $min;
    
        if (
$max != 0) {
            
$s = $delta / $max;
        } else {
            
$s = 0;
            
$h = -1;
            return array(
$h, $s, $v);
        }
    
    
        if (
$delta) {
    
            if (
$r == $max) {
                
$h = ($g - $b) / $delta;
            } else if (
$g == $max) {
                
$h = 2 + ($b - $r) / $delta;
            } else {
                
$h = 4 + ($r - $g) / $delta;
            }
    
        } else {
            
$h = 0;
        }
    
        
$h *= 60;
    
        if (
$h < 0) {
            
$h += 360;
        }
    
        return array(
$h, $s, $v);
    
    }

?>