PHP 生成条形码
<?php
class BarCode128 {
const STARTA = 103;
const STARTB = 104;
const STARTC = 105;
const STOP = 106;
private $unit_width = 2; //单位宽度 缺省1个象素
private $is_set_height = false;
private $width = -1;
private $heith = 70;
private $quiet_zone = 6;
private $font_height = 15;
private $font_type = 5;
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()
{ }
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($code='',$text='',$file_name=null,$type='B', $image_type ='png')
{
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); $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, 1, $ex, 60, $this->color); // 原 ImageFilledRectangle($this->image, $sx, $sy, $ex,$ey, $this->color); $sy 改为1 条形码距离顶部1距离 $ey 改为60 条形码高度60
$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,65,$this->text{$i},$this->color); // 原 imagechar($this->image,$this->font_type,6*$this->unit_width +$t_sx +$i*$t_x,0,$this->text{$i},$this->color); 将0改为65 文本距离顶部65距离
}
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();
//第一个参数为用来生成条形码的字符串,第二个参数为条形码下面显示的字符内容,第三个为生成的路径文件名称
$barcode->createBarCode('SP89867','SP8986712','test.png');
?>
PHP 生成条形码的更多相关文章
- C# 在Word文档中生成条形码
C# 在Word文档中生成条形码 简介 条形码是由多个不同的空白和黑条按照一定的顺序组成,用于表示各种信息如产品名称.制造商.类别.价格等.目前,条形码在我们的日常生活中有着很广泛的应用,不管是在图书 ...
- 使用html2canvas实现批量生成条形码
/*前台代码*/ <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Generat ...
- JAVA生成条形码
1.下载生成条形码所需要的jar包barcode4j.jar: 2.java生成条形码代码 import java.awt.image.BufferedImage;import java.io.Fil ...
- C# 生成条形码
原文地址:http://www.cnblogs.com/xcsn/p/4514759.html 引用BarcodeLib.dll(百度云中有)生成条形 protected void Button2_C ...
- C# 利用BarcodeLib.dll生成条形码(一维,zxing,QrCodeNet/dll二维码)
原文:http://blog.csdn.net/kongwei521/article/details/17588825 首先效果: 一.下载BarcodeLib.dll 下载地址 :http://do ...
- PHP5生成条形码器
前阵子在做一个商家优惠券的功能,需要用到条形码,于是将资料重新整理下. 1.什么是条形码? 百度百科定义:条形码(barcode)是将宽度不等的多个黑条和空白,按照一定的编码规则排列,用以表达一组信息 ...
- PHP生成条形码
前阵子在做一个商家优惠券的功能,需要用到条形码,于是将资料重新整理下. 1.什么是条形码? 百度百科定义:条形码(barcode)是将宽度不等的多个黑条和空白,按照一定的编码规则排列,用以表达一组信息 ...
- C# 利用BarcodeLib.dll生成条形码
首先效果: 1:首先下载BarcodeLib.dll 下载地址 http://pan.baidu.com/share/link?shareid=2590968386&uk=2148890391 ...
- 使用PHP-Barcode轻松生成条形码(一)
最近由于工作需要,研究了一下PHP如何生成条形码.虽然二维码时下比较流行,但是条形码依然应用广泛,不可替代.园子里有很多讲利用PHP生成条形码的文章,基本上都是围绕Barcode Bakery的,它虽 ...
- java 生成条形码
package com.sun.erwei; import java.awt.image.BufferedImage;import java.io.ByteArrayOutputStream;impo ...
随机推荐
- Python学习:18.Python异常处理
一.为什么使用异常处理 当程序运行的时候出现了异常,导致程序终止运行,为了解决这种情况,我们需要预先对可能出现的异常进行处理,一旦出现这种异常,就使用另一种方式解决问题,还有就是错误信息是使用者没有必 ...
- 【树形DP】MZOJ_1063_士兵守卫
本题也是这三天来在下写的几篇树形DP之一,但是不知道为什么洛谷上面老是unknown error,...直接去了UVa,说我编译错误...我在想是不是头文件的原因,于是被逼无奈,交了一道c89的代码. ...
- POJ3006-Dirichlet's Theorem on Arithmetic Progressions
题意: 设一个等差数列,首元素为a,公差为d 现在要求输入a,d,n ,要求找出属于该等差数列中的第n个素数并输出 思路:空间换时间是个主旋律.素数表的生成用素数筛选法.方法是从2开始,对每个目前还标 ...
- Kotlin基础学习笔记(2)
1.基本数据类型 Kotlin的基本数值类型包括byte,short,int,long,float,double等.字符不属于数值类型,是一个独立的数据类型. 数字类型中不会主动转换.例如,不能给Do ...
- 《信息安全技术》实验2——Windows口令破解
实验2 Windows口令破解 在网络界,攻击事件发生的频率越来越高,其中相当多的都是由于网站密码泄露的缘故,或是人为因素导致,或是口令遭到破解,所以从某种角度而言,密码的安全问题不仅仅是技术上的问题 ...
- 20155234java实验一
实验内容 1.使用JDK编译.运行简单的Java程序: 2.使用Eclipse 编辑.编译.运行.调试Java程序. 实验要求 1.没有Linux基础的同学建议先学习Linux基础入门(新版))Vim ...
- 《Java程序设计》课堂实践内容总结
<Java程序设计>课堂实践内容总结 实践一 要求 修改教材P98 Score2.java, 让执行结果数组填充是自己的学号: 提交在IDEA或命令行中运行结查截图,加上学号水印,没学号的 ...
- # 20155337 2016-2017-2 《Java程序设计》第五周学习总
20155337 2016-2017-2 <Java程序设计>第五周学习总结 教材学习内容总结 第八章 •语法与继承架构 •使用try.catch •特点: 使用try.catch语法,J ...
- [BZOJ4002][JLOI2015]有意义的字符串-[快速乘法+矩阵乘法]
Description 传送门 Solution 由于这里带了小数,直接计算显然会爆掉,我们要想办法去掉小数. 而由于原题给了暗示:b2<=d<=(b+1)2,我们猜测可以利用$(\fra ...
- 打豪车应用:uber详细攻略(附100元优步uber优惠码、uber优惠券、优步优惠码、优步优惠券)
在嘀嘀打车和快的打车交战热闹的时候,美国的打车应用uber进入中国.与在美国以个人司机注册做 Uber 司机为主的模式不同,Uber 在中国采用与租车公司合作.由租车公司提供车辆和司机的模式,同时中文 ...