GD Library (b)

Source: Internet
Author: User
Tags imagejpeg

PHP image Processing
1. Picture Background Management
Use the Imagecreate () and Imagecreatetruecolor () functions to create canvas resources. But if you already have a picture to work with, just use this image as a canvas resource, which is what we call creating a picture background.
Imagecreatefromjpeg ($path)
Imagecreatefrompng ($path)
Imagecreatefromgif ($path)

Get picture type width height etc getimagesize ($path)

2. Zoom in on the picture
Imagecopyresampled (Dst_img,src_img,dst_x,dst_y,src_x,src_y,dst_w,dst_h,src_w,src_h)
Custom Image Indent function:

1 functionThumb$filename,$width=200,$height=200){2 //get the width and height of the original image3 List($width _orig,$height _orig)=getimagesize($filename);4 5 //proportional to the ratio of width to height6 if($width&& ($width _orig<$height _orig)){7 $width=($height/$height _orig)*$width _orig;8}Else{9 $height=($width/$width _orig)*$height _orig;Ten } One  A $im=imagecreatetruecolor ($width,$height); - $image=imagecreatefromjpeg ($filename);//indent JPEG format -Imagecopyresampled ($im,$image, 0,0,0,0,$width,$height,$width _orig,$height _orig); the  -Imagejpeg ($im,$filename, 100); -  -Imagedestroy ($im); +Imagedestroy ($image); -}


3. Picture clipping
Custom clipping functions:

1 functionCut$filename,$x,$y,$width,$height){2 //Create a background resource3 $back=imagecreatefromjpeg ($filename);4 //Create save cropped resource5 $im=imagecreatetruecolor ($width,$height);6 //cropping7Imagecopyresampled ($im,$back, 0, 0,$x,$y,$width,$height,$width,$height);8 //overwrite the original, if you do not want to overwrite the additive prefix9Imagejpeg ($im,$filename);Ten  OneImagedestroy ($im); AImagedestroy ($back);


4. Add a watermark
Custom watermark Functions:

1 functionWatermark$filename,$water){2 //Get the width height of the background graph3 List($b _w,$b _h)=getimagesize($filename);4 //get the width and height of the watermark5 List($w _w,$w _h)=getimagesize($water);6 //The random position of the watermark in the background map7 $posx=Rand(0, ($b _w-$w _w));8 $posy=Rand(0, ($b _h-$w _h));9 //creating background and watermark ResourcesTen $back=imagecreatefromjpeg ($filename); One $water=imagecreatefrompng ($water); A //Add Watermark -Imagecopy ($back,$water,$posx,$posy, 0, 0,$w _w,$w _h); - //Save Watermark Map theImagejpeg ($back,$filename); - //Freeing Resources -Imagedestroy ($water); -Imagedestroy ($back); +}


5. Picture rotation
Custom Rotation functions:

1 functionRotate$filename,$degrees){2 //Create an image resource3 $im=imagecreatefromjpeg ($filename);4 //rotate by the specified angle5 $rotate=imagerotate ($im,$degrees, 0);6 //Save the post-rotation diagram7Imagejpeg ($im,$filename);8}






GD Library (b)

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.