首页 > 代码库 > laravel结合七牛实现base64的图片上传

laravel结合七牛实现base64的图片上传

 

确保传到后台的是纯base64字符串而不是以data开头的

//base64图片上传  
 public function uploadCompressImg(Request $request)  
 {  
  
     $base64 = preg_replace("/\s/",‘+‘,$request->input(‘img‘));  
     $img = base64_decode($base64);  
     $disk = \Storage::disk(‘qiniu‘); //使用七牛云上传  
     $img_name = date(‘Y/m/d-H:i:s-‘) . uniqid() . ‘.png‘;  
     $filename = $disk->put($img_name,$img);//上传  
     if(!$filename) {  
         return $this->apiResponse(500, ‘上传失败‘);  
     }  
     $img_url = $disk->getDriver()->downloadUrl($img_name); //获取下载链接  
     return $this->apiResponse(200, ‘上传成功‘,$img_url);  
 }  

 

laravel结合七牛实现base64的图片上传