<?PHPfunctionResizeimage ($im,$maxwidth,$maxheight,$name,$filetype) { $pic _width= Imagesx ($im); $pic _height= Imagesy ($im); if(($maxwidth&&$pic _width>$maxwidth) || ($maxheight&&$pic _height>$maxheight)) { if($maxwidth&&$pic _width>$maxwidth) { $widthratio=$maxwidth/$pic _width; $resizewidth _tag=true; } if($maxheight&&$pic _height>$maxheight) { $heightratio=$maxheight/$pic _height; $resizeheight _tag=true; } if($resizewidth _tag&&$resizeheight _tag) { if($widthratio<$heightratio) $ratio=$widthratio; Else $ratio=$heightratio; } if($resizewidth _tag&&!$resizeheight _tag) $ratio=$widthratio; if($resizeheight _tag&&!$resizewidth _tag) $ratio=$heightratio; $newwidth=$pic _width*$ratio; $newheight=$pic _height*$ratio; if(function_exists("imagecopyresampled")) { $newim= Imagecreatetruecolor ($newwidth,$newheight);//PHP System FunctionsImagecopyresampled ($newim,$im, 0,0,0,0,$newwidth,$newheight,$pic _width,$pic _height);//PHP System Functions } Else { $newim= Imagecreate ($newwidth,$newheight); Imagecopyresized ($newim,$im, 0,0,0,0,$newwidth,$newheight,$pic _width,$pic _height); } $name=$name.$filetype; Imagejpeg ($newim,$name); Imagedestroy ($newim); } Else { $name=$name.$filetype; Imagejpeg ($im,$name); } }//How to use:$im=imagecreatefromjpeg ("./20140416103023202.jpg");//parameter is the storage path of the picture$maxwidth= "600";//set the maximum width of a picture$maxheight= "400";//set the maximum height of a picture$name= "123";//the name of the picture, pick it up.$filetype= ". jpg";//type of pictureResizeimage ($im,$maxwidth,$maxheight,$name,$filetype);//Call the function above
Online excerpt, not yet tested.
PHP and other proportional compression pictures