PHP图片上传,base64转图片存储

图片上传,前端VUE,传到后台的直接是base64的编码图片,反编译后将图片存储至服务器,返回图片地址.


 public function uploads(){
         $logo_data = $this->request->post('logo_base64','');
         //$logo_data = $_POST['logo_base64'];
         
         if(!empty($logo_data)){
             //$data = file_get_contents('./1.txt');
            $reg = '/data:image\/(\w+?);base64,(.+)$/si';
             preg_match($reg,$logo_data,$match_result);
 
            $file_name = time().'.'.$match_result[1];
             
             $logo_path = WEB_PATH.'/uploads/logo/'.$file_name;
             $num = file_put_contents($logo_path,base64_decode($match_result[2]));
            
            
            if(!empty($num)){
                 //上传成功之后,再进行缩放操作
                //$image = \think\Image::open($logo_path);
                
                // 按照原图的比例生成一个最大为150*150的缩略图并保存为thumb.png
                 //$image->thumb(102, 36)->save($logo_path);
                 
                 $result = ['code'=>200,'msg'=>'ok'];
             }else{
                 $result = ['code'=>100,'msg'=>'no'];
            }
         }else{
             $result = ['code'=>100,'msg'=>'参数错误'];
         }
         return ajax($result);
     }
©️2020 CSDN 皮肤主题: 技术黑板 设计师:CSDN官方博客 返回首页