PHP 5.0 practical Ways to create graphics complete article 1th/3 page _php Tips

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

I divide the graphics editing program into two categories: one is the drawing program, which can be used to draw images in pixels one pixel. The other is a mapping program that provides a set of objects, such as lines, ellipses, and rectangles that you can use to combine into a large image, such as JPEG. The drawing program is ideal for pixel-level control. But for business graphics, a graphics program is a good way to do it, because most graphics are made up of rectangles, lines, and ellipses.

PHP's built-in mapping basic operations are very similar to the drawing program. They are very powerful for drawing images, but this is not appropriate if you want your image to be a collection of sets 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 the PHP V5.

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

   Target

Creating a Graphics object library consists of 3 main goals:

   switch from basic operation to object

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

can be sorted by Z-value

The drawing program allows the painter to move the drawing objects up and down on the surface of the picture. This library should support the ability to place an object before and after other objects: it uses a Z value to define the height at which the object starts from the drawing plane. The larger the Z-value object is drawn, the later it appears on those objects with a smaller Z value.

provides viewport conversions

Usually, the coordinate space of the data is different from the coordinate space of the image. The basic operation of graphics in PHP is to operate the coordinate plane of the image. This graphics library should support viewport specifications so that you can specify graphics in a coordinate system familiar to a programmer, and can scale automatically to fit any image size.

Because there are a lot of features here, you'll be writing code step-by-step to show how the code can add functionality.

   Basic Knowledge

Let's first look at a graphical environment object and an interface called Graphicsobject, which is implemented using a line class, which is used to draw lines. The UML is shown in Figure 1.

Figure 1. Graphics environment and Graphics 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 the interface that any drawing object must use. To start using this interface, all you have to do is use the Render method to draw the object. It is implemented by a line class that utilizes 4 coordinates: The start and end x values, the start and end y values. It also has a color. When Render is invoked, this object draws a line from Sx,sy to Ex,ey to draw a color specified by the name.

The code for 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); 
 The Public Function Getgraphicobject () {return $this->gdo; 
 The Public Function GetColor ($name) {return $this->colors[$name]; 
 The 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

 
<?php 
require_once ("glib.php"); 
$ge = new Graphicsenvironment (a); 
$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", 5, N,); 
$GOBJS []= New Line ("Blue", "M, 390, 380); 
" $GOBJS []= New Line ("Red"); 
$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 to the graphic plane. Finally, this code saves the image as Test.png.

In this article, you run this code using the following command-line interpreter, as follows:

 
% PHP test.php 
% 


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

Figure 2. Simple graphic Object Testing



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

[NextPage]

Adding dimensions

Our first requirement-the ability to provide graphics objects-has been met, and it's time to start meeting the second requirement: You can use a Z value to place an object above or below another object.

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

Figure 3. Faces with different Z-values


-We just need to change the z value to put the red circle above the black box. To achieve this, we need to have each graphicsobject have a Z () method that returns a number, which is the z value. Because you need to create different drawing objects (line, Oval, and Rectangle), you also need to create a basic class boxobject that all 3 other classes use to maintain the coordinates of the start and end points, z values, and the color of this 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. Graphics libraries that can process Z information

 
<?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); 
 The Public Function Getgraphicobject () {return $this->gdo; 
 The Public Function GetColor ($name) {return $this->colors[$name]; 
 The Public Function saveaspng ($filename) {imagepng ($this->gdo, $filename); } abstract class Graphicsobject {abstract public function RendeR ($ge); 
Abstract public Function z (); 
 The 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-& 
 GT;SX, $this->sy, $this->ex, $this->ey, $ge->getcolor ($this->color)); Class Rectangle extends Boxobject {public function render ($ge) {imagefilledrectangle ($ge->getgraphico 
 Bject (), $this->sx, $this->sy, $this->ex, $this->ey, $ge->getcolor ($this->color)); Class Oval extends Boxobject {public function render ($ge) {$w = $this-&Gt;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

 
<?php 
require_once ("glib.php"); 
function Zsort ($a, $b) 
{ 
 if ($a->z () < $b->z ()) return-1; 
 if ($a->z () > $b->z ()) return 1; 
 return 0; 
} 
$ge = new Graphicsenvironment (a); 
$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 Oval (+, "Red", X, M,); 
$GOBJS []= New Rectangle ("Black", M, N,); 
Usort ($GOBJS, "Zsort"); 
foreach ($gobjs as $gobj) {$gobj->render ($ge);} 
$ge->saveaspng ("Test.png"); 
? > 

Here are two things to watch out for. First, we add the process of creating the Oval and Rectangle objects, where the first parameter is the z value. The second is to call Usort, which uses the Zsort function to sort the drawing objects according to the z value.
Current 1/3 page 123 Next read the full text
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.