Higher-level PHP constructs for manipulating image files
A library of tools for manipulating images We have seen that we can create an image file by writing a sequence of bytes to the file But this is very tedious PHP provides a library of tools (functions) for creating and/or editing image files This library is called the GD library GD was actually written in C but "wrappers" are available for PHP, Perl and other languages The GD website
A first example program This program uses two tools from the GD library imageCreateTrueColor takes two arguments and produces a variable containing basic data for an image –the arguments specify the width and height of the image –By default, the image created by imageCreateTrueColor has a black background imageGIF takes two arguments –the data for an image and the name of a file –imageGIF puts a copy of the image data into a file with the specified name
Drawing a solid rectangle imagecolorallocate is used to add a colour to the palette that will be used in the image it takes four arguments: the image variable and the RGB values it returns an identifier for the specified colour imagefilledrectangle is used to place a solid rectangle of colour in the image it takes six arguments: the image variable, four coordinates for the rectangle and the identifier for the desired colour –the coordinates are X ul, Y ul, X lr, Y lr (ul=upper-left corner; lr=lower right corner)
Drawing two rectangles We can use imagecolorallocate several times, to add a range of colours We can use imagefilledrectangle several times, to add several solid rectangles
The sequence determines the result Above, we drew the blue rectangle last, so it appears on top of the others
Flood fill Use imagefill to flood fill the pixels around a specified coordinate The program above makes the image go through four steps, as shown Since (50,50) is in the middle of the red area, that rectangle is flooded with white
Drawing solid ellipses and circles imagefilledellipse ( $image, $cx, $cy, $width, $height, $colour ) $cx and $cy specify the centre of the shape $width and $height specify the width and height of the shape $width=$height produces a circle
Drawing polygons imagefilledpolygon ( $image, $arrayOfPoints, $numberOfPoints, $colour ) $arrayOfPoints specifies the X and Y coordinates of the vertices of the polygon $numberOfPoints specifies how many vertices –there must be at least three vertices An example is on the next slide
Drawing a triangle The vertices of the triangle are (40,50) (150,100) (40,70) Members of an array are specified like this: $result=array(val1, val2, val3,... );