如何将二进制的数据流转为图片
public function getQrcode(){
$userId = $this->request->param('user_id');
$member_app_id = $this->request->param('appid');
$token = $this->getToken($member_app_id);
$url = "https://api.weixin.qq.com/wxa/getwxacode?access_token=".$token;
$data = [
'path' =>"pages/network/network?userId=".$userId,
'scene'=>'1001',
'width'=>430,
'auto_color'=>false,
];
$data=json_encode($data);
$res = $this->request($url,$data);
$data='image/png;base64,'.base64_encode($res);
$imageName = rand(1111,9999).'.png';
if (strstr($data,",")){
$image = explode(',',$data);
$image = $image[1];
}
$path = "./attachs/uploads/".date("Ymd",time());
if (!is_dir($path)){ //判断目录是否存在 不存在就创建
mkdir($path,0777,true);
}
$imageSrc= $path."/". $imageName; //图片名字
$r = file_put_contents(ROOT_PATH .$imageSrc, base64_decode($image));//返回的是字节数
if (!$r) {
return json(['data'=>null,"code"=>1,"msg"=>"图片生成失败"]);
}else{
return json(['data'=>1,"code"=>0,"msg"=>"图片生成成功"]);
}
$this->result($data,200,'获取二维码成功','json');
}