全网整合营销服务商

电脑端+手机端+微信端=数据同步管理

免费咨询热线:400-708-3566

php实现生成code128条形码的方法详解

本文实例讲述了php实现生成code128条形码的方法。分享给大家供大家参考,具体如下:

效果图:

<?php
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","121124","121421","141122","141221","112214","112412","122114","122411","142112","142211","241211","221114","413111","241112","134111","111242","121142","121241","114212","124112","124211","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 < 32)
          $val += 64;
        else
          $val -=32;
      }
      elseif ($this->type =='B')
      {
        if ($val < 32 || $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 < 0 || (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;$i<strlen($this->code);$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;$i<strlen($this->bin_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<$t_num;$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/

PS:这里再为大家推荐一款相似的条形码生成工具供大家参考使用:

在线条形码(一维码)生成/实时预览工具:
http://tools./aideddesign/tiaoxingma

更多关于PHP相关内容感兴趣的读者可查看本站专题:《PHP图形与图片操作技巧汇总》、《PHP数组(Array)操作技巧大全》、《PHP数据结构与算法教程》、《php程序设计算法总结》、《PHP数学运算技巧总结》、《php字符串(string)用法总结》及《php常见数据库操作技巧汇总》

希望本文所述对大家PHP程序设计有所帮助。


# php  # 生成  # code128  # 条形码  # PHP生成条形码大揭秘  # php实现在线生成条形码示例分享(条形码生成器)  # php生成EAN_13标准条形码实例  # php生成条形码的图片的实例详解  # Thinkphp3.2.3整合phpqrcode生成带logo的二维码  # thinkphp3.2点击刷新生成验证码  # ThinkPHP实现生成和校验验证码功能  # thinkPHP框架实现生成条形码的方法示例  # 操作技巧  # 程序设计  # 相关内容  # 感兴趣  # 数据结构  # 给大家  # 象素  # 更多关于  # 所述  # 再为  # 讲述了  # unit_width  # Array  # is_set_height  # private  # STOP  # false  # font_height  # font_type  # quiet_zone 


相关文章: 沈阳制作网站公司排名,沈阳装饰协会官方网站?  为什么Go需要go mod文件_Go go mod文件作用说明  建站之星如何修改网站生成路径?  实现虚拟支付需哪些建站技术支撑?  Android使用GridView实现日历的简单功能  常州自助建站费用包含哪些项目?  如何在宝塔面板中创建新站点?  电商平台网站制作流程,电商网站如何制作?  网站海报制作教学视频教程,有什么免费的高清可商用图片网站,用于海报设计?  建站之星后台搭建步骤解析:模板选择与产品管理实操指南  建站主机解析:虚拟主机配置与服务器选择指南  焦点电影公司作品,电影焦点结局是什么?  建站VPS选购需注意哪些关键参数?  建站之星IIS配置教程:代码生成技巧与站点搭建指南  车管所网站制作流程,交警当场开简易程序处罚决定书,在交警网站查询不到怎么办?  公司网站的制作公司,企业网站制作基本流程有哪些?  ppt制作免费网站有哪些,ppt模板免费下载网站?  武汉网站设计制作公司,武汉有哪些比较大的同城网站或论坛,就是里面都是武汉人的?  如何制作公司的网站链接,公司想做一个网站,一般需要花多少钱?  专业的网站制作设计是什么,如何制作一个企业网站,建设网站的基本步骤有哪些?  如何通过西部建站助手安装IIS服务器?  已有域名和空间如何快速搭建网站?  如何快速搭建个人网站并优化SEO?  免费制作统计图的网站有哪些,如何看待现如今年轻人买房难的情况?  如何快速搭建自助建站会员专属系统?  建站org新手必看:2024最新搭建流程与模板选择技巧  高端网站建设与定制开发一站式解决方案 中企动力  武汉外贸网站制作公司,现在武汉外贸前景怎么样啊?  标准网站视频模板制作软件,现在有哪个网站的视频编辑素材最齐全的,背景音乐、音效等?  实例解析Array和String方法  如何在云主机快速搭建网站站点?  网站图片在线制作软件,怎么在图片上做链接?  香港网站服务器数量如何影响SEO优化效果?  如何通过虚拟主机快速完成网站搭建?  南平网站制作公司,2025年南平市事业单位报名时间?  如何用PHP快速搭建CMS系统?  定制建站如何定义?其核心优势是什么?  如何选择PHP开源工具快速搭建网站?  建站之星云端配置指南:模板选择与SEO优化一键生成  杭州银行网站设计制作流程,杭州银行怎么开通认证方式?  ,有什么在线背英语单词效率比较高的网站?  5种Android数据存储方式汇总  定制建站是什么?如何实现个性化需求?  怎么用手机制作网站链接,dw怎么把手机适应页面变成网页?  潮流网站制作头像软件下载,适合母子的网名有哪些?  如何通过免费商城建站系统源码自定义网站主题与功能?  建站之星北京办公室:智能建站系统与小程序生成方案解析  油猴 教程,油猴搜脚本为什么会网页无法显示?  如何在万网开始建站?分步指南解析  专业网站建设制作报价,网页设计制作要考什么证? 

您的项目需求

*请认真填写需求信息,我们会在24小时内与您取得联系。