Nicolase 2019-11-06
首先下载一个phpqrcode的包放到/vendor目录下
链接:https://pan.baidu.com/s/18jV9DypYB_PHDhD6C0iedQ 提取码:qxuo
如果只是单纯生成二维码那么下面代码即可:
vendor(‘phpqrcode.phpqrcode‘);//引入 $url=‘你要生成的东西:文字、数字、链接等‘; $errorCorrectionLevel = "Q"; // 容错级别:L、M、Q、H $matrixPointSize = "3.8"; // 点的大小:1到10 $qr = new \QRcode(); ob_end_clean(); $qr->png($url,false,$errorCorrectionLevel, $matrixPointSize);//false代表不保存在本地
如果要生成带背景带文字的二维码:
1、首先需要文字的汉字包放在/public下

链接:https://pan.baidu.com/s/1g8wKEcW3F8edPN-HjZcWFA 提取码:8c2e
2、将你需要的背景图放在/public某一目录下我是放在/public/images下

然后就是代码操作:
     vendor(‘phpqrcode.phpqrcode‘);//引入包
        $url=‘你需要生成的东西‘;
        $errorCorrectionLevel = "Q"; // 容错级别:L、M、Q、H
        $matrixPointSize = "3.8"; // 点的大小:1到10
        $qr = new \QRcode();
        ob_end_clean();
        $path = ‘../public/qrcode/‘;//你保存初始二维码的路径
        $QR = $path.$id.‘.png‘;//初始二维码图片名
        $qr->png($url,$QR,$errorCorrectionLevel, $matrixPointSize);//$QR是存放的路径
        $bg = ‘../public/images/bg.jpg‘;//拿到背景图
        $src_path = $QR;//拿到初始二维码
        $dst = imagecreatefromstring(file_get_contents($bg));//获取
        $src = imagecreatefromstring(file_get_contents($src_path));//获取
        list($src_w, $src_h) = getimagesize($src_path);//获取宽高
        $black = imagecolorallocate($dst, 255, 255, 255);//字体颜色(白色)
        $font = realpath(‘../public/pingfang.ttf‘);//引入字体包 必须加realpath()否则会报错
        $x = (375-$len*26)/2;//动态计算文字X坐标起始点
        imagettftext($dst, 18, 0, $x, 510, $black, $font, $title);//循环添加文字
        imagecopymerge($dst, $src, 100, 290, 0, 0, $src_w, $src_h, 100);//图片
        header("Content-type: image/png");
        imagepng($dst);//根据需要生成相应的图片并输出(如果需要保存本地就在后面加路径就好)
        imagedestroy($dst);
        imagedestroy($src);