PHP给微信二维码 添加背景和昵称 ,微信带参数海报跨坑记录。

先获取微信二维码,这里我获取的是永久二维码:

$wx= $this->getTicket($this->userid);//我带的参数是用户ID,可自由发挥。


//下面是相关函数
function getTicket($sceneid,$type='',$expire_seconds=604800){
        if($type=='temp'){
            $data = '{"expire_seconds": %s, "action_name": "QR_SCENE", "action_info": {"scene": {"scene_id": %s}}}';
            $data = sprintf($data,$expire_seconds,$sceneid);
        }else{
            $data = '{"action_name": "QR_LIMIT_SCENE", "action_info": {"scene": {"scene_id": %s}}}';
            $data = sprintf($data,$sceneid);
        }
        $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$this->appid."&secret=".$this->secret;
        $cont = json_decode($this->getToken($url));
        $url_get_Qcode_ticket = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=".$cont->access_token;
        $code = json_decode($this->getShort($data, $url_get_Qcode_ticket));

        $url_get_Qcode = "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=".$code->ticket;
        return $url_get_Qcode;
    }

    function getToken($url) {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.22 (KHTML, like Gecko)");
        curl_setopt($ch, CURLOPT_ENCODING, "gzip");//加入gzip解析
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        $output = curl_exec($ch);
        curl_close($ch);
        return $output;
    }

    function getShort($data, $url) {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.22 (KHTML, like Gecko)");
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $tmpInfo = curl_exec($ch);
        if (curl_errno($ch)){
            return curl_error($ch);
        }
        curl_close($ch);
        return $tmpInfo;
    }

下面是二维码与背景以及用户昵称的合成 :

//我的方法是每一次都重新生成一次海报并覆盖原海报。
//不想要每次都重新生成可以斟酌保存。
//这里是接着生成微信二维码写的。


$bigImgPath = '/laragon/www/nozick/public/static/home/images/2weima.jpg';//背景绝对路径。
$bigImg = imagecreatefromstring(file_get_contents($bigImgPath));//这个我忘了。
$newwidth = 440;//要设定的二维码的宽度。
$newheight= 440;//要设定的二维码的高度。
$size = getimagesize($wx);//这个是获取微信二维码的原大小吧
$qCodeImg = imagecreatefromjpeg($wx);//这儿是将微信二维码url转成图片。
$thumb = imagecreatetruecolor($newwidth,$newheight);//新建的一个空白图片,接下面
ImageCopyResampled($thumb,$qCodeImg,0,0,0,0,$newwidth,$newheight,$size[0],$size[1]);
//将新尺寸二维码放到$thumb里。

imagecopymerge($bigImg, $thumb, 323,615,0,0, $newwidth, $newheight, 100);
//背景和二维码合成,第三到第六个参数是二维码在背景上的坐标,第一位是X,第二位是Y,后面的我就不知道了,百度一下imagecopymerge()就知道了。第九个参数100是100%合并。

list($bigWidth, $bigHight) = getimagesize($bigImgPath);

$font = '/laragon/www/nozick/public/MSYH.TTC';//字体
$black = imagecolorallocate($bigImg, 0x00, 0x00, 0x00);//字体颜色
imagefttext($bigImg, 26, 0, 409, 930, $black, $font, $username);
//这里是合成昵称的。1参数是刚刚合成的二维码,2是字体大小,3是字体旋转,4是X轴,5是Y轴,6颜色,7字体,8是要显示的文字。

header('Content-Type:image/png');
$savepath='/laragon/www/nozick/public/qrcode/'.$uid.'.png'; //推广二维码本地存储路径
imagepng($bigImg,$savepath);//保存
$wxcode = '/qrcode/'.$uid.'.png';//这个可以存到数据库。
$this->assign('wx',$wxcode);//输出给前台显示。

前台直接写到<img src="">即可。

以上。

猜你喜欢

转载自blog.csdn.net/qq_34876813/article/details/82284432