thinkphp3.2集成QRcode生成二维码

一、下载QRcode源代码

https://sourceforge.net/projects/phpqrcode/files/releases/

使用phpqrcode必须开启GD2扩展,phpqrcode提供了一个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);
}

总共六个参数:
参数一:表示你要保存的文本数据。
参数二:输出的二维码图片文件。
参数三:容错率(分别是L、M、Q、H)
参数四:生成图片大小(1-10)
参数五:二维码周围边框空白区域间距值
参数六:是否保存二维码并显示

 

二、把下载好的压缩包解压,并名为QRcode,并拷贝到ThinkPHP/Library/Vendor下。然后我们在应用程序目录的Common模块下Common/function.php创建一个公共方法,用于生成二维码。

/**
 * 生成二维码
 * @param type $data 信息数据
 * @param type $size 二维码图大小,1-10可选,数字越大图片尺寸越大
 * @param type $path 图片保存根路径
 * @param type $level 纠错比例,分为L,M,Q,H四个等级,H代表最高纠错能力
 */
function createQR($data = '',$size = 8,$path = './Qr',$level = 'L') {
    if (empty($data) || empty($size) || empty($path) || empty($level)) {
        return false;
    }
    vendor('QRcode.phpqrcode');
    $size = min(max((int) $size,1),10);
    $filePath = $path . '/' . date('Ym');
    if (!file_exists($filePath)) {
        @mkdir($filePath,0766,true);
    }
    if (!in_array($level,array('L','M','Q','H'))) {
        $level = 'L';
    }
    $fileName = date('YmdHis') . '_' . uniqid() . '.png';
    $file = $filePath . '/' . $fileName;

    QRcode::png($data,$file,$level,2);

    if (file_exists($file)) {
        return $file;
    } else {
        return false;
    }
}

 




 

相关文章

(1)创建数据表: CREATE TABLE IF NOT EXISTS `think_form` ...
组合查询的主体还是采用数组方式查询,只是加入了一些特殊的...
(1)创建模版:/App/Home/View/Form/edit.html   <FORM m...
自定义配置文件user.php: <?php return array(    \'se...
在一些成熟的CMS系统中,后台一般都包含一个配置中心(如织梦...
废话不多说先上图预览下,即本博客的分页; 这个分页类是在...