qr码生成

Used widely in various recent applications, QR Codes can be seen on cola cans, business cards, in sushi bars, and in museums. QR Code is a 2-dimensional barcode specification that was invented in Japan. It is patented. but it’s inventor, Denso Wave, chose not to exercise it and left the standard open for the benefit of all. The code has since grown in popularity because of its ability to include a lot of data in a single image and the proliferation of smartphones with scanning apps.

QR码广泛用于各种最新应用中,可乐罐,名片,寿司店和博物馆中都可以看到QR码。 QR Code是日本发明的二维条形码规格。 已申请专利。 但是它的发明者Denso Wave选择不使用它,并开放标准以造福所有人。 此代码由于具有在单个图像中包含大量数据的能力以及带有扫描应用程序的智能手机的泛滥而变得越来越流行。

In this article I’ll show you how you can easily generate QR Codes from within your PHP application and share some ideas on how and when to use them, We’ll be using PHP QR Code, a library written in PHP for generating QR Codes and which doesn’t require any dependencies beyond the standard GD2 graphics extension for creating images.

在本文中,我将向您展示如何轻松地从PHP应用程序中生成QR代码,并分享有关如何以及何时使用它们的一些想法,我们将使用PHP QR Code ( PHP用PHP编写的用于生成QR代码的库)并且除了用于创建图像的标准GD2图形扩展之外,不需要任何依赖项。

生成您的第一个QR码 (Generating your First QR Code)

Start by downloading the latest PHP QR Code library from GitHub. I’ll assume you’ve extracted it successfully and you can go to http://localhost/phpqrcode in your development environment to find the demo version working. You can insert whatever text in the data field you want to be converted to a QR Code image as shown in the screenshot below. If you have any problem getting this to work, make sure you have PHP installed with the GD2 extension, double checking this if necessary using a PHP info page.

首先从GitHub下载最新PHP QR Code库。 我假设您已成功提取它,并且可以在开发环境中访问http:// localhost / phpqrcode以找到演示版本。 您可以在数据字段中插入要转换为QR Code图像的任何文本,如下面的屏幕快照所示。 如果您有任何问题要使它起作用,请确保已安装了带有GD2扩展名PHP,并在必要时使用PHP信息页仔细检查。

Create a new PHP script with the following code:

使用以下代码创建一个新PHP脚本:

<?php
include "phpqrcode/qrlib.php";
// create a QR Code with this text and display it
QRcode::png("My First QR Code");

You see how simple it is? With just two lines of code you get a perfectly good QR Code for your application. The opportunities are endless! But wait, this obviously isn’t the full story. The library has more features worth looking at.

你看这有多简单? 仅需两行代码,您就可以为您的应用程序获得完美的QR码。 机会无穷! 但是,等等,这显然不是全部。 该库具有更多值得关注的功能。

PHP QR代码库的功能 (Features of the PHP QR Code Library)

For a full blown example, try this code:

有关完整示例,请尝试以下代码:

<?php
QRcode::png("http://www.sitepoint.com", "test.png", "L", 4, 4);

The first parameter specifies the text or data which will be encoded into the image and is passed as a normal string. The second parameter is the name of the output file for the generated PNG image, if any. The default value is a boolean false, in which case the image is flushed to the browser.

第一个参数指定将被编码到图像中并作为普通字符串传递的文本或数据。 第二个参数是生成的PNG图像的输出文件的名称(如果有)。 默认值为布尔值false,在这种情况下,图像将刷新到浏览器。

The third parameter is the level of error correction for the generated barcode, passed as a single letter string. This specifies how much of the data’s codewords (8-bits per codeword) can be restored for a distorted or damaged QR Code image using the Reed-Solomon error correction algorithm. The higher the correction level, the less the data capacity of the barcode can be for a given dimension. Below is a table mapping the levels to their restore percentages and the string constants used when calling the QRcode::png(). (I’ve compiled the table from the Wikipedia article on QR Codes and the method signature in the PHP QR Code library.)

第三个参数是生成的条形码的纠错级别,以单个字母字符串形式传递。 这指定了使用Reed-Solomon纠错算法可以为失真或损坏的QR码图像恢复多少数据的码字(每个码字8位)。 校正级别越高,条形码对于给定尺寸的数据容量越小。 下表是将级别映射到其还原百分比以及调用QRcode::png()时使用的字符串常量的表。 (我从Wikipedia上关于QR Code的文章和PHP QR Code库中的方法签名编译了表格。)

The fourth parameter specifies the size of each of the barcode code squares measured in pixels. Each code square (also named “pixels” or “modules”) is 4×4px. The fifth parameter specifies the white margin boundary around the barcode, measured in code squares (eg. A 16px margin on each side for 4×4px code square).

第四个参数指定以像素为单位测量的每个条形码平方的大小。 每个代码方块(也称为“像素”或“模块”)为4×4px。 第五个参数指定条形码周围的白色边距边界,以代码正方形为单位进行度量(例如,对于4×4px代码正方形,每侧16px的边距)。

The library supports exporting PNG, SVG, and EPS images, and you can produce QR Codes in any of these formats simply by changing the method name from png() to svg() or eps() and use the correct extension for the generated image’s filename.

该库支持导出PNG,SVG和EPS图像,您只需将方法名称从png()更改为svg()eps()并为生成的图像使用正确的扩展名,即可生成任何一种格式的QR码。文档名称。

Also, you can change the background and foreground colors by passing them as additional parameters:

另外,您可以通过将背景和前景色作为附加参数传递来更改它们:

<?php
$backColor = 0xFFFF00;
$foreColor = 0xFF00FF;
// Create a QR Code and export to SVG
QRcode::svg("http://www.sitepoint.com", "test-me.svg", "L", 4, 4, false, $backColor, $foreColor);

The sixth parameter (false in the example above) seems to be a useless parameter. It should be true for saving to a file and exporting to the browser, but it simply didn’t work for me after checking it several times, so keep it false.

第六个参数(在上面的示例中为false)似乎是一个无用的参数。 保存到文件并导出到浏览器应该是正确的,但是经过多次检查后,它对我来说根本不起作用,因此请将其设置为false。

The library has more features that you can check out if you’d like, for example caching and benchmarking the image generation.

该库具有更多功能,您可以根据需要进行检查,例如缓存和基准化图像生成。

获取最终条形码的大小 (Getting the Size of the Final Bar Code)

To get the final size of the image in advance, here’s a simple formula that can use (since the image is a square, we only need to calculate a single dimension and the other will be the same):

要预先获取图像的最终尺寸,以下是一个可以使用的简单公式(由于图像是正方形,因此我们只需要计算一个尺寸,另一个尺寸将是相同的):

Image Size (px) = (Pixels per Module) × (Module Size + 8)

Where as stated earlier, Pixels per Module is specified in the method call as the fourth parameter and the Module Size is selected from these barcode sizing tables as follows:

如前所述,在方法调用中将每个模块的像素数指定为第四个参数,并从这些条形码尺寸表中选择模块尺寸,如下所示:

  1. Select the column of the string type (data bits, numeric, alphanumeric, binary, or Kanji). These specify the maximum data length of such type to be packed in a certain barcode. Earlier I used alphanumeric, but if you are using UTF-8 encoded strings then you may be using the binary type instead. Kanji is for Japanese, but is not tested by the library author.选择字符串类型的列(数据位,数字,字母数字,二进制或汉字)。 这些指定了要包装在特定条形码中的此类数据的最大数据长度。 之前我使用字母数字,但是如果您使用的是UTF-8编码的字符串,则可能会使用二进制类型。 汉字适用于日语,但未经库作者测试。
  2. Choose the desired level of error correction and for your string length find the minimum version number that can handle at least that many characters. The example used 24 or more characters of alphanumeric type at level L, so the value will be version 1 first row.选择所需的错误纠正级别,并为您的字符串长度找到可以处理至少这么多字符的最小版本号。 该示例在级别L处使用了24个或更多字母数字类型的字符,因此该值将是版本1第一行。
  3. Get the module for the version you choose, here it will be module 21×21, where the module size will be 21. The PHP QR Code library takes the next version up instead for more room as a safety, so then go up one more.获取您选择的版本的模块,这里是21×21模块,模块大小为21。PHPQR Code库采用下一个版本代替,以确保安全,因此再增加一个版本。

If you calculate the module size for the version used for the example, you will find that the produced image size should be:

如果为示例使用的版本计算模块大小,则会发现生成的图像大小应为:

Image Size = 4 × (21 + 8) = 116×116px

But the image generated is 132×132px instead. PHP QR Code took the next version (version 2 instead of version 1, or simply module 25×25), so the actual generated size will be:

但是生成的图像是132×132px。 PHP QR Code采用了下一个版本(版本2,而不是版本1,或者仅是25×25模块),因此实际生成的大小为:

Image Size = 4 × (25 + 8) = 132×132px

QR码的常见用途 (Common Uses for QR Codes)

The most common application for QR Codes is to encode website URLs, such as that to a Facebook fan page of your latest product, your company, etc. The options are endless. I myself use it on my business card and encode the URL to my LinkedIn profile.

QR代码最常见的应用是对网站URL进行编码,例如将URL编码到最新产品,您公司的Facebook粉丝页面上。选项无穷无尽。 我自己在名片上使用它,并将URL编码为我的LinkedIn个人资料。

QR Codes can also store telephone numbers, vCards, and email addresses. Some sites put them alongside blog articles to act as bookmarks.

QR码还可以存储电话号码,vCard和电子邮件地址。 一些网站将它们与博客文章一起用作书签。

When it comes to using QR Codes, your only limits are really the data capacity of the code and the space you’ll display it in.

在使用QR码时,您唯一的限制实际上就是代码的数据容量以及您将在其中显示的空间。

摘要 (Summary)

In this article you’ve seen how to generate QR Codes easily in PHP for various print and web applications. I also showed you how to calculate the final generated image size in advance, since the library doesn’t provide such facility. In short this, working with QR Codes can be enjoyable and open a lot of opportunities. How can you enhance your PHP application with them?

在本文中,您已经了解了如何在PHP中轻松地为各种打印和Web应用程序生成QR码。 我还向您展示了如何预先计算最终生成的图像大小,因为该库没有提供这种功能。 简而言之,使用QR码可以很愉快,并带来很多机会。 如何使用它们增强PHP应用程序?

Image via Fotolia

图片来自Fotolia

翻译自: https://www.sitepoint.com/generate-qr-codes-in-php/

qr码生成

qr码生成_在PHP中生成QR码相关推荐

  1. python3 socketserver源码解析_解读python中SocketServer源码

    再看继承 真正的大餐来之前,还是来点儿开胃菜!回顾一下关于类的继承的知识: 我们先看上面的代码,这是一个简单的类继承,我们可以看到父类Base和子类Son,它们中各有一个Testfunc方法,当我们实 ...

  2. idea如何反编译字节码指令_美团点评:Java字节码增强技术,线上问题诊断利器...

    作者简介:泽恩,美团到店住宿业务研发团队工程师.文章转载于公众号:美团技术团队 1. 字节码 1.1 什么是字节码? Java之所以可以"一次编译,到处运行",一是因为JVM针对各 ...

  3. 在python中设置密码登录_在python中生成密码

    在python中生成密码 我想在python中生成一些字母数字密码. 一些可能的方法是: import string from random import sample, choice chars = ...

  4. java 中生成文档_在Java中生成专业的公文文档

    在Java中生成专业的公文文档 程序库:http://myxdoc.sohuapps.com/XDocService.jar 源码:http://myxdoc.sohuapps.com/XDocSer ...

  5. python随机生成英文字母_在Python中生成随机字母

    有没有一种方法可以在Python中生成随机字母(如random.randint,但用于字母)? random.randint的范围功能会很好,但是拥有仅输出随机字母的生成器总比没有好. 简单: > ...

  6. mysql随机生成密码存到哪里_在MySQL中生成随机密码的方法

    cat /dev/urandom | LC_ALL=C tr -dc "[:alnum:]" | fold -w 10 |head -10 Si1N9kUyuZ ddzsRXFgx ...

  7. css 加随机数 引用_在CSS中生成随机数

    Python部落(python.freelycode.com)组织翻译,禁止转载,欢迎转发. Robin Rendle 于2017年1月11日 前几天,我遇到了一个特别有趣的问题.我想用random ...

  8. Java生成pgp密钥对_在Javascript中生成PGP密钥对,并使用加密的PGP私钥对文本进行签名...

    我写的东西是 needs to do electronic signatures. 有些用户会像我这样的极客,并且已经拥有自己的PGP密钥.大多数人不会,也不会想要安装或维护它. 作为一种解决方案,我 ...

  9. 源码解读_入口开始解读Vue源码系列(二)——new Vue 的故事

    作者:muwoo 转发链接:https://github.com/muwoo/blogs/blob/master/src/Vue/2.md 目录 入口开始解读Vue源码系列(一)--造物创世 入口开始 ...

  10. java 字节码查看_一种查看java字节码时显示方法调用关系图的方法与流程

    本发明涉及一种代码逻辑分析方法,具体涉及一种查看java字节码时显示方法调用关系图的方法. 背景技术: 目前软件反编译领域有不少对可执行文件进行反编译的工具如IDA,也有对Java代码生成的中间码文件 ...

最新文章

  1. Swift-Tips之重复字符串
  2. java打印6个偶数_Java编写一个应用程序,打印所有偶数从2到100
  3. java ftp主动模式和被动模式_ftp主动模式和被动模式
  4. 实战Vue简易项目(2)定制开发环境
  5. c语言蓝牙接收6,终于搞定了通过两路蓝牙接收数据
  6. Redis工作笔记-spring-data-redis的基本使用
  7. .view-content:overflow:hidden 大错误
  8. Python爬虫入门教程 15-100 石家庄政民互动数据爬取
  9. SQL那些事儿(十二)--DATASET 与 DATAREADER区别
  10. 在指定文件类型中递归查找到目标字符串
  11. 小米手机android版本怎么更新,基于Android 10的MIUI稳定版正式推送,你的小米手机更新了吗?...
  12. 创建计算机桌面快捷方式图标异常,桌面图标显示异常怎么解决
  13. 腾讯文智自然语言处理-分词API Python小实验
  14. 立秋至,暑难消,e安在线信息安全快讯
  15. 2021-10-02
  16. Clover 驱动文件夹_软网推荐:原汁原味 备份还原驱动程序
  17. Java-实现购物车功能小系统
  18. Laravel repository数据仓库使用 Star.hou红楼一梦
  19. sca标准值_SOA标准之----SCA架构思想
  20. linux下 恢复gho镜像,手动使用ghost恢复win7系统gho镜像包教程

热门文章

  1. 阿里云 OSS + CDN 部署静态网站配置
  2. DDD,这东西到底是垃圾还是银弹?
  3. Totem协议(SRP/RRP)讲解PPT
  4. wxid转扫一扫添加好友
  5. php设计模式 参考地址
  6. python判断按键是否按下_Pygame检测是否按下了一个键?
  7. 1987年,国际C语言混乱代码大赛
  8. 重新开始学Java——java中的标识符(biao,zhi,fu)、关键字和代码规范
  9. 内存取证工具Volatility学习
  10. java导出多个excel,以压缩包下载