Summary of several functions for generating images using the PHPGD Library

Source: Internet
Author: User
This article mainly introduces the Function Summary of generating images in the PHPGD Library, that is, imagegif, imagejpeg, imagepng, and imagewbmp, if you need an image, you can use the function provided in the GD library to dynamically draw the image. then, you need to output the image to your browser or save the image. In PHP, you can directly generate GIF, JPEG, PNG, and WBMP image formats based on the dynamically drawn canvas. You can call the following four functions to generate images in these formats:

Bool imagegif (resource $ image [, string $ filename]) // outputs the image in GIF format bool imagejpeg (resource $ image [, string $ filename [, int $ quality]) // output the image bool imagepng (resource $ image [, string $ filename]) in JPEG format // output the image bool imagewbmp (resource $ image [, string $ filename [, int $ foreground]) // output the image in WBMP format

The use of the above four functions is similar. the use of the first two parameters is the same. The first parameter $ image is required and is the image reference handle described earlier. If these functions provide other parameters, the original image is directly exported during access and dynamic output images are displayed in the browser. However, before the output, use the header () function to send the header information to notify the browser to parse the received content using the correct MIME type, let it know that we are sending an image without the HTML of text. The following code snippet automatically detects the image types supported by the GD library to write a PHP program with better portability. As follows:


If you want to save PHP dynamically drawn images to a local server, you must specify a file name string in the Second Optional parameter. In this way, not only does the image not be directly output to the browser, but header information does not need to be sent using the header () function. If you use the imageJPEG () function to generate an image in JPEG format, you can use the third optional parameter $ quality to specify the quality of the image in JPEG format, the value provided by this parameter is an integer ranging from 0 (the worst quality, but the smallest file) to 100 (the highest quality, the maximum file). The default value is 75. you can also provide the third optional parameter $ forground for the imageWBMP () function to specify the foreground color of the image. the default color value is black.

For more information about the function summary for generating images from the php gd library, see The PHP Chinese website!

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