PHP 50 A practical way to create graphics complete article 1th/3 page

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.
Goal
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 interfaces

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 {privat  e $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
     
     

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:


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.

[NextPage]

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. Polygons with different Z-values

We just need to change the z-value to put the red circle above the black box. To do this, we need to have a Z () method for each graphicsobject, which returns a number, which is the z value. Because you need to create different graphical objects (line, Oval, and Rectangle), you also need to create a basic class Boxobject, which is used by the other 3 classes to maintain coordinates, z-values, and the color of the object (see Figure 4).
Figure 4. Add another dimension to the system: Z-value

The new code for this graphics library is shown in Listing 3:
Listing 3. A graphics library that can handle Z information

     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); Abstract public Function z ();  } abstract class Boxobject extends Graphicsobject {protected $color;  protected $sx;  protected $sy;  protected $ex;  protected $ey;  protected $z; Public Function __constRuct ($z, $color, $sx, $sy, $ex, $ey) {$this->z = $z;  $this->color = $color;  $this->sx = $sx;  $this->sy = $sy;  $this->ex = $ex;  $this->ey = $ey; } public Function Z () {return $this->z;}} Class Line extends Boxobject {public function render ($ge) {imageline ($ge->getgraphicobject (), $this->SX, $  This->sy, $this->ex, $this->ey, $ge->getcolor ($this->color)); }} class Rectangle extends Boxobject {public function render ($ge) {Imagefilledrectangle ($ge->getgraphicobject (  ), $this->sx, $this->sy, $this->ex, $this->ey, $ge->getcolor ($this->color));  }} class Oval extends Boxobject {public function render ($ge) {$w = $this->ex-$this->sx;  $h = $this->ey-$this->sy; Imagefilledellipse ($ge->getgraphicobject (), $this->sx + ($w/2), $this->sy + ($h/2), $w, $h, $ge-  >getcolor ($this->color));  }}?>

The test code also needs to be updated, as shown in Listing 4.
Listing 4. Updated test Code
     
     Z () < $b->z ()) return-1;  if ($a->z () > $b->z ()) return 1;  

There are two things to note here. First, we added the procedure for creating Oval and Rectangle objects, where the first parameter is the z-value. The second is called Usort, which uses the Zsort function to sort the graphical objects according to their z-values.

Current 1/3 page 123 next page

The above describes the PHP 50 to create a graphical practical method of the complete chapter 1th/3, including the aspects of the content, I hope to be interested in PHP tutorial friends helpful.

  • Related Article

    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.