小智 168
值得补充的是,除了@abaumg发布的QR码库之外,Google还提供了一个QR码API QR Code API 非常感谢@Toukakoukan的链接更新.
要使用它,基本上:
https://chart.googleapis.com/chart?chs=300x300&cht=qr&chl=http%3A%2F%2Fwww.google.com%2F&choe=UTF-8
Run Code Online (Sandbox Code Playgroud)
300x300 是您要生成的QR图像的大小,chl是您要更改为QR码的网址编码字符串,以及choe是(可选的)编码.上面的链接提供了更多细节,但是使用它只是让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" />
Run Code Online (Sandbox Code Playgroud)
演示:
aba*_*umg 83
使用PHP生成QR代码的最简单方法是phpqrcode库.
Raf*_*shi 22
该phpqrcode库是非常快的配置和API文档是很容易理解.
除了abaumg的回答,我还附上PHP了http://phpqrcode.sourceforge.net/examples/index.php中的两个例子.
1.二维码编码器
首先包括本地路径中的库
include('../qrlib.php');
Run Code Online (Sandbox Code Playgroud)
然后直接输出图像作为PNG流做例如:
QRcode::png('your texte here...');
Run Code Online (Sandbox Code Playgroud)
将结果保存在本地作为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);
Run Code Online (Sandbox Code Playgroud)
2. QR码解码器
另见zxing解码器:
http://zxing.org/w/decode.jspx
检查输出非常有用.
3.数据格式列表
您可以根据数据类型在QR码中使用的数据格式列表:
http://)http://blog.thenetimpact.com/2011/07/decoding-qr-codes-how-to-format-data-for-qr-code-generators/上的更多数据类型
Iwa*_*aru 14
所述endroid/QRCODE库易于使用,保持良好,并且可以使用作曲家安装.还有一个可以直接与Symfony一起使用的软件包.
安装:
$ composer require endroid/qrcode
Run Code Online (Sandbox Code Playgroud)
用法:
<?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()));
Run Code Online (Sandbox Code Playgroud)
我知道问题是如何使用 PHP 生成 QR 码,但对于其他正在寻找一种方法来为网站生成代码的人来说,使用纯 JavaScript 执行此操作是一个很好的方法。在jQuery的QR码的jQuery插件是否良好。
我一直在使用google qrcode api,但我不太喜欢这个,因为它需要我在互联网上访问生成的图像。
我做了一些命令行研究,发现 linux 有一个qrencode用于生成二维码的命令行工具。
我写了这个小脚本。好的部分是生成的图像小于 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);
Run Code Online (Sandbox Code Playgroud)
然后在 html 中加载图像:
<img class="emrQRCode" src="<?=$imgData ?>" />
Run Code Online (Sandbox Code Playgroud)
你只需要安装它。[Linux 上的大多数成像应用程序都会在您没有意识到的情况下将其安装在引擎盖下。