1、去官网下载类库 “”,选择自己的版本下载

 

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

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

生成条形码的php代码<?php

namespace 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')}">

 

当然,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、生成条形码之后,怎么判定条形码是否能用呢?可以把条形码保存成图片到本地,打开官网“”,上传刚刚生成的条形码,如果解析出的参数跟你输入的一样,说明条形码可以用。