Php image processing functions (recommended ). 1. create image resources imagecreatetruecolor (width, height); imagecreatefromgif (image name); imagecreatefrompng (image name); imagecreatefromjpeg (image name); and draw various images
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:
/**
* Image sharpening
*/
$ 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:
/**
* Image cropping
* Edit by www.jbxue.com
*/
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 text watermarks to images
The code is as follows:
/**
*
* Adding a text watermark to an image
*/
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:
/**
* Image rotation
*/
$ Back = imagecreatefromjpeg ("./images/hee.jpg ");
$ New = imagerotate ($ back, 45, 0 );
Imagejpeg ($ new, "./images/hee9.jpg ");
?>
Horizontal flip vertical flip
The code is as follows:
/**
* Horizontal flip vertical flip
*/
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 ");
?>
Imagecreatetruecolor (width, height); imagecreatefromgif (image name); imagecreatefrompng (image name); imagecreatefromjpeg (image name); draw various images...