May 4th, 2008
Learn How to Generate Graphics in PHP - Part I
Learn How to Generate Graphics in PHP
Today we are going to learn PHP Graphics….no it’s not a new tern basically today we will Learn How to Generate Graphics through PHP and believe me, it’s nuisance convention that it’s hard to learn graphics in PHP or it’s hard way to generate graphics in PHP. No, the truth is, it’s 100% very easy and spoon feeding but one should must know the concepts before playing with it, that’s it
Without knowing concepts people do worst talks.
I divide graphics editing programs into two distinct categories: painting programs that let you tweak an image pixel by pixel and drawing programs that provide a set of objects like lines, ovals, and rectangles that you can manipulate until you render the drawing to a flat image like a JPEG. Paint programs are great for pixel-perfect control. But for business graphics, a drawing program is the way to go because most graphs are sets of rectangles, lines, and ovals.
PHP’s built-in drawing primitives are like a paint program. They’re great for rendering to an image, but they aren’t so good if you want to think of your image as a set of objects. Today I’ll show you how to build an object oriented graphics library to sit on top of the PHP graphics library. You’ll use the object oriented extensions provided in PHP5
With object oriented graphics support, your graphics code is much easier to understand and maintain. And you have the potential for rendering a graph into multiple types of media — Flash movies, SVG, etc. — from a single bit of graphing source.
If have any problem with object oriented programming in PHP (OOPS) then please refer below tutorials:
Aim of Tutorial
Instead of providing you all the native PHP graphic functions I’ll make all the process extreamly easy by making Object Oriented Graphics Library which makes your life very smooth. It doesn’t mean that PHP graphics functions are tough, I made a lib just to develop a object oriented scenario. And creating a graphics object library also involves three primary goals:
Move From Primitives to Objects
Instead of using imageline, imagefilledrectangle, and other graphics functions, this library should provide objects like Line, Rectangle, and Oval that can be rendered to an image. It should also support the ability to make larger complex objects or to group objects together.
Allow for z-ordering
Drawing programs let the artist move a graphics object above or below other objects on the drawing surface. The library should support this ability to position one object before or after another using a z value that defines the object’s height from the surface of the drawing plane. Objects with higher z values are drawn later and, thus, appear on top of objects with a lower z value.
Provide for Viewport Transformations
Often, the data’s coordinate space is not the same as the image. The graphics primitives in PHP work on the coordinate plane of the image. The graphics library should support the specification of a viewport so you can specify the graphics in a coordinate system that is friendlier to the programmer and automatically scales to fit an image of any size.
Because this is a large set of features, you’ll write the code step by step to show how the code evolves to add functionality.
Start From the Scratch
Let’s start with a graphics environment object and an interface called GraphicsObject implemented by a Line class that draws lines. The UML is shown below

The GraphicsEnvironment class holds the graphics object and a set of colors. It also contains the width and height. The saveAsPng method draws the current image out to the specified file.
The GraphicsObject is the interface that any graphics object must implement. To start with, all you need is the render method to draw the object. It’s implemented by a Line class that takes four coordinates: the starting and ending x values, and the starting and ending y values. It also has a color. When render is called, the object draws a line from sx,sy to ex,ey of the color specified by name.
Below is the code for php_graphics_lib.php library :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 | <?php class GraphicsEnvironment { public $width; public $height; public $gdo; public $colors = array(); public function __construct( $width, $height ) { $this->width = $width; $this->height = $height; $this->gdo = imagecreatetruecolor( $width, $height ); $this->addColor( "white", 255, 255, 255 ); imagefilledrectangle( $this->gdo, 0, 0, $width, $height, $this->getColor( "white" ) ); } public function width() { return $this->width; } public function height() { return $this->height; } public function addColor( $name, $r, $g, $b ) { $this->colors[ $name ] = imagecolorallocate( $this->gdo, $r, $g, $b ); } public function getGraphicObject() { return $this->gdo; } public function getColor( $name ) { return $this->colors[ $name ]; } public function saveAsPng( $filename ) { imagepng( $this->gdo, $filename ); } } abstract class GraphicsObject { abstract public function render( $ge ); } class Line extends GraphicsObject { private $color; private $sx; private $sy; private $ex; private $ey; public function __construct( $color, $sx, $sy, $ex, $ey ) { $this->color = $color; $this->sx = $sx; $this->sy = $sy; $this->ex = $ex; $this->ey = $ey; } public function render( $ge ) { imageline( $ge->getGraphicObject(), $this->sx, $this->sy, $this->ex, $this->ey, $ge->getColor( $this->color ) ); } } ?> |
Now we are going to use our PHP Graphics Library in our code. Below is the sample code that how we can use php_graphics_lib.php in our programs. Save the below code as a php_graphics_1.php :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | <?php require_once("php_graphics_lib.php"); $ge = new GraphicsEnvironment( 400, 400 ); $ge->addColor( "black", 0, 0, 0 ); $ge->addColor( "red", 255, 0, 0 ); $ge->addColor( "green", 0, 255, 0 ); $ge->addColor( "blue", 0, 0, 255 ); $gobjs = array(); $gobjs []= new Line( "black", 10, 5, 100, 200 ); $gobjs []= new Line( "blue", 200, 150, 390, 380 ); $gobjs []= new Line( "red", 60, 40, 10, 300 ); $gobjs []= new Line( "green", 5, 390, 390, 10 ); foreach( $gobjs as $gobj ) { $gobj->render( $ge ); } $ge->saveAsPng( "test.png" ); ?> |
This test creates a graphics environment. It then creates a few lines pointing in different directions and having different colors. Next, the render method draws them onto the graphics plane. At the end, the code saves the image as test.png.
The command-line interpreter runs the code throughout the article, as follows:
% php test.php %
Now have a look how test.png file looks like:

In the next part of this PHP Graphics tutorial we will make our PHP graphics library more enrich and fruitful. So click here to be there to have more fun ![]()

