直接上代码,可以写在公共文件common和继承的基础类中,方便调用
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.'/'.$info->getSaveName();
$image = \think\Image::open($imgpath);
$date_path = 'uploads/'.$filePath.'/thumb/'.date('Ymd');
if(!file_exists($date_path)){
mkdir($date_path,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{
// 上传失败获取错误信息
return $file->getError();
}
}
}
以上这篇thinkPHP5上传图片及生成缩略图公共方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持编程之家。
原文链接:https://www.f2er.com/thinkphp/16432.html