Home > Backend Development > PHP Tutorial > Example of using GD library to create a circular pie chart in PHP, gd circle_PHP tutorial

Example of using GD library to create a circular pie chart in PHP, gd circle_PHP tutorial

WBOY
Release: 2016-07-13 10:13:52
Original
965 people have browsed it

An example of using the GD library to create a circular pie chart in PHP, gd circle

In PHP, there are some simple image functions that can be used directly, but most of the images to be processed require the GD library when compiling PHP. In addition to installing the GD library, other libraries may be required in PHP, depending on which image formats need to be supported. The GD library can be downloaded for free at http://www.boutell.com/gd/. Different GD versions support different image formats. The latest GD library version supports images in GIF, JPEG, PNG, WBMP, XBM and other formats. files, and also supports some font libraries such as FreeType and Type 1. Through the functions in the GD library, you can complete the operation and processing of various points, lines, geometric figures, text and colors, and you can also create or read image files in various formats.

In PHP, the operation of processing images through the GD library is first processed in the memory. After the operation is completed, it is output to the browser in the form of a file stream or saved on the server's disk. Creating an image should be done in 4 basic steps as shown below.

 ①Create a canvas: All drawing designs need to be completed on a background image, and the canvas is actually a temporary area opened in memory to store image information. All future image operations will be based on this background canvas, and the management of this canvas is similar to the canvas we use when painting.

 ②Draw an image: After the canvas is created, you can use this canvas resource to set the color of the image, fill the canvas, draw points, line segments, various geometric figures, and add text to the image using various portrait functions.

 ③Output image: After completing the drawing of the entire image, the image needs to be saved in a certain format to a file specified by the server, or the image needs to be output directly to the browser to display to the customer. But before the image is output, you must use the header() function to send the Content-type to notify the browser. This time, the image is sent instead of text.

④Release resources: After the image is output, the content in the canvas is no longer useful. In order to save system resources, it is necessary to know all the memory resources occupied by the canvas in a timely manner.
Let’s first take a look at a very simple script to create an image. In the following script file image.php, follow the four steps of drawing an image introduced previously, and use the GD library to dynamically output a sector chart. The code looks like this:

Copy code The code is as follows:

//Create a canvas, return a resource type variable $image, and open a temporary area in memory
$image = imagecreatetruecolor(100, 100); //Create canvas size is 100x100

//Set the required color in the image, which is equivalent to the dye box prepared when painting
$white = imagecolorallocate($image, 0xFF, 0xFF, 0xFF); //Assign the color to the image to be white
$gray = imagecolorallocate($image, 0xC0, 0xC0, 0xC0); //Assign the color to the image as gray
$darkgray = imagecolorallocate($image, 0x90, 0x90, 0x90); //Assign the color to the image as dark gray
$navy = imagecolorallocate($image, 0x00, 0x00, 0x80); //Assign the color to the image as dark blue
$darknavy = imagecolorallocate($image, 0x00, 0x00, 0x50); //Assign the color to the image as dark blue
$red = imagecolorallocate($image, 0xFF, 0x00, 0x00); //Assign the color to the image as red
$darkred = imagecolorallocate($image, 0x90, 0x00, 0x00); //Assign the color to the image as dark red

Imagefill($image, 0, 0, $white); //Fill the canvas background with the background color
//Dynamic production of 3D effects
for ($i = 60; $i >50; $i--){ //Loop 10 times to draw a three-dimensional effect
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.7%', $white); //Draw a line of string horizontally
Imagestring($image, 1, 45, 35, '55.5%', $white); //Draw a line of string horizontally

//Output a GIF format 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 obtain dynamically output image results by directly requesting the script through the browser, or by assigning the URL of the script to the src attribute of the IMG tag in HTML, as shown in the figure below:

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/914042.htmlTechArticleAn example of using the GD library to create a circular pie chart in PHP, gd circle in PHP, there are some simple The image function can be used directly, but most images to be processed need to be compiled when PHP...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template