Beginning PHP4 | 5
[previous] [next] |
Beginning PHP4
Circles
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:
Rectangles
If you've picked up on any of the trends in this chapter, you'll have guessed by now that the function to create a rectangle in PHP is ImageRectangle():
ImageRectangle(image_id, x1, y1, x2, y2, color_id);
The arguments correspond to the image identifier, the x and y coordinates of the top left-hand corner of the rectangle, the x and y coordinates of the bottom right-hand corner of the rectangle, and the identifier for the color we want it drawn in.
Pretty straightforward, right? Here's the code, which now adds a box whose top right-hand corner is at x = 150, y = 65, the same as the center of the circle.
<?php
//draw3.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);
ImageRectangle($image,10,65,150,140,$blue);
ImageJPEG($image);
ImageDestroy($image);
?>
This now returns the following image:
[previous] [next] |
Revised: March 15, 2000
Created: March 15, 2000