DJ Mike's Tutorials: PHP, Ellipses and Arcs

< ^ >

Ellipses and Arcs

Ellipses

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 imagecreate400100);
$basecolor imagecolorallocate$image255255255);

$blue imagecolorallocate$image00255);
$green imagecolorallocate$image02550);
$red imagecolorallocate$image25500);

imageellipse($image100509040$red);
imagefilledellipse($image300504090$blue);

imagesetthickness($image5);
imageellipse($image2005038080$green);

imagegif($image);
imagedestroy($image);
?>

Arcs



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 imagecreate400100);
$basecolor imagecolorallocate$image255255255);

$blue imagecolorallocate$image00255);
$green imagecolorallocate$image02550);
$red imagecolorallocate$image25500);

imagearc($image1005090400270$red);
imagearc($image200501006030300$blue);

imagesetthickness($image5);
imagearc($image20050380800361$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 imagecreate400200);
$basecolor imagecolorallocate$image255255255);

$blue imagecolorallocate$image00255);
$green imagecolorallocate$image02550);
$red imagecolorallocate$image25500);
$purple imagecolorallocate$image1250125);
$yellow imagecolorallocate$image2552550);

imagesetthickness($image5);

imagefilledarc($image706090400270$green
IMG_ARC_PIE);

# boat
imagefilledarc($image200150240800180$redIMG_ARC_PIE);
imagefilledarc($image190145110260180270$purpleIMG_ARC_PIE+IMG_ARC_NOFILL+IMG_ARC_EDGED );
imagefilledarc($image19514580220270360$blueIMG_ARC_NOFILL+IMG_ARC_EDGED );

# pie chart
imagefilledarc($image3305080400361$yellowIMG_ARC_PIE);
imagefilledarc($image3305080400120$blueIMG_ARC_PIE);
imagefilledarc($image330508040060$redIMG_ARC_PIE);

imagegif($image);
imagedestroy($image);
?>


< ^ >


Created by DJ Mike from Santa Barbara

DJ Mike


Dance Away Santa Barbara's Home Page
<a href="http://www.statcounter.com/" target="_blank"> <img src="http://c5.statcounter.com/counter.php?sc_project=1321035&java=0&security=da2193dc" alt="counter free hit invisible" border="0" /></a>