Page 270 - Open Soource Technologies 304.indd
P. 270
Web Technologies-I
Notes To embed this image in a web page, use:
<img src=”black.php”>
11.4.1 The Structure of a Graphics Program
Most dynamic image-generation programs follow the same basic steps outlined in above Example.
You can create a 256-color image with the ImageCreate( ) function , which returns an image
handle:
$image = ImageCreate(width, height);
All colours used in an image must be allocated with the ImageColorAllocate( ) function . The
first color allocated becomes the background color for the image. *
* This is true only for images with a colour palette. True colour images created using
ImageCreateTrueColor( ) do not obey this rule.
$color = ImageColorAllocate(image, red, green, blue);
The arguments are the numeric RGB (red, green, blue) components of the colour. In above
Example, we wrote the colour values in hexadecimal, to bring the function call closer to the
HTML colour representation “#FFFFFF” and”#000000”.
There are many drawing primitives in GD. Above Example uses ImageFilledRectangle( ), in
which you specify the dimensions of the rectangle by passing the coordinates of the top-left
and bottom-right corners:
ImageFilledRectangle(image, tlx, tly, brx, bry, color);
The next step is to send a Content-Type header to the browser with the appropriate content type
for the kind of image being created. Once that is done, we call the appropriate output function.
The ImageJPEG( ), ImagePNG( ), and ImageWBMP( ) functions create JPEG, PNG, and
WBMP files from the image, respectively:
ImageJPEG(image [, filename [, quality ]]);
ImagePNG(image [, filename ]);
ImageWBMP(image [, filename ]);
If no filename is given, the image is sent to the browser. The quality argument for JPEGs is a
number from 0 (worst-looking) to 10 (best-looking). The lower the quality, the smaller the JPEG
file. The default setting is 7.5.
In above Example, we set the HTTP header immediately before calling the output-generating
function ImagePNG( ). If you set the Content-Type at the very start of the script, any errors
that are generated are treated as image data and the browser displays a broken image icon.
Table 11.1 lists the image formats and their Content-Type values.
Table 11.1: Content-type Values for Image Formats
Format Content-Type
GIF image/gif
JPEG image/jpeg
PNG image/png
WBMP image/vnd.wap.wbmp
264 LOVELY PROFESSIONAL UNIVERSITY