home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam  

Programming PHPProgramming PHPSearch this book

9.8. Color Handling

Color support improved markedly between GD 1.x and GD 2.x. In GD 1.x there was no notion of the alpha channel, color handling was rather simple, and the library supported only 8-bit palette images (256 colors). When creating GD 1.x 8-bit palette images, you use the ImageCreate( ) function, and the first color you allocate using the ImageColorAllocate( ) function becomes the background color.

In GD 2.x there is support for true color images complete with an alpha channel. GD 2.x has a 7-bit (0-127) alpha channel.

To create a true color image, use the ImageCreateTrueColor( ) function:

$image = ImageCreateTrueColor(width, height);

Use ImageColorResolveAlpha( ) to create a color index that includes transparency:

$color = ImageColorResolveAlpha(image, red, green, blue, alpha);

The alpha value is between 0 (opaque) and 127 (transparent).

While most people are used to an 8-bit (0-255) alpha channel, it is actually quite handy that GD's is 7-bit (0-127). Each pixel is represented by a 32-bit signed integer, with the four 8-bit bytes arranged like this:

  High Byte                  Low Byte
{Alpha Channel} {Red} {Green} {Blue}

For a signed integer, the leftmost bit, or the highest bit, is used to indicate whether the value is negative, thus leaving only 31 bits of actual information. PHP's default integer value is a signed long into which we can store a single GD palette entry. Whether that integer is positive or negative tells us whether antialiasing is enabled for that palette entry.

Unlike with palette images, with GD 2.x true color images the first color you allocate does not automatically become your background color. Call ImageFilledRectangle( ) to fill the image with any background color you want.

Example 9-11 creates a true color image and draws a semitransparent orange ellipse on a white background.

Example 9-11. A simple orange ellipse on a white background

 $im = ImageCreateTrueColor(150,150);
 $white = ImageColorAllocate($im,255,255,255);
 ImageAlphaBlending($im, false);
 $red = ImageColorResolveAlpha($im,255,50,0,50);
 header('Content-Type: image/png');

Figure 9-11 shows the output of Example 9-11.

Figure 9-11

Figure 9-11. An orange ellipse on a white background

You can use the ImageTrueColorToPalette( ) function to convert a true color image to one with a color index (also known as a paletted image).

9.8.1. Using the Alpha Channel

In Example 9-11, we turned off alpha blending before drawing our background and our ellipse. Alpha blending is a toggle that determines whether the alpha channel, if present, should be applied when drawing. If alpha blending is off, the old pixel is replaced with the new pixel. If an alpha channel exists for the new pixel, it is maintained, but all pixel information for the original pixel being overwritten is lost.

Example 9-12 illustrates alpha blending by drawing a gray rectangle with a 50% alpha channel over an orange ellipse.

Example 9-12. A gray rectangle with a 50% alpha channel overlaid

 $im = ImageCreateTrueColor(150,150);
 $white = ImageColorAllocate($im,255,255,255);
 ImageAlphaBlending($im, false);
 $red = ImageColorResolveAlpha($im,255,50,0,63);
 $gray = ImageColorResolveAlpha($im,70,70,70,63);
 ImageAlphaBlending($im, false);
 header('Content-Type: image/png');

Figure 9-12 shows the output of Example 9-12 (alpha blending is still turned off).

Figure 9-12

Figure 9-12. A gray rectangle over the orange ellipse

If we change Example 9-12 to enable alpha blending just before the call to ImageFilledRectangle( ), we get the image shown in Figure 9-13.

Figure 9-13

Figure 9-13. Image with alpha blending enabled

9.8.3. True Color Color Indexes

The color index returned by ImageColorResolveAlpha( ) is really a 32-bit signed long, with the first three 8-bit bytes holding the red, green, and blue values, respectively. The next bit indicates whether antialiasing is enabled for this color, and the remaining seven bits hold the transparency value.

For example:

$green = ImageColorResolveAlpha($im,0,0,255,127);

This code sets $green to 2130771712, which in hex is 0x7F00FF00 and in binary is 01111111000000001111111100000000.

This is equivalent to the following ImageColorResolveAlpha( ) call:

$green = 127<<24 | 0<<16 | 255<<8 | 0;

You can also drop the two 0 entries in this example and just make it:

$green = 127<<24 | 255<<8;

To deconstruct this value, you can use something like this:

$a = ($col & 0x7F000000) >> 24;
$r = ($col & 0x00FF0000) >> 16;
$g = ($col & 0x0000FF00) >> 8;
$b = ($col & 0x000000FF);

Direct manipulation of true color color values like this is rarely necessary. One application is to generate a color-testing image that shows the pure shades of red, green, and blue. For example:

$im = ImageCreateTrueColor(256,60);
for($x=0; $x<256; $x++) {
    ImageLine($im, $x, 0, $x, 19, $x);
    ImageLine($im, 255-$x, 20, 255-$x, 39, $x<<8);
    ImageLine($im, $x, 40, $x, 59, $x<<16);

Figure 9-14 shows the output of the color-testing program.

Figure 9-14

Figure 9-14. The color test

Obviously it will be much more colorful than what we can show you here in black and white, so try this example for yourself. In this particular example it is much easier to simply calculate the pixel color than to call ImageColorResolveAlpha( ) for every color.

Library Navigation Links

Copyright © 2003 O'Reilly & Associates. All rights reserved.