Drawing an Arc


The imagearc function lets you draw arcs, which include partial circles and partial ellipses as well as complete circles and ellipses:

 imagearc(resource image, int cx, int cy, int w, int h, int s, int e, int color) 

This function is designed to draw arcs centered at cx, cy in the image represented by image. The w and h values specify the ellipse's width and height, respectively, while the start and end points are specified in degrees indicated by the s and e arguments (here, 0° is located at the three-o'clock position). The arc itself is drawn clockwise.

Say that you wanted to draw a smiling face using imagearc. All you need to do is to create the image, set the drawing color, and use imagearc. Here's what it looks like, where we're drawing a complete circle:

 $image_height = 100; $image_width = 300; $image = imagecreate($image_width, $image_height); $backcolor = imagecolorallocate($image, 200, 200, 200); $drawing_color = imagecolorallocate($image, 0, 0, 0); imagearc($image, 150, 50, 50, 50, 10, 170, $drawing_color);         .         .         . 

Then you can use imagearc to draw the rest of the face, as shown in phparc.php, Example 7.

Example 7. Drawing arcs, phparc.php
 <?php     $image_height = 100;     $image_width = 300;     $image = imagecreate($image_width, $image_height);     $backcolor = imagecolorallocate($image, 200, 200, 200);     $drawing_color = imagecolorallocate($image, 0, 0, 0);     imagearc($image, 150, 50, 50, 50, 10, 170, $drawing_color);     imagearc($image, 150, 50, 70, 70, 0, 360, $drawing_color);     imagearc($image, 135, 45, 20, 20, 190, -10, $drawing_color);     imagearc($image, 165, 45, 20, 20, 190, -10, $drawing_color);     header("Content-type: image/jpeg");     imagejpeg($image);     imagedestroy($image); ?> 

We'll embed phparc.php in phparc.html like this:

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

You can see the resulting smiling face in Figure 8.

Figure 8. Drawing arcs.




    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