Home > Article > Backend Development > Detailed explanation of how PHP generates QR codes based on the phpqrcode class
The example in this article describes how PHP generates QR codes based on the phpqrcode class. Share it with everyone for your reference, the details are as follows:
It is quite difficult to generate QR codes using PHP language. Of course, except for calling the interface that generates QR code images (such as the interface of Liantu http://www.liantu.com/), if you write the code yourself, There's really no way to start. However, we can use phpqrcode, a ready-made class file and PHP QR code generation class library, to easily generate QR codes.
Preparation:
1. Download the phpqrcode class file, download address: https://sourceforge.net/projects/phpqrcode/
2. The PHP environment must enable GD2 extension library support (usually enabled)
Interpretation of method:
The downloaded class file is a compressed package, which contains many files and demonstration programs. We only need the phpqrcode.php file inside to generate the QR code. It is a collection file of multiple classes. We need to use the png() method (line 3090) of the QRcode class (line 2963) inside:
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); }
The first parameter $text: The content contained in the QR code, which can be a link, text, json string, etc.;
The second parameter $outfile: The default is false, no file is generated, and only the QR code image is returned to the output; otherwise, the file name and path to store the generated QR code image need to be given;
The third parameter$level: The default is L , the values that can be passed by this parameter are L(QR_ECLEVEL_L, 7%), M(QR_ECLEVEL_M, 15%), Q(QR_ECLEVEL_Q, 25%), H(QR_ECLEVEL_H, 30%). This parameter controls the error tolerance rate of the QR code. Different parameters represent the percentage of the area that can be covered by the QR code, that is, the covered area can still be recognized;
The fourth parameter$size: controls the size of the generated image, the default is 4;
The 5th parameter$margin: Controls the size of the blank area for generating QR codes;
The 6th parameter$saveandprint: Saves the QR code image and displays it , $outfile must pass the image path;
Usage example:
1. Generate QR code (generate picture file)
// 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="使用微信扫描支付">'; } //调用查看结果 echo scerweima('https://www.baidu.com');
2. Add logo to the generated QR code (generate image file)
//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="使用微信扫描支付">'; } //调用查看结果 echo scerweima1('https://www.baidu.com');
3. Generate QR code (do not generate image files)
//3. 生成原始的二维码(不生成图片文件) function scerweima2($url=''){ require_once 'phpqrcode.php'; $value = $url; //二维码内容 $errorCorrectionLevel = 'L'; //容错级别 $matrixPointSize = 5; //生成图片大小 //生成二维码图片 $QR = QRcode::png($value,false,$errorCorrectionLevel, $matrixPointSize, 2); } //调用查看结果 scerweima2('https://www.baidu.com');
The first two methods will generate a QR code image locally every time they are called. The third method will directly output the QR code to the browser without generating a file.
The above is the detailed content of Detailed explanation of how PHP generates QR codes based on the phpqrcode class. For more information, please follow other related articles on the PHP Chinese website!