阅读背景:

BASE64 接收图片上传到阿里云OSS_weixin_34235135的博客

来源:互联网 
            $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'] = 'https://***/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(),'图片编码错误');
			}       



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: