<?php // +---------------------------------------------------------------------- // | qq:1656312770 // +---------------------------------------------------------------------- // | Author: zhangbo // +---------------------------------------------------------------------- namespace app\index\controller; use app\BaseController; use app\common\validate\GenerateQrcode as GenerateQrcodeValidate; use think\facade\Db; class GenerateQrcode extends BaseController { /** * 二维码生成 * @author zhangbo */ public function qrindex(){ header(\'Access-Control-Allow-Origin:*\'); header(\'Access-Control-Allow-Methods:POST,GET\'); header(\'Access-Control-Allow-Headers:x-requested-with,content-type\'); require root_path().\'vendor/phpqrcode/phpqrcode.php\'; $code = new \QRcode(); if(request()->isPost()){ $param = request()->post(); $validate = new GenerateQrcodeValidate(); if (!$validate->scene(\'add\')->check($param)) { return error_code(10001, $validate->getError()); } $value ="user_id=".$param[\'user_id\']."&mobile=".$param[\'mobile\']."&course_id=".$param[\'course_id\']."&order_num=".$param[\'order_num\']; //二维码内容 $where = []; $where[\'a.user_id\'] = $param[\'user_id\']; $where[\'b.mobile\'] = $param[\'mobile\']; $where[\'a.course_manage_id\'] = $param[\'course_id\']; $where[\'a.order_num\'] = $param[\'order_num\']; $is_have = Db::table(\'zdcs_course_order\') ->alias("a") ->field("a.*,b.mobile") ->leftJoin(\'zdcs_user_info\'.\' b\', \'b.id = a.user_id\') ->where($where) ->find(); $is_have_manage = Db::table(\'zdcs_course_manage\')->where([\'id\'=>$param[\'course_id\'],\'status\'=>1])->find(); if($is_have && $is_have_manage){ $errorCorrectionLevel = \'H\'; //容错级别 $matrixPointSize = 5; //生成图片大小 $filename = root_path()."public/qrcode/".time().\'.png\'; $filename = str_replace("\\",\'/\',$filename); //反斜杠转化为正斜杠 $data = $code->png($value,$filename,$errorCorrectionLevel, $matrixPointSize, 2); $data= $filename; // $QR = $filename; //已经生成的原始二维码图片文件 exit(json_encode([\'code\'=>\'200\',\'msg\'=>\'OK\',\'data\'=>$data],JSON_UNESCAPED_UNICODE)); } if(!$is_have_manage){ exit(json_encode([\'code\'=>\'201\',\'msg\'=>\'该课程已下架啦!\'])); } if(!$is_have){ exit(json_encode([\'code\'=>\'201\',\'msg\'=>\'该订单不存在啦!\'])); } } return json([\'code\'=>\'500\',\'msg\'=>\'非法请求\']); } }
本文实例讲述了PHP基于phpqrcode类生成二维码的方法。分享给大家供大家参考,具体如下:
使用PHP语言生成二维码,还是挺有难度的,当然调用生成二维码图片的接口(比如:联图网http://www.liantu.com/的接口)除外,如果自己写代码生成,真的无从下手。然而,我们可以使用phpqrcode这个现成的类文件,PHP二维码生成类库,利用它可以轻松生成二维码。
前期准备:
1.phpqrcode类文件下载,下载地址:https://sourceforge.net/projects/phpqrcode/
2.PHP环境必须开启支持GD2扩展库支持(一般情况下都是开启状态)
方法解读:
下载下来的类文件是一个压缩包,里边包含很多文件和演示程序,我们只需要里边的phpqrcode.php这一个文件就可以生成二维码了。它是一个多个类的集合文件,我们需要用到里边的QRcode类(第2963行)的png()方法(第3090行):
|
1
2
3
4
5
|
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);
} |
第1个参数$text:二维码包含的内容,可以是链接、文字、json字符串等等;
第2个参数$outfile:默认为false,不生成文件,只将二维码图片返回输出;否则需要给出存放生成二维码图片的文件名及路径;
第3个参数$level:默认为L,这个参数可传递的值分别是L(QR_ECLEVEL_L,7%)、M(QR_ECLEVEL_M,15%)、Q(QR_ECLEVEL_Q,25%)、H(QR_ECLEVEL_H,30%),这个参数控制二维码容错率,不同的参数表示二维码可被覆盖的区域百分比,也就是被覆盖的区域还能识别;
第4个参数$size:控制生成图片的大小,默认为4;
第5个参数$margin:控制生成二维码的空白区域大小;
第6个参数$saveandprint:保存二维码图片并显示出来,$outfile必须传递图片路径;
使用示例:
1. 生成二维码(生成图片文件)
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
// 1. 生成原始的二维码(生成图片文件)function scerweima($url=\'\'){
require_once \'phpqrcode.php\';
$value = $url; //二维码内容
$errorCorrectionLevel = \'L\'; //容错级别
$matrixPointSize = 5; //生成图片大小
//生成二维码图片
$filename = \'qrcode/\'.microtime().\'.png\';
QRcode::png($value,$filename , $errorCorrectionLevel, $matrixPointSize, 2);
$QR = $filename; //已经生成的原始二维码图片文件
$QR = imagecreatefromstring(file_get_contents($QR));
//输出图片
imagepng($QR, \'qrcode.png\');
imagedestroy($QR);
return \'<img src="qrcode.png" alt="使用微信扫描支付">\';
}//调用查看结果 |
2. 在生成的二维码中加上logo(生成图片文件)
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
//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="使用微信扫描支付">\';
}//调用查看结果 |
3. 生成二维码(不生成图片文件)
|
1
2
3
4
5
6
7
8
9
10
11
|
//3. 生成原始的二维码(不生成图片文件)function scerweima2($url=\'\'){
require_once \'phpqrcode.php\';
$value = $url; //二维码内容
$errorCorrectionLevel = \'L\'; //容错级别
$matrixPointSize = 5; //生成图片大小
//生成二维码图片
$QR = QRcode::png($value,false,$errorCorrectionLevel, $matrixPointSize, 2);
}//调用查看结果 |
前两种方法,每调用一次都会在本地生成一张二维码图片,第三种方法,不生成文件,会直接输出二维码到浏览器中。
PS:这里再为大家推荐两款二维码相关在线工具供大家参考使用:
在线生成二维码工具(加强版)
http://tools.jb51.net/transcoding/jb51qrcode