#代码#Laravel5.6 + 阿里云OSS 完成图文分离架构( 二 )


到此文件就已经加载完成 。二. 编写接口控制器方法
新建控制器:UploadsController 继承 Controller类 , 方便操作 。第一步:编写API路由Route::post('/index/image', '\App\Http\Controllers\Index\UploadsController@index');第二步:编写控制器 , 上传图片(核心代码)public function index(Request $request) {$disk = \Storage::disk('oss');if (!isset($request->image)) {return $this->array_format('图片信息错误' . __LINE__, 414);}switch ($request->source) {case 'file':if (!($request->hasFile('image') && $request->file('image')->isValid())) {return $this->array_format('图片信息错误' . __LINE__, 414);}$file = $request->file('image');$image_str = @file_get_contents($file->getPathname());$base64_str = base64_encode($image_str);break;case 'url':$image_str = @file_get_contents($request->image);$base64_str = base64_encode($image_str);break;case 'base64':$base64_str = $request->image;break;default:return $this->array_format('图片类型错误', 414);break;}//获取图片信息$image_info = $this->base64_image_format($base64_str);if (!$image_info) {return $this->array_format('图片信息错误' . __LINE__, 414);}if ($image_info['image_size'] > 10 * 1024 * 1024) {return $this->array_format('图片信息太大', 414);}$image_path = 'uploads/image/'. date('Ym');$image_name = $image_path . '/' . md5($image_info['image_str']) . '.' . $image_info['image_suffix'];//上传图片$temp = $disk->put($image_name, $image_info['image_str']);if (!$temp) {return $this->array_format('上传失败', 414);}return $this->array_format('上传成功', 200, ['image_name' => $image_name,'image_url' => $disk->url($image_name),]); }四. Postman工具接口测试
【#代码#Laravel5.6 + 阿里云OSS 完成图文分离架构】#代码#Laravel5.6 + 阿里云OSS 完成图文分离架构
文章图片

文章图片


推荐阅读