4.2. Image Manipulations

When you know which libraries are installed on your system, it's time to put them to good use. The rest of this chapter covers the most common uses of the GD extension.

4.2.1. Resizing images

If you allow users to upload their own images, resizing is going to be a concern for you. You'll have users who try to upload pictures that are 2,304 × 1,728 pixels (or bigger)! Obviously, those won't fit into your site design, and such large images will take ages to load even on a high-bandwidth connection. You could simply refuse to allow the upload if it's larger than a given size, say 100 × 100 pixels, but that will put off some users who don't have the software or the expertise to resize their images.

The other option is to accept the upload and resize it programmatically before you store or use the image. GD gives you two ways to do this:

  • ImageCopyResized()

  • ImageCopyResampled()

ImageCopyResized() is faster, but it tends to leave jagged edges in resized images. ImageCopyResampled() uses pixel interpolation to produce smoother results.

Both functions take the same arguments:

  • Dest: Image handle for the destination image

  • Src: Image handle for the source image

  • Dx, Dy: The X and Y coordinates in the destination image where the region will be created

  • Sx, Sy: The coordinates of the top-left corner of the source image

  • Sw, Sh: The width and height of the source image

  • Dw, Dh: The width and height of the destination image

The following code uses ImageCopyResampled() to generate ...

Get PHP & MySQL® Web Development All-in-One Desk Reference for Dummies® now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.