Page 266 - Open Soource Technologies 304.indd
P. 266
Web Technologies-I
Notes Once you have an editable canvas, you get access to drawing colours by calling
ImageColorAllocate( ) :
$background_color = ImageColorAllocate($image, 255, 255, 255); // white
$gray = ImageColorAllocate($image, 204, 204, 204); // gray
The ImageColorAllocate( ) function takes an image handle to allocate the colour to and
three integers. The three integers each range from 0 to 255 and specify the red, green, and blue
components of the colour. This is the same RGB colour combination that is used in HTML to
set a font or background color. So, white is 255, 255, 255; black is 0, 0, 0, and everything else is
somewhere in between.
The first call to ImageAllocateColor( ) sets the background colour. Additional calls allocate
colours for drawing lines, shapes, or text. Therefore, set the background colour to 255, 255, 255
and then grab a gray pen with ImageAllocateColor($image, 204, 204, 204). It may seem odd
that the background colour is determined by the order ImageAllocateColor() is called and
not by a separate function. But, that’s how things work in GD, so PHP respects the convention.
Call ImageFilledRectangle( ) to place a box onto the canvas. ImageFilledRectangle( ) takes
many parameters: the image to draw on, the x and y coordinates of the upper left corner of
the rectangle, the x and y coordinates of the lower right corner of the rectangle, and finally, the
color to use to draw the shape. Tell ImageFilledRectangle( ) to draw a rectangle on $image,
starting at (50,10) and going to (150,40), in the colour gray:
ImageFilledRectangle($image, 50, 10, 150, 40, $gray);
Unlike a Cartesian graph, (0, 0) is not in the lower left corner; instead, it is in the upper left
corner. So, the vertical coordinate of the spot 10 pixels from the top of a 50 pixel high canvas
is 10 because it is 10 pixels down from the top of the canvas. It is not 40, because you measure
from the top down, not the bottom up. And it is not -10, because down is considered the positive
direction, not the negative one.
Now that the image is all ready to go, you can serve it up. First, send a Content-type header
to let the browser know what type of image you are sending. In this case, we display a PNG.
Next, have PHP write the PNG image out using ImagePNG( ). Once the image is sent, your
task is over:
header (‘Content-Type: image/png’); ImagePNG($image);
To write the image to disk instead of sending it to the browser, provide a second argument to
ImagePNG( ) with where to save the file:
ImagePng($image, ‘/path/to/your/new/image.png’);
Since the file is not going to the browser, there’s no need to call header ( ). Make sure to specify
a path and an image name, and be sure PHP has permission to write to that location.
PHP cleans up the image when the script ends, but, if you wish to manually deallocate the
memory used by the image, calling ImageDestroy($image)forces PHP to get rid of the image
immediately.
If you want to use a feature that is not enabled, you should rebuild PHP
yourself or get your ISP to do so.
260 LOVELY PROFESSIONAL UNIVERSITY