php code128条码,PHP教程:php实现生成code128条形码的方法详解

news/2024/11/13 8:56:21/

《PHP教程:php实现生成code128条形码的方法详解》要点:

本文介绍了PHP教程:php实现生成code128条形码的方法详解,希望对您有用。如果有疑问,可以联系我们。

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

效果图:PHP教程

df9e89bc80d346512404e7e597b368b6.pngPHP教程

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的字w的度10,4或5的字w度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/PHP教程

351d327e640d24ec6c42e52250de74bd.pngPHP教程

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

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

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

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


http://www.ppmy.cn/news/703674.html

相关文章

硬件检测信息记录

硬盘&#xff1a;东芝 THNSNJ128GCSY (固态硬盘 [基础信息] ---------------------------------------------------------------------------------- 名称 东芝 THNSNJ128GCSY (固态硬盘) 容量 128 GB 磁盘已使用 共 1604 …

Barco无线演示系统受多个漏洞困扰

F-Secure的IT安全研究人员在Barco的Clickshare产品&#xff08;一种无线演示系统&#xff09;中发现了一些高危漏洞&#xff0c;可以让攻击者在演示期间窃取到关键信息&#xff0c;甚至还会影响密码等敏感信息。 此外&#xff0c;通过这些漏洞攻击者还可以在目标设备上安装后门…

盘点6月Sui生态发展,了解Sui的近期成长历程!

自5月Sui主网上线以来&#xff0c;已两月有余&#xff0c;最近一个月Sui网络进行多次迭代更新&#xff0c;生态正在不断稳步发展。为吸引更多的项目或开发者前来构建&#xff0c;Sui基金会推出了黑客松以及多项生态建设活动&#xff0c;进一步助力生态持续发展。 以下是Sui的近…

MES生产管理系统与ERP系统的集成以及优势

导言&#xff1a; 在当今数字化转型的浪潮中&#xff0c;企业越来越意识到整合各个部门的数据和流程的重要性。MES生产管理系统和ERP系统是两个关键的管理工具&#xff0c;它们在企业中发挥着不可或缺的作用。本文将探讨企业MES管理系统与ERP系统进行集成&#xff0c;以及这种…

【linux】一些linux系统下的常用命令(本地使用、远程服务器连接常用)

一些linux常用命令 linux 常用命令打开某个文件夹查看某个目录的所有内容复制文件命令移动命令创建文件夹删除文件或目录当前目录创建文件找文件/文件夹查看目录或文件夹大小更改文件或目录权限查看文件内容之vim查看之前输入的命令行下载某个东西到某个位置解压缩查看CPU占用情…

棋牌游戏网站分析——远航游戏中心

作为行业的分析师&#xff0c;该拿出点东西让市场了解一下环境了&#xff01;今天先拿远航游戏中心开刀讨论一下。 棋牌游戏的成功代表作品包括联众&#xff0c;QQ游戏&#xff0c;边锋等。其中包含游戏典型有升级、梭哈、斗地主、锄大地、四国军棋等。最近一段时间由于棋牌的…

安全连接Internet-让ISA Server 2006做为企业中的代理服务器

ISA Server 2006不仅功能强大&#xff0c;而且设置与使用也很方便&#xff0c;入门也很容易。如果读者有配置其他防火墙的经验&#xff0c;在做过一些实际操作后&#xff0c;可以很容易的掌握ISA Server 2006的使用。 ISA Server 2006将网络划分为内网、外网、本地主机三部分(以…

常见网络游戏的端口列表

一,常见网络游戏的端口列表 1、帝国时代:在“虚拟服务器”的“端口应用”中设置 进端口范围 : 2300 - 2400 出端口范围: 2300 - 2400; 进端口范围: 47624-47624 出端口范围:47624-42624 才能让对方与你成功连线。 2、星际争霸:进入“虚拟服务器”,在“内部端口范围”中填入…