Specifications
if (empty($button_text) || empty($color))
{
echo “Could not create image - form not filled out correctly”;
exit;
}
// create an image of the right background and check size
$im = imagecreatefrompng (“$color-button.png”);
$width_image = ImageSX($im);
$height_image = ImageSY($im);
// Our images need an 18 pixel margin in from the edge image
$width_image_wo_margins = $width_image - (2 * 18);
$height_image_wo_margins = $height_image - (2 * 18);
// Work out if the font size will fit and make it smaller until it does
// Start out with the biggest size that will reasonably fit on our buttons
$font_size = 33;
do
{
$font_size--;
// find out the size of the text at that font size
$bbox=imagettfbbox ($font_size, 0, “arial.ttf”, $button_text);
$right_text = $bbox[2]; // right co-ordinate
$left_text = $bbox[0]; // left co-ordinate
$width_text = $right_text - $left_text; // how wide is it?
$height_text = abs($bbox[7] - $bbox[1]); // how tall is it?
} while ( $font_size>8 &&
( $height_text>$height_image_wo_margins ||
$width_text>$width_image_wo_margins )
);
if ( $height_text>$height_image_wo_margins ||
$width_text>$width_image_wo_margins )
{
// no readable font size will fit on button
echo “Text given will not fit on button.<BR>”;
}
else
Generating Images
C
HAPTER 19
19
G
ENERATING
IMAGES
413
LISTING 19.2 Continued
24 7842 CH19 3/6/01 3:42 PM Page 413










