Graph processing using PHP and GD

Source: Internet
Author: User
Tags color identifier
As more and more web sites begin to work with databases in part of their designs, we can use the data collected in the database to display statistical results. Today, many web sites are interested in using certain polls or voting applications. Displaying data in a table is one thing, but creating a data graph is another thing. Of course, as more and more web sites begin to integrate with the database in some of their designs, we can use the data collected in the database.

Displays statistics. Today, many web sites are interested in using certain polls or voting applications. Displaying data in a table is one thing,

However, creating a data graph is another thing. Of course, we can use tables to display available Bar images together,

But how do we create a line chart or pie chart? PHP has a series of graphic functions that allow us to create images for us to see

First, what are the advantages of these functions for using and creating some images.



Note, however, that graph functions in PHP require your system to install the GD graphics library. You can find it at www.boutell.com/gd,

You can also get some guidance on how to install on your system.



Note: I wrote this article on the Red Hat Linux 6.2 machine, and then ended it on Windows 2000 machine. In Red

Hat I am running the latest PHP and MySQL versions, but on Windows I have to go back to MySQL 3.21.29 and PHP 3.0.11 versions,

Because it has everything I want to deal with-SQL and PHP run does not need to be modified. Here, let's not put the whole thing in hand

On the cross platform, OK?



To keep the data simple and allow us to focus on creating graphs, I use a small hypothetical dataset-

International company sales indicators for the first six months of this year. I am based on one office located in London and the other in Atlanta.





Month 1 Month 2 Month 3 Month 4 Month 5 Month 6

London 325 345 400 390 370 320

Atlanta 300 280 270 300 350 410

Sales indicators for the first six months



Then I input the data into a MySQL database. I include a data unload below:



# MySQL dump 7.1

#

# Host: localhost Database: graphing

#--------------------------------------------------------

# Server version 3.22.32



#

# Table structure for table 'sales'

#

Create table sales (

G_id int (11) DEFAULT '0' not null auto_increment,

G_month tinyint (4) DEFAULT '0' not null,

G_team tinytext not null,

G_num int (11) DEFAULT '0' not null,

Primary key (g_id)

);



#

# Dumping data for table 'sales'

#



Insert into sales VALUES (1, 1, 'London ', 325 );

Insert into sales VALUES (300, 'Atlanta );

Insert into sales VALUES (345, 'London );

Insert into sales VALUES (280, 'Atlanta );

Insert into sales VALUES (400, 'London );

Insert into sales VALUES (6, 3, 'Atlanta ', 270 );

Insert into sales VALUES (390, 'London );

Insert into sales VALUES (300, 'Atlanta );

Insert into sales VALUES (9, 5, 'London, 370 );

Insert into sales VALUES (350, 'Atlanta );

Insert into sales VALUES (320, 'London );

Insert into sales VALUES (410, 'Atlanta );



My database is called graphing, and the table containing data is called sales. I use a month as an integer in the g_month field.



Before we start plotting, we should first learn some basic knowledge to see how PHP creates an image. The first thing we need to do is

Is to tell the browser that it is getting an image and the type of the image:






Header ("Content-type: image/gif ");



?>



Now that the browser knows that it is getting a GIF image, we can start creating the image. First, we need to create

The blank canvas of the drawing. The ImageCreate function can do this. ImageCreate returns the identifier of an image.

The V. s function uses pixels to calculate the size of the canvas, x (width) and y (height ).






$ Image = imagecreate (200,200 );



?>



Now we get a blank canvas where 200 pixels multiply by 200 pixels and can be drawn. The next step is to create some

Color. To do this, we need to use the ImageColorAllocate function and the color RGB value. ImageColorAllocate

Returns a new color identifier. We will use the color identifier when drawing on the canvas. How does ImageColorAllocate work?

We need to assign a color for each processed image -- so if we create 3 GIF images and use red on each image

Should be assigned three times in red (one GIF at a time ). I will assign a color named $ maroon. the given red value is 100, and the green value is 0 and

Blue is 0. At the same time, I will create white.






$ Maroon = ImageColorAllocate ($ image, 100,0, 0 );

$ White = imagcolorallocate ($ image, 255,255,255 );



?>



Now that we have the color, we can draw something. The first thing to do is to paint the canvas white. Function

ImageFilledRectangle draws a rectangle on the canvas and fills it with the specified color.






ImageFilledRectangle ($ image, 0, 0, 200,200, $ white );



?>



The first thing that tells ImageFilledRectangle is (also used for all image functions) which image is being processed,

Therefore, the $ image ID is passed to it. Then it needs to know the x and y coordinates to start to draw the rectangle (200 -- upper left corner) and the coordinate of the ending rectangle,

200 -- bottom right corner of the canvas ). The last thing we tell it is the color filled in the rectangle. This example is $ maroon. Now we can

Began drawing on the white background.






ImageRectangle ($ image, 10, 10, 190,190, $ maroon );

ImageFilledRectangle ($ image, 50, 50, 150,150, $ maroon );



?>



ImageRectangle works exactly the same way as ImageFilledRectangle, except that it does not use color-filled rectangles. Once

After drawing, we can output the image --






ImageGIF ($ image );



?>



Then, clear the images that exist in the memory.






ImageDestroy ($ image );



?>



This is what we get:







However, it is not a required graph.



(To be continued)

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.