thinkphp5 + barcode 生成条形码的方法
发布时间 - 2020-04-12 00:00:00 点击率:次1、去官网下载类库 “https://www.barcodebakery.com/en/download”,选择自己的版本下载
推荐教程:thinkphp教程
2、解压放到“E:\phpstudy\PHPTutorial\WWW\guahao\vendor\下”,其中class文件是所有的类文件,生成条形码就是调用文件夹里的类,font文件是字体,index.php是一个可选择条件生成条形码的功能,是主程序的入口,test_1D.php是给的生成条形码的例子,test_1D.html是对应的渲染条形码的页面
3、我们可以直接使用官方给的例子(test_1D.php),复制到自己需要用的地方,然后根据自己的需求稍加改动即可,需要注意的是,加载第三方类库的路径需要改一下。
生成条形码的php代码
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代码
'123'))}">
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 o
f 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
# html
# thinkphp
# class
# https
# phpstudy
# 自己的
# 官网
# 的是
# 是一个
# 类库
# 还可以
# 主程序
# 可以用
# 只需
# 我们可以
相关栏目:
【
网站优化151355 】
【
网络推广146373 】
【
网络技术251813 】
【
AI营销90571 】
相关推荐:
js实现获取鼠标当前的位置
QQ浏览器网页版登录入口 个人中心在线进入
制作旅游网站html,怎样注册旅游网站?
iOS UIView常见属性方法小结
ChatGPT 4.0官网入口地址 ChatGPT在线体验官网
Python自然语言搜索引擎项目教程_倒排索引查询优化案例
邀请函制作网站有哪些,有没有做年会邀请函的网站啊?在线制作,模板很多的那种?
HTML透明颜色代码怎么让图片透明_给img元素加透明色的技巧【方法】
历史网站制作软件,华为如何找回被删除的网站?
在线制作视频的网站有哪些,电脑如何制作视频短片?
如何在阿里云部署织梦网站?
Java垃圾回收器的方法和原理总结
Google浏览器为什么这么卡 Google浏览器提速优化设置步骤【方法】
新三国志曹操传主线渭水交兵攻略
微信h5制作网站有哪些,免费微信H5页面制作工具?
今日头条微视频如何找选题 今日头条微视频找选题技巧【指南】
Swift中switch语句区间和元组模式匹配
laravel怎么通过契约(Contracts)编程_laravel契约(Contracts)编程方法
html5源代码发行怎么设置权限_访问权限控制方法与实践【指南】
网站制作怎么样才能赚钱,用自己的电脑做服务器架设网站有什么利弊,能赚钱吗?
Laravel如何处理JSON字段的查询和更新_Laravel JSON列操作与查询技巧
如何在万网自助建站中设置域名及备案?
阿里云网站搭建费用解析:服务器价格与建站成本优化指南
如何快速配置高效服务器建站软件?
如何注册花生壳免费域名并搭建个人网站?
Laravel如何自定义错误页面(404, 500)?(代码示例)
百度浏览器如何管理插件 百度浏览器插件管理方法
Laravel Facade的原理是什么_深入理解Laravel门面及其工作机制
标题:Vue + Vuex + JWT 身份认证的正确实践与常见误区解析
如何获取上海专业网站定制建站电话?
Laravel广播系统如何实现实时通信_Laravel Reverb与WebSockets实战教程
Laravel如何创建自定义Facades?(详细步骤)
Laravel如何升级到最新版本?(升级指南和步骤)
Laravel的Blade指令怎么自定义_创建你自己的Laravel Blade Directives
php打包exe后无法访问网络共享_共享权限设置方法【教程】
Laravel Seeder填充数据教程_Laravel模型工厂Factory使用
大型企业网站制作流程,做网站需要注册公司吗?
如何在服务器上配置二级域名建站?
Python3.6正式版新特性预览
使用豆包 AI 辅助进行简单网页 HTML 结构设计
制作公司内部网站有哪些,内网如何建网站?
Laravel如何实现本地化和多语言支持?(i18n教程)
Laravel如何使用.env文件管理环境变量?(最佳实践)
Linux后台任务运行方法_nohup与&使用技巧【技巧】
装修招标网站设计制作流程,装修招标流程?
Laravel 419 page expired怎么解决_Laravel CSRF令牌过期处理
EditPlus中的正则表达式 实战(4)
MySQL查询结果复制到新表的方法(更新、插入)
如何用wdcp快速搭建高效网站?
Laravel怎么写单元测试_PHPUnit在Laravel项目中的基础测试入门


f 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);