Page 280 - Open Soource Technologies 304.indd
P. 280
Web Technologies-I
Notes In GD 2.x there is support for true colour images complete with an alpha channel. GD 2.x has
a 7-bit (0-127) alpha channel.
To create a true colour image, use the ImageCreateTrueColor( ) function:
$image = ImageCreateTrueColor(width, height);
Use ImageColorResolveAlpha( ) to create a colour 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 colour you allocate does not
automatically become your background colour. Call ImageFilledRectangle( ) to fill the image
with any background colour you want.
Example creates a true colour image and draws a semitransparent orange ellipse on a white
background.
A simple orange ellipse on a white background.
<?php
$im = ImageCreateTrueColor(150,150);
$white = ImageColorAllocate($im,255,255,255);
ImageAlphaBlending($im, false);
ImageFilledRectangle($im,0,0,150,150,$white);
$red = ImageColorResolveAlpha($im,255,50,0,50);
ImageFilledEllipse($im,75,75,80,63,$red);
header(‘Content-Type: image/png’);
ImagePNG($im);
?>
Figure 11.11 shows the output of above Example.
Figure 11.11: An Orange Ellipse on a White Background
274 LOVELY PROFESSIONAL UNIVERSITY