


Detailed explanation of the steps to generate QR code using PHP based on phpqrcode class
This time I will bring you a detailed explanation of the steps to generate a QR code based on the phpqrcode class in PHP. What are the precautions for PHP to generate a QR code based on the phpqrcode class. The following is a practical case, let's take a look.
It is quite difficult to generate QR codes using PHP language, except of course for calling the interface that generates QR code images (such as the interface of Liantu http://www.liantu.com/). If you write code generation yourself, you really have no way to start. However, we can use phpqrcode, a ready-made class file, PHP QR code generation class library, which can easily generate QR codes.
Preliminary preparation:
1.phpqrcode classFile download, 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 can be links, text, json strings, etc.;
The second parameter $outfile: The default is false, no file is generated, and only the second parameter is The QR code image is returned as 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 indicate that the QR code can be The percentage of the area covered, that is, the covered area can still be identified;
The fourth parameter$size: Controls the size of the generated image, the default is 4;
The fifth parameter$margin: Control the size of the blank area for generating QR codes;
The sixth parameter $saveandprint: Save the QR code image and display it, $outfile must pass the image path;
1. Generate QR code (generate image 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="/static/imghwm/default1.png" data-src="qrcode.png" class="lazy" alt="使用微信扫描支付">'; } //调用查看结果 echo scerweima('https://www.baidu.com');
2. Add the 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="/static/imghwm/default1.png" data-src="qrcode.png" class="lazy" alt="使用微信扫描支付">'; } //调用查看结果 echo scerweima1('https://www.baidu.com');3. 生成二维码(不生成图片文件)
//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');
I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the PHP Chinese website!
Recommended reading:
Detailed explanation of the steps to implement WeChat refund application with PHP
ThinkPHP steps to implement WeChat payment (jsapi payment) Detailed explanation
The above is the detailed content of Detailed explanation of the steps to generate QR code using PHP based on phpqrcode class. For more information, please follow other related articles on the PHP Chinese website!

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP is not dying, but constantly adapting and evolving. 1) PHP has undergone multiple version iterations since 1994 to adapt to new technology trends. 2) It is currently widely used in e-commerce, content management systems and other fields. 3) PHP8 introduces JIT compiler and other functions to improve performance and modernization. 4) Use OPcache and follow PSR-12 standards to optimize performance and code quality.

The future of PHP will be achieved by adapting to new technology trends and introducing innovative features: 1) Adapting to cloud computing, containerization and microservice architectures, supporting Docker and Kubernetes; 2) introducing JIT compilers and enumeration types to improve performance and data processing efficiency; 3) Continuously optimize performance and promote best practices.

In PHP, trait is suitable for situations where method reuse is required but not suitable for inheritance. 1) Trait allows multiplexing methods in classes to avoid multiple inheritance complexity. 2) When using trait, you need to pay attention to method conflicts, which can be resolved through the alternative and as keywords. 3) Overuse of trait should be avoided and its single responsibility should be maintained to optimize performance and improve code maintainability.

Dependency Injection Container (DIC) is a tool that manages and provides object dependencies for use in PHP projects. The main benefits of DIC include: 1. Decoupling, making components independent, and the code is easy to maintain and test; 2. Flexibility, easy to replace or modify dependencies; 3. Testability, convenient for injecting mock objects for unit testing.

SplFixedArray is a fixed-size array in PHP, suitable for scenarios where high performance and low memory usage are required. 1) It needs to specify the size when creating to avoid the overhead caused by dynamic adjustment. 2) Based on C language array, directly operates memory and fast access speed. 3) Suitable for large-scale data processing and memory-sensitive environments, but it needs to be used with caution because its size is fixed.

PHP handles file uploads through the $\_FILES variable. The methods to ensure security include: 1. Check upload errors, 2. Verify file type and size, 3. Prevent file overwriting, 4. Move files to a permanent storage location.

In JavaScript, you can use NullCoalescingOperator(??) and NullCoalescingAssignmentOperator(??=). 1.??Returns the first non-null or non-undefined operand. 2.??= Assign the variable to the value of the right operand, but only if the variable is null or undefined. These operators simplify code logic, improve readability and performance.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Dreamweaver Mac version
Visual web development tools

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

WebStorm Mac version
Useful JavaScript development tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Mac version
God-level code editing software (SublimeText3)