Home  >  Article  >  Backend Development  >  Clever way to create graphics using PHP 5.0_PHP Tutorial

Clever way to create graphics using PHP 5.0_PHP Tutorial

WBOY
WBOYOriginal
2016-07-13 17:34:42730browse

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
Clever way to create graphics using PHP 5.0_PHP Tutorial

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 ) );
            }
            }
            ?>
            

The test code is shown in Listing 2:

Listing 2. Test code for basic graphics library

            <?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" );
            ?>
            

This test program creates a graphical environment. Then create several lines that point in different directions and have different colors. The render method can then draw them onto the graphics plane. Finally, this code saves the image as test.png.

In this article, the following command line interpreter is used to run this code, as shown below:

            % php test.php
            %
            

Figure 2 shows what the generated test.png file looks like in Firefox.

Figure 2. Simple graphic object test

Clever way to create graphics using PHP 5.0_PHP Tutorial

This is of course not as beautiful as the Mona Lisa, but it can meet the current work needs.
Add dimensions

Now that our first requirement—the ability to provide graphical objects—is satisfied, we can now move on to our second requirement: the ability to place one object above or below other objects using a z-value.

We can think of each z value as a face of the original image. Elements are drawn in order from smallest to largest z-value. For example, let's draw two graphic elements: a red circle and a black box. The circle has a z-value of 100, while the black box has a z-value of 200. This will place the circle behind the box, as shown in Figure 3:

 Figure 3. Surfaces with different z values

不同 z 值的面

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/508467.htmlTechArticleThis article will show how to use PHP to build an object-oriented graphics layer. Using an object-oriented system can be used to build more complex graphics than using the basic functionality provided in the standard PHP library...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn