ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、视频、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
①\QRcode::png ②QRencode->encodePNG ③QRimage::png ④QRimage::image ⑤系统函数ImageCreate($w,$h) 参考:https://ihavenolimitations.xyz/a173512/php_note/1690618 ``` public static function png($text, $outfile = false, $level = QR_ECLEVEL_L, $size = 3, $margin = 4, $saveandprint=false) { $enc = QRencode::factory($level, $size, $margin); return $enc->encodePNG($text, $outfile, $saveandprint=false); } //使用: \QRcode::png('www.xxx.com',false,'L',2.1,1); ``` 前提 1.phpqrcode类文件下载,下载地址:[https://sourceforge.net/projects/phpqrcode/](https://sourceforge.net/projects/phpqrcode/) 2.PHP环境必须开启支持GD2扩展库支持(一般情况下都是开启状态) png($text, $outfile = false, $level = QR_ECLEVEL_L, $size = 3, $margin = 4, $saveandprint=false) >[info] 含margin边框图片的实际宽度=(25+2*$margin)/$size; $size=含margin边框图片的实际宽度/(25+2*$margin) 由于存在误差 最后+0.01确保结果接近准确 $size=bcadd(bcdiv($width, bcadd(25,bcmul(2,$margin, 2), 2), 2), 0.01, 2); ``` include app()->getRootPath().'extend/phpqrcode/phpqrcode.php'; $value='https://www.baidu.com'; $errorCorrectionLevel="L"; $matrixPointSize="4"; \QRcode::png($value,'aaaa.png',$errorCorrectionLevel,$matrixPointSize); ``` ## **在生成的二维码中加上logo(生成图片文件)** ``` //2. 在生成的二维码中加上logo(生成图片文件) function scerweima1($url=''){ require_once 'phpqrcode.php'; $value = $url; //二维码内容 $errorCorrectionLevel = 'H'; //容错级别 $matrixPointSize = 6; //生成图片大小 //生成二维码图片 $filename = 'qrcode/'.microtime().'.png'; QRcode::png($value,$filename , $errorCorrectionLevel, $matrixPointSize, 2); $logo = 'qrcode/logo.jpg'; //准备好的logo图片 $QR = $filename; //已经生成的原始二维码图 if (file_exists($logo)) { $QR = imagecreatefromstring(file_get_contents($QR)); //目标图象连接资源。 $logo = imagecreatefromstring(file_get_contents($logo)); //源图象连接资源。 $QR_width = imagesx($QR); //二维码图片宽度 $QR_height = imagesy($QR); //二维码图片高度 $logo_width = imagesx($logo); //logo图片宽度 $logo_height = imagesy($logo); //logo图片高度 $logo_qr_width = $QR_width / 4; //组合之后logo的宽度(占二维码的1/5) $scale = $logo_width/$logo_qr_width; //logo的宽度缩放比(本身宽度/组合后的宽度) $logo_qr_height = $logo_height/$scale; //组合之后logo的高度 $from_width = ($QR_width - $logo_qr_width) / 2; //组合之后logo左上角所在坐标点 //重新组合图片并调整大小 /* * imagecopyresampled() 将一幅图像(源图象)中的一块正方形区域拷贝到另一个图像中 */ imagecopyresampled($QR, $logo, $from_width, $from_width, 0, 0, $logo_qr_width,$logo_qr_height, $logo_width, $logo_height); } //输出图片 imagepng($QR, 'qrcode.png'); imagedestroy($QR); imagedestroy($logo); return '<img src="qrcode.png" alt="使用微信扫描支付">'; } //调用查看结果 echo scerweima1('https://www.baidu.com'); ``` ![](https://img.kancloud.cn/c6/36/c636d7a7298b82381dcdc3cbd68adb7b_234x230.png) 生成二维码(不生成图片文件) ``` //3. 生成原始的二维码(不生成图片文件) function scerweima2($url=''){ require_once 'phpqrcode.php'; $value = $url; //二维码内容 $errorCorrectionLevel = 'L'; //容错级别 $matrixPointSize = 5; //生成图片大小 //生成二维码图片 $QR = QRcode::png($value,false,$errorCorrectionLevel, $matrixPointSize, 2); } //调用查看结果 scerweima2('https://www.baidu.com'); ``` 前两种方法,每调用一次都会在本地生成一张二维码图片,第三种方法,不生成文件,会直接输出二维码到浏览器中 phpqrcode设置二维码图片的像素宽度 ``` $margin='1'; $size = getQrSize(260); QRcode::png($data,false,'H',$size,$margin); //获取制定宽度下二维码的Size function getQrSize($width,$margin='4'){ return bcadd(bcdiv($width, bcadd(25,bcmul(2,$margin, 2), 2), 2), 0.01, 2); } ``` >[danger] 注意size最大值为:$size = (int)(1024/ (count($tab)+2*$this->margin)); > 例如$margin为1时它的最大值为37 > 例如$margin为默认的4时它的最大值为31,所以此时最大值的范围时0~31,超出他的最大值时取1 > 1024为php限制为1024? > $tab = $this->encode(图片的内容即本例的$data,它可是文字内容或者url); >$size=含margin边框图片的实际宽度/(25+2*$margin) 由于存在误差 最后+0.01确保结果接近准确 ## 生成二维码并强制下载 ``` function qrcodeDowload(){ $filename='xxx'; $filename .= '_' . date('Y-m-d',time()) . '.png'; // 追加二维码名称 header('Pragma: public'); // required header('Expires: 0'); // no cache header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); header('Cache-Control: private',false); header('Content-Type: application/force-download'); header('Content-Disposition: attachment; filename="'.$filename.'"'); header('Content-Transfer-Encoding: binary'); header('Connection: close'); include app()->getRootPath().'extend/phpqrcode/phpqrcode.php'; $value='http://www.phpstudy.net'; $errorCorrectionLevel="L"; $matrixPointSize="6.55"; $margin="1"; return \QRcode::png($value,false,$errorCorrectionLevel,$matrixPointSize,$margin); } ``` 上面的phpqrcode文件为合并版本,不需要加载额外的文件 include('../lib/full/qrlib.php'); ## **将生成的图片显示在浏览器上** 新建一个文件调用公共函数 ``` namespace app\home\controller; class QrcodeImg extends BaseAuth{ public function index(){ $url="http://www.baidu.com"; makeQrcode($url); exit; } } ``` 公共函数 ``` function makeQrcode($url){ include app()->getRootPath().'extend/phpqrcode/phpqrcode.php'; $value='http://www.phpstudy.net'; $value=$url; $errorCorrectionLevel="L"; $matrixPointSize="6.55"; $margin="1"; $matrixPointSize=bcadd(bcdiv(275, bcadd(25,bcmul(2,$margin, 2), 2), 2), 0.01, 2); header("Content-type: image/png"); //在网站根目录生成aaa.png \QRcode::png($value,false,$errorCorrectionLevel,$matrixPointSize,$margin); // exit; } ``` 前端访问图片地址 ``` <div><img src="http://www.xxx.com/index.php/admin/QrcodeImg/" alt=""><p>企业二维码</p></div> ``` phpqrcode生成二维码直接输出(不用exit) ``` /** * phpqrcode php生成二维码 * $frame string 二维码内容 * $filename string|false 默认为否,不生成文件,只将二维码图片返回,否则需要给出存放生成二维码图片的路径 * $level 默认为L,这个参数可传递的值分别是L(QR_ECLEVEL_L,7%),M(QR_ECLEVEL_M,15%),Q(QR_ECLEVEL_Q,25%),H(QR_ECLEVEL_H,30%)。这个参数控制二维码容错率,不同的参数表示二维码可被覆盖的区域百分比。 * $size int 生成二维码的区域大小点的大小:1到10 * $margin int 图片留白大小 * $saveandprint string 保存二维码图片并显示出来,$outfile必须传递图片路径 */ function qrcode($frame, $filename = false, $level = 'L', $size = 5, $margin = 2, $saveandprint=false){ header('Content-Type: image/png'); include app()->getRootPath().'extend/phpqrcode/phpqrcode.php'; $qrcode = new \QRcode(); ob_clean(); $png = $qrcode->png($frame, $filename , $level , $size , $margin , $saveandprint); return $png; } /** * 二维码base64 * @param [type] $content 二维码内容 (可以是文字或者url) * @param string $errorCorrectionLevel 容错级别 默认为L,这个参数可传递的值分别是L(QR_ECLEVEL_L,7%),M(QR_ECLEVEL_M,15%),Q(QR_ECLEVEL_Q,25%),H(QR_ECLEVEL_H,30%)。这个参数控制二维码容错率,不同的参数表示二维码可被覆盖的区域百分比。 * @param integer $with 生成图片宽度 * @param integer $margin 图片留白大小 * @return string 返回二维码base64格式 */ function qrcode64($content, $errorCorrectionLevel = 'L', $with = 275, $margin = 2){ include app()->getRootPath().'extend/phpqrcode/phpqrcode.php'; $QRcode = new \QRcode(); ob_start(); // 在服务器打开一个缓冲区来保存所有的输出 $matrixPointSize=bcadd(bcdiv($with, bcadd(25,bcmul(2,$margin, 2), 2), 2), 0.01, 2); $QRcode->png($content,false,$errorCorrectionLevel,$matrixPointSize,$margin); $imageString = base64_encode(ob_get_contents()); ob_end_clean(); //清除缓冲区的内容,并将缓冲区关闭,但不会输出内容 return "data:image/jpg;base64,".$imageString; } ``` qrcode();常规生成二维码 qrcode64以base64输出图片流使用时 ``` $img = qrcode64('http://www.baidu.com'); echo "<img src='{$img}'>"; 或者 <img src="{:qrcode64('http://www.baidu.com')}" alt=""> <img src="{:qrcode64((string)url('QrcodeImg/index')->domain(true))}" alt=""> ``` 改进输入可理解的宽度生成二维码(实际有点差距但不大) ``` /** * 二维码base64 * @param [type] $content 二维码内容 (可以是文字或者url) * @param string $errorCorrectionLevel 容错级别 默认为L,这个参数可传递的值分别是L(QR_ECLEVEL_L,7%),M(QR_ECLEVEL_M,15%),Q(QR_ECLEVEL_Q,25%),H(QR_ECLEVEL_H,30%)。这个参数控制二维码容错率,不同的参数表示二维码可被覆盖的区域百分比。 * @param integer $with 生成图片宽度 * @param integer $margin 图片留白大小 * @return string 返回二维码base64格式 */ function qrcode64($content,$with = 159, $margin = 0, $errorCorrectionLevel = 'L', $outfile=false, $saveandprint=false){ include_once app()->getRootPath().'extend/phpqrcode/phpqrcode.php'; switch ($errorCorrectionLevel .'') { case '0': case '1': case '2': case '3': $level = $level; break; case 'l': case 'L': $level = QR_ECLEVEL_L; break; case 'm': case 'M': $level = QR_ECLEVEL_M; break; case 'q': case 'Q': $level = QR_ECLEVEL_Q; break; case 'h': case 'H': $level = QR_ECLEVEL_H; break; } ob_start(); // 在服务器打开一个缓冲区来保存所有的输出 $qrc=new \QRencode(); $qrc->level=$level; // $qrc->size = 10; $qrc->margin = $margin; $tab=$qrc->encode($content,false,false); $maxSize = (int)(1024 / (count($tab)+2*$qrc->margin)); $matrixPointSize=bcadd(bcdiv($with, bcadd($maxSize,bcmul(2,$margin, 2), 2), 2), 0.01, 2); \QRcode::png($content,false,$errorCorrectionLevel,$matrixPointSize,$margin); $imageString = base64_encode(ob_get_contents()); ob_end_clean(); //清除缓冲区的内容,并将缓冲区关闭,但不会输出内容 return "data:image/jpg;base64,".$imageString; } ```