TP5生成条形码

下载barcodegen扩展库

下载链接https://www.barcodebakery.com/en/download

我用的v5.2.1

将扩展包解压至vendor目录下

service方法封装

 

public function createCode($text,$type)
    {
        $class_dir = VENDOR_PATH . 'barcodegen/class/';
        // Including all required classes
        require_once($class_dir . 'BCGColor.php');
        require_once($class_dir . 'BCGDrawing.php');
        require_once($class_dir . 'BCGcode128.barcode.php');
        require_once($class_dir . 'BCGean13.barcode.php');
        require_once($class_dir . 'BCGFontFile.php');

        // Loading Font
        // 注意font和class是同一级文件夹
        $font = new \BCGFontFile(VENDOR_PATH . 'barcodegen/class/font/Arial.ttf', 13);// The arguments are R, G, B for color.
        $color_black = new \BCGColor(0, 0, 0);
        $color_white = new \BCGColor(255, 255, 255);
        // $text = 'hello';
        $drawException = null;
        try {
            $code = $type == 1 ? new \BCGcode128() : new \BCGean13();
            $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不显示文字
            $code->parse($text); // Text
        } catch (Exception $exception) {
            $drawException = $exception;
        }
        $filename = '';
        //路径是否存在
        $drawing = new \BCGDrawing($filename, $color_white);//filename为空直接输出至页面
        if ($drawException) {
            $drawing->drawException($drawException);
        } else {
            $drawing->setBarcode($code);
            $drawing->draw();
        }
        header('Content-Type: image/png');
        header('Content-Disposition: inline; filename="barcode.png"');
        $drawing->finish(\BCGDrawing::IMG_FORMAT_PNG);
    }

控制器调用

    /**
     * @throws \BCGArgumentException
     * @throws \BCGDrawException
     */
    public function createBarcode()
    {
        $goods_sn = input('goods_sn');
        !preg_match("/^\d{13}$/",$goods_sn) ? $this->error('商品编号不符合规则') : '';
        $info = \app\common\model\Goods::where("goodsn",$goods_sn)->find();
        empty($info) ? $this->error('该商品不存在') : '';
        $barcode = new Barcodegen();
        $barcode->createCode($goods_sn,$info['bar_code_switch']);
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值