Drawing a Rectangle


You can draw plenty of figures using lines alone, but far more GD functions are available. One of them, imagerectangle, draws rectangles:

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

This function creates a rectangle of color color in image image starting at upper-left coordinate x1, y1 and ending at bottom-right coordinate x2, y2.

Here's an example. We'll start by using black as our drawing color:

 $draw_color = imagecolorallocate($image, 0, 0, 0);     .     .     . 

Then we can draw a few rectangles using imagerectangle:

 $draw_color = imagecolorallocate($image, 0, 0, 0); imagerectangle($image, 20, 20, 40, 80, $draw_color); imagerectangle($image, 60, 20, 140, 80, $draw_color); imagerectangle($image, 160, 20, 270, 80, $draw_color);     .     .     . 

All that's left is to send the results to the browser, as in phprectangle.php, Example 4.

Example 4. Drawing rectangles, phprectangle.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);     imagerectangle($image, 20, 20, 40, 80, $draw_color);     imagerectangle($image, 60, 20, 140, 80, $draw_color);     imagerectangle($image, 160, 20, 270, 80, $draw_color);     header('Content-Type: image/jpeg');     imagejpeg($image);     imagedestroy($image); ?> 

We'll embed the image created by phprectangle.php in a web page, phprectangle.html:

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

The results appear in Figure 5.

Figure 5. Drawing 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