HP QR Code 生成二維碼
最近需要做一個掃碼登錄的功能,通過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 dir if (!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引用進來
本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!