前期准备:
1.phpqrcode类文件下载,下载地址: https://sourceforge.net/projects/phpqrcode/
2.PHP环境必须开启支持GD2扩展库支持(一般情况下都是开启状态)
方法解读:
下载下来的类文件是一个压缩包,里边包含很多文件和演示程序,我们只需要里边的phpqrcode.php(放在与index.php同级的目录)这一个文件就可以生成二维码了。它是一个多个类的集合文件,我们需要用到里边的QRcode类(第2963行)的png()方法(第3090行):
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必须传递图片路径;
使用示例:
- 生成二维码(不生成图片文件)
public function scerweima1(){
require_once 'phpqrcode.php';
$value = 'https://www.baidu.com'; //二维码内容
$errorCorrectionLevel = 'L'; //容错级别
$matrixPointSize = 5; //生成图片大小
//生成二维码图片
$QR = \QRcode::png($value,false,$errorCorrectionLevel, $matrixPointSize, 2);
exit();//不加这句会出现乱码的现象
}
- 生成二维码(生成图片文件)
public function scerweima2(){
require_once 'phpqrcode.php';
$value = 'https://www.baidu.com'; //二维码内容
$errorCorrectionLevel = 'H'; //容错级别
$matrixPointSize = 6; //生成图片大小
//生成二维码图片
$filename = 'public/qrcode/'.microtime().'.png';
\QRcode::png($value,$filename , $errorCorrectionLevel, $matrixPointSize, 2);
$QR = $filename; //已经生成的原始二维码图
$QR = imagecreatefromstring(file_get_contents($QR)); //转换为图像,不加的话后面imagepng和imagedestroy会报错,
//这两方法第一个参数为recourse类型,不加的话会报imagepng() expects parameter 1 to be resource, string given
//输出图片
imagepng($QR, 'public/qrcode/qrcode.png');
imagedestroy($QR);
return '<img src="/public/qrcode/qrcode.png" alt="使用微信扫描支付">';
}
- 在生成的二维码中加上logo(生成图片文件)
public function scerweima3(){
require_once 'phpqrcode.php';
$value = 'https://www.baidu.com'; //二维码内容
$errorCorrectionLevel = 'H'; //容错级别
$matrixPointSize = 6; //生成图片大小
//生成二维码图片
$filename = 'public/qrcode/'.microtime().'.png';
\QRcode::png($value,$filename , $errorCorrectionLevel, $matrixPointSize, 2);
$logo = 'public/image/1.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, 'public/qrcode/qrcode1.png');
imagedestroy($QR);
imagedestroy($logo);
return '<img src="/public/qrcode/qrcode1.png" alt="使用微信扫描支付">';
}
后两种方法,每调用一次都会在本地生成一张二维码图片,第一种方法,不生成文件,会直接输出二维码到浏览器中。
注意
$QR = imagecreatefromstring(file_get_contents($QR)); //转换为图像,不加的话后面imagepng和imagedestroy会报错,
//这两方法第一个参数为recourse类型,不加的话会报imagepng() expects parameter 1 to be resource, string given
* imagecreatefromstring() - 由文件或 URL 创建一个新图象。
* imagecreatefromjpeg() - 由文件或 URL 创建一个新图象。
* imagecreatefrompng() - 由文件或 URL 创建一个新图象。
* imagecreatefromgif() - 由文件或 URL 创建一个新图象。
* imagecreatetruecolor() - 新建一个真彩色图像
image.png
imagegif(resource $image[, string $filename])
imagejpeg(resource $image[, string $filename[, int $quality]])
imagepng(resource $image[, string $filename])
$image 为创建的图像资源;$filename 为可选参数,用来设置文件的保存路径,
如果设置为 NULL,则将会直接输出原始图像流;$quality 为可选参数,用来设置输出图片的质量,
范围从 0(最差质量,文件更小)到 100(最佳质量,文件最大)。默认为 IJG 默认的质量值(大约为 75)。