/** * Function: Resize picture or generate thumbnail v 1.1 * @param $Image need to adjust the picture (including path) * @param $Dw the maximum width when adjusting, absolute width when thumbnail * @param $Dh adjustment Maximum height, absolute height at thumbnail * @param $Type 1, size adjustment; 2, generate thumbnails * @return bool*/ PublicfunctionCompressimg ($image,$Dw,$Dh,$type) { if(!file_exists($image)) { return false; } //If you need to generate thumbnails, copy the original image to $image assignment (generate thumbnail action)///When the type==1 is not copied, it will regenerate the reduced image on the original image file (resize operation) if($type! = 1) { Copy($image,Str_replace(".", "_x.",$image)); $image=Str_replace(".", "_x.",$image); } //get the type of file, build different objects according to different types $ImgInfo=getimagesize($image); Switch($ImgInfo[2]) { Case1:$Img= @imagecreatefromgif ($image); Break; Case2:$Img= @imagecreatefromjpeg ($image); Break; Case3:$Img= @imagecreatefrompng ($image); Break; } //if the object is not created successfully, the non-picture file if(Empty($Img)) { //If an error occurs when generating thumbnails, you need to delete the files that have already been copied if($type! = 1) { unlink($image); } return false; } //If you are performing a resize operation if($type= = 1) { $w= Imagesx ($Img); $h= Imagesy ($Img); $width=$w; $height=$h; if($width>$Dw) { $Par=$Dw/$width; $width=$Dw; $height=$height*$Par; if($height>$Dh) { $Par=$Dh/$height; $height=$Dh; $width=$width*$Par; } } ElseIf($height>$Dh) { $Par=$Dh/$height; $height=$Dh; $width=$width*$Par; if($width>$Dw) { $Par=$Dw/$width; $width=$Dw; $height=$height*$Par; } } Else { $width=$Dw; $height=$Dh; } $nImg= Imagecreatetruecolor ($Dw,$Dh);//Create a new true Color canvas $white= Imagecolorallocate ($nImg, 255, 255, 255); //Fill white backgroundImagefill ($nImg, 0, 0,$white); if($h/$w>$Dh/$Dw) {//high and relatively large $width=$w* ($Dh/$h); $IntNW=$Dw-$width; $Dx=$IntNW/2; $Dy= 0; } Else{//wide Larger $height=$h* ($Dw/$w); $IntNH=$Dh-$height; $Dx= 0; $Dy=$IntNH/2; } imagecopyresampled ($nImg,$Img,$Dx,$Dy, 0, 0,$width,$height,$w,$h);//Resample and resize a portion of an imageImagejpeg ($nImg,$image);//output images to a browser or file in JPEG format return true; } Else{//If you are performing a build thumbnail operation $w= Imagesx ($Img); $h= Imagesy ($Img); $nImg= Imagecreatetruecolor ($Dw,$Dh); $white= Imagecolorallocate ($nImg, 255, 255, 255); //Fill white backgroundImagefill ($nImg, 0, 0,$white); if($h/$w>$Dh/$Dw) {//high and relatively large $width=$w* ($Dh/$h); $IntNW=$Dw-$width; Imagecopyresampled ($nImg,$Img,$IntNW/2, 0, 0, 0,$width,$Dh,$w,$h); } Else{//wide Larger $height=$h* ($Dw/$w); $IntNH=$Dh-$height; Imagecopyresampled ($nImg,$Img, 0,$IntNH/2, 0, 0,$Dw,$height,$w,$h); } imagejpeg ($nImg,$image); return true; } }
It is important to note that if you want to create a picture of equal width and height, you should also set the width to the high ratio when you output to the page.
Compress pictures when uploading pictures-backend practices