Summary of various php image processing function code

Source: Internet
Author: User
Tags image flip imagecopy imagejpeg transparent color
This article mainly introduces php's image processing implementation code, including scaling, cropping, scaling, flip, rotating, transparent, sharpening, and other image operations. For more information, see I. create image resources

Imagecreatetruecolor (width, height );
Imagecreatefromgif (image name );
Imagecreatefrompng (image name );
Imagecreatefromjpeg (image name); draw various image imagegif (image resource, save path );
Imagepng ()
Imagejpeg ();

II. retrieving image attributes

Imagesx (res // width
Imagesy (res // height
Getimagesize (file path)
Returns an array with four units. Index 0 contains the pixel value of the image width, and index 1 contains the pixel value of the image height. Index 2 is the mark of the image type: 1 = GIF, 2 = JPG, 3 = PNG, 4 = SWF, 5 = PSD, 6 = BMP, 7 = TIFF (intel byte order), 8 = TIFF (motorola byte order), 9 = JPC, 10 = JP2, 11 = JPX, 12 = JB2, 13 = SWC, 14 = IFF, 15 = WBMP, 16 = XBM. These tags correspond to the newly added IMAGETYPE constant in PHP 4.3.0. Index 3 is a text string with the content "height =" yyy "width =" xxx "and can be directly used for IMG marking.
Destroy Image resources
Imagedestroy (image resources );

3. transparent processing

PNG and jpeg transparent colors are normal, only gif is abnormal

Imagecolortransparent (resource image [, int color]) // sets a color to transparent.
Imagecolorstotal ()
Imagecolorforindex ();

IV. image cropping

Imagecopyresized ()
Imagecopyresampled ();

5. add watermarks (text and images)

String Encoding and conversion string iconv (string $ in_charset, string $ out_charset, string $ str)

VI. rotating images

Imagerotate (); // specifies the Image flip angle

VII. Image flip

Flip along x axis along y axis

8. sharpen

Imagecolorsforindex ()

Imagecolorat ()

Image $ img = imagecreatefromgif ("./images/map.gif ");

The code is as follows:


$ Red = imagecolorallocate ($ img, 255, 0, 0 );
Imageline ($ img, 0, 0,100,100, $ red );
Imageellipse ($ img, 200,100,100,100, $ red );
Imagegif ($ img, "./images/map2.gif ");
Imagedestroy ($ img );

Normal image scaling

The code is as follows:


$ Filename = "./images/hee.jpg ";
$ Per = 0.3;
List ($ width, $ height) = getimagesize ($ filename );
$ N_w = $ width * $ per;
$ N_h = $ width * $ per;

$ New = imagecreatetruecolor ($ n_w, $ n_h );

$ Img = imagecreatefromjpeg ($ filename );
// Copy and adjust part of the image

Imagecopyresized ($ new, $ img, 0, 0, 0, $ n_w, $ n_h, $ width, $ height );
// Output new image and save it

Imagejpeg ($ new, "./images/hee2.jpg ");

Imagedestroy ($ new );
Imagedestroy ($ img );


Proportional scaling of images, not processing transparent colors

The code is as follows:


Function thumn ($ background, $ width, $ height, $ newfile ){
List ($ s_w, $ s_h) = getimagesize ($ background); // Obtain the height and width of the original image.

If ($ width & ($ s_w <$ s_h )){
$ Width = ($ height/$ s_h) * $ s_w;
} Else {
$ Height = ($ width/$ s_w) * $ s_h;
}

$ New = imagecreatetruecolor ($ width, $ height );

$ Img = imagecreatefromjpeg ($ background );

Imagecopyresampled ($ new, $ img, 0, 0, 0, 0, $ width, $ height, $ s_w, $ s_h );

Imagejpeg ($ new, $ newfile );

Imagedestroy ($ new );
Imagedestroy ($ img );
}

Thumn ("images/hee.jpg", 200,200, "./images/hee3.jpg ");


Gif transparent color processing

The code is as follows:


Function thumn ($ background, $ width, $ height, $ newfile ){
List ($ s_w, $ s_h) = getimagesize ($ background );

If ($ width & ($ s_w <$ s_h )){
$ Width = ($ height/$ s_h) * $ s_w;
} Else {
$ Height = ($ width/$ s_w) * $ s_h;
}

$ New = imagecreatetruecolor ($ width, $ height );

$ Img = imagecreatefromgif ($ background );

$ Otsc = imagecolortransparent ($ img );
If ($ otsc> = 0 & $ otst <imagecolorstotal ($ img) {// you can determine the index color.
$ Tran = imagecolorsforindex ($ img, $ otsc); // Index color value

$ Newt = imagecolorallocate ($ new, $ tran ["red"], $ tran ["green"], $ tran ["blue"]);

Imagefill ($ new, 0, 0, $ newt );

Imagecolortransparent ($ new, $ newt );
}

Imagecopyresized ($ new, $ img, 0, 0, 0, 0, $ width, $ height, $ s_w, $ s_h );

Imagegif ($ new, $ newfile );

Imagedestroy ($ new );
Imagedestroy ($ img );
}

Thumn ("images/map.gif", 200,200, "./images/map3.gif ");


Crop Images

The code is as follows:


Function cut ($ background, $ cut_x, $ cut_y, $ cut_width, $ cut_height, $ location ){

$ Back = imagecreatefromjpeg ($ background );

$ New = imagecreatetruecolor ($ cut_width, $ cut_height );

Imagecopyresampled ($ new, $ back, 0, 0, $ cut_x, $ cut_y, $ cut_width, $ cut_height, $ cut_width, $ cut_height );

Imagejpeg ($ new, $ location );

Imagedestroy ($ new );
Imagedestroy ($ back );
}

Cut ("./images/hee.jpg", 440,140,117,112, "./images/hee5.jpg ");


Add watermarks to images

Text watermark

The code is as follows:


Function mark_text ($ background, $ text, $ x, $ y ){
$ Back = imagecreatefromjpeg ($ background );

$ Color = imagecolorallocate ($ back, 0,255, 0 );

Imagettftext ($ back, 20, 0, $ x, $ y, $ color, "simkai. ttf", $ text );

Imagejpeg ($ back, "./images/hee7.jpg ");

Imagedestroy ($ back );
}

Mark_text ("./images/hee.jpg", "PHP", 150,250 );

// Image watermark
Function mark_pic ($ background, $ waterpic, $ x, $ y ){
$ Back = imagecreatefromjpeg ($ background );
$ Water = imagecreatefromgif ($ waterpic );
$ W_w = imagesx ($ water );
$ W_h = imagesy ($ water );
Imagecopy ($ back, $ water, $ x, $ y, 0, 0, $ w_w, $ w_h );
Imagejpeg ($ back, "./images/hee8.jpg ");
Imagedestroy ($ back );
Imagedestroy ($ water );
}
Mark_pic ("./images/hee.jpg", "./images/gaolf.gif", 50,200 );

Image Rotation

The code is as follows:


$ Back = imagecreatefromjpeg ("./images/hee.jpg ");
$ New = imagerotate ($ back, 45, 0 );
Imagejpeg ($ new, "./images/hee9.jpg ");


Horizontal flip vertical flip

The code is as follows:


Function turn_y ($ background, $ newfile ){
$ Back = imagecreatefromjpeg ($ background );

$ Width = imagesx ($ back );
$ Height = imagesy ($ back );

$ New = imagecreatetruecolor ($ width, $ height );

For ($ x = 0; $ x <$ width; $ x ++ ){
Imagecopy ($ new, $ back, $ width-$ X-1, 0, $ x, 0, 1, $ height );
}

Imagejpeg ($ new, $ newfile );

Imagedestroy ($ back );
Imagedestroy ($ new );
}

Function turn_x ($ background, $ newfile ){
$ Back = imagecreatefromjpeg ($ background );

$ Width = imagesx ($ back );
$ Height = imagesy ($ back );

$ New = imagecreatetruecolor ($ width, $ height );

For ($ y = 0; $ y <$ height; $ y ++ ){
Imagecopy ($ new, $ back, 0, $ height-$ Y-1, 0, $ y, $ width, 1 );
}

Imagejpeg ($ new, $ newfile );

Imagedestroy ($ back );
Imagedestroy ($ new );
}

Turn_y ("./images/hee.jpg", "./images/hee11.jpg ");
Turn_x ("./images/hee.jpg", "./images/hee12.jpg ");


Image Sharpening

The code is as follows:


Function sharp ($ background, $ degree, $ save ){
$ Back = imagecreatefromjpeg ($ background );

$ B _x = imagesx ($ back );
$ B _y = imagesy ($ back );

$ Dst = imagecreatefromjpeg ($ background );
For ($ I = 0; $ I <$ B _x; $ I ++ ){
For ($ j = 0; $ j <$ B _y; $ j ++ ){
$ B _clr1 = imagecolorsforindex ($ back, imagecolorat ($ back, $ i-1, $ J-1); \ previous pixel color array
$ B _clr2 = imagecolorsforindex ($ back, imagecolorat ($ back, $ I, $ j); \ retrieves the current color array

$ R = intval ($ B _clr2 ["red"] + $ degree * ($ B _clr2 ["red"]-$ B _clr1 ["red"]); \ deepen
$ G = intval ($ B _clr2 ["green"] + $ degree * ($ B _clr2 ["green"]-$ B _clr1 ["green"]);
$ B = intval ($ B _clr2 ["blue"] + $ degree * ($ B _clr2 ["blue"]-$ B _clr1 ["blue"]);

$ R = min (255, max ($ r, 0); // limits the r range to 0.
$ G = min (255, max ($ g, 0 ));
$ B = min (255, max ($ B, 0 ));

If ($ d_clr = imagecolorexact ($ dst, $ r, $ g, $ B) =-1) {// equal to 1 is not in the color range
$ D_clr = Imagecolorallocate ($ dst, $ r, $ g, $ B); // create a color
}

Imagesetpixel ($ dst, $ I, $ j, $ d_clr );
}

}
Imagejpeg ($ dst, $ save );
Imagedestroy ($ back );
Imagedestroy ($ dst );
}

Sharp ("./images/hee.jpg", 20, "./images/hee13.jpg ");

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.