thinkphp5上传图片及生成缩略图公共方法(分享)

直接上代码,可以写在公共文件common和继承的基础类中,方便调用

/* * $name为表单上传的name值 * $filePath为为保存在入口文件夹public下面uploads/下面的文件夹名称,没有的话会自动创建 * $width指定缩略宽度 * $height指定缩略高度 * 自动生成的缩略图保存在$filePath文件夹下面的thumb文件夹里,自动创建 * @return array 一个是图片路径,一个是缩略图路径,如下: * array(2) { ["img"] => string(57) "uploads/img/20171211\3d4ca4098a8fb0f90e5f53fd7cd71535.jpg" ["thumb_img"] => string(63) "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.'https://www.jb51.net/'.$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.'https://www.jb51.net/'.$info->getFilename(); $image->thumb($width, $height)->save($thumb_path); $data['img'] = $imgpath; $data['thumb_img'] = $thumb_path; return $data; }else{ // 上传失败获取错误信息 return $file->getError(); } } }

以上这篇thinkphp5上传图片及生成缩略图公共方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。

您可能感兴趣的文章:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/50221e4c1c4149ee9cf5cbd91fa537ba.html