Create | Graphics This article will show you how to build an object-oriented graphics layer using PHP. Using object-oriented systems can be used to build complex graphics, which is much simpler than using the basic functionality provided in a standard PHP library to build graphics.
I divide the graphics editing program into two categories: one is the drawing program, which can be used to draw images in pixels one pixel. The other is a mapping program that provides a set of objects, such as lines, ellipses, and rectangles that you can use to combine into a large image, such as JPEG. The drawing program is ideal for pixel-level control. But for business graphics, a graphics program is a good way to do it, because most graphics are made up of rectangles, lines, and ellipses.
PHP's built-in mapping basic operations are very similar to the drawing program. They are very powerful for drawing images, but this is not appropriate if you want your image to be a collection of sets 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 the PHP V5.
With object-oriented graphics support, your graphics code is easy to understand and maintain. You may also want to combine graphics from a single source of graphics into multiple types of media: Flash movies, SVG, and so on.
Target
Creating a Graphics object library consists of 3 main goals:
switch from basic operation to object
It does not use Imageline, Imagefilledrectangle, and other graphical functions, and this library should provide objects such as line, Rectangle, and Oval, which can be used to make images. It should also support the ability to build larger complex objects or to group objects.
can be sorted by Z-value
The drawing program allows the painter to move the drawing objects up and down on the surface of the picture. This library should support the ability to place an object before and after other objects: it uses a Z value to define the height at which the object starts from the drawing plane. The larger the Z-value object is drawn, the later it appears on those objects with a smaller Z value.
provides viewport conversions
Usually, the coordinate space of the data is different from the coordinate space of the image. The basic operation of graphics in PHP is to operate the coordinate plane of the image. This graphics library should support viewport specifications so that you can specify graphics in a coordinate system familiar to a programmer, and can scale automatically to fit any image size.
Because there are a lot of features here, you'll be writing code step-by-step to show how the code can add functionality.
Basic Knowledge
Let's first look at a graphical environment object and an interface called Graphicsobject, which is implemented using a line class, which is used to draw lines. The UML is shown in Figure 1.
Figure 1. Graphics environment and Graphics object interface
The Graphicsenvironment class holds graphic objects and a set of colors, and also includes width and height. The Saveaspng method is responsible for outputting the current image to the specified file.
Graphicsobject is the interface that any drawing object must use. To start using this interface, all you have to do is use the Render method to draw the object. It is implemented by a line class that utilizes 4 coordinates: The start and end x values, the start and end y values. It also has a color. When Render is invoked, this object draws a line from Sx,sy to Ex,ey to draw a color specified by the name.
The code for this library is shown in Listing 1.
Listing 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); The Public Function Getgraphicobject () {return $this->gdo; The Public Function GetColor ($name) {return $this->colors[$name]; The 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; The public function render ($ge) {imageline ($ge->getgraphicobject (), $this->sx, $this->sy, $ This->ex, $this->ey, $ge->getcolor ($this->color)); }}?>
The test code is shown in Listing 2:
[1] [2] [3] [4] [5] Next page