/**
* 单文件上传
* name:表单上传文件的名字
* ext: 文件允许的后缀,字符串形式
* path:文件保存目录
*/
public function file_upload($name,$ext,$path){
$dir_path=ROOT_PATH.'/public/uploads/'.$path;
if (!is_dir($dir_path))mkdir($dir_path, 0777);// 使用最大权限0777创建文件
$file = request()->file($name);
$info = $file->validate(['size'=>1572864,'ext'=>$ext])->move($dir_path,true,false);
if($info){
// 成功上传后 获取上传信息
$file_path = $info->getSaveName();
$data['file_path'] = '/uploads/'.$path.'/'.$info->getSaveName();
}else{
// 上传失败获取错误信息
$data['file_path'] =$file->getError();
}
return $data;
}
/**
* 图片缩率图处理
* file_path:文件路径
* width :目标宽度
* height:目标高
* path:文件保存目录 : 需事先手动创建
*/
public function file_upload_thumb($file_path,$width,$height,$path){
$fiel_url = ROOT_PATH."/public".$file_path;
$image = \think\Image::open($fiel_url);
$save_url = '/uploads/'.$path."/".sha1($fiel_url)."_".$width."_".$height.".".$image->type();
$save_name = ROOT_PATH."/public".$save_url;
$image->thumb($width, $height)->save($save_name);
return $save_url;
}
/*
* 图片上传接口*/
public function img_upload(){
if(!empty($_FILES["file_img"]["name"])){
$file_mes = $this->file_upload('file_img','jpg,png,gif,jpeg','gift');
$file_path = $file_mes['file_path'];
switch ($upload_type) {
case '1':
$return_data['prize_img'] = $file_path;
$return_data['prize_img_view'] = Config::get("config.host_url").$file_path;
$return_data['prize_img_400'] = $this->file_upload_thumb($file_path,400,400,'gift_thumb');
$return_data['prize_img_200'] = $this->file_upload_thumb($file_path,200,200,'gift_thumb');
$return_data['prize_img_150'] = $this->file_upload_thumb($file_path,150,150,'gift_thumb');
break;
default:
$return_data['img_path'] ='';
break;
}
return json(['code' => '1', 'message' => '上传成功','result'=>$return_data]);
}else{
return json(['code' => '2', 'message' => '图片不能为空!','result'=>null]);
}
}