thinkphp 生成带头像的小程序码

//获得二维码
public function qrcode() {
header(‘content-type:text/html;charset=utf-8’);
//配置APPID、APPSECRET
i d = id= id=_GET[‘uid’];
$APPID = C(‘WX_APPID’);
$APPSECRET =C(‘WX_SECRET’);
//获取access_token
KaTeX parse error: Expected 'EOF', got '&' at position 85: …ient_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);
           }
}

//生成圆形头像

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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值