Page 272 - Open Soource Technologies 304.indd
P. 272
Web Technologies-I
Notes 11.4.4 Reading an Existing File
If you want to start with an existing image and then modify it, use either ImageCre-
ateFromJPEG( ) or ImageCreateFromPNG( ):
$image = ImageCreateFromJPEG(filename);
$image = ImageCreateFromPNG(filename);
11.4.5 Basic Drawing Functions
GD has functions for drawing basic points, lines, arcs, rectangles, and polygons. This describes
the base functions supported by GD 2.x.
The most basic function is ImageSetPixel( ), which sets the colour of a specified pixel:
ImageSetPixel(image, x, y, color);
There are two functions for drawing lines, ImageLine( ) and ImageDashedLine( ):
ImageLine(image, start_x, start_ y, end_x, end_ y, color);
ImageDashedLine(image, start_x, start_ y, end_x, end_ y, color);
There are two functions for drawing rectangles, one that simply draws the outline and one that
fills the rectangle with the specified colour:
ImageRectangle(image, tlx, tly, brx, bry, color);
ImageFilledRectangle(image, tlx, tly, brx, bry, color);
Specify the location and size of the rectangle by passing the coordinates of the top-left and
bottom-right corners.
You can draw arbitrary polygons with the ImagePolygon( ) and ImageFilledPolygon( )
functions:
ImagePolygon(image, points, number, color);
ImageFilledPolygon(image, points, number, color);
Both functions take an array of points. This array has two integers (the x and y coordinates)
for each vertex on the polygon. The number argument is the number of vertices in the array
(typically count($points)/2).
The ImageArc( ) function draws an arc (a portion of an ellipse):
ImageArc(image, center_x, center_ y, width, height, start, end, color);
The ellipse is defined by its center, width, and height (height and width are the same for a
circle). The start and end points of the arc are given as degrees counting counterclockwise from
3 o’clock. Draw the full ellipse with a start of 0° and an end of 360°.
There are two ways to fill in already-drawn shapes. The ImageFill( ) function performs a flood
fill, changing the colour of the pixels starting at the given location. Any change in pixel colour
266 LOVELY PROFESSIONAL UNIVERSITY