You can make hollow ellipses with imageellipse() or filled ellipses with imagefilledellipse(). Both require six arguments: an image resource, an x cooridinate for the center, a y cooridinate for the center, a width, a height and a color resource. The following code was used to draw the red ellipse in the image above:
imageellipse($image, 100, 50, 90, 40, $red);
and the next block of code drew the blue ellipse
imagefilledellipse($image, 300, 50, 40, 90, $blue);
According to the PHP Manual, you can change the line thickness of ellipses using imagesetthickness() you can for rectangles but as you can see, when I used the code below, the thickness did not change.
imagesetthickness($image, 5);
imageellipse($image, 200, 50, 380, 80, $green);
This is a known bug and you can get around it by using imagearc() instead.
<?
header("Content-type: image/gif");
$image = imagecreate( 400, 100);
$basecolor = imagecolorallocate( $image, 255, 255, 255);
$blue = imagecolorallocate( $image, 0, 0, 255);
$green = imagecolorallocate( $image, 0, 255, 0);
$red = imagecolorallocate( $image, 255, 0, 0);
imageellipse($image, 100, 50, 90, 40, $red);
imagefilledellipse($image, 300, 50, 40, 90, $blue);
imagesetthickness($image, 5);
imageellipse($image, 200, 50, 380, 80, $green);
imagegif($image);
imagedestroy($image);
?>
imagearc() has the same arguments as imageellipse() with two additions: a start and and end point measured in degrees. Arcs are drawn clockwise starting at 3 o'clock. The format is:
imagearc($image, $x, $y, $width, $height, $start, $end, $color);
To draw the red arc in the image above I used this code:
imagearc($image, 100, 50, 90, 40, 0, 270, $red);
You can see that imagesetthickness() worked on the green ellipse but there is a trick to get it to work:
imagesetthickness($image, 5);
imagearc($image, 200, 50, 380, 80, 0, 361, $green);
Notice that the arc starts at 0 and ends at 361. When I tried ending it at 360º, the line thickness went to one pixel so I made it go one more degree. Here is all of the code for the image:
<?
header("Content-type: image/gif");
$image = imagecreate( 400, 100);
$basecolor = imagecolorallocate( $image, 255, 255, 255);
$blue = imagecolorallocate( $image, 0, 0, 255);
$green = imagecolorallocate( $image, 0, 255, 0);
$red = imagecolorallocate( $image, 255, 0, 0);
imagearc($image, 100, 50, 90, 40, 0, 270, $red);
imagearc($image, 200, 50, 100, 60, 30, 300, $blue);
imagesetthickness($image, 5);
imagearc($image, 200, 50, 380, 80, 0, 361, $green);
imagegif($image);
imagedestroy($image);
?>
imagefilledarc() has the same arguments as imagearc() with the the addition of an argument for arc style. Arc style is defined by the following built in constants:
IMG_ARC_PIE "Pie" arc of an ellipse
IMG_ARC_CHORD Joins ends of arc
IMG_ARC_NOFILL Unfilled arc
IMG_ARC_EDGED Joins end points of arc to center
IMG_ARC_PIE and IMG_ARC_CHORD are mutually exclusive but you can add the others to get different effects.
In the image above, the green pie section is drawn with this code block:
imagefilledarc($image, 70, 60, 90, 40, 0, 270, $green,
IMG_ARC_PIE);
The purple arc was made into an outline by adding IMG_ARC_PIE, IMG_ARC_NOFILL and IMG_ARC_EDGED like this:
imagefilledarc($image, 190, 145, 110, 260, 180, 270, $purple, IMG_ARC_PIE+IMG_ARC_NOFILL+IMG_ARC_EDGED );
The "pie in the sky" on the right was made by layering three filled arcs; a 361º yellow arc, a 120º blue arc and a 60º red arc.
<?
header("Content-type: image/gif");
$image = imagecreate( 400, 200);
$basecolor = imagecolorallocate( $image, 255, 255, 255);
$blue = imagecolorallocate( $image, 0, 0, 255);
$green = imagecolorallocate( $image, 0, 255, 0);
$red = imagecolorallocate( $image, 255, 0, 0);
$purple = imagecolorallocate( $image, 125, 0, 125);
$yellow = imagecolorallocate( $image, 255, 255, 0);
imagesetthickness($image, 5);
imagefilledarc($image, 70, 60, 90, 40, 0, 270, $green,
IMG_ARC_PIE);
# boat
imagefilledarc($image, 200, 150, 240, 80, 0, 180, $red, IMG_ARC_PIE);
imagefilledarc($image, 190, 145, 110, 260, 180, 270, $purple, IMG_ARC_PIE+IMG_ARC_NOFILL+IMG_ARC_EDGED );
imagefilledarc($image, 195, 145, 80, 220, 270, 360, $blue, IMG_ARC_NOFILL+IMG_ARC_EDGED );
# pie chart
imagefilledarc($image, 330, 50, 80, 40, 0, 361, $yellow, IMG_ARC_PIE);
imagefilledarc($image, 330, 50, 80, 40, 0, 120, $blue, IMG_ARC_PIE);
imagefilledarc($image, 330, 50, 80, 40, 0, 60, $red, IMG_ARC_PIE);
imagegif($image);
imagedestroy($image);
?>
|
|
|