QR码(二维码)是一款机器可读代码,由白色背景上的黑色正方形组成。
QR码通常用于存储相机可读的信息。
有时我们需要为应用程序中的产品,成员或者其他项目生成动态QR码。
我们将找到许多QR码生成器库来创建QR码。
但Google图表API提供了一种简单的方法,可以在没有任何库或者插件的情况下在PHP中生成QR码。
在本教程中,我们将展示如何使用Google Chart API和CURL使用PHP生成QR码。
我们的PHP QR Code Generator脚本允许我们为URL,文本,电子邮件,电话,短信,联系方式和其他内容生成动态QR码。
我们已创建名为“QR_BarCode”的PHP QR码生成器类,有助于生成QR码图像(PNG)或者将QR码图像另存为PNG文件。
<?php /** * QR_BarCode - Barcode QR Code Image Generator * @author onitroad * @url http://www.onitroad.com * @license http://www.onitroad.com/license/ */ class QR_BarCode{ //Google Chart API URL private $googleChartAPI = 'http://chart.apis.google.com/chart'; //Code data private $codeData; /** * URL QR code * @param string $url */ public function url($url = null){ $this->codeData = preg_match("#^https?\:\/\/#", $url) ? $url : "http://{$url}"; } /** * Text QR code * @param string $text */ public function text($text){ $this->codeData = $text; } /** * Email address QR code * * @param string $email * @param string $subject * @param string $message */ public function email($email = null, $subject = null, $message = null) { $this->codeData = "MATMSG:TO:{$email};SUB:{$subject};BODY:{$message};;"; } /** * Phone QR code * @param string $phone */ public function phone($phone){ $this->codeData = "TEL:{$phone}"; } /** * SMS QR code * * @param string $phone * @param string $text */ public function sms($phone = null, $msg = null) { $this->codeData = "SMSTO:{$phone}:{$msg}"; } /** * VCARD QR code * * @param string $name * @param string $address * @param string $phone * @param string $email */ public function contact($name = null, $address = null, $phone = null, $email = null) { $this->codeData = "MECARD:N:{$name};ADR:{$address};TEL:{$phone};EMAIL:{$email};;"; } /** * Content (gif, jpg, png, etc.) QR code * * @param string $type * @param string $size * @param string $content */ public function content($type = null, $size = null, $content = null) { $this->codeData = "CNTS:TYPE:{$type};LNG:{$size};BODY:{$content};;"; } /** * Generate QR code image * * @param int $size * @param string $filename * @return bool */ public function qrCode($size = 200, $filename = null) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $this->googleChartAPI); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, "chs={$size}x{$size}&cht=qr&chl=" . urlencode($this->codeData)); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HEADER, false); curl_setopt($ch, CURLOPT_TIMEOUT, 30); $img = curl_exec($ch); curl_close($ch); if($img) { if($filename) { if(!preg_match("#\.png$#i", $filename)) { $filename .= ".png"; } return file_put_contents($filename, $img); } else { header("Content-type: image/png"); print $img; return true; } } return false; } } ?>
要创建QR码PNG图像,请使用以下内容QR_BarCode类。
//include QR_BarCode class include "QR_BarCode.php"; //QR_BarCode object $qr = new QR_BarCode(); //create text QR code $qr->text('onitroad'); //display QR code image $qr->qrCode();
上面的示例代码将生成并显示如下所示的QR码。
如果要保存QR代码镜像,请使用以下类似QR_BarCode类。
<?php //save QR code image $qr->qrCode(350,'images/cw-qr.png');
QR_BarCode类在PHP中生成不同类型的QR码。
<?php //create url QR code $qr->url('URL'); //create text QR code $qr->text('textContent'); //create email QR code $qr->email('emailAddress', 'subject', 'message'); //create phone QR code $qr->phone('phoneNumber'); //create sms QR code $qr->sms('phoneNumber', 'message'); //create contact QR code $qr->contact('name', 'address', 'phone', 'email'); //create content QR code $qr->content('type', 'size', 'content');
日期:2020-06-02 22:15:30 来源:oir作者:oir