PHP cropping pictures

Source: Internet
Author: User
Tags encode string

PHP cropping pictures

<?PHP/** * Image clipping * @param $title string Original path * @param $content the width of the string to be cropped * @param $encode string needs to be cropped high*/functionImagecropper ($source _path,$target _width,$target _height){$source _info=getimagesize($source _path);$source _width=$source _info[0];$source _height=$source _info[1];$source _mime=$source _info[' MIME '];$source _ratio=$source _height/$source _width;$target _ratio=$target _height/$target _width;//source graph too highif($source _ratio>$target _ratio){$cropped _width=$source _width;$cropped _height=$source _width*$target _ratio;$source _x= 0;$source _y= ($source _height-$cropped _height)/2;}//source map too wideElseIf($source _ratio<$target _ratio){$cropped _width=$source _height/$target _ratio;$cropped _height=$source _height;$source _x= ($source _width-$cropped _width)/2;$source _y= 0;}//Moderate Source GraphElse{$cropped _width=$source _width;$cropped _height=$source _height;$source _x= 0;$source _y= 0;}Switch($source _mime){ Case' Image/gif ':$source _image= Imagecreatefromgif ($source _path); Break; Case' Image/jpeg ':$source _image= Imagecreatefromjpeg ($source _path); Break; Case' Image/png ':$source _image= Imagecreatefrompng ($source _path); Break;default:return false; Break;}$target _image= Imagecreatetruecolor ($target _width,$target _height);$cropped _image= Imagecreatetruecolor ($cropped _width,$cropped _height);//croppingImagecopy ($cropped _image,$source _image, 0, 0,$source _x,$source _y,$cropped _width,$cropped _height);//ZoomImagecopyresampled ($target _image,$cropped _image, 0, 0, 0, 0,$target _width,$target _height,$cropped _width,$cropped _height);//Save the picture locally (choose both)//$randNumber = Mt_rand (00000, 99999). Mt_rand (999);//$fileName = substr (MD5 ($randNumber), 8, 16) .". PNG ";//imagepng ($target _image, './'. $fileName);//imagedestroy ($target _image);//output pictures directly in the browser (choose one)Header(' Content-type:image/jpeg '); Imagepng ($target _image); Imagedestroy ($target _image); Imagejpeg ($target _image); Imagedestroy ($source _image); Imagedestroy ($target _image); Imagedestroy ($cropped _image);}//call//imagecropper ('./img033.jpg ', 300,300);Imagecropper ('./img033.jpg ', 140,140);//imagecropper ('./img033.jpg ', 55,55);

PHP cropping pictures

PHP cropping pictures

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.