• thinphp 生成带头像的小程序码


        //获得二维码
        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 ThinkImage();
                    //$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,ThinkImage::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,ThinkImage::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;
        }

  • 相关阅读:
    通过vue-cli命令行安装uni-app
    微信小程序中父子通信
    react启动问题
    react 父子通信
    windows下MongoDB的安装和启动服务--转载
    vue中使用骨架 vue-skeleton-webpack-plugin
    像企业一样思考
    Promise原理详解
    如何封装一个Cookie库
    你应该知道的浏览器缓存知识
  • 原文地址:https://www.cnblogs.com/xiaoqiangjun/p/11246056.html
Copyright © 2020-2023  润新知