Use PHP5.0 to create a graph

Source: Internet
Author: User
This article will show you how to use PHP to build an object-oriented graphic layer. The use of object-oriented systems can be used to build complex graphics, which is much easier than using the basic functions provided in the standard PHP library to build graphics. I divide a graphic editing program into two types: a plotting program, which allows a pixel SyntaxHighlighter. all (). This article will show you how to use PHP to build an object-oriented graphic layer. The use of object-oriented systems can be used to build complex graphics, which is much easier than using the basic functions provided in the standard PHP library to build graphics.

I divide a graphic editing program into two types: a plotting program, which can be used to draw an image in pixels or pixels; and a plotting program, which provides a set of objects, for example, line, ellipse, and rectangle, you can use these objects to combine them into a large image, such as JPEG. The plotting program is ideal for pixel-level control. However, for business graphics, the ing program is a good method, because most graphics are composed of rectangles, lines, and edges.

The basic operations of PHP built-in plotting are very similar to those of the plotting program. They are very powerful for image rendering, but this is not suitable if you want your images to be a set 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 extension provided in PHP V5.

With the support of object-oriented graphics, your graphic code is very easy to understand and maintain. You may also need to combine images from a single graphic source into multiple types of media: Flash movies, SVG, and so on.

   Target

Creating a Graph object library includes three main objectives:

   Switch from basic operation to object

It does not use imageline, imagefilledrectangle, and other graphic functions. this library should provide some objects, such as Line, Rectangle, and Oval, which can be used to create images. It should also support building larger complex objects or grouping objects.

Z-value sorting is supported.

The plotting program allows the painter to move the graphic objects up and down on the screen surface. This library should support the function of placing an object before and after other objects: it uses a z value to define the height of the object starting from the graphic plane. The later the object with a larger z value is drawn, the more objects with a smaller z value appear.

Provides viewport conversion.

Generally, the coordinate space of the data is different from that of the image. Basic Graph operations in PHP are performed on the coordinate plane of the image. This graphics library should support the viewport specification, so that you can specify the image in a coordinate system that programmers are familiar with, and can automatically scale to adapt to any image size.

Because there are many features, you will write code step by step to show how the code is constantly adding features.

   Basic knowledge

Let's first look at a graphical environment object and an interface called GraphicsObject, which is implemented using a Line class and is used to draw a Line. UML 1.

Figure 1. graphic environment and graphic object interface


The GraphicsEnvironment class saves the graphic objects and a set of colors, including the width and height. The saveAsPng method outputs the current image to a specified file.

GraphicsObject is a required interface for any graphic object. To start using this interface, you must use the render method to draw this object. It is implemented by a Line class. it uses four coordinates: the x value of start and end, and the y value of start and end. It also has a color. When render is called, this object draws a line of color from sx, sy to ex, ey specified by the name.

The code of this library is shown in listing 1.

Listing 1. basic graphics library

            
     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 list 2:

List 2. test code of the basic graphics library

            
     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 to different directions and have different colors. Then, the render method can draw them to the graphic plane. Finally, this code saves the image as test.png.

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

            % php test.php            %            

Figure 2 shows the format of the generated test.png file in Firefox.

. Simple graphic object testing



This is certainly not as beautiful as Mona Lisa, but it can meet the current work needs.
  Add dimension

Our first requirement-the ability to provide graphical objects-has been met, and now we should begin to meet the second requirement: you can use a z value to put an object above or below another object.

We can regard each z value as a surface of the original image. The elements are drawn in the order of minimum to maximum z values. For example, let's draw two graphic elements: a red circle and a black box. The z value of the circle is 100, while the z value of the black box is 200. This will place the circle behind the box, as shown in 3:

. Faces with different z values

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.