侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

thinkphp5 + barcode 生成条形码的方法

ThinkPHP  /  管理员 发布于 8年前   374

1、去官网下载类库 “https://www.barcodebakery.com/en/download”,选择自己的版本下载

9d180cfbcac078f49065314823f6819.png

推荐教程:thinkphp教程

2、解压放到“E:\phpstudy\PHPTutorial\WWW\guahao\vendor\下”,其中class文件是所有的类文件,生成条形码就是调用文件夹里的类,font文件是字体,index.php是一个可选择条件生成条形码的功能,是主程序的入口,test_1D.php是给的生成条形码的例子,test_1D.html是对应的渲染条形码的页面

33e92f0b17fb76e5be1d5f5a7728db8.png

3、我们可以直接使用官方给的例子(test_1D.php),复制到自己需要用的地方,然后根据自己的需求稍加改动即可,需要注意的是,加载第三方类库的路径需要改一下。

生成条形码的php代码

<?phpnamespace 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代码

<img src="{:url('createBarcode')}">

1f3f3982e09e64dabf49a522e997e3c.png

当然,src还可以携带参数,只需更改以下代码

html代码

<img src="{:url('createBarcode',array('text'=>'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 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/”,上传刚刚生成的条形码,如果解析出的参数跟你输入的一样,说明条形码可以用。

c29618de19152f792d304f461355199.png

以上就是thinkphp5 + barcode 生成条形码的方法的详细内容,更多请关注其它相关文章!


  • 上一条:
    ThinkPHP中几种文件加载方式
    下一条:
    thinkphp防止重复提交表单的技巧
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • thinkphp + mongodb项目中数据加载慢问题分析及解决(0个评论)
    • thinkphp6框架中封装redis操作类(0个评论)
    • thinkphp6框架中实现定时任务功能流程步骤(0个评论)
    • Thinkphp5.1框架中实现Session+Redis会话共享流程步骤(0个评论)
    • TP5框架版本5.0.10安全漏洞根据官方补丁修复,也是本站安全漏洞修复(0个评论)
    • 近期文章
    • 智能合约Solidity学习CryptoZombie二课:让你的僵尸猎食(0个评论)
    • 智能合约Solidity学习CryptoZombie第一课:生成一只你的僵尸(0个评论)
    • 在go中实现一个常用的先进先出的缓存淘汰算法示例代码(0个评论)
    • 在go+gin中使用"github.com/skip2/go-qrcode"实现url转二维码功能(0个评论)
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 欧盟关于强迫劳动的规定的官方举报渠道及官方举报网站(0个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf文件功能(0个评论)
    • Laravel从Accel获得5700万美元A轮融资(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2016-10
    • 2017-07
    • 2017-08
    • 2017-09
    • 2017-10
    • 2017-12
    • 2018-01
    • 2018-02
    • 2020-03
    • 2021-07
    • 2021-12
    • 2022-05
    • 2022-06
    • 2022-09
    • 2023-01
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客