[ Team LiB ] Previous Section Next Section

Acquiring Color

To work with color, you need to acquire a color resource. You can do this with the imagecolorallocate() function, which requires an image resource and three integers between 0 and 255 representing red, green, and blue. The function returns an image resource that you can use to define the color of shapes, fills, and text:

$red = imagecolorallocate( $image, 255,0,0 );

Coincidentally, the first time you call imagecolorallocate(), you also set the default color for your image.

Now that you have an image resource and a color allocated, you are nearly ready to output your first image to the browser. To do this, you need to use the imagepng() function, which requires the image resource as an argument. imagepng() also accepts an optional path argument. If you provide a path here, PHP will attempt to write the data to a file rather than to the browser. This can be useful for caching dynamically generated images.

Listing 15.1 uses these functions to create and output an image.

Listing 15.1 A Dynamically Created Image
1: <?php
2: header("Content-type: image/png");
3: $image = imagecreate( 200, 200 );
4: $red = imagecolorallocate( $image, 255, 0, 0 );
5: imagepng($image);
6: ?>

Notice that we sent a Content-type header to the browser (line 2) before doing anything else. We need to tell the browser to expect image information; otherwise, it treats the script's output as HTML. This script can now be called directly by the browser, or as part of an IMG element, like so:

<img src="listing15.1.php" alt="a PHP generated image">

Figure 15.1 shows the output of Listing 15.1.

Figure 15.1. A dynamically created image.


We have created a square, but we have no way as yet of controlling its color.


Depending on your setup, you might be able to output image formats other than PNGs. To add JPEG support on a Unix system, for example, you might need an external JPEG library from the Independent JPEG Group at http://www.ijg.org/files/jpegsrc.v6b.tar.gz.

In the following fragment, we unpack the archive and install it from a Linux command line:

tar -xvzf jpegsrc.v6b.tar.gz
cd jpeg-6b
  --enable-shared \
  --enable-static \
make install

After the JPEG library is installed, we can ensure that PHP can use it when we run PHP's configure script:

./configure --with-apxs=/home/apache/bin/apxs' \
  --with-gd \
  --with-freetype=/usr/include/freetype/ \
  --with-ttf \
  --with-zlib-dir=/usr/include \

After PHP is compiled, we can substitute imagejpeg() for imagepng() to write JPEG rather than PNG data.

Like imagepng(), imagejpeg() accepts an image resource and optional second argument, which you can use to write an image to a file. It also accepts a third integer argument representing the quality of the image you want to output. This can be a value between 1 and 100. If you omit the third argument, a default of 75 is used.

As you read this, you might find that JPEG support is bundled with your version of PHP. Use gd_info() to check your configuration before recompiling.

    [ Team LiB ] Previous Section Next Section