BASE64 接收图片上传到阿里云OSS
2017-12-27 16:31
1606 查看
$pic = I('post.pic'); if($pic){ if(preg_match('/^(data:\s*image\/(\w+);base64,)/', $pic, $result)){ $type = $result[2]; if(in_array($type,array('pjpeg','jpeg','jpg','gif','bmp','png'))){ $pic_name = time().rand(1,1000).'.'.$type; $file = './Public/headPic/'.$pic_name; $object = 'head-image/'.$pic_name; if(file_put_contents($file, base64_decode(str_replace($result[1], '', $pic)))){ vendor('aliyun.autoload'); $ossClient = new \OSS\OssClient(C('accessKeyId'),C('accessKeySecret'),C('endpoint')); try{ $getOssInfo = $ossClient->uploadFile(C('bucket'),$object,$file); $getOssPdfUrl = $getOssInfo['info']['url']; if($getOssPdfUrl){ unlink($file); $data['pic'] = 'http://***/head-image/'.$pic_name; } }catch(OssException $e){ printf($e->getMessage() . "\n"); return; } }else $this->returnResult(false,array(),'图片上传失败'); }else $this->returnResult(false,array(),'图片类型错误'); }else $this->returnResult(true,array(),'图片编码错误'); }
代码是搜到的 做了点修改 改成app常用的base64 传到后端
查了很多资料 不能直接上传到oss 必须经过本地这一环节 所以最后操作完把本地图unlink一下
$file 是存在本地的图片路径
$object是oss里空间下的路径
(开源中国好像不能上传文件 我的是thinkphp里写的 要sdk的私我)
相关文章推荐
- Android 上传图片到阿里云 OSS
- Java实现阿里云OSS云存储对象之上传图片
- 阿里云OSS进行图片上传
- laravel下使用阿里云oss上传图片
- 根据阿里云的OSS服务上传图片以及文件
- oss 阿里云 javascript 上传图片
- base64编码上传图片java后台接收实例
- Spring+SpringMVC+Mybatis整合百度富文本编辑器Ueditor,上传图片到阿里云OSS对象存储。
- [微信小程序]上传图片到阿里云OSS
- Yii2.0使用阿里云OSS的SDK上传图片、下载、删除图片示例
- 阿里云OSS上传图片,图片处理
- base64编码上传图片java后台接收实例
- vue用阿里云oss上传图片使用分片上传只能上传100kb以内的解决办法
- Java使用阿里云OSS对象存储上传图片
- file does not exist 阿里云OSS图片上传遇到的问题
- Java使用阿里云OSS对象存储上传图片
- The difference between the request time and the current time is too large.阿里云oss上传图片报错
- C# .net Ueditor实现图片上传到阿里云OSS 对象存储
- 安卓手把手教你结合阿里云OSS存储实现视频(音频,图片)的上传与下载
- **关于阿里云oss图片批量上传问题解析**