我正在尝试在我的网站上生成二维码。他们所要做的就是在其中包含一个 URL,我网站上的一个变量将提供该 URL。最简单的方法是什么?
值得补充的是,除了@abaumg发布的QR码库外,谷歌还提供了QR码APIQR码API非常感谢@Toukakoukan的链接更新 .
要使用它,基本上:
https://chart.googleapis.com/chart?chs=300x300&cht=qr&chl=http%3A%2F%2Fwww.google.com%2F&choe=UTF-8
300x300
是你要生成的二维码图片的大小,chl
是你要转成二维码的url编码字符串,和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" />
演示:
使用 PHP 生成 QR 码的最简单方法是 phpqrcode 库。
android/QrCode库使用方便,维护良好,可以使用composer安装。还有一个 bundle 可以直接与 Symfony 一起使用。
安装:
$ composer require endroid/qrcode
用法:
<?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()));
phpqrcode 库配置非常快,API 文档也很容易理解。
除了abaumg的回答我在
PHP
中附上了2个例子来自http://phpqrcode.sourceforge.net/examples/index.php
1。 QR码编码器
首先包含本地路径中的库
include('../qrlib.php');
然后将图像直接输出为 PNG 流,例如:
QRcode::png('your texte here...');
将结果保存为 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。二维码解码器
另见zxing解码器:
http://zxing.org/w/decode.jspx
对于检查输出非常有用。
3。数据格式列表
根据数据类型,您可以在 QR 码中使用的数据格式列表:
http://
)我知道问题是如何使用 PHP 生成 QR 码,但对于正在寻找一种方法来为网站生成代码的其他人来说,使用纯 javascript 执行此操作是一个很好的方法。 jquery-qrcode jquery 插件做得很好。
我一直在使用 google qrcode api 一段时间,但我不太喜欢这个,因为它需要我在互联网上才能访问生成的图像。
我做了一些命令行研究,发现 linux 有一个命令行工具
qrencode
用于生成二维码。
我写了这个小脚本。好的部分是生成的图像大小小于 1KB。好吧,提供的数据只是一个 url。
$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);
然后在 html 中加载图像:
<img class="emrQRCode" src="<?=$imgData ?>" />
你只需要安装它。 [Linux 上的大多数成像应用程序都会在您没有意识到的情况下将其安装在引擎盖下。