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


Posted in PHP onJuly 19, 2017

本文实例讲述了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; //????或3的字?的??度??0,??或5的字???度??1
    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/

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

PHP 相关文章推荐
967 个函式
Oct 09 PHP
写一个用户在线显示的程序
Oct 09 PHP
一步一步学习PHP(3) php 函数
Feb 15 PHP
解决php使用异步调用获取数据时出现(错误c00ce56e导致此项操作无法完成)
Jul 03 PHP
php打印输出棋盘的实现方法
Dec 23 PHP
PHP获取网站中各文章的第一张图片的代码示例
May 20 PHP
php安装php_rar扩展实现rar文件读取和解压的方法
Nov 17 PHP
PHP开发APP端微信支付功能
Feb 17 PHP
php利用云片网实现短信验证码功能的示例代码
Nov 18 PHP
laravel框架 api自定义全局异常处理方法
Oct 11 PHP
Thinkphp5.0 框架视图view的比较标签用法分析
Oct 12 PHP
深入理解PHP+Mysql分布式事务与解决方案
Dec 03 PHP
php-beanstalkd消息队列类实例分享
Jul 19 #PHP
php+ajax实现异步上传文件或图片功能
Jul 18 #PHP
Joomla框架实现字符串截取的方法示例
Jul 18 #PHP
PHP新特性详解之命名空间、性状与生成器
Jul 18 #PHP
PHP实现基于面向对象的mysqli扩展库增删改查操作工具类
Jul 18 #PHP
PHP基于XMLWriter操作xml的方法分析
Jul 17 #PHP
PHP基于DOMDocument解析和生成xml的方法分析
Jul 17 #PHP
You might like
php empty函数判断mysql表单是否为空
2010/04/12 PHP
php实现的zip文件内容比较类
2014/09/24 PHP
php获取远程文件的内容和大小
2015/11/03 PHP
JavaScript中的Array对象使用说明
2011/01/17 Javascript
Jquery倒数计时按钮setTimeout的实例代码
2013/07/04 Javascript
js select option对象小结
2013/12/20 Javascript
js 设置缓存及获取设置的缓存
2014/05/08 Javascript
浅析JavaScript中的事件机制
2015/06/04 Javascript
javascript控制台详解
2015/06/25 Javascript
实现音乐播放器的代码(html5+css3+jquery)
2015/08/04 Javascript
JavaScript中无法通过div.style.left获取值的解决方法
2017/02/19 Javascript
jQuery布局组件EasyUI Layout使用方法详解
2017/02/28 Javascript
bootstrap datepicker插件默认英文修改为中文
2017/07/28 Javascript
Vue中使用webpack别名的方法实例详解
2018/06/19 Javascript
vue.js 图片上传并预览及图片更换功能的实现代码
2018/08/27 Javascript
JavaScript基于数组实现的栈与队列操作示例
2018/12/22 Javascript
JS中数据结构之栈
2019/01/01 Javascript
Python实现爬虫爬取NBA数据功能示例
2018/05/28 Python
基于python指定包的安装路径方法
2018/10/27 Python
在Python中字典根据多项规则排序的方法
2019/01/21 Python
Python实现深度遍历和广度遍历的方法
2019/01/22 Python
Python基础学习之时间转换函数用法详解
2019/06/18 Python
浅析Python与Mongodb数据库之间的操作方法
2019/07/01 Python
python requests抓取one推送文字和图片代码实例
2019/11/04 Python
HTML5+CSS3绘制锯齿状的矩形
2016/03/01 HTML / CSS
处理HTML5新标签的浏览器兼容版问题
2017/03/13 HTML / CSS
英国女士家居服网站:hush
2017/08/09 全球购物
新西兰珠宝品牌:Michael Hill
2017/09/16 全球购物
三陽商会官方网站:Sanyo iStore
2019/05/15 全球购物
《浅水洼里的小鱼》听课反思
2014/02/28 职场文书
文案策划求职信
2014/03/18 职场文书
市场开发与营销专业求职信范文
2014/05/01 职场文书
产品设计开发计划书
2014/05/07 职场文书
故宫英文导游词
2015/01/31 职场文书
2015年公务员转正工作总结
2015/04/24 职场文书
检讨书模板大全
2015/05/07 职场文书