最近需要做一个扫码登录的功能,通过HP QR Code来实现,HP QR Code是一个开放源代码的php生成二维码的类库
地址:http://phpqrcode.sourceforge.net/
通过 phpqrcode.php 的png()方法即可生成二维码图片,png()方法参数说明:
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); }
第一个参数$text,
第二个参数$outfile默认为否,不生成文件,只将二维码图片返回,否则需要给出存放生成二维码图片的路径
第三个参数$level默认为L,这个参数可传递的值分别是L(QR_ECLEVEL_L,7%),M(QR_ECLEVEL_M,15%),Q(QR_ECLEVEL_Q,25%),H(QR_ECLEVEL_H,30%)。这个参数控制二维码容错率,不同的参数表示二维码可被覆盖的区域百分比。
利用二维维码的容错率,我们可以将头像放置在生成的二维码图片任何区域。
第四个参数$size,控制生成图片的大小,默认为4
第五个参数$margin,控制生成二维码的空白区域大小
第六个参数$saveandprint,保存二维码图片并显示出来,$outfile必须传递图片路径。
官方文档实例(生成图片):
echo "<h1>PHP QR Code</h1><hr/>"; //set it to writable location, a place for temp generated PNG files $PNG_TEMP_DIR = dirname(__FILE__).DIRECTORY_SEPARATOR.'temp'.DIRECTORY_SEPARATOR; //html PNG location prefix $PNG_WEB_DIR = 'temp/'; include "qrlib.php"; //ofcourse we need rights to create temp dir if (!file_exists($PNG_TEMP_DIR)) mkdir($PNG_TEMP_DIR); $filename = $PNG_TEMP_DIR.'test.png'; //processing form input //remember to sanitize user input in real-life solution !!! $errorCorrectionLevel = 'L'; if (isset($_REQUEST['level']) && in_array($_REQUEST['level'], array('L','M','Q','H'))) $errorCorrectionLevel = $_REQUEST['level']; $matrixPointSize = 4; if (isset($_REQUEST['size'])) $matrixPointSize = min(max((int)$_REQUEST['size'], 1), 10); if (isset($_REQUEST['data'])) { //it's very important! if (trim($_REQUEST['data']) == '') die('data cannot be empty! <a href="?">back</a>'); // user data $filename = $PNG_TEMP_DIR.'test'.md5($_REQUEST['data'].'|'.$errorCorrectionLevel.'|'.$matrixPointSize).'.png'; QRcode::png($_REQUEST['data'], $filename, $errorCorrectionLevel, $matrixPointSize, 2); } else { //default data echo 'You can provide data in GET parameter: <a href="?data=like_that">like that</a><hr/>'; QRcode::png('PHP QR Code :)', $filename, $errorCorrectionLevel, $matrixPointSize, 2); }
项目实例(不生成图片,直接在浏览器输出):
test.php
include "qrlib.php"; if (empty($PNG_TEMP_DIR)) { //set it to writable location, a place for temp generated PNG files $PNG_TEMP_DIR = dirname(__DIR__).DIRECTORY_SEPARATOR.'../web/upload'.DIRECTORY_SEPARATOR; }if (empty($PNG_WEB_DIR)) { //html PNG location prefix $PNG_WEB_DIR = 'web/upload/'; }//ofcourse we need rights to create temp dirif (!file_exists($PNG_TEMP_DIR)) mkdir($PNG_TEMP_DIR);$filename = $PNG_TEMP_DIR.time().'.png';//processing form input //remember to sanitize user input in real-life solution !!!if (in_array($errorCorrectionLevel, array('L','M','Q','H'))) { $errorCorrectionLevel = $errorCorrectionLevel; } else { $errorCorrectionLevel = "L"; }if ($matrixPointSize) $matrixPointSize = min(max((int)$matrixPointSize, 1), 10);if (!empty($data)) { //it's very important! if (trim($data) == '') die('data cannot be empty! <a href="?">back</a>'); // user data $filename = $PNG_TEMP_DIR.time().md5($data.'|'.$errorCorrectionLevel.'|'.$matrixPointSize).'.png'; QRcode::png($data, false, $errorCorrectionLevel, $matrixPointSize, 2); } else { //default data //echo 'You can provide data in GET parameter: <a href="?data=like_that">like that</a><hr/>'; QRcode::png('PHP QR Code :)', false, $errorCorrectionLevel, $matrixPointSize, 2); }
新建一个页面用于直接输出二维码
/** * 在浏览器上直接生成二维码 */ public function actionPng() { $data = \Yii::$app->request->get('data',''); $data = base64_decode($data); $PNG_TEMP_DIR = ''; $PNG_WEB_DIR = ''; $errorCorrectionLevel = "M"; $matrixPointSize = 3; include '../components/phpqrcode/test.php'; }
引用:
<iframe src="<?=/tv-wall/png?data='.base64_encode($data)?>"></iframe>
注:如果直接在页面上输出,将输出图片的二进制码,通过浏览器解析显示出图片,页面不能输出任何其他东西,不然浏览器无法解析将直接输出二进制码,所以这里在一个独立的页面显示二维码图片,然后通过iframe引用进来