Example of using the GD library in PHP to create a circular pie chart: gd circle
Some simple image functions can be directly used in PHP, but most of the images to be processed need to be added to the GD library during PHP compilation. In addition to installing the GD library, other libraries may be required in PHP, depending on the image formats supported. The GD library can be used in the library such as step 1. Using Functions in the GD library, you can perform operations on various points, lines, ry, text, and colors, or create or read image files in multiple formats.
In PHP, image processing through the GD library is performed in the memory first, and then output to the browser or stored on the server disk as a file stream after the operation is completed. To create an image, follow these steps.
① Create a canvas: All drawing designs must be completed on a background image, and the canvas is actually a temporary area opened up in the memory for storing image information. In the future, image operations will be based on this background canvas. The management of this canvas is similar to the canvas we use for painting.
② Draw an image: After the canvas is created, you can use the canvas resources to set the image color, fill the canvas, draw points, line segments, and various geometric figures using various portrait functions, and add text to the image.
③ Output image: After the entire image is drawn, you need to save the image to a file specified by the server in a certain format, or directly output the image to the browser for display to the customer. Before the image is output, use the header () function to send the Content-type notification to the browser. This time, the image is not text.
④ Release resources: After an image is output, the content in the canvas is no longer useful. To save system resources, you need to know all the memory resources occupied by the canvas in a timely manner.
Let's take a look at a very simple image creation script. In the following script file image. php, use the GD library to dynamically output a sector statistical chart based on the four steps described above. The Code is as follows:
Copy codeThe Code is as follows:
<? Php
// Create a canvas, return a resource type variable $ image, and open a temporary area in the memory
$ Image = imagecreatetruecolor (100,100); // the size of the canvas to be created is 100x100.
// Set the color required in the image, which is equivalent to the dye box prepared during painting.
$ White = imagecolorallocate ($ image, 0xFF, 0xFF, 0xFF); // assign a white color to the image
$ Gray = imagecolorallocate ($ image, 0xC0, 0xC0, 0xC0); // assign a gray color to the image
$ Darkgray = imagecolorallocate ($ image, 0x90, 0x90, 0x90); // assign a dark gray color to the image
$ Navy = imagecolorallocate ($ image, 0x00, 0x00, 0x80); // assign a dark blue color to the image
$ Darknavy = imagecolorallocate ($ image, 0x00, 0x00, 0x50); // assign a dark blue color to the image
$ Red = imagecolorallocate ($ image, 0xFF, 0x00, 0x00); // assign a red color to the image
$ Darkred = imagecolorallocate ($ image, 0x90, 0x00, 0x00); // assign a dark red color to the image
Imagefill ($ image, 0, 0, $ white); // fill the background color with the canvas background
// Dynamically create 3D Effects
For ($ I = 60; $ I> 50; $ I --) {// draw the stereoscopic effect 10 times in a loop
Imagefilledarc ($ image, 50, $ I, 100, 50,-160, 40, $ darknavy, IMG_ARC_PIE );
Imagefilledarc ($ image, 50, $ I, 100, 50, 40, 75, $ darkgray, IMG_ARC_PIE );
Imagefilledarc ($ image, 50, $ I, 100, 50, 75,200, $ darkred, IMG_ARC_PIE );
}
Imagefilledarc ($ image, 50, 50,100, 50,-160, 40, $ navy, IMG_ARC_PIE); // draw an elliptical arc and fill it
Imagefilledarc ($ image, 50, 50,100, 50, 40, 75, $ gray, IMG_ARC_PIE); // draw an elliptical arc and fill it
Imagefilledarc ($ image, 50, 50,100, 50, 75,200, $ red, IMG_ARC_PIE); // draw an elliptical arc and fill it
Imagestring ($ image, 1, 15, 55, '34. 100', $ white); // draw a string horizontally.
Imagestring ($ image, 1, 45, 35, '55. 000000', $ white); // draw a string horizontally.
// Output a GIF image to the browser
Header ('content-type: image/png '); // use the header function to tell the browser to process the following output as an image
Imagepng ($ image); // output to the browser
Imagedestroy ($ image); // destroy the image to release resources
?>
You can directly request the script through a browser, or assign the URL of the script to the src attribute marked by IMG in HTML to obtain the dynamic output image results, as shown in: