Drawing Shapes and Lines with PHP
Drawing shapes and lines with PHP is nothing like drawing with image editing program. Actually, when drawing with PHP you become the editing program. You use individual PHP functions to define colors, draw and fill shapes, re-size and save the image. These functions are part of the Thomas Boutell’s GD graphics library that was bundled beginning with PHP version 4.3.0.
Drawing New Image
The basic function used to create a new image is called ImageCreate(). This function creates the canvas area for your new image. For example to create an image that is 300px wide and 300px high you would use following code:
$imageOne = ImageCreate(300, 300);
Now that you have canvis you need to define colors you want to use in it. Colors are defined using RGB color system. Using decimal values from 0 to 255 for each of the red (R), green (G), and blue(B) you can define a specific color. The function used to define colors is ImageColorAllocate().
The first color you allocate is used as the background color of the image.
$white = ImageColorAllocate($imageOne, 255, 255, 255); $red = ImageColorAllocate($imageOne, 255, 0, 0); $blue = ImageColorAllocate($imageOne, 0, 0, 255); $green = ImageColorAllocate($imageOne, 0, 255, 0);
Drawing Lines and Shapes
There are several PHP functions to assist you in drawing lines and shapes. And as you can see below the function names are very descriptive.
- ImageEllipse() – to draw an ellipse
- ImageArc() – to draw arc (partial ellipse)
- ImagePolygon() – to draw a polygon
- ImageRectangle() – to draw a rectangle
- ImageLine() – to draw a line
Each of these functions use x-axis and y-axis coordinates as indicators of where to start and stop the drawing on the canvas. Here is sample of green rectangle that is 30px wide, 50px high and 10px from the left edge and 20px away from the top of the canvas.
ImageRectangle($imageOne, 10, 20, 40, 70, $green);
[flickr id="5821700292" thumbnail="medium" overlay="false" size="medium" group="" align="none"]
As you can see drawing with PHP requires some planning ahead.
Using a Color Fill
PHP can also fill the shapes with solid color. Functions to do that are:
- ImageFilledEllipse() – to fill an ellipse
- ImageFilledArc() – to fill a partial ellipse
- ImageFilledPlygon() – to fill a polygon
- ImageFilledRectangle() – to fill a rectangle
These functions are used just like nonfill drawing functions explained above.
ImageFilledRectangle($imageOne, 10, 20, 40, 70, $green);
PHP comes with several built-in styles that are used in the display. For example IMG_ARC_PIE says to create a rounded edge. To learn more about styles in PHP visit: http://www.php.net/image.
I hope these basic functions help you understand image drawing in PHP. You can use dynamic data as part of these functions to create pie charts or even 3D charts. If you want to create JPEG and PNG files you will need to download and install some additional libraries. Here are some resources:
- JPEG library – ftp://ftp.uu.net/graphics/jpeg/
- PNG library – http://www.libpng.org/pub/png/libpng.html and zlib library http://www.zlib.net/