//得到二維碼
public function qrcode() {
header('content-type:text/html;charset=utf-8');
//配置APPID、APPSECRET
$id=$_GET['uid'];
$APPID = C('WX_APPID');
$APPSECRET =C('WX_SECRET');
//獲取access_token
$access_token = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$APPID&secret=$APPSECRET";
//緩存access_token
session_start();
$_SESSION['access_token'] = "";
$_SESSION['expires_in'] = 0;
$ACCESS_TOKEN = "";
if(!isset($_SESSION['access_token']) || (isset($_SESSION['expires_in']) && time() > $_SESSION['expires_in'])){
$json =$this->httpRequest($access_token);
$json = json_decode($json,true);
// var_dump($json);
$_SESSION['access_token'] = $json['access_token'];
$_SESSION['expires_in'] = time()+7200;
$ACCESS_TOKEN = $json["access_token"];
}else{
$ACCESS_TOKEN = $_SESSION["access_token"];
}
$qcode = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=".$ACCESS_TOKEN;
$data = array();
$data['scene'] =$id;
$data['page'] = 'pages/login/welcome';
$data['width'] = "150";
$param = json_encode($data);
//POST參數
$result = $this->httpRequest($qcode, $param,"POST");
//生成二維碼
//$filename="/mnt/data/wwwroot/bzl/Qrcode/$id".".png";
$filename="./Qrcode/$id".".png";
$aa=file_put_contents($filename, $result);
if($aa){
//生成帶水印的圖片
$image = new \Think\Image();
//$img="https://".$_SERVER[SERVER_NAME]."/Qrcode/$id".".png";
//$imgs="/mnt/data/wwwroot/bzl/Qrcode/$id".".png";
$imgs="./Qrcode/$id".".png"; //小程序碼做爲背景圖
//$bgm='./Public/heibeijingtu.jpg'; //
//商品圖
$where['id']=$id;
$user=M('user')->field('avatarurl')->where($where)->find();
$url=$user['avatarurl']; //用戶微信頭像
//將頭像地址保存服務器文件
$img_file = file_get_contents($url);
$img_content= base64_encode($img_file);
//$new_file="/mnt/data/wwwroot/bzl/Public/avatarurl/$id".".png";
//$new_file"./aa-"."$id".".png";
$new_file= "./Public/avatarurl/$id".".png"; //微信頭像保存的位置
if (file_put_contents($new_file, base64_decode($img_content)))
{
//將頭像縮放
$thumb="./Public/avatarurl/thumb-"."$id".".jpg"; //頭像縮放保存位置
$image->open($new_file)->thumb(120, 120,\Think\Image::IMAGE_THUMB_FILLED)->save($thumb); //縮放頭像
$res=$this->yuan_img($thumb); //生成圓形頭像
$img_path="/mnt/data/wwwroot/bzl/Qrcode/circular/$id".".png";//圓形頭像保存位置
$test=imagepng($res,$img_path);
$img="/mnt/data/wwwroot/bzl/Qrcode/watermark/$id".".png";//帶圓形頭像的二維碼保存位置
$image->open($imgs)->water($img_path,\Think\Image::IMAGE_WATER_CENTER,100)->save($img);//圓形頭像水印添加到二維碼
$touxiang="https://".$_SERVER[SERVER_NAME]."/Qrcode/watermark/$id".".png"; //返回帶頭像的二維碼
$touxiangs="https://".$_SERVER[SERVER_NAME]."/Qrcode/$id".".png";//返回二維碼
$arr=array('msg'=>'成功','code'=>'0','img'=>$touxiang);
echo json_encode($arr,JSON_UNESCAPED_UNICODE);
}else{
$arr=array('msg'=>'成功','code'=>'0','img'=>$touxiangs);
echo json_encode($arr,JSON_UNESCAPED_UNICODE);
}
}else{
$img="";
$arr=array('msg'=>'失敗','code'=>'1','img'=>$img);
echo json_encode($arr,JSON_UNESCAPED_UNICODE);
}
}html
//生成圓形頭像json
public function yuan_img($imgpath) {
$ext = pathinfo($imgpath);
$src_img = null;
switch ($ext['extension']) {
case 'jpg':
$src_img = imagecreatefromjpeg($imgpath);
break;
case 'png':
$src_img = imagecreatefrompng($imgpath);
break;
}
$wh = getimagesize($imgpath);
$w = $wh[0];
$h = $wh[1];
$w = min($w, $h);
$h = $w;
$img = imagecreatetruecolor($w, $h);
//這一句必定要有
imagesavealpha($img, true);
//拾取一個徹底透明的顏色,最後一個參數127爲全透明
$bg = imagecolorallocatealpha($img, 255, 255, 255, 127);
imagefill($img, 0, 0, $bg);
$r = $w / 2; //圓半徑
$y_x = $r; //圓心X座標
$y_y = $r; //圓心Y座標
for ($x = 0; $x < $w; $x++) {
for ($y = 0; $y < $h; $y++) {
$rgbColor = imagecolorat($src_img, $x, $y);
if (((($x - $r) * ($x - $r) + ($y - $r) * ($y - $r)) < ($r * $r))) {
imagesetpixel($img, $x, $y, $rgbColor);
}
}
}
return $img;
}小程序
//把請求發送到微信服務器換取二維碼
public function httpRequest($url, $data='', $method='GET'){
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_AUTOREFERER, 1);
if($method=='POST') {
curl_setopt($curl, CURLOPT_POST, 1);
if ($data != '') {
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
}
}
curl_setopt($curl, CURLOPT_TIMEOUT, 30);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($curl);
curl_close($curl);
return $result;
}
public function curl_get($url) {
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
return $data;
}
public function get_http_array($url,$post_data) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
//curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //沒有這個會自動輸出,不用print_r();也會在後面多個1
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
$output = curl_exec($ch);
curl_close($ch);
$out = json_decode($output);
return $out;
}api