thinkphp5上传图片及生成缩略图公共方法(分享)
发布时间:2020-12-14 14:09:52 所属栏目:大数据 来源:网络整理
导读:直接上代码,可以写在公共文件common和继承的基础类中,方便调用 string(57) "uploads/img/201712113d4ca4098a8fb0f90e5f53fd7cd71535.jpg" ["thumb_img"] => string(63) "uploads/img/thumb/20171211/3d4ca4098a8fb0f90e5f53fd7cd71535.jpg" } */ protecte
直接上代码,可以写在公共文件common和继承的基础类中,方便调用 string(57) "uploads/img/201712113d4ca4098a8fb0f90e5f53fd7cd71535.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 = thinkImage::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上传图片及生成缩略图公共方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持编程之家。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |