Developer

10 tips for PHP scripts: Create and manipulate images

Learn how you can create and manipulate images in PHP on the fly.

By Julie Meloni
(2/6/01)

With a few installed third-party libraries and a bit of geometric skill, you can create and manipulate images on the fly with PHP. Actually, you don't need all that much skill in geometry, because I flunked it in high school and can still create images in PHP!

Before you can use the basic image creation functions, you need to install the GD library. To use the JPEG-related subset of image creation functions, you need to install jpeg-6b. If you want to use Type 1 fonts in your images, you must install t1lib.

There are a few more tweaks you that have to do to get your environment set up. First, install t1lib and get it out of the way. Next, install jpeg-6b. Third, install the GD library. Do those three things in the order given here, because you need to compile the GD library to make use of the jpeg-6b library, and if jpeg-6b isn't there first, the compilation won't be correct and you'll run around chasing your tail for a while.

After these three libraries are installed, you need to reconfigure PHP. This would be one of those instances in which you're glad you installed the DSO version of PHP. Do a make clean, then add the following into the current configuration directive:

—with-gd=[/path/to/gd]
—with-jpeg-dir=[/path/to/jpeg-6b]
—with-t1lib=[/path/to/t1lib]

Finish it up by doing a make, then make install. Restart Apache, do a phpinfo() to see if your new functionality shows up, and you're on your way.

Depending on which version of the GD library you have installed, you may or may not be able to create GIFs or PNGs. Here's the key: If you installed gd-1.6 or earlier, you can work with GIFs but not PNGs. If you installed gd-1.6 or later, you can work with PNGs but not GIFs.

Creating a simple image requires a number of functions. I'll step through them one by one.

Output a header containing the MIME type of the image that you're creating—in this case, a PNG.

<? header ("Content-type: image/png");

Create a variable to hold the blank image you'll make using ImageCreate(). This function requires a size in pixels. The format is ImageCreate(x_size, y_size), so for a 250-by-250-pixel image, you'd use:

$newImg = ImageCreate(250,250);

Because your image is blank, you're going to want to fill it with a color of some sort. However, first you need to allocate a name for a color by its RGB values, using the ImageColorAllocate() function. The format for this function is ImageColorAllocate([image], [red], [green], [blue]). For a nice sky-blue color, you'd use:

$skyblue = ImageColorAllocate($newImg,136,193,255);

Next, you need to fill the image with this color using the ImageFill() function. There are actually several versions of ImageFill(), such as ImageFillRectangle(), ImageFillPolygon(), and so on. For simplicity's sake, we'll just use ImageFill() to do a flood file, with the following format:

ImageFill([image], [start x point], [start y point], [color])
ImageFill($newImg,0,0,$skyblue);

Lastly, you create the final image and destroy the graphics stream to free up memory and clean up after yourself:

ImagePNG($newImg);
ImageDestroy($newImg); ?>

Your code should look something like this:

<? header ("Content-type: image/png");
$newImg = ImageCreate(250,250);
$skyblue = ImageColorAllocate($newImg,136,193,255);
ImageFill($newImg,0,0,$skyblue);
ImagePNG($newImg);
ImageDestroy($newImg);
?>

If you call this script skyblue.php and access it with your browser, you should see a 250-by-250-pixel, sky-blue PNG.

You can also use image creation functions to manipulate images, as in creating a thumbnail of a larger image.

Suppose you have an image from which you want to make a 35-by-35-pixel thumbnail. What you'll be doing is creating a new image that is 35 by 35 pixels in size; making a graphics stream containing the contents of the original image; and then placing a resized version of the original image into the new, empty graphic.

The key function used to achieve this is ImageCopyResized(), which requires a format like this: ImageCopyResized([new image handle],[original image handle],[new image X], [new Image Y], [original image X], [original image Y], [new image X], [new image Y], [original image X], [original image Y]);

Comments are in the code below:

<? /* send a header so that the browser knows the content-type of the file */
header("Content-type: image/png");

/* set up variables to hold the height and width of your new image */
$newWidth = 35;
$newHeight = 35;

/* create a blank, new image of the given new height and width */
$newImg = ImageCreate($newWidth,$newHeight);

/* get the data from the original, large image */
$origImg = ImageCreateFromPNG("test.png");

/* copy the resized image. Use the ImageSX() and ImageSY functions to get the x and y sizes of the orginal image. */
ImageCopyResized($newImg,$origImg,0,0,0,0,$newWidth,$newHeight,ImageSX($origImg),ImageSY($origImg));

/* create final image and free up the memory */
ImagePNG($newImg);
ImageDestroy($newImg); ?>

If you call this script resized.php and access it with your browser, you should see a 35-by-35-pixel thumbnail PNG.

Julie Meloni is the technical director at i2i Interactive and is an avowed proponent of Linux and the open source community. A regular contribtor to CNET Builder.com, she has written a few books on PHP and other technologies.

Editor's Picks