全网整合营销服务商

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

免费咨询热线: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 


相关文章: 如何通过网站建站时间优化SEO与用户体验?  建站之星收费标准详解:套餐费用及年费价格表一览  网站视频制作书签怎么做,ie浏览器怎么将网站固定在书签工具栏?  建站主机是否等同于虚拟主机?  济南专业网站制作公司,济南信息工程学校怎么样?  如何快速生成可下载的建站源码工具?  网页制作模板网站推荐,网页设计海报之类的素材哪里好?  洛阳网站制作公司有哪些,洛阳的招聘网站都有哪些?  网站规划与制作是什么,电子商务网站系统规划的内容及步骤是什么?  如何在IIS中新建站点并配置端口与IP地址?  如何用wdcp快速搭建高效网站?  C#如何使用XPathNavigator高效查询XML  网站制作公司,橙子建站是合法的吗?  建站之星代理商如何保障技术支持与售后服务?  再谈Python中的字符串与字符编码(推荐)  ,怎么在广州志愿者网站注册?  内部网站制作流程,如何建立公司内部网站?  如何自定义建站之星网站的导航菜单样式?  建站主机与虚拟主机有何区别?如何选择最优方案?  如何高效完成自助建站业务培训?  深圳网站制作培训,深圳哪些招聘网站比较好?  极客网站有哪些,DoNews、36氪、爱范儿、虎嗅、雷锋网、极客公园这些互联网媒体网站有什么差异?  如何在IIS中配置站点IP、端口及主机头?  网站制作大概要多少钱一个,做一个平台网站大概多少钱?  建站之星CMS建站配置指南:模板选择与SEO优化技巧  学生网站制作软件,一个12岁的学生写小说,应该去什么样的网站?  seo网站制作优化,网站SEO优化步骤有哪些?  如何用景安虚拟主机手机版绑定域名建站?  高防服务器租用指南:配置选择与快速部署攻略  较简单的网站制作软件有哪些,手机版网页制作用什么软件?  贸易公司网站制作流程,出口贸易网站设计怎么做?  如何通过云梦建站系统实现SEO快速优化?  教程网站设计制作软件,怎么创建自己的一个网站?  金*站制作公司有哪些,金华教育集团官网?  ,网站推广常用方法?  简历在线制作网站免费,免费下载个人简历的网站是哪些?  Android滚轮选择时间控件使用详解  已有域名和空间如何快速搭建网站?  深圳企业网站制作设计,在深圳如何网上全流程注册公司?  如何用腾讯建站主机快速创建免费网站?  网站制作免费,什么网站能看正片电影?  山东网站制作公司有哪些,山东大源集团官网?  如何通过二级域名建站提升品牌影响力?  如何在七牛云存储上搭建网站并设置自定义域名?  广州美橙建站如何快速搭建多端合一网站?  如何用y主机助手快速搭建网站?  武汉外贸网站制作公司,现在武汉外贸前景怎么样啊?  制作网站公司那家好,网络公司是做什么的?  家族网站制作贴纸教程视频,用豆子做粘帖画怎么制作?  如何通过主机屋免费建站教程十分钟搭建网站? 

您的项目需求

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