laravel5结合七牛云存储图片
首先去官网注册账号以及完成实名认证https://www.qiniu.com/
1.新建对象存储
2.查询我们所需要的数据
3.借助第三方集成扩展处理上传
4.然后就是修改普通的上传代码
存储图片:
1
| Storage::disk('qiniu')->put($newFileName, File::get($request->file('file')->path()));
|
获取图片地址:
1
| 'path' => Storage::disk('qiniu')->getDriver()->downloadUrl($newFileName)
|
当然,也可以先把图片保存在本地再上传:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| if ($request->hasFile('image')) { $file = $data['image']; if ($file->isValid()) { $originalName = $file->getClientOriginalName(); $ext = $file->getClientOriginalExtension(); $type = $file->getClientMimeType(); $realPath = $file->getRealPath();
$Images = substr($realPath, 0, -4) . '.' . $ext;
$filename = uniqid() . '.' . $ext;
Image::make($realPath)->resize(1920, 260)->save($Images);
$bool = Storage::disk('public')->put($filename, file_get_contents($Images)); Storage::disk('qiniu')->put($filename, $realPath); if ($bool) { $data['image'] = Storage::disk('public')->url($filename);
} } }
|