/**
* 上传图片并保存到七牛云
*/
public function uploadPic(){
//接收文件
$file=request()->file('file');
if($file==null){
exit(json_encode(array(
'code'=>'error',
'msg'=>'请上传文件!'
)));
}
//路径
$path = 'default' . DS .date('Ymd',time());
$filePaths= 'upload' . DS . $path;
if(!empty($file)){
$info = $file->validate(['size'=>10485760,'ext'=>'gif,jpg,jpeg,png,bmp,swf,mp4,mov'])->rule('uniqid')->move($filePaths);
$error = $file->getError();
//验证文件后缀后大小
if(!empty($error)){
return ['code'=>404,'msg'=>$error];
}
if($info){
// 成功上传后 获取上传信息
$suffix = $info->getExtension();//文件后缀
$image=['gif','GIF','jpg','JPG','jpeg','JPEG','png','PNG','bmp','BMP','swf','SWF'];
$video=['mp4','MP4','mov','MOV'];
if(in_array($suffix,$image)){
$file_type = 'image';
}else if(in_array($suffix,$video)){
$file_type = 'video';
}else{
$file_type = 'fail';
}
$info->getSaveName();
$photo = $info->getSavename();
//上传农场背景图片到七牛云
//本地路径
$image_url = $filePaths. DS .$photo;
//上传到七牛云图片名称
$name = $path. DS .$photo;
//获取token值
$accessKey = '**************************************';
$secretKey = '**************************************';
// 初始化签权对象
$auth = new Auth($accessKey, $secretKey);
$bucket = '******';
$token = $auth->uploadToken($bucket);
$uploadMgr = new UploadManager();
// 调用 UploadManager 的 putFile 方法进行文件的上传。
list($ret, $err) = $uploadMgr->putFile($token, $name, $image_url);
//删除本地临时图片
if(is_file($image_url)) {
unlink($image_url);
}
//七牛云图片路径
$image_url = cmf_get_image_url($name) . '?imageView2/1/w/300/h/300';
//去除水印操作
$image_url = str_replace('!watermark','',$image_url);
}else{
// 上传失败获取错误信息
$file->getError();
}
}else{
$photo = '';
}
if($photo !== ''){
$src = cmf_get_image_url($path.DS.$photo);
return ['code'=>1,'msg'=>'成功','file_type'=>$file_type,'photo'=>$path.DS.$photo,'src'=>$image_url,'base64_image'=>''];
}else{
return ['code'=>404,'msg'=>'失败'];
}
}