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:
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: