本文实例讲述了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","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;$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/

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

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

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

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

php+条形码在线怎么生成,php实现生成code128条形码的方法详解相关推荐

  1. 大杂烩 -- 四种生成和解析XML文档的方法详解

    基础大杂烩 -- 目录 众所周知,现在解析XML的方法越来越多,但主流的方法也就四种,即:DOM.SAX.JDOM和DOM4J DOM:在现在的Java JDK里都自带了,在xml-apis.jar包 ...

  2. 四种生成和解析XML文档的方法详解(介绍+优缺点比较+示例)

    众所周知,现在解析XML的方法越来越多,但主流的方法也就四种,即:DOM.SAX.JDOM和DOM4J 下面首先给出这四种方法的jar包下载地址 DOM:在现在的Java JDK里都自带了,在xml- ...

  3. mysql 获取当前整点_Oracle 生成未来三天的整点时间(步骤详解)

    需求:X坐标轴时间都为整点时间,展示未来三天内的预测(x轴展示未来三天的整点时间),每3个小时一个刻度,横坐标共计24个刻度 步骤一:取当前时间 SELECT SYSDATE FROM DUAL 步骤 ...

  4. php 2003生成word,使用PHPWord生成word文档的方法详解

    使用PHPWord生成word文档的方法详解 来源:中文源码网    浏览: 次    日期:2019年11月5日 [下载文档:  使用PHPWord生成word文档的方法详解.txt ] (友情提示 ...

  5. 【科研绘图】3dmax一键生成太阳系插件SolarSystem使用方法详解

    3DMAX太阳系恒星系建模插件(一键生成太阳系插件),太阳系(恒星系)参数化建模并生成动画插件.该插件提供了恒星.行星.卫星.小行星带和彗星的生成功能. [主要功能特性] --太阳系的参数化建模 -- ...

  6. for根据ID去重_汽车ECU参数标定之配置Overlay RAM实现Qorivva MPC57xx系列MCU参数在线标定和代码重映射原理和方法详解...

    内容提要 引言 1. MPC5744P的Overlay RAM工作原理介绍 2 MPC5744P的Flash Overlay配置详解 2.1 平台Flash标定区域描述字寄存器配置字0--PFLASH ...

  7. 站长在线Python精讲:在Python中匹配字符串的3个方法详解

    欢迎你来到站长在线的站长学堂学习Python知识,本文学习的是<在Python中匹配字符串的3个方法详解>.本知识点主要内容有:使用match()方法进行匹配.使用search()方法进行 ...

  8. 站长在线Python精讲:在Python中格式化字符串的两种方法详解

    欢迎你来到站长在线的站长学堂学习Python知识,本文学习的是<在Python中格式化字符串的两种方法详解>.本知识点主要内容有:使用%操作符格式化字符串和使用format()方法格式化字 ...

  9. 站长在线Python精讲:Python中集合的交集、并集、差集和对称差集运算方法详解

    欢迎你来到站长在线的站长学堂学习Python知识,本文学习的是<Python中集合的交集.并集.差集和对称差集运算方法详解>.主要讲的是集合运算的相关的概念,及运算方法,包括:集合的交集. ...

最新文章

  1. 【Linux】Linux简单操作之安装、使用tomcat
  2. NTT高级科学家:光子是深度学习的未来!光子有望替代电子计算机加速神经网络计算...
  3. Python性能测试
  4. mysql关于or的索引问题
  5. 公布自己的pods到CocoaPods trunk 及问题记录
  6. Java之volatile如何保证可见性和指令重排序
  7. 统计文件里有多少个字符(only a simple cpp)
  8. 中断处理程序与中断服务例程
  9. 死锁的充分必要条件、死锁预防、死锁避免、死锁检测和解除
  10. flink 1.9 编译:flink.shaded.netty4.io.netty 找不到
  11. 安装oracle12.1  32位客户端时,出现[INS-10102]未能初始化安装程序错误
  12. 国内三大常见核心期刊体系-CSSCI、CSCD与中文核心
  13. 利用vlmcs客户端区分KMS服务器是KMS模拟器还是正版微软KMS服务器
  14. Unity3D在C盘的缓存文件
  15. AI弄潮!深圳第一高楼智能访客系统“刷脸”通行
  16. 6s微信连接不上服务器失败是什么原因,6s手机微信打不开怎么回事
  17. PHP7封装了str_ends_with, phper看了都说好
  18. 7-2 打印九九口诀表 (10 分)
  19. VBA 64 32 调用dll的区别
  20. Python3.7中,Django配置MySql数据库

热门文章

  1. R语言描述性统计:使用mean函数计算dataframe数据中指定数据列的均值
  2. 莎斯莱思时尚男装2019秋季新品上市!
  3. 小米2021春招高频题汇总 | 备战春招,刷这30题就够了!
  4. web初学经验:对于IOS不兼容 text-algin:justify
  5. 小学美术计算机教案模板,【实用】小学美术教案模板五篇
  6. 一文教你挖掘用户评论典型意见
  7. vue 加密手机号(过滤器)
  8. Error copying library net.java.jinput:jinput:2.0.5 解决办法
  9. 面试被军训汇总(小红书、滴滴、招商银行、快看漫画)
  10. ESP32开发学习之路_VsCode+PlatformlO