13.09.2016 Views

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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Drawing Figures <strong>and</strong> Graphing Data<br />

499<br />

$above_line_text = abs($bbox[7]);<br />

$text_y += $above_line_text;<br />

// how far above the baseline?<br />

// add baseline factor<br />

$text_y -= 2; // adjustment factor for shape of our template<br />

These correction factors allow for the baseline <strong>and</strong> a little adjustment because the image<br />

is a bit “top heavy.”<br />

Writing the Text onto the Button<br />

After that, it’s all smooth sailing.You set up the text color, which will be white:<br />

$white = ImageColorAllocate ($im, 255, 255, 255);<br />

You can then use the imagettftext() function to actually draw the text onto the<br />

button:<br />

imagettftext ($im, $font_size, 0, $text_x, $text_y, $white, $fontname,<br />

$button_text);<br />

This function takes quite a lot of parameters. In order, they are the image identifier, the<br />

font size in points, the angle you want to draw the text at, the starting x <strong>and</strong> y coordinates<br />

of the text, the text color, the font file, <strong>and</strong>, finally, the actual text to go on the<br />

button.<br />

Note<br />

The font file needs to be available on the server <strong>and</strong> is not required on the client’s machine because she will<br />

see it as an image.<br />

Finishing Up<br />

Finally, you can output the button to the browser:<br />

Header (‘Content-type: image/png’);<br />

imagepng ($im);<br />

Then it’s time to clean up resources <strong>and</strong> end the script:<br />

imagedestroy ($im);<br />

That’s it! If all went well, you should now have a button in the browser window that<br />

looks similar to the one you saw in Figure 22.5.<br />

Drawing Figures <strong>and</strong> Graphing Data<br />

In the preceding application, we looked at existing images <strong>and</strong> text.We haven’t yet<br />

looked at an example with drawing, so let’s do that now.

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

Saved successfully!

Ooh no, something went wrong!