Home Backend Development PHP Tutorial Generate QR code using phpqrcode

Generate QR code using phpqrcode

Dec 13, 2019 pm 02:57 PM

It is quite difficult to use PHP language to generate QR codes, except for calling the interface to generate QR code images. If you write the code to generate it yourself, you really have 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.

Preliminary preparation:

1.phpqrcode class file 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. It contains many files and demonstration programs. We only need the phpqrcode.php file inside to generate a 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:

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);

}

Copy after login

The first parameter $text: two-dimensional The content contained in the code can be links, text, json strings, 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 you need to give Output the file name and path to store the generated QR code image;

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 the QR code can be covered, that is, the covered area is still Can be recognized;

The fourth parameter $size: controls the size of the generated image, the default is 4;

The fifth parameter $margin: controls 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;

Usage example:

1. Generate QR code ( Generate image file)

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

// 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 &#39;<img src="/static/imghw/default1.png"  data-src="qrcode.png"  class="lazy"   alt="使用微信扫描支付">&#39;;  

}

  

//调用查看结果

echo scerweima(&#39;https://www.baidu.com&#39;);

Copy after login

2. Add logo to the generated QR code (generate image file)

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

37

38

39

//2. 在生成的二维码中加上logo(生成图片文件)

function scerweima1($url=&#39;&#39;){

require_once &#39;phpqrcode.php&#39;;

$value = $url;//二维码内容 

$errorCorrectionLevel = &#39;H&#39;;//容错级别 

$matrixPointSize = 6;//生成图片大小 

//生成二维码图片

$filename = &#39;qrcode/&#39;.microtime().&#39;.png&#39;;

QRcode::png($value,$filename , $errorCorrectionLevel, $matrixPointSize, 2); 

$logo = &#39;qrcode/logo.jpg&#39;; //准备好的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, &#39;qrcode.png&#39;); 

imagedestroy($QR);

imagedestroy($logo);

return &#39;<img src="/static/imghw/default1.png"  data-src="qrcode.png"  class="lazy"   alt="使用微信扫描支付">&#39;;  

}

  

//调用查看结果

echo scerweima1(&#39;https://www.baidu.com&#39;);

Copy after login

Generate QR code using phpqrcode

3. Generate QR code (Do not generate image files)

1

2

3

4

5

6

7

8

9

10

11

//3. 生成原始的二维码(不生成图片文件)

function scerweima2($url=&#39;&#39;){

require_once &#39;phpqrcode.php&#39;;

$value = $url;//二维码内容

$errorCorrectionLevel = &#39;L&#39;;//容错级别

$matrixPointSize = 5;//生成图片大小 

//生成二维码图片

$QR = QRcode::png($value,false,$errorCorrectionLevel, $matrixPointSize, 2);

}

//调用查看结果

scerweima2(&#39;https://www.baidu.com&#39;);

Copy after login

* The first two methods will generate a QR code image locally every time it is called. The third method does not generate a file and will directly output the QR code to the browser. .

The above is the detailed content of Generate QR code using phpqrcode. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Describe the SOLID principles and how they apply to PHP development. Describe the SOLID principles and how they apply to PHP development. Apr 03, 2025 am 12:04 AM

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

Explain the concept of late static binding in PHP. Explain the concept of late static binding in PHP. Mar 21, 2025 pm 01:33 PM

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

How to automatically set permissions of unixsocket after system restart? How to automatically set permissions of unixsocket after system restart? Mar 31, 2025 pm 11:54 PM

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

How to debug CLI mode in PHPStorm? How to debug CLI mode in PHPStorm? Apr 01, 2025 pm 02:57 PM

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

How does session hijacking work and how can you mitigate it in PHP? How does session hijacking work and how can you mitigate it in PHP? Apr 06, 2025 am 12:02 AM

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

See all articles