Specifications

{
// We have found a font size that will fit
// Now work out where to put it
$text_x = $width_image/2.0 - $width_text/2.0;
$text_y = $height_image/2.0 - $height_text/2.0 ;
if ($left_text < 0)
$text_x += abs($left_text); // add factor for left overhang
$above_line_text = abs($bbox[7]); // how far above the baseline?
$text_y += $above_line_text; // add baseline factor
$text_y -= 2; // adjustment factor for shape of our template
$white = ImageColorAllocate ($im, 255, 255, 255);
ImageTTFText ($im, $font_size, 0, $text_x, $text_y, $white, “arial.ttf”,
$button_text);
Header (“Content-type: image/png”);
ImagePng ($im);
}
ImageDestroy ($im);
?>
This is one of the longest scripts weve looked at so far. Lets step through it section by sec-
tion. We begin with some basic error checking, and then set up the canvas on which were
going to work.
Setting Up the Base Canvas
In Listing 19.2, rather than starting from scratch, we will start with an existing image for the
button. We have a choice of three colors in the basic button: red (red-button.png), green
(green-button.png), and blue (blue-button.png).
The users chosen color is stored in the $color variable from the form.
We begin by setting up a new image identifier based on the appropriate button:
$im = imagecreatefrompng (“$color-button.png”);
Advanced PHP Techniques
P
ART IV
414
LISTING 19.2 Continued
24 7842 CH19 3/6/01 3:42 PM Page 414