Drawing On Our Image: Circles - Page 5
December 15, 2000
To create an arc, circle or ellipse in PHP, we use a function
called ImageArc() with the following syntax:
ImageArc(image_id, x, y, width, height, start, end, color_id);
As you can see this function takes quite a few arguments. The
first is, as usual, the image identifier. Next are our x and y
coordinates, and in this case they specify the center point of
our arc. The width and height are the width and height of the
circle or ellipse from which we take the arc.
Note we don't use a radius, as this would limit us to using a
perfect arc or circle. The option to have different height and
width means that we can use this function to create ellipses. A
circle is simply an ellipse whose height and width are equal.
| Start and end points for the arc are measured clockwise
in degrees from the right-hand horizontal radius (that is, three
o'clock): |
|
The slanted line we created earlier had one end at the position x
= 150, y = 30. We're now going to create a circle that's 70
pixels across, the top of which touches this end of that line.
The x value for the center of our circle will therefore be 150
again, but the y value must be greater than that of the line end
by 35 (that is, half the width of the circle remember
we're dealing with a width of 70, not a radius).
The y value for the end of the line is 30, the circle center must
have y = 65. Since we are going to create a full circle, we must
draw our arc through a complete 360 degrees: start = 0 and end =
360. We'll draw the circle in blue as well.
Our code now looks like this:
<?php
//draw2.php
Header("Content-type: image/jpeg");
$image = ImageCreate(200,150);
$gray = ImageColorAllocate($image,204,204,204);
$blue = ImageColorAllocate($image,0,0,255);
ImageLine($image,10,10,150,30,$blue);
ImageArc($image,150,65,70,70,0,360,$blue);
ImageJPEG($image);
ImageDestroy($image);
?>
| and produces this: |
|
Drawing On Our Image: Lines - Page 4
Beginning PHP4
Drawing On Our Image: Rectangles - Page 6
|