Use the gd library of PHP

Source: Internet
Author: User
Some simple image functions can be directly used in php, but most of the functions to be processed need to be added to the gd library during php compilation. Besides installing the gd Library,

Some simple image functions can be directly used in php, but most of the functions 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. Different gd versions support different image formats. The latest gd Library versions support image files in gif, jpeg, png, wbmp, xbm, and other formats, it also supports some library types such as freetype and type 1. Using functions in the gd library, you can operate and process various points, lines, Ry, text, and colors, or create or read image files in multiple formats.
In php, operations processed through the db database are first processed in the memory. after the operation is completed, the operations are output to the browser or saved to the server disk as a file stream. To create an image, follow these steps.
(1) create a canvas: All the drawing designs must be completed on a background image, and the canvas is actually a temporary area opened up in the memory. Used to store 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.
(2) Image Painting: After the canvas is created, you can use the canvas resources to set the image color, fill the canvas, painting points, line segments, and various geometric figures using various portrait functions, and add text to the image.
(3) output Image: After the entire image is drawn, you need to save the image to a file specified by the server in a certain number, or directly output the image to the browser for display to the user. 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.
(4) release resources: after an image is output, the content in the canvas is no longer useful. To save system resources, you need to clear all memory resources occupied by the canvas in time.
Canvas management
When using the gd library of php to process images, you must manage the canvas. Creating a canvas is to open up a storage area in the memory. in the future, all operations on the image in php are based on this canvas, and the canvas is an image resource. In php, you can use the imagecreate () and imagecreatetruecolor () functions to create the specified canvas. The two functions are used to create a canvas of the specified size. their prototype is as follows:

Resource imagecreate (int $ x_size, int $ y_size) // create a palette-based image.
Resource imagecreatetruecolor (int $ x_size, int $ y_size) // create a true color image


Although both functions can create a new canvas, their total numbers of colors are different. the imagecreate () function can create an image based on a normal palette, generally, 256 colors are supported. The imagecreatetruecolor () function can create a real-color image, but this function cannot be used in gif file format. After the canvas is created, an image identifier is returned, representing a blank image reference handle with a width of $ x_size and a height of $ y_size. In the subsequent drawing process, you need to use the resource type handle. For example, you can call the imagex () and imagey () functions to obtain the image size. The code is as follows:
$ Img = imagecreatetruecolor (300,200); // create a 300*200 canvas


Echo imagese ($ img); // The width of the output canvas is 300

Echo imagesy ($ img) // output canvas height 200


In addition, if the reference handle of the canvas is no longer used, the resource must be destroyed to release the memory and the storage unit of the image. The canvas destruction process is very simple. you can call the imagedestroy () function. The syntax format is as follows:

Bool imagedestroy (resource $ image) // destroy the image

If the method is called successfully, the memory associated with the parameter $ image is released. The parameter $ image is the image ID returned by the image creation function.

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.