php生成code128条形码

小磊吖_4194

小磊吖_4194

2016-06-20

2206人浏览

原创

php生成code128条形码

效果图:

class BarCode128 {
    const STARTA = 103;
    const STARTB = 104;
    const STARTC = 105;
    const STOP = 106;
    private $unit_width = 1; //单位宽度 缺省1个象素
    private $is_set_height = false;
    private $width = -1;
    private $heith = 35;
    private $quiet_zone = 6;
    private $font_height = 15;
    private $font_type = 4;
    private $color =0x000000;
    private $bgcolor =0xFFFFFF;
    private $image = null;
    private $codes = array("212222","222122","222221","121223","121322","131222","122213","122312","132212","221213","221312","231212","112232","122132","122231","113222","123122","123221","223211","221132","221231","213212","223112","312131","311222","321122","321221","312212","322112","322211","212123","212321","232121","111323","131123","131321","112313","132113","132311","211313","231113","231311","112133","112331","132131","113123","113321","133121","313121","211331","231131","213113","213311","213131","311123","311321","331121","312113","312311","332111","314111","221411","431111","111224","111422","121124124","121421","141122","141221","112214","112412412","122114","122411","142112","142211","241211","221114","413111","241112","134111","111241242","121142","121241241","114212","124124112","124124211","411212","421112","421211","212141","214121","412121","111143","111341","131141","114113","114311","411113","411311","113141","114131","311141","411131","211412","211214","211412","2331112");
    private $valid_code = -1;
    private $type ='B';
    private $start_codes =array('A'=>self::STARTA,'B'=>self::STARTB,'C'=>self::STARTC);
    private $code ='';
    private $bin_code ='';
    private $text ='';

    public function __construct($code='',$text='',$type='B')
    {
        if (in_array($type,array('A','B','C')))
            $this->setType($type);
        else
            $this->setType('B');
        if ($code !=='')
            $this->setCode($code);
        if ($text !=='')
            $this->setText($text);
    }
    public function setUnitWidth($unit_width)
    {
        $this->unit_width = $unit_width;
        $this->quiet_zone = $this->unit_width*6;
        $this->font_height = $this->unit_width*15;
        if (!$this->is_set_height)
        {
            $this->heith = $this->unit_width*35;
        }
    }
    public function setFontType($font_type)
    {
        $this->font_type = $font_type;
    }
    public function setBgcolor($bgcoloe)
    {
        $this->bgcolor = $bgcoloe;
    }
    public function setColor($color)
    {
        $this->color = $color;
    }
    public function setCode($code)
    {
        if ($code !='')
        {
            $this->code= $code;
            if ($this->text ==='')
                $this->text = $code;
        }
    }
    public function setText($text)
    {
        $this->text = $text;
    }

    public function setType($type)
    {
        $this->type = $type;
    }
    public function setHeight($height)
    {
        $this->height = $height;
        $this->is_set_height = true;
    }

    private function getValueFromChar($ch)
    {
        $val = ord($ch);
        try
        {
            if ($this->type =='A')
            {
                if ($val > 95)
                    throw new Exception(' illegal barcode character '.$ch.' for code128A in '.__FILE__.' on line '.__LINE__);
                if ($val                      $val += 64;
                else
                    $val -=32;
            }
            elseif ($this->type =='B')
            {
                if ($val  127)
                    throw new Exception(' illegal barcode character '.$ch.' for code128B in '.__FILE__.' on line '.__LINE__);
                else
                    $val -=32;
            }
            else
            {
                if (!is_numeric($ch) || (int)$ch  99)
                    throw new Exception(' illegal barcode character '.$ch.' for code128C in '.__FILE__.' on line '.__LINE__);
                else
                {
                    if (strlen($ch) ==1)
                        $ch .='0';
                    $val = (int)($ch);
                }
            }
        }
        catch(Exception $ex)
        {
            errorlog('die',$ex->getMessage());
        }
        return $val;
    }
    private function parseCode()
    {
        $this->type=='C'?$step=2:$step=1;
        $val_sum = $this->start_codes[$this->type];
        $this->width = 35;
        $this->bin_code = $this->codes[$val_sum];
        for($i =0;$icode);$i+=$step)
        {
            $this->width +=11;
            $ch = substr($this->code,$i,$step);
            $val = $this->getValueFromChar($ch);
            $val_sum += $val;
            $this->bin_code .= $this->codes[$val];
        }
        $this->width *=$this->unit_width;
        $val_sum = $val_sum%103;
        $this->valid_code = $val_sum;
        $this->bin_code .= $this->codes[$this->valid_code];
        $this->bin_code .= $this->codes[self::STOP];
    }
    public function getValidCode()
    {
        if ($this->valid_code == -1)
            $this->parseCode();
        return $this->valid_code;
    }
    public function getWidth()
    {
        if ($this->width ==-1)
            $this->parseCode();
        return $this->width;
    }

    public function getHeight()
    {
        if ($this->width ==-1)
            $this->parseCode();
        return $this->height;
    }

    public function createBarCode($image_type ='png',$file_name=null)
    {
        $this->parseCode();
        $this->image = ImageCreate($this->width+2*$this->quiet_zone,$this->heith + $this->font_height);
        $this->bgcolor = imagecolorallocate($this->image,$this->bgcolor >> 16,($this->bgcolor >> 8)&0x00FF,$this->bgcolor & 0xFF);
        $this->color = imagecolorallocate($this->image,$this->color >> 16,($this->color >> 8)&0x00FF,$this->color & 0xFF);
        ImageFilledRectangle($this->image, 0, 0, $this->width + 2*$this->quiet_zone,$this->heith + $this->font_height, $this->bgcolor);
        $sx = $this->quiet_zone;
        $sy = $this->font_height -1;
        $fw = 10; //編號為2或3的字體的寬度為10,為4或5的字體寬度為11
        if ($this->font_type >3)
        {
            $sy++;
            $fw=11;
        }
        $ex = 0;
        $ey = $this->heith + $this->font_height - 2;
        for($i=0;$ibin_code);$i++)
        {
            $ex = $sx + $this->unit_width*(int) $this->bin_code{$i} -1;
            if ($i%2==0)
                ImageFilledRectangle($this->image, $sx, $sy, $ex,$ey, $this->color);
            $sx =$ex + 1;
        }
        $t_num = strlen($this->text);
        $t_x = $this->width/$t_num;
        $t_sx = ($t_x -$fw)/2;        //目的为了使文字居中平均分布
        for($i=0;$i         {
            imagechar($this->image,$this->font_type,6*$this->unit_width +$t_sx +$i*$t_x,0,$this->text{$i},$this->color);
        }
        if (!$file_name)
        {
            header("Content-Type: image/".$image_type);
        }
        switch ($image_type)
        {
            case 'jpg':
            case 'jpeg':
                Imagejpeg($this->image,$file_name);
                break;
            case 'png':
                Imagepng($this->image,$file_name);
                break;
            case 'gif':
                break;
                Imagegif($this->image,$file_name);
            default:
                Imagepng($this->image,$file_name);
                break;
        }
    }
}

$barcode = new BarCode128('88888888');
$barcode->createBarCode();
?>
附加一个强大的条码生成扩展包
http://www.barcodebakery.com/
">
http://www.barcodebakery.com/


AD:真正免费,域名+虚机+企业邮箱=0元

php免费学习视频:立即使用
踏上前端学习之旅,开启通往精通之路!从前端基础到项目实战,循序渐进,一步一个脚印,迈向巅峰!

相关文章

PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载

相关标签:

本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

相关专题

更多
Go-Zero API开发使用教程
Go-Zero API开发使用教程

本专题聚焦Go-Zero的API开发,拆解.api描述文件的语法结构、路由与分组定义、参数绑定与校验规则,说明统一响应体、JWT鉴权、跨域与接口文档生成的落地方式,并给出网关聚合与接口测试的实践建议。

2026.11.12

120

15

Go-Zero安装与goctl环境配置教程
Go-Zero安装与goctl环境配置教程

本专题围绕Go-Zero的安装与工具链配置展开,讲清Go版本要求、goctl安装方式、代理镜像、protoc依赖与环境变量设置,说明多版本共存、升级回退与CI流水线安装的做法,并整理安装阶段的常见报错与验证步骤。

2026.11.12

100

15

Go-Zero零基础入门教程
Go-Zero零基础入门教程

本专题整理Go-Zero的入门知识,从框架定位、环境准备到跑通第一个API服务,讲清目录分层、配置加载、日志与中间件的基本用法,并对比Gin、Kratos的差异,帮助Go开发者建立整体认知,判断它是否适合当前项目。

2026.11.12

60

15

Postman身份认证怎么配置
Postman身份认证怎么配置

本专题从实际API接口调试场景出发,讲解Postman身份认证的完整配置流程,包括常见认证协议介绍、授权参数设置、访问令牌填写、OAuth 2.0授权及认证失败排查,帮助用户正确完成接口授权与访问验证。

2026.11.06

160

12

Postman请求头Headers怎么设置
Postman请求头Headers怎么设置

围绕Postman HTTP请求头配置展开,讲解Headers选项的具体使用方式,包括Key和Value参数填写、请求头信息添加、常见Header字段设置及请求头修改方法,帮助用户解决接口调试过程中遇到的请求头配置问题。

2026.11.06

140

13

Postman接口请求发送教程
Postman接口请求发送教程

详细介绍Postman接口请求的创建与发送方法,涵盖GET和POST请求、URL配置、请求参数、Headers请求头、Body请求体设置以及响应结果查看,帮助用户掌握Postman接口请求的完整操作流程。

2026.11.06

120

10

DBeaver怎么导入数据
DBeaver怎么导入数据

DBeaver数据导入专题,整理Excel、CSV、SQL、TXT等常见文件的数据导入方法,涵盖目标数据库选择、数据表创建、字段映射、数据类型设置及导入参数配置等操作,帮助用户将本地文件中的数据写入数据库,并了解批量导入、重复数据处理和导入失败等常见问题的解决方法。

2026.11.05

100

12

DBeaver安全防护教程大全
DBeaver安全防护教程大全

DBeaver安全防护专题,围绕数据库连接安全、账号密码保护和访问权限管理展开,整理SSL/TLS加密连接、SSH隧道、数据库凭据保存及证书验证等配置方法,同时介绍敏感信息保护、远程连接风险和高权限账号使用等注意事项,帮助用户提高数据库日常管理的安全性。

2026.11.05

100

12

DBeaver连接创建与配置教程
DBeaver连接创建与配置教程

本专题聚焦DBeaver数据库连接配置与管理,覆盖从新建连接、选择数据库类型到填写连接参数和验证连接的完整流程。针对MySQL、PostgreSQL、SQL Server等常见数据库,整理相应的连接设置方法,并提供驱动缺失、认证失败和连接异常等问题的排查方向。

2026.11.04

100

16

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
动力学院设计模式PHP视频教程
动力学院设计模式PHP视频教程

共15课时 | 3.5万人学习