tp框架 中生成微信推广二维码

1.第一步下载二维码类文件

下载地址链接:https://pan.baidu.com/s/1ramNa2o    密码:8ok8

2.把类文件放到第三方类中:  ThinkPHP\Library\Vendor\phpqrcode.php

3.首先微信公众平台账号有这个权限,没有的话去开通。


一切准备就绪开始上代码:

控制器里面方法代码为:

      //推广二维码
        public function extendewm()
        {   
            $user_id=I('get.id');//获取当前登录用户id,后以做二维码参数(scene_id)
            $usermobile=$_SESSION['usermobile'];//当前登录者用户手机号
            //获取access_token
            //请求url地址
            $appId = 'wxc7245e01b2b4b965';
            $appSecret = '5cb65e4111e750497e32fdf44c91a3b9';
            $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appId."&secret=".$appSecret;
            //初始化curl
            $ch = curl_init($url);
            //3.设置参数
            curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);//跳过证书验证
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);  // 从证书中检查SSL加密算法是否存在
            //4.调用接口
            $res = curl_exec($ch);
            if(curl_errno($ch)){
                var_dump(curl_error($ch));
            }
            $resArr = json_decode($res,true);
            //5.关闭curl
            curl_close($ch);
            $access_token=$resArr['access_token'];
            //创建二维码ticket,根据当前登陆者角色分别生成不同的ticket,进而生成临时和永久二维码
            $usermobile=$_SESSION['usermobile'];//当前登录者用户手机号
            $user=M('user');
            $userdata=$user->where(array('tellphone'=>$usermobile))->find();
            $user_id=$userdata['id'];
            $role=$userdata['role'];
            //$arrrole=explode(",",$role);
            $url1="https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=".$access_token;
            if($role==2)//商家属性 
            {
                $dataforever = '{"action_name": "QR_LIMIT_SCENE", "action_info": {"scene": {"scene_id": "$user_id"}}}';//永久二维码请求数据(数字形式的场景值ID)

 // $seller="seller".$user_id;//字符串参数
                // $dataforever='{"action_name": "QR_LIMIT_STR_SCENE", "action_info": {"scene": {"scene_str": "'.$seller.'"}}}';//永久二维码请求数据(字符串形式的场景值ID)
                $curl = curl_init(); // 启动一个CURL会话
                curl_setopt($curl, CURLOPT_URL, $url1); // 要访问的地址
                curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); // 对认证证书来源的检查
                curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE); // 从证书中检查SSL加密算法是否存在
                curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); // 模拟用户使用的浏览器
                curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 0); // 使用自动跳转
                curl_setopt($curl, CURLOPT_AUTOREFERER, 1); // 自动设置Referer
                curl_setopt($curl, CURLOPT_POST, 1); // 发送一个常规的Post请求
                curl_setopt($curl, CURLOPT_POSTFIELDS, $dataforever); // Post提交的数据包
                curl_setopt($curl, CURLOPT_COOKIEFILE, 'cookie.txt'); // 读取上面所储存的Cookie信息
                curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环
                curl_setopt($curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容
                curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回
                $tmpInfo = curl_exec($curl); // 执行操作
                curl_close($curl);
                $dataticket=json_decode($tmpInfo,true);
                $ticket=$dataticket['ticket'];
                $url=$dataticket['url'];
                Vendor("phpqrcode");
                // 把生成二维码图片保存到本地
                $path = "Public/Uploads/" . $user_id . ".png"; //本地文件存储路径
                // 纠错级别:L、M、Q、H
                $level = 'H';
                // 点的大小:1到10,用于手机端4就可以了
                $size =5;
                $object=new \QRcode();
                $object->png($url,$path,$level,$size);
                //给生成的二维码图片添加背景图(方法一)
                // $image=new \Think\Image();
                // $image->open('./Public/Uploads/back.png')->water($path,array(240,860),100)->save("./Public/Uploads/".$usermobile.".jpg");
                // //$image->open('./Public/Uploads/back.png')->water($path,array(250,870),100)->text('$usermobile','msyhbd.ttf',20,'#000000')->save("./Public/Uploads/".$usermobile.".jpg");
                // $this->assign("usermobile",$usermobile);
                // $this->display();
                //给生成的二维码添加背景图(方法二)
                $bigImgPath = 'Public/Uploads/back.png';
                $qCodePath = $path;
                $bigImg = imagecreatefromstring(file_get_contents($bigImgPath));
                $qCodeImg = imagecreatefromstring(file_get_contents($qCodePath));
                list($qCodeWidth, $qCodeHight, $qCodeType) = getimagesize($qCodePath);
                // imagecopymerge使用注解
                imagecopymerge($bigImg, $qCodeImg, 255, 875, 0, 0, $qCodeWidth, $qCodeHight, 100);
                // list($bigWidth, $bigHight, $bigType) = getimagesize($bigImgPath);
                list($bigWidth, $bigHight) = getimagesize($bigImgPath);
                $yourname = $usermobile;
                $red = imagecolorallocate($bigImg, 241,215, 60);
                imagefttext($bigImg, 30, 0, 310, 728, $red, 'msyhbd.ttf', $yourname);
                header('Content-Type:image/png');
                $savepath="Public/Uploads/".$usermobile.".png"; //推广二维码本地存储路径
                imagepng($bigImg,$savepath);//推广二维码存到本地
                $this->assign("usermobile",$usermobile);
                $this->display();
                }else{//业务员属性
                $datalinshi = '{"expire_seconds": 604800, "action_name": "QR_SCENE", "action_info": {"scene": {"scene_id":  "$user_id"}}}';//临时二维码请求数据
                $curl = curl_init(); // 启动一个CURL会话
                curl_setopt($curl, CURLOPT_URL, $url1); // 要访问的地址
                curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); // 对认证证书来源的检查
                curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE); // 从证书中检查SSL加密算法是否存在
                curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); // 模拟用户使用的浏览器
                curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 0); // 使用自动跳转
                curl_setopt($curl, CURLOPT_AUTOREFERER, 1); // 自动设置Referer
                curl_setopt($curl, CURLOPT_POST, 1); // 发送一个常规的Post请求
                curl_setopt($curl, CURLOPT_POSTFIELDS, $datalinshi); // Post提交的数据包
                curl_setopt($curl, CURLOPT_COOKIEFILE, 'cookie.txt'); // 读取上面所储存的Cookie信息
                curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环
                curl_setopt($curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容
                curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回
                $tmpInfo = curl_exec($curl); // 执行操作
                curl_close($curl);
                $dataticket=json_decode($tmpInfo,true);
                $ticket=$dataticket['ticket'];
                $url=$dataticket['url'];
                Vendor("phpqrcode");
                // 把生成二维码图片保存到本地
                $path = "Public/Uploads/" . $user_id . ".png"; //本地文件存储路径
                // 纠错级别:L、M、Q、H
                $level = 'H';
                // 点的大小:1到10,用于手机端4就可以了
                $size = 5;
                $object=new \QRcode();
                $object->png($url,$path,$level,$size);
                //给生成的二维码图片添加背景图(方法一)
                // $image=new \Think\Image();
                // $image->open('./Public/Uploads/back.png')->water($path,array(250,870),100)->save("./Public/Uploads/".$usermobile.".jpg");
                // //$image->open('./Public/Uploads/back.png')->water($path,array(250,870),100)->text('$usermobile','msyhbd.ttf',20,'#000000')->save("./Public/Uploads/".$usermobile.".jpg");
                // $this->assign("usermobile",$usermobile);
                // $this->display();
                //给生成的二维码添加背景图(方法二)
                $bigImgPath = 'Public/Uploads/back.png';
                $qCodePath = $path;
                $bigImg = imagecreatefromstring(file_get_contents($bigImgPath));
                $qCodeImg = imagecreatefromstring(file_get_contents($qCodePath));
                list($qCodeWidth, $qCodeHight, $qCodeType) = getimagesize($qCodePath);
                // imagecopymerge使用注解
                imagecopymerge($bigImg, $qCodeImg, 250, 870, 0, 0, $qCodeWidth, $qCodeHight, 100);
                // list($bigWidth, $bigHight, $bigType) = getimagesize($bigImgPath);
                list($bigWidth, $bigHight) = getimagesize($bigImgPath);
                $yourname = $usermobile;
                $red = imagecolorallocate($bigImg, 241,215, 60);
                imagefttext($bigImg, 30, 0, 310, 728, $red, 'msyhbd.ttf', $yourname);
                header('Content-Type:image/png');
                imagepng($bigImg);
                
                
                }

            }


第三步视图层为:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
    <title>我的推广二维码</title>
</head>
<body>
<img src="__ROOT__/Public/Uploads/{$usermobile}.png" alt="" style="width: 264px"/>
</body>
</html>

























猜你喜欢

转载自blog.csdn.net/adminyan/article/details/79237319
今日推荐