Some functions of generating images in PHP GD library _php Tips

Source: Internet
Author: User
Tags imagejpeg php server

After using the functions provided in the GD library to dynamically draw the finished image, you need to output to the browser or save the image. In PHP, you can directly generate GIF, JPEG, PNG, and wbmp four image formats by dynamically drawing the finished canvas. You can generate images of these formats by calling the following four functions:

Copy Code code as follows:

BOOL Imagegif (Resource $image [, String $filename])//The image output in GIF format
BOOL Imagejpeg (Resource $image [, String $filename [, int $quality]]//image output in JPEG format
BOOL Imagepng (Resource $image [, String $filename])//The image is exported in PNG format
BOOL Imagewbmp (Resource $image [, String $filename [, int $foreground]]//output image in wbmp format

The above four functions are similar in use, and the first two parameters are used the same. The first parameter, $image, is the required option and is the image reference handle described earlier. If parts of these functions provide additional parameters, the access will flow directly out of the original image and display the dynamically exported image in the browser's use. But be sure to use the header () function to send header information before the output to tell the browser to parse the received content with the correct MIME type to let it know that we are sending the HTML of the picture rather than the text. The following code snippet writes a better ported PHP program by automatically detecting the types of images supported by the GD library. As shown below:

Copy Code code as follows:

<?php
if (function_exists ("Imagegif")) {//Determine if the function that generated the GIF format image exists
Header ("Content-type:image/gif"); Send header information set MIME type to Image/gif
Imagegif ($im); Print an image to a 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 ("Image not supported in PHP server");
}
?>

If you want to save a dynamically drawn image of PHP on the local server, you must specify a filename string in the second optional parameter. This will not only output the image directly to the browser, nor do you need to use the header () function to send header information. If you use the imagejpeg () function to generate images in JPEG format, you can also specify the quality of the JPEG format image by using the third optional parameter, which can provide a value from 0 (the worst quality, but the smallest file) to 100 (highest quality, file is also the largest) integer, The default value is 75. You can also provide a third optional parameter $forground for function imagewbmp (), specify the foreground color of the image, and the default color value is black.

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.