php tp5生成条形码,thinkphp5 + barcode 生成条形码的方法
1、去官网下载类库 “https://www.barcodebakery.com/en/download”,选择自己的版本下载
2、解压放到“E:\phpstudy\PHPTutorial\WWW\guahao\vendor\下”,其中class文件是所有的类文件,生成条形码就是调用文件夹里的类,font文件是字体,index.php是一个可选择条件生成条形码的功能,是主程序的入口,test_1D.php是给的生成条形码的例子,test_1D.html是对应的渲染条形码的页面
3、我们可以直接使用官方给的例子(test_1D.php),复制到自己需要用的地方,然后根据自己的需求稍加改动即可,需要注意的是,加载第三方类库的路径需要改一下。
生成条形码的php代码<?php
namespace app\index\controller;
use think\Controller;
/**
* 条形码操作类
*/
class Barcode extends Controller
{
public function createBarcode()
{
$class_dir = VENDOR_PATH.'barcode/class/';
// Including all required classes
require_once($class_dir.'BCGFontFile.php');
require_once($class_dir.'BCGColor.php');
require_once($class_dir.'BCGDrawing.php');
require_once($class_dir.'BCGcode39.barcode.php');
// Loading Font
// 注意font和class是同一级文件夹
$font = new \BCGFontFile(VENDOR_PATH.'barcode/font/Arial.ttf', 18);// The arguments are R, G, B for color.
$color_black = new \BCGColor(0, 0, 0);
$color_white = new \BCGColor(255, 255, 255);
$drawException = null;
try {
$code = new \BCGcode39();
$code->setScale(2); // Resolution
$code->setThickness(30); // Thickness
$code->setForegroundColor($color_black); // Color of bars
$code->setBackgroundColor($color_white); // Color of spaces
$code->setFont($font); // Font (or 0) 0不显示文字
$text = isset($_GET['text']) ? $_GET['text'] : 'HELLO';
$code->parse($text); // Text
} catch(Exception $exception) {
$drawException = $exception;
}
/* Here is the list of the arguments
1 - Filename (empty : display on screen)
2 - Background color */
$drawing = new \BCGDrawing('', $color_white);
if($drawException) {
$drawing->drawException($drawException);
} else {
$drawing->setBarcode($code);
$drawing->draw();
}
// Header that says it is an image (remove it if you save the barcode to a file)
header('Content-Type: image/png');
header('Content-Disposition: inline; filename="barcode.png"');
// Draw (or save) the image into PNG format.
$drawing->finish(\BCGDrawing::IMG_FORMAT_PNG);
}
public function barcodedes()
{
return $this->fetch();
}
}
?>
接受渲染条形码的Html代码
当然,src还可以携带参数,只需更改以下代码
html代码
php代码
把$text = isset($_GET['text']) ? $_GET['text'] : 'HELLO';
改成$text = input('text'); //接收的参数
4、如果想把条形码保存到本地,在实例化“BCGDrawing”的时候填写保存路径即可// 文件路径
$file_dir = 'uploads/barcode/'.date('Y-m-d');
if (!file_exists($file_dir)) {
mkdir($file_dir,0755,true);
}
$imgUrl = $file_dir.'/'.time().'.png';
$class_dir = VENDOR_PATH.'barcode/class/';
// Including all required classes
require_once($class_dir.'BCGFontFile.php');
require_once($class_dir.'BCGColor.php');
require_once($class_dir.'BCGDrawing.php');
require_once($class_dir.'BCGcode39.barcode.php');
// Loading Font
// 注意font和class是同一级文件夹
$font = new \BCGFontFile(VENDOR_PATH.'barcode/font/Arial.ttf', 18);
// Don't forget to sanitize user inputs
// $text = isset($_GET['text']) ? $_GET['text'] : 'HELLO';
// The arguments are R, G, B for color.
$color_black = new \BCGColor(0, 0, 0);
$color_white = new \BCGColor(255, 255, 255);
$drawException = null;
try {
$code = new \BCGcode39();
$code->setScale(2); // Resolution
$code->setThickness(30); // Thickness
$code->setForegroundColor($color_black); // Color of bars
$code->setBackgroundColor($color_white); // Color of spaces
$code->setFont($font); // Font (or 0)
$text = input('text'); //接收的参数
$text = isset($text) ? $text :'无参数';
$code->parse($text); // Text
} catch(Exception $exception) {
$drawException = $exception;
}
/* Here is the list of the arguments
1 - Filename (empty : display on screen)
2 - Background color */
// 保存到本地 (路径,颜色)路径为空则表示显示到页面上
$drawing = new \BCGDrawing($imgUrl, $color_white);
if($drawException) {
$drawing->drawException($drawException);
} else {
$drawing->setBarcode($code);
$drawing->draw();
}
$drawing->finish(\BCGDrawing::IMG_FORMAT_PNG);
5、生成条形码之后,怎么判定条形码是否能用呢?可以把条形码保存成图片到本地,打开官网“https://www.onlinebarcodereader.com/”,上传刚刚生成的条形码,如果解析出的参数跟你输入的一样,说明条形码可以用。
php tp5生成条形码,thinkphp5 + barcode 生成条形码的方法相关推荐
- php tp5生成条形码,thinkphp5 + barcode 生成条形码
2.解压放到"E:phpstudyPHPTutorialWWWguahaovendor下",其中class文件是所有的类文件,生成条形码就是调用文件夹里的类,font文件是字体,i ...
- 条形码识别软件linux,条形码生成和识别库 Aspose.BarCode
Aspose.BarCode 是一个的条形码生成和识别库.支持29种条形码类型,包括:MSI, QR, OneCode, Australia Post, Aztec, Code128, Code11, ...
- 条形码(barcode)code128生成代码
条形码(barcode)code128生成代码 很简单 多些这位兄弟https://bbs.csdn.net/topics/350125614 下面是我的DEMO 直接放到VS2005下面编译即可 # ...
- qt barcode 生成一维条形码
感谢博主Qt 中利用 GNU barcode 生成一维条形码_liyuanbhu的博客-CSDN博客_qt 生成条形码 提供的资料.然后自己弄了一个测试工程,包含了已经编译好的barcode库.直接调 ...
- ZXing 生成二维码和条形码
今天,做项目需要使用条形码扫描枪扫描二维码,以后后续手动生成二维码和条形码.看了一下,同事写的例子以及自己在网上查看了一下源码,至于源码怎么搞的,没看,直接上使用功能! Step1:下载地址:http ...
- Android基于Google Zxing实现二维码/条形码扫描、生成二维码/条形码
二维码/条形码生成器 二维码/条形码扫描器 一.二维码与条形码工作原理 目前的很多应用上都有扫码功能,当时微信推出二维码扫码功能时,觉得imagine,通过一张简单的图片就能扫描添加还有,还有分 ...
- 【Demo】 生成二维码 和 条形码
为什么80%的码农都做不了架构师?>>> 条形码 和 二维码 对比 一维条形码只是在一个方向(一般是水平方向)表达信息,而在垂直方向则不表达任何信息,其一定的高度通常是为了便于 ...
- uniapp生成二维码和条形码
uniapp生成二维码和条形码 我们做小程序在我的这个页面经常会遇到有生成二维码的需求,那么我们使用tki-barcode和tki-qrcode这两个组件进行实现我们的需求 组件下载地址:" ...
- php+条形码在线怎么生成,php实现生成code128条形码的方法详解
本文实例讲述了php实现生成code128条形码的方法.分享给大家供大家参考,具体如下: 效果图: class BarCode128 { const STARTA = 103; const START ...
最新文章
- 假如王撕葱是程序员......
- 计算机应用英语考什么,网考计算机应用基础(本)试卷10(国外英语资料).doc
- java对象的访问定位_JVM创建对象及访问定位过程详解
- ApplicationContext和BeanFactory
- 牛客 - 走迷宫(模拟+离线)
- 计算机文化基础性考二,电大计算机文化基础形考二答案
- 卡通角色表情驱动系列二
- aba问题mysql_面试题总结:可能是全网最好的MySQL重要知识点
- 二叉树 的建立及遍历 过程
- 使用PING判断TCP/IP故障
- 吴恩达神经网络和深度学习-学习笔记-15-局部最优
- 如何让自己的CS水平更进一步?(二)了解武器
- atv320说明书_施耐德变频器参数设置ATV320学习资料
- 斯人已去长风存 谈谈 CyanogenMod 的前身今世
- matlab 生成hasse图,Hasse图详解
- VC中鼠标移动点击操作
- 微带线与带状线的区别
- mysql8.0安装/配置教程。
- 【知识图谱综述】Knowledge Graphs: A Survey
- 【黑金原创教程】【Modelsim】【第六章】结束就是开始
热门文章
- 网络创业者之家:互联网创业的优势与不足,你真到了解吗?
- python广义极值_广义极值(GEV)极大似然拟合的奇异pdf
- python添加pythonhome参数,如何在python中向烧瓶烧瓶添加参数?
- 将指定的计数添加到该信号量中会导致其超过最大计数
- 电脑会员管理系统怎么弄,电脑会员卡管理系统怎么弄
- 【奶奶看了也不会】AI绘画 Mac安装stable-diffusion-webui绘制AI妹子保姆级教程
- 写给面临危机的计算机类学生们的肺腑之言 转
- css--盒子的尺寸
- (二)postman批量执行用例
- .NET Core、Xamarin、.NET Standard和.NET Framework四者之间的区别