1、先生成二维码保存到服务器
2、上传七牛云删除服务器图片
use Qiniu\Auth;
use Qiniu\Storage\UploadManager;
use think\Request;
use app\common\library\Upload;
use Qiniu\Storage\ResumeUploader;
public function curl_post_https($url,$data){
$curl = curl_init();
$headerArray =array("Content-type:application/json;charset='utf-8'","Accept:application/json");
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST,FALSE);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl,CURLOPT_HTTPHEADER,$headerArray);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($curl);
curl_close($curl);
return $output;
}
// 图片上传服务器
public function UploadImageQrCode($img){
$fileimgname = time()."-".rand(1000,9999).".png";
$filecachs = 'code'.'/'.$fileimgname;
$fanhuistr = file_put_contents($filecachs,$img);
return $fileimgname;
}
/**
* 获取token
*
*/
public function htoken(){
$appid = $this->appid;
$secret = $this->secret;
$get_token_url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$appid.'&secret='.$secret;
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$get_token_url);
curl_setopt($ch,CURLOPT_HEADER,0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
$res = curl_exec($ch);
curl_close($ch);
$json_obj = json_decode($res,true);
return $json_obj['access_token'];
}
// 图片上传服务器
public function erwerima($aid){
$url="https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=".$this->htoken();
$data=[
'scene'=>$aid,
'path'=>'pages/sofa/sofa',
'width'=>130,
'auto_color'=>false,
];
$data=json_encode($data);
//拿到二维码
$result = $this->curl_post_https($url,$data);
//把二维码存到服务器端
$res = $this->UploadImageQrCode($result);
return $res;
}
//上传图片至本地、然后上传至七牛云、然后删除本地文件
public function image($image){
// 将字符串转换为图片,然后上传七牛云
$key = 'uploads'.$image;
$save_path = $image;
$filePath = '.'.$image;
$accessKey = 'xxxxxxxxx';
$secretKey = 'xxxxxxxxx';
$auth = new Auth($accessKey, $secretKey);
$bucket = "dybd";
$token = $auth->uploadToken($bucket);
$uploadMgr = new UploadManager();
list($ret, $err) = $uploadMgr->putFile($token, $key, $filePath);
$url = "七牛云地址".$ret['key'];
//删除本地文件
$res = unlink($filePath);
if($res){
return '/'.$key;
}else{
return 1;
}
}