php 二维码生成类
时间:2022-05-08
本文章向大家介绍php 二维码生成类,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
<?php
/**
* BarcodeQR - Code QR Barcode Image Generator (PNG)
* @package BarcodeQR
* @category BarcodeQR
* @name BarcodeQR
* @version 1.0
* @author Shay Anderson 05.11
* @link http://www.shayanderson.com/php/php-qr-code-generator-class.htm
* @license http://www.gnu.org/licenses/gpl.html GPL License
* This is free software and is distributed WITHOUT ANY WARRANTY
*/
final class BarcodeQR {
/**
* Google Chart API URL
*/
const API_CHART_URL = "http://chart.apis.google.com/chart";
/**
* Code data
* @var string $_data
*/
private $_data;
/**
* Bookmark code
* @param string $title
* @param string $url
*/
public function bookmark($title = null, $url = null) {
$this->_data = "MEBKM:TITLE:{$title};URL:{$url};;";
}
/**
* MECARD 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->_data = "MECARD:N:{$name};ADR:{$address};TEL:{$phone};EMAIL:{$email};;";
}
/**
* Create code with GIF, JPG, etc.
* @param string $type
* @param string $size
* @param string $content
*/
public function content($type = null, $size = null, $content = null) {
$this->_data = "CNTS:TYPE:{$type};LNG:{$size};BODY:{$content};;";
}
/**
* Generate QR code image
* @param int $size
* @param string $filename
* @return bool
*/
public function draw($size = 150, $filename = null) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, self::API_CHART_URL);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, "chs={$size}x{$size}&cht=qr&chl=" . urlencode($this->_data));
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;
}
/**
* Email address code
* @param string $email
* @param string $subject
* @param string $message
*/
public function email($email = null, $subject = null, $message = null) {
$this->_data = "MATMSG:TO:{$email};SUB:{$subject};BODY:{$message};;";
}
/**
* Geo location code
* @param string $lat
* @param string $lon
* @param string $height
*/
public function geo($lat = null, $lon = null, $height = null) {
$this->_data = "GEO:{$lat},{$lon},{$height}";
}
/**
* Telephone number code
* @param string $phone
*/
public function phone($phone = null) {
$this->_data = "TEL:{$phone}";
}
/**
* SMS code
* @param string $phone
* @param string $text
*/
public function sms($phone = null, $text = null) {
$this->_data = "SMSTO:{$phone}:{$text}";
}
/**
* Text code
* @param string $text
*/
public function text($text = null) {
$this->_data = $text;
}
/**
* URL code
* @param string $url
*/
public function url($url = null) {
$this->_data = preg_match("#^https?://#", $url) ? $url : "http://{$url}";
}
/**
* Wifi code
* @param string $type
* @param string $ssid
* @param string $password
*/
public function wifi($type = null, $ssid = null, $password = null) {
$this->_data = "WIFI:T:{$type};S{$ssid};{$password};;";
}
}
// 生成二维码 Demo
$qrimage_path = '/home/www';
$qr = new BarcodeQR();
$qr_text = '[User ID] Content';
$uri_hash = md5($user_id);
$qrimg_file = $qrimage_path . DIRECTORY_SEPARATOR . $uri_hash . '.png';
$qr->text($qr_text);
return $qr->draw(154, $qrimg_file);
- php概述
- php教程
- php环境搭建
- PHP书写格式
- php变量
- php常量
- PHP注释
- php数组
- php字符串 string
- PHP整型 integer
- PHP浮点型 float
- php布尔型
- php数据类型之数组
- php数据类型之对象
- php数据类型之null
- php数据类型之间的转换
- php运算符
- php表达式
- PHP循环控制
- PHP流程控制
- php函数
- php全局变量
- PHP魔术变量
- php命名空间
- php 日期
- PHP包含文件
- php文件
- PHP 文件上传
- php Cookies
- php Sessions
- php email
- php安全email
- php错误处理
- PHP异常处理
- php过滤器
- PHP 高级过滤器
- php json
- php 表单
- PHP MySQL 简介
- PHP 连接 MySQL
- php创建数据库
- php 创建表
- php mysq 插入数据
- PHP MySQL 插入多条数据
- PHP MySQL 预处理语句
- php mysql 读取数据
- php mysql where
- PHP MySQL Order By
- PHP MySQL Update
- PHP MySQL Delete
- php ODBC
- 基于Tensorflow的MNIST手写数字识别分类
- Yii框架ACF(accessController)简单权限控制操作示例
- tensorflow 动态获取 BatchSzie 的大小实例
- TP5.0框架实现无限极回复功能的方法分析
- Tensorflow之MNIST CNN实现并保存、加载模型
- tensorflow 大于某个值为1,小于为0的实例
- YII框架模块化处理操作示例
- 基于tensorflow for循环 while循环案例
- 浅谈Python 命令行参数argparse写入图片路径操作
- 硬核干货丨游戏大世界的超远视距处理手法,建议收藏!
- python实现npy格式文件转换为txt文件操作
- 从0开始打造UI框架:动态化框架Scrollview物理学算法解析
- 基于Keras的格式化输出Loss实现方式
- PHP信号处理机制的操作代码讲解
- php防止表单重复提交实例讲解