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 相关文章推荐
使用php4加速网络传输
Oct 09 PHP
php数组总结篇(一)
Sep 30 PHP
浅析Yii中使用RBAC的完全指南(用户角色权限控制)
Jun 20 PHP
PHP中使用memcache存储session的三种配置方法
Apr 05 PHP
ThinkPHP3.1数据CURD操作快速入门
Jun 19 PHP
php使用pdo连接报错Connection failed SQLSTATE的解决方法
Dec 15 PHP
PHP里的单例类写法实例
Jun 25 PHP
discuz图片顺序混乱解决方案
Jul 29 PHP
PHP实现连接设备、通讯和发送命令的方法
Oct 13 PHP
ECSHOP完美解决Deprecated: preg_replace()报错的问题
May 17 PHP
PHP两种实现无级递归分类的方法
Mar 02 PHP
PHP匿名函数(闭包函数)详解
Mar 22 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 url 加密解密函数代码
2011/08/26 PHP
php配合jquery实现增删操作具体实例
2013/12/12 PHP
PHP正则删除HTML代码中宽高样式的方法
2017/06/12 PHP
js 禁用浏览器的后退功能的简单方法
2008/12/10 Javascript
点击文章内容处弹出页面代码
2009/10/01 Javascript
JavaScript下通过的XMLHttpRequest发送请求的代码
2011/06/28 Javascript
iphone safari不支持position fixed的解决方法
2012/05/04 Javascript
js控制页面控件隐藏显示的两种方法介绍
2013/10/09 Javascript
引入JS文件IE6报语法错误或缺少对象问题的解决方法
2014/01/09 Javascript
原生JS实现首页进度加载动画
2016/09/14 Javascript
javascript数据结构中栈的应用之符号平衡问题
2017/04/11 Javascript
DataTables添加额外的查询参数和删除columns等无用参数实例
2017/07/04 Javascript
JScript实现地址选择功能
2017/08/15 Javascript
vue左右侧联动滚动的实现代码
2018/06/06 Javascript
微信小程序canvas实现刮刮乐效果
2018/07/09 Javascript
新手如何快速理解js异步编程
2019/06/24 Javascript
javascript随机变色实例代码
2019/10/15 Javascript
Javascript实现html转pdf高清版(提高分辨率)
2020/02/19 Javascript
Nest.js 授权验证的方法示例
2021/02/22 Javascript
[04:49]期待西雅图之战 2016国际邀请赛中国区预选赛WINGS战队赛后采访
2016/06/29 DOTA
Python socket编程实例详解
2015/05/27 Python
python安装教程
2018/02/28 Python
python每天定时运行某程序代码
2019/08/16 Python
opencv转换颜色空间更改图片背景
2019/08/20 Python
Python实现非正太分布的异常值检测方式
2019/12/09 Python
python如何实现不用装饰器实现登陆器小程序
2019/12/14 Python
Windows下pycharm安装第三方库失败(通用解决方案)
2020/09/17 Python
聊聊Python pandas 中loc函数的使用,及跟iloc的区别说明
2021/03/03 Python
德国传统玻璃制造商:Cristalica
2018/04/23 全球购物
Sahajan美国:阿育吠陀护肤品牌
2021/01/09 全球购物
信息技术教学反思
2014/02/12 职场文书
大学生优秀自荐信范文
2014/02/25 职场文书
化工工艺设计求职信
2014/06/25 职场文书
小学优秀教师材料
2014/12/15 职场文书
2015年考研复习计划
2015/01/19 职场文书
Python写情书? 10行代码展示如何把情书写在她的照片里
2022/04/21 Python