This article will show you how to build an object-oriented graphics layer using PHP. Using an object-oriented system can be used to build complex graphics much more easily than using the basic functionality provided in the standard PHP library.
I divide graphics editing programs into two categories: drawing programs, which draw an image pixel by pixel, and drawing programs, which provide a set of objects such as lines, ellipses, and Rectangles, you can use these objects to combine into a large image, such as a JPEG. Painting programs are great for pixel-level control. But for business graphics, a drawing program is a better way because most graphics are composed of rectangles, lines, and ellipses.
The basic drawing operations built into PHP are very similar to drawing programs. They are very powerful for drawing images; however, they are not suitable if you want your image to be a collection of objects. This article will show you how to build an object-oriented graphics library based on the PHP graphics library. You will use the object-oriented extensions provided in PHP V5.
With object-oriented graphics support, your graphics code is much easier to understand and maintain. You may also need to composite graphics into multiple types of media from a single graphics source: Flash movie, SVG, and so on.
Goal
Creating a graphical object library includes 3 main goals:
Switch from basic operations to objects
Rather than using imageline, imagefilledrectangle, and other graphics functions, this library should provide objects such as Line, Rectangle, and Oval that can be used to make images. It should also support the ability to build larger complex objects or group objects.
Can be sorted by z value
Drawing programs allow painters to move graphic objects up and down the surface of the drawing. This library is supposed to support placing an object in front of or behind another object: it uses a z-value that defines the height of the object from the drawing plane. Objects with larger z-values are drawn later and appear above those with smaller z-values.
Provide viewport conversion
Usually, the coordinate space of the data is different from the coordinate space of the image. The basic graphics operations in PHP operate on the coordinate plane of the image. The graphics library should support the viewport specification so that you can specify graphics in a coordinate system that is familiar to programmers and automatically scale to fit any image size.
Because there are so many features here, you'll write code step by step to show how the code adds functionality.
Basic knowledge
Let us first look at a graphics environment object and an interface called GraphicsObject, which is implemented using a Line class, and its function is to draw lines. UML is shown in Figure 1.
Figure 1. Graphical environment and graphical object interface
The GraphicsEnvironment class stores graphics objects and a set of colors, including width and height. The saveAsPng method is responsible for outputting the current image to the specified file.
GraphicsObject is an interface that any graphics object must use. To start using this interface, all you need to do is draw the object using the render method. It is implemented by a Line class that utilizes 4 coordinates: start and end x-values, start and end y-values. It also has a color. When render is called, this object draws a line from sx,sy to ex,ey with the color specified by the name.
The code for this library is shown in Listing 1.
List 1. Basic graphics library
<?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 ) ); } } ?> Copy after login |
<?php require_once( "glib.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" ); ?> Copy after login |
% php test.php % Copy after login |