13.09.2016 Views

PHP and MySQL Web Development 4th Ed-tqw-_darksiderg

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Using Text <strong>and</strong> Fonts to Create Images<br />

497<br />

Figure 22.6 Coordinates of the bounding box are given relative to the<br />

baseline.The origin of the coordinates is shown here as (0,0).<br />

To get the dimensions of the box, you call<br />

$bbox=imagettfbbox ($font_size, 0, $fontname, $button_text);<br />

This call says,“For given font size $font_size, with text slanted on an angle of zero<br />

degrees, using the TrueType font Arial, tell me the dimensions of the text in<br />

$button_text.”<br />

Note that you actually need to pass the path to the file containing the font into the<br />

function. In this case, it’s in the same directory as the script (the default), so we didn’t<br />

specify a longer path.<br />

The function returns an array containing the coordinates of the corners of the<br />

bounding box.The contents of the array are shown in Table 22.1.<br />

Table 22.1 Contents of the Bounding Box Array<br />

Array Index Contents<br />

0 x coordinate, lower-left corner<br />

1 y coordinate, lower-left corner<br />

2 x coordinate, lower-right corner<br />

3 y coordinate, lower-right corner<br />

4 x coordinate, upper-right corner<br />

5 y coordinate, upper-right corner<br />

6 x coordinate, upper-left corner<br />

7 y coordinate, upper-left corner<br />

To remember what the contents of the array are, just remember that the numbering<br />

starts at the bottom-left corner of the bounding box <strong>and</strong> works its way around counterclockwise.<br />

There is one tricky thing about the values returned from the imagettfbbox() function.They<br />

are coordinate values, specified from an origin. However, unlike coordinates<br />

for images, which are specified relative to the top-left corner, they are specified relative<br />

to a baseline.<br />

Look at Figure 22.6 again.You will see that we have drawn a line along the bottom<br />

of most of the text.This is known as the baseline. Some letters hang below the baseline,<br />

such as y in this example.These parts of the letters are called descenders.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!