登录  /  注册
首页 > php框架 > ThinkPHP > 正文

图文详解thinkphp5+barcode生成条形码

藏色散人
发布: 2021-03-03 15:47:26
转载
1877人浏览过

下面由thinkphp教程栏目给大家介绍thinkphp5 + barcode 生成条形码,希望对需要的朋友有所帮助!

thinkphp5 + barcode 生成条形码

092796de1cd6e8df738beaa5928c6d8.png

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

cb4b027fc82584948fc6c45766aba33.png

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

fd3c93d3c0fd3577af312fe5bb9e4c3.png

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

生成条形码的php代码

<?php namespace app\index\controller;
use think\Controller;

/**
* 条形码操作类
*/
class Barcode extends Controller
{
    public function createBarcode()
    {
        $class_dir = VENDOR_PATH.&#39;barcode/class/&#39;;
        // Including all required classes
        require_once($class_dir.&#39;BCGFontFile.php&#39;);
        require_once($class_dir.&#39;BCGColor.php&#39;);
        require_once($class_dir.&#39;BCGDrawing.php&#39;);
        require_once($class_dir.&#39;BCGcode39.barcode.php&#39;);

        // Loading Font
        // 注意font和class是同一级文件夹
        $font = new \BCGFontFile(VENDOR_PATH.&#39;barcode/font/Arial.ttf&#39;, 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-&gt;setThickness(30); // Thickness
            $code-&gt;setForegroundColor($color_black); // Color of bars
            $code-&gt;setBackgroundColor($color_white); // Color of spaces
            $code-&gt;setFont($font); // Font (or 0)  0不显示文字
         $text = isset($_GET['text']) ? $_GET['text'] : 'HELLO';
            $code-&gt;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-&gt;drawException($drawException);
        } else {
            $drawing-&gt;setBarcode($code);
            $drawing-&gt;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-&gt;finish(\BCGDrawing::IMG_FORMAT_PNG);

    }

    public function barcodedes()
    {
        return $this-&gt;fetch();
    }
}
?&gt;
登录后复制

接受渲染条形码的Html代码

<img  alt="图文详解thinkphp5+barcode生成条形码" >
登录后复制

66372b47a784c21488e2f8984fdf53b.png

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

html代码

<img  alt="图文详解thinkphp5+barcode生成条形码" >'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-&gt;setScale(2); // Resolution
            $code-&gt;setThickness(30); // Thickness
            $code-&gt;setForegroundColor($color_black); // Color of bars
            $code-&gt;setBackgroundColor($color_white); // Color of spaces
            $code-&gt;setFont($font); // Font (or 0)
            $text = input('text');      //接收的参数
            $text = isset($text) ? $text :'无参数';      
            $code-&gt;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-&gt;drawException($drawException);
        } else {
            $drawing-&gt;setBarcode($code);
            $drawing-&gt;draw();
        }
        $drawing-&gt;finish(\BCGDrawing::IMG_FORMAT_PNG);
登录后复制

5、生成条形码之后,怎么判定条形码是否能用呢?

可以把条形码保存成图片到本地,打开官网“[https://www.barcodebakery.com/en/download]”,上传刚刚生成的条形码,如果解析出的参数跟你输入的一样,说明条形码可以用。

8b0c37e72f707eb7ea5330002a0960a.png

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

智能AI问答
PHP中文网智能助手能迅速回答你的编程问题,提供实时的代码和解决方案,帮助你解决各种难题。不仅如此,它还能提供编程资源和学习指导,帮助你快速提升编程技能。无论你是初学者还是专业人士,AI智能助手都能成为你的可靠助手,助力你在编程领域取得更大的成就。
相关标签:
来源:segmentfault网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
最新问题
关于CSS思维导图的课件在哪? 课件
凡人来自于2024-04-16 10:10:18
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2024 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号