Filling in Figures


Besides drawing figures in outline, you can also fill in figures with color using various functions such as these:

  • imagefilledarc. Draw a partial ellipse and fill it

  • imagefilledellipse. Draw a filled ellipse

  • imagefilledpolygon. Draw a filled polygon

  • imagefilledrectangle. Draw a filled rectangle

For example, take a look at imagefilledrectangle:

 imagefilledrectangle(resource image, int x1, int y1, int x2, int y2, int color) 

This function creates a filled rectangle of color color in image image starting at upper-left coordinates x1, y1 and ending at bottom-right coordinates x2, y2. This function is easy enough to use; start out as usual by creating an image and setting a drawing color:

 $image = imagecreate($image_width, $image_height); $back_color = imagecolorallocate($image, 200, 200, 200); $draw_color = imagecolorallocate($image, 0, 0, 0);          .          .          . 

Now you can draw a number of filled rectangles using imagefilledrectangle:

 $image = imagecreate($image_width, $image_height); $back_color = imagecolorallocate($image, 200, 200, 200); $draw_color = imagecolorallocate($image, 0, 0, 0); imagefilledrectangle($image, 20, 20, 40, 80, $draw_color); imagefilledrectangle($image, 60, 20, 140, 80, $draw_color); imagefilledrectangle($image, 160, 20, 270, 80, $draw_color); 

All this is put to work in phpfilledrectangle.php, Example 9, where we're drawing three filled-in rectangles.

Example 9. Filling in rectangles, phpfilledrectangle.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);     imagefilledrectangle($image, 20, 20, 40, 80, $draw_color);     imagefilledrectangle($image, 60, 20, 140, 80, $draw_color);     imagefilledrectangle($image, 160, 20, 270, 80, $draw_color);     header('Content-Type: image/jpeg');     imagejpeg($image);     imagedestroy($image); ?> 

Here's the HTML page for this example, phpfilledrectangle.html:

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

The results appear in Figure 10.

Figure 10. Drawing filled rectangles.




    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