05.05.2013 Views

Programming PHP

Programming PHP

Programming PHP

SHOW MORE
SHOW LESS

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

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

Example 9-7. Creating a dynamic button (continued)<br />

$y = ( ImageSy($im) - $dy ) / 2 + $dy;<br />

// draw text<br />

$black = ImageColorAllocate($im,0,0,0);<br />

ImageTTFText($im, $size, 0, $x, $y, $black, $font, $text);<br />

header('Content-Type: image/png');<br />

ImagePNG($im);<br />

?><br />

In this case, the blank button (button.png) looks as shown in Figure 9-6.<br />

Figure 9-6. Blank button<br />

Note that if you are using GD 2.0.1, antialiased TrueType fonts work only if the<br />

background image is indexed. If you are having problems with your text looking terrible,<br />

load your background image into any image-editing tool and convert it from a<br />

true color image to one with an 8-bit indexed palette. Alternatively, upgrade from<br />

GD 2.0.1 to GD 2.0.2 or later.<br />

The script in Example 9-7 can be called from a page like this:<br />

<br />

This HTML generates the button shown in Figure 9-7.<br />

Figure 9-7. Generated button<br />

The + character in the URL is the encoded form of a space. Spaces are illegal in URLs<br />

and must be encoded. Use <strong>PHP</strong>’s urlencode( ) function to encode your button<br />

strings. For example:<br />

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

Saved successfully!

Ooh no, something went wrong!