18.9 Drawing Rectangles


Drawing text to images isn't enough for you ”you need to make art!

Technique

Use the ImageRectangle() and the ImageFilledRectangle() functions to draw rectangles in your images.

Example 1 ”A simple, filled rectangle:

 <?php header("Content-type: image/png"); $im = ImageCreate(100, 20); $white = ImageColorAllocate($im, 255, 255, 255); $light_blue = ImageColorAllocate($im, 20, 93, 233); ImageFilledRectangle($im, 5, 10, 60, 14, $light_blue); ImagePng($im); ImageDestroy($im); ?> 

Example 2 ”A simple rectangle (not filled):

 <?php header("Content-type: image/png"); $im = ImageCreate(100, 20); $white = ImageColorAllocate($im, 255, 255, 255); $light_blue = ImageColorAllocate($im, 20, 93, 233); ImageRectangle($im, 5, 10, 60, 14, $light_blue); ImagePng($im); ImageDestroy($im); ?> 

Comments

In example 1, we use the ImageFilledRectangle() function to draw a simple, filled rectangle to the image stream, $im . This function takes an image stream, $im , and draws a rectangle starting at the X coordinate, 5 , and the Y coordinate 10 , to the X coordinate 60 and the Y coordinate 14 , with the color $light_blue .

In example 2, we use the ImageRectangle() function, which draws an unfilled rectangle to image $im , with a border of $light_blue . This function has an argument order identical to that of the ImageFilledRectangle() function discussed earlier.

A line is a rectangle with a nonexistent width. Therefore, the ImageFilledRectangle() function can be used not only to draw rectangles but also to simulate the drawing of lines. But in reality, lines have to be at least one pixel wide, so you need to specify a width of 1 for the rectangle so that it looks like a line:

 <?php header("Content-type: image/png"); $im = ImageCreate(100, 20); $white = ImageCollorAllocate($im, 255, 255, 255); ImageColorTransparent($im, $white); $light_red = ImageColorAllocate($im, 233, 93, 20); ImageFilledRectangle($im, 5, 10, 60, 11, $light_red); ImagePng($im); ImageDestroy($im); ?> 


PHP Developer's Cookbook
PHP Developers Cookbook (2nd Edition)
ISBN: 0672323257
EAN: 2147483647
Year: 2000
Pages: 351

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