用 PHP 动态生成二维码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5943368/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 22:58:44  来源:igfitidea点击:

Dynamically generating a QR code with PHP

phpqr-code

提问by AKor

I'm trying to generate QR codes on my website. All they have to do is have a URL in them, which a variable on my site will provide. What would be the easiest way to do this?

我正在尝试在我的网站上生成二维码。他们所要做的就是在其中包含一个 URL,我网站上的一个变量将提供该 URL。什么是最简单的方法来做到这一点?

回答by

It's worth adding that, in addition to the QR codes library posted by @abaumg, Google provides a QR Codes APIQR Codes APImany thanks to @Toukakoukanfor the link update.

值得补充的是,除了@abaumg发布的二维码库之外,Google 还提供了一个二维码 APIQR Codes API非常感谢@Toukakoukan链接更新

To use this , basically:

要使用 this ,基本上:

https://chart.googleapis.com/chart?chs=300x300&cht=qr&chl=http%3A%2F%2Fwww.google.com%2F&choe=UTF-8
  • 300x300is the size of the QR image you want to generate,
  • the chlis the url-encoded string you want to change into a QR code, and
  • the choeis the (optional) encoding.
  • 300x300是您要生成的 QR 图像的大小,
  • chl是你想改变成一个QR代码的URL编码字符串,
  • choe是(可选的)编码。

The link, above, gives more detail, but to use it just have the srcof an image point to the manipulated value, like so:

上面的链接提供了更多详细信息,但要使用它,只需src将图像指向操作值,如下所示:

<img src="https://chart.googleapis.com/chart?chs=300x300&cht=qr&chl=http%3A%2F%2Fwww.google.com%2F&choe=UTF-8" title="Link to Google.com" />

Demo:

演示:

回答by abaumg

The easiest way to generate QR codes with PHP is the phpqrcode library.

使用 PHP 生成 QR 码的最简单方法是phpqrcode 库

回答by RafaSashi

The phpqrcode libraryis really fast to configure and the API documentation is easy to understand.

phpqrcode库是非常快的配置和API文档是很容易理解。

In addition to abaumg's answerI have attached 2 examples in PHPfrom http://phpqrcode.sourceforge.net/examples/index.php

除了 abaumg 的回答,我还附上了PHP来自http://phpqrcode.sourceforge.net/examples/index.php 的2 个例子

1. QR code encoder

1.二维码编码器

first include the library from your local path

首先包含本地路径中的库

include('../qrlib.php');

then to output the image directly as PNG stream do for example:

然后将图像直接输出为 PNG 流,例如:

QRcode::png('your texte here...');

to save the result locally as a PNG image:

将结果本地保存为 PNG 图像:

$tempDir = EXAMPLE_TMP_SERVERPATH;

$codeContents = 'your message here...';

$fileName = 'qrcode_name.png';

$pngAbsoluteFilePath = $tempDir.$fileName;
$urlRelativeFilePath = EXAMPLE_TMP_URLRELPATH.$fileName;

QRcode::png($codeContents, $pngAbsoluteFilePath); 

2. QR code decoder

2.二维码解码器

See also the zxingdecoder:

另见zxing解码器:

http://zxing.org/w/decode.jspx

http://zxing.org/w/decode.jspx

Pretty useful to check the output.

检查输出非常有用。

3. List of Data format

3. 数据格式列表

A list of data format you can use in your QR code according to the data type :

根据数据类型,您可以在二维码中使用的数据格式列表:

  • Website URL: http://stackoverflow.com(including the protocole http://)
  • email address: mailto:[email protected]
  • Telephone Number: +16365553344 (including country code)
  • SMS Message: smsto:number:message
  • MMS Message: mms:number:subject
  • YouTube Video: youtube://ID (may work on iPhone, not standardized)
  • 网址:http: //stackoverflow.com(包括协议http://
  • 电子邮件地址:mailto:[email protected]
  • 电话号码:+16365553344(含国家代码)
  • 短信:smsto:number:message
  • 彩信:彩信:号码:主题
  • YouTube 视频:youtube://ID(可能适用于 iPhone,未标准化)

回答by Iwazaru

The endroid/QrCode libraryis easy to use, well maintained, and can be installed using composer. There is also a bundleto use directly with Symfony.

所述endroid / QRCODE库易于使用,保持良好,并且可以使用作曲家安装。还有一个可以直接与 Symfony 一起使用的

Installing :

安装:

$ composer require endroid/qrcode

Usage :

用法 :

<?php

use Endroid\QrCode\QrCode;

$qrCode = new QrCode();
$qrCode
    ->setText('Life is too short to be generating QR codes')
    ->setSize(300)
    ->setPadding(10)
    ->setErrorCorrection('high')
    ->setForegroundColor(array('r' => 0, 'g' => 0, 'b' => 0, 'a' => 0))
    ->setBackgroundColor(array('r' => 255, 'g' => 255, 'b' => 255, 'a' => 0))
    ->setLabel('Scan the code')
    ->setLabelFontSize(16)
    ->setImageType(QrCode::IMAGE_TYPE_PNG)
;

// now we can directly output the qrcode
header('Content-Type: '.$qrCode->getContentType());
$qrCode->render();

// or create a response object
$response = new Response($qrCode->get(), 200, array('Content-Type' => $qrCode->getContentType()));

The generated QRCode

The generated QRCode

回答by praba230890

qrcode-generatoron Github. Simplest script and works like charm.

Github 上的二维码生成器。最简单的脚本,就像魅力一样工作。

Pros:

优点:

  • No third party dependency
  • No limitations for the number of QR code generations
  • 无第三方依赖
  • 二维码生成次数没有限制

回答by Peter

I have been using google qrcode api for sometime, but I didn't quite like this because it requires me to be on the Internet to access the generated image.

我一直在使用google qrcode api,但我不太喜欢这个,因为它需要我在互联网上访问生成的图像。

I did a little comand-line research and found out that linux has a command line tool qrencodefor generating qr-codes.

我做了一些命令行研究,发现 linux 有一个qrencode用于生成二维码的命令行工具。

I wrote this little script. And the good part is that the generated image is less than 1KB in size. Well the supplied data is simply a url.

我写了这个小脚本。好的部分是生成的图像小于 1KB。那么提供的数据只是一个网址。

$url = ($_SERVER['HTTPS'] ? "https://" : "http://").$_SERVER['HTTP_HOST'].'/profile.php?id='.$_GET['pid'];
$img = shell_exec('qrencode --output=- -m=1 '.escapeshellarg($url));

$imgData = "data:image/png;base64,".base64_encode($img);

Then in the html I load the image:

然后在 html 中加载图像:

<img class="emrQRCode" src="<?=$imgData ?>" />

You just need to have installed it. [most imaging apps on linux would have installed it under the hood without you realizing.

你只需要安装它。[Linux 上的大多数成像应用程序都会在您没有意识到的情况下将其安装在引擎盖下。

回答by Quinn Comendant

I know the question is how to generate QR codes using PHP, but for others who are looking for a way to generate codes for websites doing this in pure javascript is a good way to do it. The jquery-qrcodejquery plugin does it well.

我知道问题是如何使用 PHP 生成 QR 码,但对于其他正在寻找一种方法来为网站生成代码的人来说,使用纯 JavaScript 执行此操作是一个很好的方法。在jQuery的QR码的jQuery插件是否良好。