This article is mainly for you to share a thinkphp5 upload pictures and generate thumbnails public methods, has a good reference value, I hope to be helpful to everyone. Follow the small series together to see it, hope to help everyone.
Directly on the code, can be written in the common file common and inheritance of the base class, convenient to call
/* * $name The name value that is uploaded for the form * $filePath the folder names below uploads/for saving in the Portal folder public, no words will be automatically created * $width Specify the thumbnail width * $height Specify the thumbnail height * from Generated thumbnails are saved in the Thumb folder under the $filepath folder, automatically created * @return array one is the picture path, one is the thumbnail path, as follows: * Array (2) {["img"] and string (57) "Uploads/img/20171211\3d4ca4098a8fb0f90e5f53fd7cd71535.jpg" ["thumb_img"] + string (+) "uploads/img/thumb/ 20171211/3d4ca4098a8fb0f90e5f53fd7cd71535.jpg "} */Protected function UploadFile ($name, $filePath, $width, $height) { $file = Request ()->file ($name); if ($file) {$filePaths = Root_path. ' Public '. Ds. ' Uploads '. DS. $filePath; if (!file_exists ($filePaths)) {mkdir ($filePaths, 0777,true); } $info = $file->move ($filePaths); if ($info) {$imgpath = ' uploads/'. $filePath. ' /'. $info->getsavename (); $image = \think\image::open ($imgpath); $date _path = ' uploads/'. $filePath. ' /thumb/'. Date (' Ymd '); if (!file_exists ($date _path)) {mkdir ($date _path,0777,true); } $thumb _path = $date _path. ' /'. $info->getfilename (); $image->thumb ($width, $height)->save ($thumb _path); $data [' img '] = $imgpath; $data [' thumb_img '] = $thumb _path; return $data; }else{//upload failed to get error message return $file->geterror (); } } }