PHP has a surprising ability-you can use its server-side script to create dynamic graphics. This function is based on the GD library, which is designed by Thomas Boutell ANSI C library, which is supported in addition to the library. Most common graphics file formats outside of GIF files (although the designer of the library has promised to add the LZW patent as soon as it expires on July 7, 2004. GIF support).
PHP4.3 and its newer version integrates the GD library. If you are using an older version of PHP, you will need to install graphics support manually. There is a lot of information about it here.
Line chart
To demonstrate how to create a dynamic graphic with PHP, we build some custom graphics. The first example is a line chart drawn on the grid, as shown in Figure A.
Figure A
We call this page grid.php (related accessories: Listing a). To invoke a dynamically generated graphic for a Web page, you only need to access the PHP page, which passes the graphic to the browser. The IMG element can do the job well. The following is an example code to implement this feature:
Now we're going to start writing code to create the graphic. The following is a source code fragment in grid.php:
?
Add the value of a graphic
$graphValues =array (0,80,23,11,190,245,50,80,111,240,55);
First, we define the value of the graph. In this example, the graphic value is written directly into an array in the code, but you can easily rewrite the code to get the values from the XML file, table, or database. These values range from 0 to 250 (graphic dimensions in pixels). These values determine the initial pixel position of the segment on each grid. If you want to use values 0 and 100 (expressed as percentages), you just multiply these values by 2.5来 to determine the pixel position on the grid.
We then send a PNG header and define the height and width of the image:
Define. PNG image
Header ("Content-type:image/png");
$imgWidth = 250;
$imgHeight = 250;
We send a graphics header to "trick" the browser so that it thinks our PHP page is a real image so it can be displayed correctly on the screen. The server sends the information generated by the program to the browser in the form of binary data streams.
PNG (Portable Network Graphic, Portable Network Graphics) standard is a lossless graphic format, which was proposed in 1995 due to the legal issue of GIF's LZW algorithm patent.
Now, we're going to instantiate the drawing object and define the colors we use in the graph:
Create an image, define a color
$image =imagecreate ($imgWidth, $imgHeight);
$colorWhite =imagecolorallocate ($image, 255, 255, 255);
$colorGrey =imagecolorallocate ($image, 192, 192, 192);
$colorBlue =imagecolorallocate ($image, 0, 0, 255);
We set the white background, the gray frame and the blue polyline. You can easily modify or add colors by creating new variables and assigning different RGB values.
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