This article mainly and everyone to share the PHP implementation of upload cropping images, mainly in the form of code and we share, hope to help everyone.
/Picture clipping upload//$source _path = ' E:\www\cylxsh\storage\app\\ '. $url; The original path $source _path = Storage_path (' app\\ '. $url); Original path $target _width = 1000; Target width $target _height = 100; Target height $source _info = getimagesize ($source _path); Get image information $source _width = $source _info[0]; Original width $source _height = $source _info[1]; Original height $source _mime = $source _info[' mime '); File type $source _ratio = $source _height/$source _width; The original proportion $target _ratio = $target _height/$target _width; Target ratio//source graph too high if ($source _ratio > $target _ratio) {$cropped _width = $source _width;// The width of the crop $cropped _height = $source _width * $target _ratio; The height of the cut $source _x = 0; $source _y = ($source _height-$cropped _height)/2; }//source map too wide ElseIf ($source _ratio < $target _ratio) {$cropped _width = $source _height/$t Arget_ratio; $cropped _height = $source_height; $source _x = ($source _width-$cropped _width)/2; $source _y = 0; }//Source graph moderate Else {$cropped _width = $source _width; $cropped _height = $source _height; $source _x = 0; $source _y = 0; } switch ($source _mime) {case ' image/gif ': $source _image = Imagecreatefromgif ($so Urce_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); Crop Imagecopy ($cropped _image, $source _image, 0, 0, $source _x, $source _y, $cropped _width, $cropped _height); Zoom imagecopyresampled ($target _image, $cropped _image, 0, 0, 0, 0, $target _width, $target _height, $cropped _width, $cropped _height); Set the random file name $chars =null; $len = 50; if (Is_null ($chars)) {$chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; } mt_srand (10000000* (double) microtime ()); for ($i = 0, $fileName = ", $LC = strlen ($chars)-1; $i < $len; $i + +) {$fileName. = $chars [Mt_rand (0, $LC)] ; }//Save the picture to the local $res = Imagepng ($target _image,storage_path (' \app\uploads\\ '. Date (' Y '). ' \ \ '. Date (' m '). ' \ \ '. Date (' d '). ' \ \ '. $fileName)); if ($res = = True) {unlink (Storage_path (' app\\ '. $url)); $url = Storage_path (' \app\uploads\\ '. Date (' Y '). ' \ \ '. Date (' m '). ' \ \ '. Date (' d '). ' \ \ '. $fileName); }else{return View ("Alert", ["msg" = "image upload failed"]); }