使用PHP动态生成QR码(二维码)

在线有许多开源库可用于生成快速响应 (QR) 代码。sourceforge中提供了一个用于在 PHP 中生成 QR 代码的优秀开源库。只需要下载并复制到项目文件夹中即可。这包括一个名为“phpqrcode”的模块,其中有一个名为“qrlib.php”的文件。该文件必须包含在代码中才能使用名为“png()”的函数,该函数位于 QRcode 类中。当我们传递一些文本作为参数时,.png() 函数会直接在浏览器中输出二维码,但我们也可以创建一个文件并将其存储。
句法:

QRcode::png($text, $file, $ecc, $pixel_Size, $frame_Size);

参数:该函数接受上面提到的五个参数,如下所述:

$text:该参数给出需要以二维码形式显示的消息。它是强制参数。
$file:指定生成的二维码的保存位置。
$ecc:该参数指定QR的纠错能力。它有 4 个级别 L、M、Q 和 H。
$pixel_Size:指定 QR 的像素大小。
$frame_Size:指定 Qr 的大小。它是从1-10级。

示例1:生成QR码的PHP程序。

<?php

// Include the qrlib file
include 'phpqrcode/qrlib.php';

// $text variable has data for QR
$text = "GEEKS FOR GEEKS";

// QR Code generation using png()
// When this function has only the
// text parameter it directly
// outputs QR in the browser
QRcode::png($text);
?>

输出


注意:此输出直接在浏览器中生成。此代码无法在在线 IDE 上运行,因为它不能包含“phpqrcode”模块。

示例2: PHP程序生成QR码并创建文件。

<?php
// Include the qrlib file
include 'phpqrcode/qrlib.php';
$text = "GEEKS FOR GEEKS";

// $path variable store the location where to
// store image and $file creates directory name
// of the QR code file by using 'uniqid'
// uniqid creates unique id based on microtime
$path = 'images/';
$file = $path.uniqid().".png";

// $ecc stores error correction capability('L')
$ecc = 'L';
$pixel_Size = 10;
$frame_Size = 10;

// Generates QR Code and Stores it in directory given
QRcode::png($text, $file, $ecc, $pixel_Size, $frame_size);

// Displaying the stored QR code from directory
echo "<center><img src='".$file."'></center>";
?>

 

输出

注意:两个示例的输出不同。在第一个示例中,输出将以直接在浏览器上生成的默认帧和像素大小显示,而第二个示例的输出是存储在目录中的像素和帧大小为 10 的“png”文件。

THE END
分享
二维码
海报
使用PHP动态生成QR码(二维码)
在线有许多开源库可用于生成快速响应 (QR) 代码。sourceforge中提供了一个用于在 PHP 中生成 QR 代码的优秀开源库。
<<上一篇
下一篇>>