A clever way to create a graph using PHP 5.0

Source: Internet
Author: User
Tags getcolor php source code

Introduction: This is a detailed page for using PHP 5.0 to create graphics. It introduces PHP, related knowledge, skills, experience, and some PHP source code.

Class = 'pingjiaf' frameborder = '0' src = 'HTTP: // biancheng.dnbc?info/pingjia.php? Id = 323796 'rolling = 'no'>
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.

Edit the imageProgramThere are two types: graphics programs, which can be used to draw images in one pixel and one pixel, and graphics programs, which provide 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 graphicsCodeIt 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

<? 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, $ File Name) ;}} 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-> getgraphi Cobject (), $ this-> Sx, $ this-> Sy, $ this-> ex, $ this-> ey, $ Ge-> getcolor ($ this-> color) ;}}?>

More articles on "clever ways to create images using PHP 5.0"

Love J2EE follow Java Michael Jackson video station JSON online tools

Http://biancheng.dnbcw.info/php/323796.html pageno: 15.

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.