Function generation image provided in php gd Library

Source: Internet
Author: User
Tags install php imagejpeg php server

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:
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 segment automatically detects the image types supported by the GD library to write better portability
PHP program. As follows:

The code is as follows: Copy code
<? 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.

Note: the gd Library in php is not enabled by default. We need to open it in php. ini. The specific steps are as follows:

Enable GD library support for PHP in Linux

The code is as follows: Copy code
# The following methods are supported to enable the GD Library:
# Check whether the GD Library has the Installation Command
Php5-m | grep-I gd
Or
Php-I | grep-I -- color gd
# If the GD library is not installed, install it on the server as follows:
### If the installation is source code, add the parameter
-- With-gd
### If it is a debian linux system, use apt-get to install it, as shown below:
Apt-get install php5-gd
### If it is a CentOS system, install it using yum, as shown below:
Yum install php-gd
### If the suse linux system is used, install it using yast, as shown below:
Yast-I php5_gd
### If this world is not enough, you can add

First, go to the zlib source code, libpng source code, and gd source code.
Decompress the package to the source code directory.
 

The code is as follows: Copy code
Zlib Directory
./Configure -- prefix =/usr/local/zlib
Make; make install
Make clean
Libpng Directory
Cp scripts/makefile. linux./makefile
./Configure -- prefix =/usr/local/libpng
Make; make install
Make clean
Gd Directory
./Configure -- prefix =/usr/local/libgd -- with-png =/usr/local/libpng
Make; make install
Make clean
 

Finally, find [gd] in php. ini and add a line below.

The code is as follows: Copy code
Extension =/usr/local/libgdgd. so

Restart the php service. If not, try reboot.

Okay, but I 'd like to remind you that there are many surprises in the world. For source code installation, only the gd Library is added. In this case, the PHP version and library version are different, so:
-There is no guarantee that there will be a return if you pay so much.
-It is not guaranteed that gd. so can be loaded successfully.
-No pregnancy is guaranteed

Therefore, if the source code is used for installation, it is best to add the parameter -- with-gd when compiling PHP.

Enable GD library support for PHP in Windows

Find php. ini, open the content, and find:

; Extension = php_gd2.dll

Remove the semicolon ";" at the beginning and save it. If there is no semicolon, it is enabled.

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.