thinkphp 二维码封装函数
2012-11-26 11:05 freefei 阅读(526) 评论(0) 编辑 收藏 举报
一个二维码封装的thinkphp函数
Qrcode 类为开源生成的类 在 vendor目录下
需要注意的是时间每天生成的二维码 都放在当天的文件夹下 切记
[php]
// 二维码函数
function qrcode($value,$errorCorrectionLevel, $matrixPointSize){
vendor('Qrcode.qrlib');
//$value 传入的值
//$filename 文件名称
//$errorCorrectionLevel 类型:L M Q H;
//$matrixPointSize 大小 1 2 3 4 5 6 7 8 9 10 中的一个
//$info=QRcode::png($value,$filename,$errorCorrectionLevel, $matrixPointSize, 2);
// $PNG_TEMP_DIR ='/Upload/'.'Qrcode'.'/';
//根据时间创建目录 每天创建一个
$time=date("Ymd",time());
$PNG_TEMP_DIR =$_SERVER['DOCUMENT_ROOT'].DIRECTORY_SEPARATOR.'Upload'.DIRECTORY_SEPARATOR.'qrcode'.DIRECTORY_SEPARATOR.$time.DIRECTORY_SEPARATOR;
//$PNG_TEMP_DIR = dirname(__FILE__).DIRECTORY_SEPARATOR.'temp'.DIRECTORY_SEPARATOR;
//html PNG location prefix
//$now=strtotime($time);
//dump($time);
$PNG_WEB_DIR = '/Upload/qrcode/'.$time.DIRECTORY_SEPARATOR;
//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 = $errorCorrectionLevel;
$matrixPointSize = $matrixPointSize;
if(isset($value)) {
//it's very important!
if (trim($value) == '')
die('data cannot be empty! <a href="?">back</a>');
// user data
$filename = $PNG_TEMP_DIR.'test'.md5($value.'|'.$errorCorrectionLevel.'|'.$matrixPointSize).'.png';
$bb= QRcode::png($value, $filename, $errorCorrectionLevel, $matrixPointSize, 2);
// dump($PNG_TEMP_DIR);
// dump($bb);
// dump($_SERVER['DOCUMENT_ROOT']);
} else {
echo '可能值为空所以生成失败!';
//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);
}
// echo '<img src="'.$PNG_WEB_DIR.basename($filename).'" /><hr/>';
$qrcode_path=$PNG_WEB_DIR.basename($filename);
return $qrcode_path;
}
[/php]