Drawing an Ellipse


The imageellipse function, as you can guess, draws ellipses:

 imageellipse(resource image, int cx, int cy, int w, int h, int color) 

This function draws an ellipse centered at cx, cy in the image represented by image. The w and h values specify the ellipse's width and height, respectively. The color of the ellipse is specified by color. This function is pretty close to imagerectangle, but instead of specifying the upper-left and lower-right corners, you specify the center of the ellipse and its width and height.

Here's an example of putting this drawing function to work. As usual, we'll start by setting the drawing color. Then we'll draw a few ellipses with this function; here's what it looks like:

 $back_color = imagecolorallocate($image, 200, 200, 200); $draw_color = imagecolorallocate($image, 0, 0, 0); imageellipse($image, 100, 50, 150, 50, $draw_color); imageellipse($image, 150, 40, 150, 50, $draw_color); imageellipse($image, 200, 30, 150, 50, $draw_color); 

You can see all the code in phpellipse.php, Example 5.

Example 5. Drawing ellipses, phpellipse.php
 <?php     $image_height = 100;     $image_width = 300;     $image = imagecreate($image_width, $image_height);     $back_color = imagecolorallocate($image, 200, 200, 200);     $draw_color = imagecolorallocate($image, 0, 0, 0);     imageellipse($image, 100, 50, 150, 50, $draw_color);     imageellipse($image, 150, 40, 150, 50, $draw_color);     imageellipse($image, 200, 30, 150, 50, $draw_color);     header('Content-Type: image/jpeg');     imagejpeg($image);     imagedestroy($image); ?> 

And we'll embed phpellipse.php in phpellipse.html:

 <HTML>     <HEAD>         <TITLE>             Drawing ellipses         </TITLE>     </HEAD>     <BODY>         <CENTER>             <H1>                 Drawing ellipses             </H1>             <BR>             <IMG src="/books/1/265/1/html/2/phpellipse.php">         </CENTER>     </BODY> </HTML> 

The new ellipses appear in phpellipse.html, Figure 6. Perfect.

Figure 6. Drawing ellipses.


That gets lines, rectangles, and ellipses into our drawing arsenal. A good start.



    Spring Into PHP 5
    Spring Into PHP 5
    ISBN: 0131498622
    EAN: 2147483647
    Year: 2006
    Pages: 254

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net