start page | rating of books | rating of authors | reviews | copyrights

Book HomePHP CookbookSearch this book

15.3. Drawing Arcs, Ellipses, and Circles

15.3.1. Problem

You want to draw open or filled curves. For example, you want to draw a pie chart showing the results of a user poll.

15.3.2. Solution

To draw an arc, use ImageArc( ):

ImageArc($image, $x, $y, $width, $height, $start, $end, $color);

To draw an ellipse, use ImageArc( ) and set $start to 0 and $end to 360:

ImageArc($image, $x, $y, $width, $height, 0, 360, $color);

To draw a circle, use ImageArc( ), set $start to 0, set $end to 360, and use the same value for both $width and $height:

ImageArc($image, $x, $y, $diameter, $diameter, 0, 360, $color);

15.3.3. Discussion

Because the ImageArc( ) function is highly flexible, you can easily create common curves such as ellipses and circles by passing it the right values. Like many GD functions, the first parameter is the canvas. The next two parameters are the x and y coordinates for the center position of the arc. After that comes the arc width and height. Since a circle is an arc with the same width and height, to draw a circle, set both numbers to your circle's diameter.

The sixth and seventh parameters are the starting and ending angles, in degrees. A value of 0 is at 3 o'clock. The arc then moves clockwise, so 90 is at 6 o'clock, 180 is at 9 o'clock, and 270 is at the top of the hour. (Be careful, this behavior is not consistent among all GD functions. For example, when you rotate text, you turn in a counter-clockwise direction.) Since the arc's center is located at ($x,$y), if you draw a semicircle from 0 to 180, it doesn't start at ($x,$y); instead, it begins at ($x+($diameter/2),$y).

As usual, the last parameter is the arc color.

For example, this draws an open black circle with a diameter of 100 pixels centered on the canvas, as shown in left half of Figure 15-3:

$image = ImageCreate(100,100);
$bg = ImageColorAllocate($image, 255, 255, 255);
$black = ImageColorAllocate($image, 0, 0, 0);
ImageArc($image, 50, 50, 100, 100, 0, 360, $black);

To produce a solid-colored ellipse or circle, call ImageFillToBorder( ):

ImageArc($image, $x, $y, $diameter, $diameter, 0, 360, $color);
ImageFillToBorder($image, $x, $y, $color, $color);

The ImageFillToBorder( ) function floods a region beginning at ($x,$y) with the color specified as the last parameter until it hits the edge of the canvas or runs into a line with the same color as the third parameter.

Incorporating this into the earlier example gives:

$image = ImageCreate(100,100);
$bg = ImageColorAllocate($image, 255, 255, 255);
$black = ImageColorAllocate($image, 0, 0, 0);
ImageArc($image, 50, 50, 100, 100, 0, 360, $black);
ImageFillToBorder($image, 50, 50, $black, $black);

The output is shown in the right half of Figure 15-3.

Figure 15-3

Figure 15-3. An open black circle and a filled black circle

If you're running GD 2.x, you can call ImageFilledArc( ) and pass in a final parameter that describes the fill style. GD 2.x also supports specific ImageEllipse( ) and ImageFilledEllipse( ) functions.

15.3.4. See Also

Recipe 15.2 for more on drawing other types of shapes; Recipe 15.4 for more on drawing with styles and brushes; documentation on ImageArc( ) at http://www.php.net/imagearc, ImageFilledArc( ) at http://www.php.net/imagefilledarc, and ImageFillToBorder( ) at http://www.php.net/imagefilltoborder.



Library Navigation Links

Copyright © 2003 O'Reilly & Associates. All rights reserved.