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