Example of using GD library to create round pie chart in PHP, gd round _php tutorial

Source: Internet
Author: User
Tags 0xc0

Example of using the GD library to create a round pie chart in PHP, GD round


In PHP, there are some simple image functions can be used directly, but most of the images to be processed, you need to compile PHP with the GD library. In addition to installing the GD library, additional libraries may be required in PHP, depending on which image format you need to support. GD Library can be downloaded in http://www.boutell.com/gd/free, different GD version support image format is not exactly the same, the latest GD library version supports GIF, JPEG, PNG, WBMP, XBM and other formats of image files, also supports some font libraries such as FreeType, Type 1. Functions in the GD library allow you to manipulate various points, lines, geometries, text, and colors, as well as create or read image files in multiple formats.

In PHP, the operation of image processing through the GD library is processed in memory first, and then exported to the browser or saved on the server's disk after the operation is completed. Creating an image should complete the 4 basic steps shown below.

① Create a canvas: All the drawing designs need to be done on a background image, and the canvas is actually a temporary area in memory that is used to store the information for the image. Future image operations will be based on this background canvas, which is managed similar to the canvas we use when drawing.

② drawing images: Once the canvas is created, you can use this canvas resource to set the color of the image, fill the canvas, Draw points, segment, various geometries, and add text to the image using various portrait functions.

③ output Image: After you finish drawing the entire image, you need to save the image in some format to the file specified by the server, or output the image directly to the browser to display to the customer. However, before the image output, be sure to use the header () function to send the CONTENT-TYPE notification browser, this time sending a picture is not text.

④ frees up resources: the contents of the canvas are no longer useful after the image is output. To conserve system resources, you need to be aware of all the memory resources occupied by the canvas in a timely manner.
Let's take a look at a very simple create image script. In the following script file image.php, you use the GD library to dynamically output a sector chart, as described in the previous four steps of drawing an image. The code looks like this:
Copy CodeThe code is as follows:
<?php
Create a canvas, return a variable $image of a resource type, and open a staging area in memory
$image = Imagecreatetruecolor (100, 100); Create a canvas size of 100x100

Set the desired color in the image, which is equivalent to the dye box that is prepared when painting
$white = Imagecolorallocate ($image, 0xFF, 0xFF, 0xFF); Assign the image color to white
$gray = Imagecolorallocate ($image, 0xC0, 0xC0, 0xC0); Assign the image color to gray
$darkgray = Imagecolorallocate ($image, 0x90, 0x90, 0x90); Assign the image a dark gray color
$navy = Imagecolorallocate ($image, 0x00, 0x00, 0x80); Assign the image a dark blue color
$darknavy = Imagecolorallocate ($image, 0x00, 0x00, 0x50); Assign a color to dark dark blue for an image
$red = Imagecolorallocate ($image, 0xFF, 0x00, 0x00); Assign a color red to an image
$darkred = Imagecolorallocate ($image, 0x90, 0x00, 0x00); Assign the image a dark red color

Imagefill ($image, 0, 0, $white); Fill the background color for the canvas background
Dynamically Create 3D effects
for ($i = five, $i >50; $i-) {//Loop 10 times to draw a stereo effect
Imagefilledarc ($image, $i, -160, $darknavy, Img_arc_pie);
Imagefilledarc ($image, $i, $darkgray, and Img_arc_pie);
Imagefilledarc ($image, $darkred, Img_arc_pie, $i, N, Max, Max, Max);
}

Imagefilledarc ($image, -160, Max, $navy, Img_arc_pie); Draw an elliptical arc and fill
Imagefilledarc ($image,----------------$gray, Img_arc_pie); Draw an elliptical arc and fill
Imagefilledarc ($image,-------------------$red, Img_arc_pie); Draw an elliptical arc and fill

Imagestring ($image, 1, 34.7%, $white); To draw a line of strings horizontally
Imagestring ($image, 1, A, 55.5%, $white); To draw a line of strings horizontally

Output a picture in GIF format 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 Browser
Imagedestroy ($image); Destroy Image release Resources
?>

By requesting the script directly from the browser, or by assigning the URL of the script to the SRC attribute of the IMG tag in the HTML, you can get the image result of the dynamic output, as shown in:

http://www.bkjia.com/PHPjc/914042.html www.bkjia.com true http://www.bkjia.com/PHPjc/914042.html techarticle PHP using the GD library to create a circular pie chart example, GD Circle in PHP, there are some simple image functions can be used directly, but most of the images to be processed, you need to compile PHP ...

  • 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.