生成条形码_tp5+ barcode 生成条形码

本文介绍了如何在PHP环境中利用BarcodeBakery库生成条形码。首先从官网下载并解压库文件,然后将其放置在项目目录下。接着,通过引入所需类文件,创建条形码对象并设置参数,如分辨率、厚度、颜色等。提供了生成条形码的PHP代码示例,并展示了如何保存条形码为本地图片。最后,给出了渲染条形码的HTML代码片段,帮助开发者快速实现条形码功能。
摘要由CSDN通过智能技术生成
1、去官网下载类库 “https://www.barcodebakery.com/en/download”,选择自己的版本下载>2、解压放到“E:phpstudyPHPTutorialWWWguahaovendor下”,其中class文件是所有的类文件,生成条形码就是调用文件夹里的类,font文件是字体,index.php是一个可选择条件生成条形码的功能,是主程序的入口,test_1D.php是给的生成条形码的例子,test_1D.html是对应的渲染条形码的页面>3、我们可以直接使用官方给的例子(test_1D.php),复制到自己需要用的地方,然后根据自己的需求稍加改动即可,需要注意的是,加载第三方类库的路径需要改一下。>4-注意代码:require_once($class_dir.'BCGcode128.barcode.php'); //条码类型$code = new BCGcode128();//条码类型以上两行代码:是生成对应的条码类似  ,根据实际的要求进行处理>生成条形码的php代码```<?phpnamespace appindexcontroller;use thinkController;/*** 条形码操作类*/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');        require_once($class_dir.'BCGcode128.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 = new BCGcode128();//条码类型            $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- Filename (empty : display on screen)- 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代码``````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');        require_once($class_dir.'BCGcode128.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 = new BCGcode128();//条码类型            $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- Filename (empty : display on screen)- 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);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值