(Advanced) php image processing functions and Code examples

Source: Internet
Author: User
Tags image flip imagecopy
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. I. create image resources

Imagecreatetruecolor (width, height); imagecreatefromgif (image name); Watermark (image name); imagecreatefromjpeg (image name); draw various image imagegif (image resource, storage 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]) // set 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:

 


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 some images and adjust imagecopyresized ($ new, $ img, 0, 0, $ n_w, $ n_h, $ width, $ height ); // output a new image and save it as 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, $ 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 )) {// Determine the index color $ tran = imagecolorsforindex ($ img, $ otsc); // The 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:

  


Add text watermarks to images

The code is as follows:

  


Image Rotation

The code is as follows:

  


Horizontal flip vertical flip

The code is as follows:

  

The above is (Advanced article) the content of php image processing functions and code examples. For more information, see PHP Chinese website (www.php1.cn )!

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.