A clever way to create graphics using PHP 5.0 _php Tutorial

Source: Internet
Author: User
Tags getcolor
This article shows you how to use PHP to build an object-oriented graphics layer. Using an object-oriented system can be used to build complex graphics, which is much simpler to build than using the basic functionality provided in a standard PHP library.

I have divided the graphics editing program into two categories: one is a drawing program, using this program can be a pixel to draw the image, and the other is a cartographic program, which provides a set of objects, such as lines, ellipses and rectangles, you can use these objects to combine into a large image, such as JPEG. The drawing program is ideal for pixel-level control. But for business graphics, cartographic programs are a good way, because most of the graphics are made up of rectangles, lines, and ellipses.

PHP's built-in cartographic basic operations are very similar to drawing programs. They are very powerful for drawing images, but this is not appropriate 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 easy to understand and maintain. You may also want to combine graphics into multiple types of media from a single graphical source: Flash movies, SVG, and more.

   Target

Creating a drawing Object library consists of 3 main goals:

   switch from basic operation to object

It does not use Imageline, Imagefilledrectangle, and other graphical functions, and the 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 group objects.

Z-values can be sorted

The charting program allows the painter to move the graphic object up and down on the surface of the screen. This library should be able to support the ability to put an object before and after other objects: it uses a Z-value to define the height at which the object starts from the cartographic plane. Objects with a higher Z-value are drawn later, and appear on objects with smaller Z-values.

provides conversion of viewport

In general, the coordinate space of the data is different from the coordinate space of the image. The basic operation of graphics in PHP is to manipulate the coordinate plane of an image. This graphics library should support the viewport specification so that you can specify graphics in a familiar coordinate system for programmers and can automatically scale to fit any image size.

Because there are many features here, you'll step through the code to show how the code is constantly adding functionality.

   Basic Knowledge

Let's start by looking at a graphical environment object and an interface named Graphicsobject, which is implemented using a line class that is used to draw lines. As shown in UML 1.

Figure 1: Graphical Environment and graphical 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 an interface that any graphical object must use. To get started with this interface, all you need to do is draw the object using the Render method. It is implemented by a line class that takes advantage of 4 coordinates: The start and end of the X value, the start and end of the Y value. It also has a color. When Render is called, the object draws a line of color specified by the name from Sx,sy to Ex,ey.

The code for 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; 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:

Listing 2: Test code for 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", 5, +, +);            $GOBJS []= New Line ("Blue", Max, 390, 380);            $GOBJS []= New Line ("Red", "Max", "ten");            $GOBJS []= New Line ("Green", 5, 390, 390, ten);            foreach ($gobjs as $gobj) {$gobj->render ($ge);}            $ge->saveaspng ("Test.png");            ? >            

This test program creates a graphical environment. Then create a few 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, you use the following command-line interpreter to run this code, as follows:

            % PHP test.php            %            

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

Figure 2: Simple graphical object Testing



This is certainly not as beautiful as the Mona Lisa, but it can meet the current job needs.
  Add a number of dimensions

Our first requirement-the ability to provide graphical objects-has been met and should now begin to meet the second requirement: You can use a Z-value to place an object above or below other objects.

We can treat each Z-value as a polygon of the original image. The elements drawn are drawn in the order of the z-values from the smallest to the largest. For example, let's draw two graphical elements: a red circle and a black box. The z-value of the circle is 100, and the z-value of the black box is 200. This puts the circle after the box, as shown in 3:

Figure 3. Faces with different Z-values

http://www.bkjia.com/PHPjc/508467.html www.bkjia.com true http://www.bkjia.com/PHPjc/508467.html techarticle This article shows you how to use PHP to build an object-oriented graphics layer. Using an object-oriented system can be used to build complex graphics, rather than using the basic functionality provided in standard PHP libraries ...

  • 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.