Summary of several functions of image generation in PHP GD library

  • 2021-08-05 09:02:51
  • OfStack

Once the image is rendered dynamically using the functions provided in the GD library, you need to export it to the browser or save the image. In PHP, the dynamic canvas can be directly generated into GIF, JPEG, PNG and WBMP. Images in these formats can be generated by calling the following four functions:


   bool imagegif(resource $image[,string $filename])                              // With GIF Format to output the image
   bool imagejpeg(resource $image[,string $filename[,int $quality]])                        // With JPEG Format to output the image
   bool imagepng(resource $image[,string $filename])                                 // With PNG Format to output the image
   bool imagewbmp(resource $image[,string $filename[,int $foreground]])                      // With WBMP Format to output the image

The use of the above four functions is similar, and 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 will flow out directly when accessing, and the dynamic output image will be displayed in the browser. But 1 must use the header () function to send header information before outputting, which tells the browser to parse the received content with the correct MIME type, so that it knows that we are sending HTML that is pictures instead of text. The following code snippet automatically detects the image types supported by the GD library to write a more portable PHP program. As shown below:


<?php
    if(function_exists("imagegif")){                // Judgment generation GIF Is there a function to format the image
        header("Content-type:image/gif");           // Send header information setting MIME Type is image/gif
        imagegif($im);                              // With GIF Format to output images to browsers
    }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(" In PHP Images are not supported on the server ");
    }
?>

If you want the PHP dynamically drawn image to be saved on the local server, you must specify a file name string in the second optional parameter. This not only does not output the image directly to the browser, but also does not need to use the header () function to send header information. If you use the imageJPEG () function to generate an image in JPEG format, You can also specify the quality of an JPEG format image with a third optional parameter $quality, which can provide an integer from 0 (worst quality, but smallest file) to 100 (highest quality, also largest file), with a default value of 75. You can also provide a third optional parameter $forground for the function imageWBMP (), which specifies the foreground color of the image, with a default color value of black.


Related articles: