直接上代码,可以写在公共文件common和继承的基础类中,方便调用
/*
* $name为表单上传的name值
* $filepath为为保存在入口文件夹public下面uploads/下面的文件夹名称,没有的话会自动创建
* $width指定缩略宽度
* $height指定缩略高度
* 自动生成的缩略图保存在$filepath文件夹下面的thumb文件夹里,自动创建
* @return array 一个是图片路径,一个是缩略图路径,如下:
* array(2) {
["img"] => 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,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{
// 上传失败获取错误信息
return $file->geterror();
}
}
}
以上这篇thinkphp5上传图片及生成缩略图公共方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持硕编程。
【说明】:本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!