Php gd library to generate image function summary, phpgd
After the image is dynamically drawn using the function provided in the GD library, it needs to be output to the browser or saved. 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:
Copy codeThe Code is as follows:
Bool imagegif (resource $ image [, string $ filename]) // outputs images in GIF format
Bool imagejpeg (resource $ image [, string $ filename [, int $ quality]) // outputs the image in JPEG format
Bool imagepng (resource $ image [, string $ filename]) // outputs the image in PNG format
Bool imagewbmp (resource $ image [, string $ filename [, int $ foreground]) // outputs 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:
Copy codeThe Code is as follows:
<? Php
If (function_exists ("imagegif") {// determines whether the function for generating GIF images exists.
Header ("Content-type: image/gif"); // set the MIME type to image/gif.
Imagegif ($ im); // output the image to the browser in GIF format
} Elseif (function_exists ("imageipeg ")){
Header ("Content-type: image/jpeg ");
Imagejpeg ($ im, "", 0.5 );
} Elseif (function_exists ("imagepng ")){
Header ("Content-type: image/png ");
Imagepng ($ im );
} Elseif (function_exists ("imagewbmp ")){
Header ("Content-type: image/wbmp ");
Imagewbmp ($ im );
} Else {
Die ("images are not supported on the PHP server ");
}
?>
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.