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
ImageArc($image, $x, $y, $width, $height, $start, $end, $color);
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).
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);
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.
