<?
/***********************************************************************
written by caocao
[email protected]
http://nethermit.yeah.net
显示code39条形码的类
使用方法:
$convert=new code39;
$str=$convert->decode($str);
***********************************************************************/
class pattenclass
{
var $color;
var $width;
function pattenclass($color,$width)
{
$this->color=$color;
$this->width=$width;
}
}
class code39
{
var $zoom;
var $height;
var $patten=array();
function code39($zoom, $height)
{
$zoom=intval($zoom);
$this->zoom=$zoom<1||$zoom>20?3:$zoom;
$height=intval($height);
$this->height=$height<1||$height>80?30:$height;
$this->patten[]=new pattenclass("#ffffff",1*$this->zoom);
$this->patten[]=new pattenclass("#ffffff",3*$this->zoom);
$this->patten[]=new pattenclass("#000000",1*$this->zoom);
$this->patten[]=new pattenclass("#000000",3*$this->zoom);
}
function makecode($code)//code39解码
{
switch ($code)
{
case "0":return ("202130302");
case "1":return ("302120203");
case "2":return ("203120203");
case "3":return ("303120202");
case "4":return ("202130203");
case "5":return ("302130202");
case "6":return ("203130202");
case "7":return ("202120303");
case "8":return ("302120302");
case "9":return ("203120302");
case "a":return ("302021203");
case "b":return ("203021203");
case "c":return ("303021202");
case "d":return ("202031203");
case "e":return ("302031202");
case "f":return ("203031202");
case "g":return ("202021303");
case "h":return ("302021302");
case "i":return ("203021300");
case "j":return ("202031302");
case "k":return ("302020213");
case "l":return ("203020213");
case "m":return ("303020212");
case "n":return ("202030213");
case "o":return ("302030212");
case "p":return ("203030212");
case "q":return ("202020313");
case "r":return ("302020312");
case "s":return ("203020312");
case "t":return ("202030312");
case "u":return ("312020203");
case "v":return ("213020203");
case "w":return ("313020202");
case "x":return ("212030203");
case "y":return ("312030202");
case "z":return ("213030202");
case "-":return ("212020303");
case ".":return ("312020302");
case " ":return ("213020302");
case "*":return ("212030302");
case "$":return ("212121202");
case "/":return ("212120212");
case "+":return ("212021212");
case "%":return ("202121212");
}
return ("212030302");
}
function display($code)//输出单个字符
{
$output="";
for ($i=0;$i<9;$i++)
$output.="<td height=".$this->height." bgcolor=".$this->patten[$code[$i]]->color." width=".$this->patten[$code[$i]]->width."></td>";
return $output;
}
function decode($code)//全部输出
{
$output="<table width=".($this->height/2*(strlen($code)+2)*$this->zoom)." height=".$this->height." border=0 cellspacing=0 cellpadding=0 align=/"center/"><tr>";
$output.=$this->display($this->makecode("*"));
$output.="<td height=".$this->height." bgcolor=".$this->patten[0]->color." width=".$this->patten[0]->width."></td>";
$length=strlen($code);
for ($i=0;$i<$length;$i++)
{
$output.=$this->display($this->makecode($code[$i]));
$output.="<td height=".$this->height." bgcolor=".$this->patten[0]->color." width=".$this->patten[0]->width."></td>";
}
$output.=$this->display($this->makecode("*"));
$output.="</tr></table>";
return $output;
}
}
?>
新闻热点
疑难解答