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.

You can also take this a step further and get your button.php script to support multiple<br />

image types. Simply check $ext and call the appropriate ImagePNG( ), ImageJPEG( ),<br />

or ImageGIF( ) function at the end of the script. You can also parse the filename and<br />

add modifiers such as color, size, and font, or pass them right in the URL. Because of<br />

the parse_str( ) call in the example, a URL such as http://your.site/buttons/php.<br />

png?size=16 displays “php” in a font size of 16.<br />

Scaling Images<br />

There are two ways to change the size of an image. The ImageCopyResized( ) function<br />

is available in all versions of GD, but its resizing algorithm is crude and may lead<br />

to jagged edges in your newimages. The ImageCopyResampled( ) function is newin<br />

GD 2.x and features pixel interpolation to give smooth edges and clarity to resized<br />

images (it is, however, slower than ImageCopyResized( )). Both functions take the<br />

same arguments:<br />

ImageCopyResized(dest, src, dx, dy, sx, sy, dw, dh, sw, sh);<br />

ImageCopyResampled(dest, src, dx, dy, sx, sy, dw, dh, sw, sh);<br />

The dest and src parameters are image handles. The point (dx,dy) is the point in the<br />

destination image where the region will be copied. The point (sx,sy) is the upperleft<br />

corner of the source image. The sw, sh, dw, and dh parameters give the width and<br />

height of the copy regions in the source and destination.<br />

Example 9-10 takes the php.jpg image shown in Figure 9-8 and smoothly scales it<br />

down to one-quarter of its size, yielding the image in Figure 9-9.<br />

Figure 9-8. Original php.jpg image<br />

Example 9-10. Resizing with ImageCopyResampled( )<br />

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

Saved successfully!

Ooh no, something went wrong!