thinkphp5 整合 条形码类

浏览:6030 发布日期:2016/07/18 分类:ThinkPHP5专区 关键字: 条形码
类文件是放在 扩展目录extend之下的 $content 为条形码内容信息 此为测试给的固定值
public function barcode_create(){
        $content='123';
        // 引用barcode文件夹对应的类
        Loader::import('BCode.BCGFontFile',EXTEND_PATH);
        //Loader::import('BCode.BCGColor',EXTEND_PATH);
        Loader::import('BCode.BCGDrawing',EXTEND_PATH);
        // 条形码的编码格式
        Loader::import('BCode.BCGcode39',EXTEND_PATH,'.barcode.php');
       // $code = '';
        // 加载字体大小
        //$font = new BCGFontFile('./class/font/Arial.ttf', 18);
       //颜色条形码
        $color_black = new \BCGColor(0, 0, 0);
        $color_white = new \BCGColor(255, 255, 255);

        $drawException = null;
        try
        {
            $code = new \BCGcode39();
            $code->setScale(2);
            $code->setThickness(30); // 条形码的厚度
            $code->setForegroundColor($color_black); // 条形码颜色
            $code->setBackgroundColor($color_white); // 空白间隙颜色
           // $code->setFont($font); //
            $code->parse($content); // 条形码需要的数据内容
        }
        catch(\Exception $exception)
        {
            $drawException = $exception;
        }

        //根据以上条件绘制条形码
        $drawing = new \BCGDrawing('', $color_white);
        if($drawException) {
            $drawing->drawException($drawException);
        }else{
            $drawing->setBarcode($code);
            $drawing->draw();
        }

        // 生成PNG格式的图片
        header('Content-Type: image/png');
       // header('Content-Disposition:attachment; filename="barcode.png"'); //自动下载
        $drawing->finish(\BCGDrawing::IMG_FORMAT_PNG);
    }

附件 BCode.rar ( 64.3 KB 下载:870 次 )

最佳答案
评论( 相关
后面还有条评论,点击查看>>