Specifications

// 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 )
);
This code tests the size of the text by looking at what is called the bounding box of the text.
We do this using the
ImageGetTTFBBox() function, which is one of the TrueType font func-
tions. We will, after we have figured out the size, print on the button using a TrueType font and
the ImageTTFText() function.
The bounding box of a piece of text is the smallest box you could draw around the text. An
example of a bounding box is shown in Figure 19.6.
Advanced PHP Techniques
P
ART IV
416
FIGURE 19.6
Coordinates of the bounding box are given relative to the baseline. The origin of the coordinates is shown here
as (0,0).
To get the dimensions of the box, we call
$bbox=imagettfbbox ($font_size, 0, “arial.ttf”, $button_text);
This call says, For given font size $font_size, with text slanted on an angle of zero degrees,
using the TrueType font Arial, tell me the dimensions of the text in $button_text.
Note that you actually need to pass the path to the file containing the font into the function. In
this case, its in the same directory as the script (the default), so we havent specified a longer
path.
The function returns an array containing the coordinates of the corners of the bounding box.
The contents of the array are shown in Table 19.1.
24 7842 CH19 3/6/01 3:42 PM Page 416