resource imagecreatetruecolor ( int x_size, int y_size)
bool imagefilledellipse ( resource image, int centre_x, int centre_y, int width, int height, int color)
bool imagefilledarc ( resource image, int centre_x, int centre_y, int width, int height, int start, int end, int color)
bool imageellipse ( resource image, int centre_x, int centre_y, int width, int height, int color)
bool imagearc ( resource image, int centre_x, int centre_y, int width, int height, int start, int end, int color)
bool imagerectangle ( resource image, int x1, int y1, int x2, int y2, int color)
Rectangles are, as you can imagine, the easiest graphical shape to draw, however it is not a great deal harder to draw more complicated images thanks to the ease of the GD library that PHP uses. Using three new functions we can make a much more complicated image, and these are: imagecreatetruecolor(), imagefilledellipse(), and imagefilledarc(). As you can see, the function names are getting so long that you start to wish there were underscores between words!
Here is a script using these new functions:
<?php header("content-type: image/png"); $image = imagecreatetruecolor(400,300); $blue = imagecolorallocate($image, 0, 0, 255); $green = imagecolorallocate($image, 0, 255, 0); $red = imagecolorallocate($image, 255, 0, 0); imagefilledellipse($image, 200, 150, 200, 200, $red); imagefilledellipse($image, 200, 150, 180, 180, $blue); imagefilledellipse($image, 200, 150, 50, 50, $red); imagefilledarc($image, 200, 150, 200, 200, 345, 15, $green, IMG_ARC_PIE); imagefilledarc($image, 200, 150, 200, 200, 255, 285, $green, IMG_ARC_PIE); imagefilledarc($image, 200, 150, 200, 200, 165, 195, $green, IMG_ARC_PIE); imagefilledarc($image, 200, 150, 200, 200, 75, 105, $green, IMG_ARC_PIE); imagepng($image); imagedestroy($image); ?>
That's a bit more complicated than our previous efforts, but if you run the code you'll see it was worth it - the image is much more interesting, with more colours now. Save your own copy as picture2.php, edit your HTML file to point at the new picture, and try it yourself.
Using imagecreatetruecolor() is exactly the same as imagecreate() - it takes the same two parameters, and returns an image resource that is freed using imagedestroy(). The differences between the two are that imagecreatetruecolor() returns an image with a true-colour palette, whereas imagecreate() only lets you use 256 colours at once. Furthermore, the image resource returned by imagecreatetruecolor() automatically has a black background, so you need not worry about the first allocated colour being used as the image background colour - note that we don't have any $black colour at all in the script.
The two new shape functions take quite a few parameters, so you may need to keep the list to hand when working with them. The parameters for imagefilledellipse() are: image resource, centre of ellipse (X co-ordinate), centre of ellipse (Y co-ordinate), height, width, and colour. As there are more parameters required to draw an arc, imagefilledarc() is a little more complicated: image resource, centre X, centre Y, height, width, then the start and end points of the arc specified in degrees, followed by colour, and finally the type of arc to draw.
The start and end points for arcs are specified from 0 to 359 degrees, with 0 pointing directly to the right, or 3 o'clock if you think in clock faces. To draw a complete circle rather than just a section as in the example, you would specifying 0 and 359 as the start and end points, although in this case it is easier just to use imagefilledellipse()!. The final parameter to imagefilledarc() is the type of arc to draw, and you have the choice of the following:
IMG_ARC_PIE, as in the example, which draws a filled wedge shape with a curved edge
IMG_ARC_CHORD, which draws a straight line between the starting and ending angles
IMG_ARC_NOFILL, which draws the outside edge line, without drawing the two lines towards the centre of the arc
IMG_ARC_EDGED, draws an unfilled wedge shape with a curved edge
You can combine these four together in various ways to make your own style of arc, with the exception of IMG_ARC_CHORD and IMG_ARC_PIE, which cannot be combined together as they conflict geometrically. Some examples:
imagefilledarc($image, 200, 150, 200, 200, 345, 15, $green, IMG_ARC_CHORD | IMG_ARC_NOFILL); imagefilledarc($image, 200, 150, 200, 200, 345, 15, $green, IMG_ARC_EDGED | IMG_ARC_NOFILL);
Using that first one to replace the first call in picture2.php and the second one to replace the third call should make the right-hand arc become a straight line on the outside edge of the arc, and make the left-hand arc become an unfilled wedge.
So far we've only been looking at the filled shapes, but there are unfilled varieties too: imageellipse() complements imagefilledellipse(), imagearc() complements imagefilledarc(), and imagerectangle() complements imagefilledrectangle(). The first and last of these work the same whether they are filled or otherwise, but imagefilledarc() is slightly different - you do not need the last parameter, because the arc is always the equivalent of IMG_ARC_NOFILL.
Want to learn PHP 7?
Hacking with PHP has been fully updated for PHP 7, and is now available as a downloadable PDF. Get over 1200 pages of hands-on PHP learning today!
If this was helpful, please take a moment to tell others about Hacking with PHP by tweeting about it!
Next chapter: Complex shapes >>
Previous chapter: Getting arty
Home: Table of Contents
Copyright ©2015 Paul Hudson. Follow me: @twostraws.