Common functions related to image processing in the PHPGD Library (2)

Source: Internet
Author: User
Tags color identifier image flip imagecopy imagejpeg transparent color
In this section, the main idea is to summarize some processing of the special format (png.jpg, gif) images. Application Scenario: process the user's uploaded avatar, process uploaded images ...... Imagecreatefromgif (); imagecreatefrompng (); imagecreatefromjpeg (); The above three functions indicate opening an image resource. The parameter is used to input the corresponding image address .? Php $ im

This section mainly summarizes some processing of specific images (png. jpg, gif. Application Scenario: process the user's uploaded avatar, process uploaded images ...... Imagecreatefromgif (); imagecreatefrompng (); imagecreatefromjpeg (); the preceding three functions indicate opening an image resource. The parameter is used to input the corresponding image address. ? Php $ im

This section mainly summarizes the processing of images in a specific format (png. jpg, gif. Application Scenario: process the user's uploaded avatar, process uploaded images ......

Imagecreatefromgif ();

Imagecreatefrompng ();

Imagecreatefromjpeg ();

The above three functions indicate opening an image resource. The parameter is to input the corresponding image address.

 '; // Obtain the image width. A resource parameter is required to obtain echo 'height:', imagesy ($ img ),'
'; $ Arr = getimagesize ('. /clock.gif '); // you can obtain the image information by using a file name: print_r ($ arr); // subscript 2 indicates the image type echo'
Width: ', $ arr [0],'
'; // Obtain the image width echo 'height:', $ arr [1],'
'; Imagegif ($ img,'./dealed/clock.gif '); imagedestroy ($ img);?>
The output result of the above Code in the browser is:

Width: 550
Height: 400
Array ([0] => 550 [1] => 400 [2] => 1 [3] => width = "550" height = "400" [bits] => 7 [channels] => 3 [mime] => image/gif)
Width: 550
Height: 400

I. Image Scaling and transparency Processing

 

The above code will have a scaled image under the dealed directory.

The following code scales proportionally:

 $ Ratio) // compare the maximum output image ratio with the original image ratio {$ width = $ height * $ ratio; // Description: The height is large.} else {$ height = $ width/$ ratio; // Description: The width is large.} $ new_s = imagecreatetruecolor ($ width, $ height ); $ img = imagecreatefromjpeg ($ source); imagecopyresampled ($ new_s, $ img, 0, 0, 0, 0, $ width, $ height, $ source_w, $ source_h ); imagejpeg ($ new_s, $ newFileName, 100); imagedestroy ($ new_s); imagedestroy ($ img);} // in this example, an image resample is displayed with a maximum width and height of 200 pixels. _ Pic ('./fire.jpg ', 200,200,'./dealed/fire1.jpg ');?>
Imagecopyresampled (): uses the interpolation algorithm to smoothly insert images, which is slower than imagecopyresized (). If you do not have high requirements on thumbnails, you can use imagecopyresized ().

However, this problem occurs when you scale an image in gif format, the transparency of the scaled gif image is not normal, while that of the png and jpeg images is normal. So how to deal with GIF images? Check the Code:

 $ Ratio) // compare the maximum output image ratio with the original image ratio {$ width = $ height * $ ratio; // higher description} else {$ height = $ width/$ ratio; // large description}/*** imagecolortransparent (resource $ image [, int color]) to define a color as transparent. If color is omitted, the identifier of the current transparent color is returned, * and int imagecolorat (resource $ image, int $ x, int $ y) the returned value is the same as * imagecolorstotal (resource $ image). The number of colors in the color palette of an image is obtained. * imagecolorsforindex (resource $ image, int $ index) gets the color of an index, this function returns The associated array of key names of d, green, blue, and alpha * contains the corresponding values of the specified color index. **/$ new_s = imagecreatetruecolor ($ width, $ height ); $ img = imagecreatefromgif ($ source); // Solution to the Problem of opacity after the GIF image is scaled: $ trans_index = imagecolortransparent ($ img ); // returns the if ($ trans_index> = 0 & $ trans_index <imagecolorstotal ($ img) transparent color identifier of the current image )) {// the original image has a transparent color $ color_index = imagecolorsforindex ($ img, $ trans_index); // obtain the rgb value of the transparent color // print_r ($ color_index); // specify the transparent color $ Trans_color = imagecolorallocate ($ img, $ color_index ['red'], $ color_index ['green'], $ color_index ['blue']); imagefill ($ new_s, 0, 0, $ trans_color); // fill in the background color, but not transparent imagecolortransparent ($ new_s, $ trans_color); // specify transparent color for the image} imagecopyresampled ($ new_s, $ img, 0, 0, 0, 0, $ width, $ height, $ source_w, $ source_h); imagegif ($ new_s, $ newFileName, 100); imagedestroy ($ new_s ); imagedestroy ($ img);} // In this example, the maximum width and height are 200 pixels display an image resample_pic ('./heben.gif', 200,200, './dealed/heben.gif');?>

Ii. Image Cropping

 

3. Add watermarks (text and images)

Add a text watermark:

 

Effect:


Add an image watermark:
 

:


Iv. Image Rotation
 

The effect is as follows.



5. Image flip

Along the Y axis:

 
Effect:


Along the X axis:

 

Effect


Vi. image effects

BoolImagefilter(Resource$src_im, Int$filtertype[, Int$arg1[, Int$arg2[, Int$arg3])

Visit more special effects, see http://www.php.net/manual/zh/function.imagefilter.php

 
Figure:


Finally, let's look at several interesting functions:

 '; Imagefill ($ img, 0, 0, $ hotpink); $ index_tran = imagecolortransparent ($ img, $ hotpink); // define a color as transparent echo $ index_tran ,'
'; $ Index_at = imagecolorat ($ img, 200,200); // obtain the color index value of the specific position pixel echo $ index_tran ,'
'; $ Index_exact = imagecolorexact ($ img, 255,105,180); // obtain the index value of the specified color echo $ index_exact; imagepng ($ img ,'. /test.png '); imagedestroy ($ img);?>
The output result is as follows:

16738740
16738740
16738740
16738740
Can we see their similarities?

The above are some of the exercises I have learned about the php GD extension library. If you have any questions, please give me more advice.

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.