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 in addition to the GD library. In addition to installing the GD library, additional libraries may be required in PHP, depending on which image formats you need to support. GD Library can be downloaded free of charge in http://www.boutell.com/gd/, different GD version support image format is not exactly the same, the latest version of the GD library support GIF, JPEG, PNG, WBMP, XBM and other formats of image files, It also supports some font libraries such as FreeType, Type 1, and so on. Through the functions in the GD library, you can complete various points, lines, geometry, text and color operation processing, you can also create or read multiple formats of image files.
In PHP, the operation of image processing through the GD library is first processed in memory, then output to the browser or saved in the server's disk after the operation is completed. Creating an image should complete the 4 basic steps shown below.
① Create canvas: All drawing designs need to be done on a background image, and the canvas is actually a temporary area in memory to store the image information. Future image operations will be based on this background canvas, which is managed similar to the canvas we use when drawing.
② drawing images: After the canvas is created, you can use the various image functions to set the color of the images, fill the canvas, Draw points, line segments, various geometries, and add text to the image.
③ output Image: After the entire image is drawn, you need to save the image in some format to the server specified file, or directly output the image 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 send a picture is not text.
④ frees up resources: After an image is exported, the contents of the canvas are no longer useful. To conserve system resources, we need to be aware of all the memory resources occupied by the canvas in time.
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 pie chart by using the four steps of the image you described earlier. The code looks like this:
Copy Code code as follows:
<?php
Creates a canvas, returns a variable $image of a resource type, and opens a staging area in memory
$image = Imagecreatetruecolor (100, 100); Create a canvas size of 100x100
Set the desired color in the image, equivalent to the dye box prepared in the drawing
$white = Imagecolorallocate ($image, 0xFF, 0xFF, 0xFF); Assign a color to a white image
$gray = Imagecolorallocate ($image, 0xc0, 0xc0, 0xc0); Assign a color to the image to be gray
$darkgray = Imagecolorallocate ($image, 0x90, 0x90, 0x90); Assign a color to an image to be dark gray
$navy = Imagecolorallocate ($image, 0x00, 0x00, 0x80); Assign color to dark blue for an image
$darknavy = Imagecolorallocate ($image, 0x00, 0x00, 0x50); Assign a color to the image dark dark blue
$red = Imagecolorallocate ($image, 0xFF, 0x00, 0x00); Assign a color to a red image
$darkred = Imagecolorallocate ($image, 0x90, 0x00, 0x00); Assign a color to a dark red image
Imagefill ($image, 0, 0, $white); Fill background color for canvas background
Dynamic production of 3D effects
for ($i = $i >50; $i-) {//Loop 10 times draw three-dimensional effect
Imagefilledarc ($image, $i, M, -160, $darknavy, Img_arc_pie);
Imagefilledarc ($image, $i, M, N, $darkgray, Img_arc_pie);
Imagefilledarc ($image, $i, M, N, $darkred, Img_arc_pie);
}
Imagefilledarc ($image, M, -160, $navy, Img_arc_pie); Draw an elliptical arc and fill
Imagefilledarc ($image, $gray, M, Img_arc_pie);; Draw an elliptical arc and fill
Imagefilledarc ($image, N, $red, Img_arc_pie), and Draw an elliptical arc and fill
Imagestring ($image, 1, MB, ' 34.7% ', $white); To draw a line of strings horizontally
Imagestring ($image, 1,,, ' 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 in image mode
Imagepng ($image); Output to Browser
Imagedestroy ($image); Destroying image release resources
?>
Either request the script directly from the browser, or assign the URL of the script to the SRC attribute of the IMG tag in HTML, you can get the dynamic output image results as shown in the following illustration: