We often encounter the image of the cutting action, the following code is the source of clipping
The processing methods are:
1. When the width or height of the original image is smaller than the specified size, only the same as the thumbnail processing,
2. When the original image width and height are larger than the specified size, first of equal than the abbreviated processing, and then calculate the center position for cropping
/* * $o _photo Original path * $d _photo after processing picture path * $width definition Width * $height Define High * Call method Cutphoto ("t Est.jpg "," temp.jpg ", 256,146); */function Cutphoto ($o _photo, $d _photo, $width, $height) {$temp _img = imagecreatefromjpeg ($o _photo); $o _width = imagesx ($temp _img); Obtain the original width $o _height = Imagesy ($temp _img); Obtain the original image Height//Judgment processing method if ($width > $o _width | | $height > $o _height) {//Huantuquan or higher than the specified size, compression $newwidth = $o _ Width $newheight = $o _height; if ($o _width > $width) {$newwidth = $width; $newheight = $o _height * $width/$o _width; if ($newheight > $height) {$newwidth = $newwidth * $height/$newheight; $newheight = $height; }//thumbnail image $new _img = Imagecreatetruecolor ($newwidth, $newheight); Imagecopyresampled ($new _img, $temp _img, 0, 0, 0, 0, $newwidth, $newheight, $o _width, $o _heighT); Imagejpeg ($new _img, $d _photo); Imagedestroy ($new _img); } else {//Huantuquan and height are larger than the specified size, and clipped if after compression ( $o _height * $width/$o _width > $height) {//Make sure width is the same as specified, if height is greater than specified, OK $newwidth = $width; $newheight = $o _height * $width/$o _width; $x = 0; $y = ($newheight-$height)/2; } else {//otherwise determine the height and the same as the rule, Width adaptive $new width = $o _width * $height/$o _height; $newheight = $height; $x = ($newwidth-$width)/2; $y = 0; }//thumbnail image $new _img = Imagecreatetruecolor ($newwidth, $newheight); Imagecopyresampled ($new _img, $temp _img, 0, 0, 0, 0, $newwidth, $newheight, $o _width, $o _height); Imagejpeg ($new _img, $d _photo); Imagedestroy ($new _img); $temp _img = imagecreatefromjpeg ($d _phoTo); $o _width = imagesx ($temp _img); Get thumbnail width $o _height = Imagesy ($temp _img); Get thumbnail height//crop picture $new _IMGX = Imagecreatetruecolor ($width, $height); Imagecopyresampled ($new _imgx, $temp _img, 0, 0, $x, $y, $width, $height, $width, $height); Imagejpeg ($new _imgx, $d _photo); Imagedestroy ($new _IMGX); }}cutphoto ("HTTP://TP3.SINAIMG.CN/1700691210/180/1280120708/1", "temp1.jpg", +, +) Cutphoto ("http://hhcf.tk/ Img/avatar_big/1318072877.jpg "," temp2.jpg ", 100, 100);