search
HomeBackend DevelopmentPHP ProblemHow to generate and identify QR codes with PHP

This article will introduce to you how to generate and identify QR codes in PHP. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.

How to generate and identify QR codes with PHP

Classification of QR codes

  • Linear stacked QR codes

  • Matrix QR code

Advantages and disadvantages of QR code

Advantages

  • Large information capacity

  • Wide coding range

  • Strong fault tolerance

  • High decoding reliability

  • Encryption measures can be introduced

  • Low cost and easy to make

Disadvantages

  • QR code technology has become a new channel for the spread of mobile phone viruses and phishing websites

  • Information leakage

  • PDF417: Chinese is not supported

  • DM: The patent is not disclosed and patent fees need to be paid

  • ##QR CODE : Patent disclosed, supports Chinese

QR CODE error correction capability

  • L level: approximately 7% of data codewords can be corrected

  • M level: approximately 15% of the data code words can be corrected

  • Q level: approximately 25% of the data code words can be corrected

  • Level H: approximately 30% of the data code words can be corrected

Prerequisite

  • GD Library

1. PHP generates QR CODE

  • Official website: http://phpqrcode.sourceforge.net/

  • Download source code: https://github.com/endroid/qr-code

How to generate and identify QR codes with PHP

##1、qrcode_create.php

ps: Generate ordinary QR code

<?php
include_once "./qrcode/phpqrcode.php";

/**
 * 参数:p1:二维码包含的内容 p2:输出的文件名 p3:容错级别 p4:大小 p5:外边距margin p6:保存路径
 * 在浏览器上直接生成一个二维码(内容为abc)
 */
QRcode::png("abc");
QRcode::png("ABC",false,QR_ECLEVEL_L,10,5,false);

/**
 * 生成文件到本地
 * 参数:p1:二维码包含的内容 p2:输出的文件名 p3:容错级别 p4:大小 p5:外边距margin p6:是否保存并打印(false 直接生成 true 生成且打印)
 * PS:$saveandprint源码的p6参数做了修改
 */

QRcode::png("ABC","ABC.jpg",QR_ECLEVEL_H,10,2,false);

2. qrcode_logo.php

ps: Generate QR code with Logo

<?php
/**
 * Created by PhpStorm.
 * User: user
 * Date: 2018/8/16
 * Time: 10:43
 */
include "./qrcode/phpqrcode.php";
$txt = "测试内容";
$picPathAndName = "./pic/ABC.jpg";//二维码保存路径和名称
$level = &#39;L&#39;;
$size = 5;
$is_logo = 1;//是否包含Logo 0否 1是
$margin = 2;//边距
$saveAndPrint = true;//是否保存,保存时,$picPathAndName设置为true

//生成二维码图片QRcode::png($txt, $picPathAndName, $level, $size, $margin,$saveAndPrint);

if($is_logo == 1){
    $QR = $picPathAndName; //已经生成的原始二维码图
    $Logo = &#39;./pic/logo.png&#39;;
    $Logo_re = &#39;./pic/test_logo.png&#39;;
    $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 / 5;
    $scale = $logo_width / $logo_qr_width;
    $logo_qr_height = $logo_height / $scale;
    $from_width = ($QR_width - $logo_qr_width) / 2;
    imagecopyresampled($QR, $Logo, $from_width, $from_width, 0, 0, $logo_qr_width, $logo_qr_height, $logo_width, $logo_height);
    //输出图片
    imagepng($QR, $Logo_re);
}

3、qrcode_vcard.php

(Generate electronic signature)vCard format: https://zh.wikipedia.org/wiki/VCard

<?php
/**
 * Created by PhpStorm.
 * User: user
 * Date: 2018/8/15
 * Time: 23:00
 */
require_once "./qrcode/phpqrcode.php";

/**
 * 生成电子签名
 * PS;使用微信扫描二维码
 */
$content = &#39;BEGIN:VCARD&#39; . "\n";//起始标志
$content .= &#39;VERSION:2.1&#39; . "\n";//当前版本
$content .= &#39;N:周&#39; . "\n";//姓
$content .= &#39;FN:勇&#39; . "\n";//名
$content .= &#39;ORG:江苏东大集成电路系统有限公司&#39; . "\n";//公司名称
$content .= &#39;TITLE:PHP研发程序员&#39; . "\n";//职位
$content .= &#39;TEL;WORK;VOICE:0523-83623173&#39; . "\n";//工作电话
$content .= &#39;ADR;WORK:;;高新区星火路#2;南京市;江苏省;225762;中国&#39; . "\n";//工作地址
$content .= &#39;ADR;HOME:;;下圩镇王横村178号;兴化市;江苏省;225762;中国&#39; . "\n";//家庭地址(街道,地级市,省,邮编,国家)
$content .= &#39;TEL;TYPE:18000001111&#39; . "\n";//移动电话
$content .= &#39;EMAIL:123456@qq.com&#39; . "\n";//邮箱
$content .= &#39;URL:www.baidu.com&#39; . "\n";//个人主页
$content .= &#39;END:VCARD&#39; . "\n";//结束标志
QRcode::png($content);

2. JQUERY generates QR CODE

Source code address: https://github.com/jeromeetienne/jquery-qrcode

##jquery_create.php

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Jquery生成二维码</title>
    <script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
    <script type="text/javascript" src="./jquery-qrcode/jquery.qrcode.min.js"></script>
</head>
<body>
<div id="qrcode"></div>
<script>
    //$(&#39;#qrcode&#39;).qrcode("this plugin is great");
    $(&#39;#qrcode&#39;).qrcode({width: 64,height: 64,text: "jason"});
</script>
</body>
</html>
3. PHP recognizes QR code

1. Method 1

Environment requirements, install the following extensions

- ImageMagick

- zbar

- php-zbarcode

2. Method 2

PHP identifies QR code (no need to install extension) , ordinary QR codes can be tested initially, QR codes with logo, the fault tolerance level needs to be set higher, the page can be

qrReader class: https://github.com/baagee/php_QrReader

<?php
include_once(&#39;./qrReader/lib/QrReader.php&#39;);
$qrcode = new QrReader(&#39;./test_logo.png&#39;);  //图片路径
$text = $qrcode->text(); //返回识别后的文本
echo $text;

Recommended learning:

php video tutorial

The above is the detailed content of How to generate and identify QR codes with PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:csdn. If there is any infringement, please contact admin@php.cn delete
ACID vs BASE Database: Differences and when to use each.ACID vs BASE Database: Differences and when to use each.Mar 26, 2025 pm 04:19 PM

The article compares ACID and BASE database models, detailing their characteristics and appropriate use cases. ACID prioritizes data integrity and consistency, suitable for financial and e-commerce applications, while BASE focuses on availability and

PHP Secure File Uploads: Preventing file-related vulnerabilities.PHP Secure File Uploads: Preventing file-related vulnerabilities.Mar 26, 2025 pm 04:18 PM

The article discusses securing PHP file uploads to prevent vulnerabilities like code injection. It focuses on file type validation, secure storage, and error handling to enhance application security.

PHP Input Validation: Best practices.PHP Input Validation: Best practices.Mar 26, 2025 pm 04:17 PM

Article discusses best practices for PHP input validation to enhance security, focusing on techniques like using built-in functions, whitelist approach, and server-side validation.

PHP API Rate Limiting: Implementation strategies.PHP API Rate Limiting: Implementation strategies.Mar 26, 2025 pm 04:16 PM

The article discusses strategies for implementing API rate limiting in PHP, including algorithms like Token Bucket and Leaky Bucket, and using libraries like symfony/rate-limiter. It also covers monitoring, dynamically adjusting rate limits, and hand

PHP Password Hashing: password_hash and password_verify.PHP Password Hashing: password_hash and password_verify.Mar 26, 2025 pm 04:15 PM

The article discusses the benefits of using password_hash and password_verify in PHP for securing passwords. The main argument is that these functions enhance password protection through automatic salt generation, strong hashing algorithms, and secur

OWASP Top 10 PHP: Describe and mitigate common vulnerabilities.OWASP Top 10 PHP: Describe and mitigate common vulnerabilities.Mar 26, 2025 pm 04:13 PM

The article discusses OWASP Top 10 vulnerabilities in PHP and mitigation strategies. Key issues include injection, broken authentication, and XSS, with recommended tools for monitoring and securing PHP applications.

PHP XSS Prevention: How to protect against XSS.PHP XSS Prevention: How to protect against XSS.Mar 26, 2025 pm 04:12 PM

The article discusses strategies to prevent XSS attacks in PHP, focusing on input sanitization, output encoding, and using security-enhancing libraries and frameworks.

PHP Interface vs Abstract Class: When to use each.PHP Interface vs Abstract Class: When to use each.Mar 26, 2025 pm 04:11 PM

The article discusses the use of interfaces and abstract classes in PHP, focusing on when to use each. Interfaces define a contract without implementation, suitable for unrelated classes and multiple inheritance. Abstract classes provide common funct

See all articles

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

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft