-
<?php
-
/*
-
* Bar Code
-
* author: Seven Yu
-
* E-mail: dofyyu@gmail.com
-
*/
-
class BarCode{
-
private $data;
-
private $order;
-
private $code;
-
private $mode;
-
-
private $startX, $endX, $startY, $offsetX, $offsetY;
-
private $fontsize, $txtbgX0;
-
private $txtbgX1, $txtbgX2, $txtbgX3, $txtbgX4, $txtbgY1, $txtbgY2;
-
-
private $im;
-
-
private $b, $s;
-
-
function __construct($code = null, $mode = 'html'){
-
-
$this->startX = 20;
-
$this->startY = 10;
-
$this->offsetX = 2;
-
$this->offsetY = 80;
-
-
$this->fontsize = 5;
-
-
$this->txtbgX0 = $this->startX;
-
$this->txtbgX1 = $this->startX+$this->offsetX*3;
-
$this->txtbgX2 = $this->txtbgX1+$this->offsetX*42;
-
$this->txtbgX3 = $this->txtbgX2+$this->offsetX*5;
-
$this->txtbgX4 = $this->txtbgX3+$this->offsetX*42;
-
$this->txtbgY2 = $this->startY+$this->offsetY;
-
$this->txtbgY1 = $this->txtbgY2-15;
-
-
$this->loadData();
-
$this->setMode($mode);
-
-
$this->setCode($code);
-
}
-
-
function setMode($mode){
-
$this->
mode =
strtolower($mode) ==
'img'?
'img':
'html';
-
}
-
-
function setCode($code){
-
$reg = "/^\d{13}$/";
-
-
$this->code = $code;
-
}else{
-
$this->code = null;
-
$this->ERROR('Need a 13 bit Number.');
-
}
-
}
-
-
function makeImg($width, $height){
-
header('Content-type: image/png');
-
header('Content-Disposition: attachment; filename="BarCode.png"');
-
$this->im = imagecreate($width, $height);
-
$this->s = imagecolorallocate($this->im, 255, 255, 255);
-
$this->b = imagecolorallocate($this->im, 0, 0, 0);
-
$this->t = imagecolorallocate($this->im, 100, 100, 100);
-
}
-
-
function endImg(){
-
imagepng($this->im);
-
}
-
-
function makeRect($col){
-
$this->endX = $this->startX + $this->offsetX;
-
$col = $col == 1? $this->b: $this->s;
-
-
$this->myRect($this->startX, $this->startY, $this->endX, $this->startY+$this->offsetY, $col);
-
$this->startX = $this->endX;
-
}
-
-
function myRect($x1, $y1, $x2, $y2, $col){
-
imagefilledrectangle($this->im, $x1, $y1, $x2-1, $y2-1, $col);
-
}
-
-
function myText($x1, $y1, $str){
-
imagestring($this->im, $this->fontsize, $x1, $y1, $str, $this->b);
-
}
-
-
function ERROR($msg){
-
if($this->mode == 'img'){
-
$this->
makeImg(strlen($msg)*
10,
30);
-
$this->myText(5, 5, $msg);
-
$this->endImg();
-
}else{
-
-
}
-
}
-
-
function makeBarCode(){
-
-
$this->ERROR('Need Code');
-
}else{
-
// get array
-
-
$rule = $this->order[$this->code[0]];
-
-
for($i=1; $i<7; $i++){
-
$final =
array_merge($final,
$this->
data[$this->
code[$i]][$rule[$i-
1]]);
-
}
-
-
for(; $i<13; $i++){
-
$final =
array_merge($final,
$this->
data[$this->
code[$i]]['c']);
-
}
-
-
-
if($this->mode == 'img'){
-
// make img
-
$width = $this->startX*2 + $this->offsetX*95;
-
$height = $this->startY*2 + $this->offsetY;
-
$this->makeImg($width, $height);
-
// draw bar
-
for($i=0; $i<95; $i++){
-
$this->makeRect($final[$i]);
-
}
-
// draw text background
-
$this->myRect($this->txtbgX1, $this->txtbgY1, $this->txtbgX2, $this->txtbgY2, $this->s);
-
$this->myRect($this->txtbgX3, $this->txtbgY1, $this->txtbgX4, $this->txtbgY2, $this->s);
-
// write text
-
for($i=0; $i<13; $i++){
-
if($i == 0){
-
$offsetX = -2;
-
}else if($i <7){
-
$offsetX = $this->offsetX*3;
-
}else{
-
$offsetX = $this->offsetX*8;
-
}
-
$this->myText($offsetX+$this->txtbgX0+$this->offsetX*7*$i-$this->offsetX*5, $this->txtbgY1, $this->code[$i]);
-
}
-
$this->myText($this->startX+3, $this->txtbgY1, '>');
-
-
$this->endImg();
-
}else{
-
// html mode
-
echo '<style type="text/css">
-
.line_0, .line_1{
-
float: left;
-
width: ' . $this->offsetX . 'px;
-
height: 100px;
-
}
-
.line_0{
-
background: #FFFFFF;
-
}
-
.line_1{
-
background: #000000;
-
}
-
</style>';
-
for($i=0; $i<95; $i++){
-
echo "\n" .
'<div class="line_' .
$final[$i] .
'"></div>';
-
}
-
}
-
}
-
-
}
-
-
function loadData(){
-
-
-
-
$this->
data['bgn'] =
array(1,
0,
1);
-
$this->
data['mid'] =
array(0,
1,
0,
1,
0);
-
$this->
data['end'] =
array(1,
0,
1);
-
$this->
data[0]['a'] =
array(0,
0,
0,
1,
1,
0,
1);
-
$this->
data[0]['b'] =
array(0,
1,
0,
0,
1,
1,
1);
-
$this->
data[0]['c'] =
array(1,
1,
1,
0,
0,
1,
0);
-
$this->
data[1]['a'] =
array(0,
0,
1,
1,
0,
0,
1);
-
$this->
data[1]['b'] =
array(0,
1,
1,
0,
0,
1,
1);
-
$this->
data[1]['c'] =
array(1,
1,
0,
0,
1,
1,
0);
-
$this->
data[2]['a'] =
array(0,
0,
1,
0,
0,
1,
1);
-
$this->
data[2]['b'] =
array(0,
0,
1,
1,
0,
1,
1);
-
$this->
data[2]['c'] =
array(1,
1,
0,
1,
1,
0,
0);
-
$this->
data[3]['a'] =
array(0,
1,
1,
1,
1,
0,
1);
-
$this->
data[3]['b'] =
array(0,
1,
0,
0,
0,
0,
1);
-
$this->
data[3]['c'] =
array(1,
0,
0,
0,
0,
1,
0);
-
$this->
data[4]['a'] =
array(0,
1,
0,
0,
0,
1,
1);
-
$this->
data[4]['b'] =
array(0,
0,
1,
1,
1,
0,
1);
-
$this->
data[4]['c'] =
array(1,
0,
1,
1,
1,
0,
0);
-
$this->
data[5]['a'] =
array(0,
1,
1,
0,
0,
0,
1);
-
$this->
data[5]['b'] =
array(0,
1,
1,
1,
0,
0,
1);
-
$this->
data[5]['c'] =
array(1,
0,
0,
1,
1,
1,
0);
-
$this->
data[6]['a'] =
array(0,
1,
0,
1,
1,
1,
1);
-
$this->
data[6]['b'] =
array(0,
0,
0,
0,
1,
0,
1);
-
$this->
data[6]['c'] =
array(1,
0,
1,
0,
0,
0,
0);
-
$this->
data[7]['a'] =
array(0,
1,
1,
1,
0,
1,
1);
-
$this->
data[7]['b'] =
array(0,
0,
1,
0,
0,
0,
1);
-
$this->
data[7]['c'] =
array(1,
0,
0,
0,
1,
0,
0);
-
$this->
data[8]['a'] =
array(0,
1,
1,
0,
1,
1,
1);
-
$this->
data[8]['b'] =
array(0,
0,
0,
1,
0,
0,
1);
-
$this->
data[8]['c'] =
array(1,
0,
0,
1,
0,
0,
0);
-
$this->
data[9]['a'] =
array(0,
0,
0,
1,
0,
1,
1);
-
$this->
data[9]['b'] =
array(0,
0,
1,
0,
1,
1,
1);
-
$this->
data[9]['c'] =
array(1,
1,
1,
0,
1,
0,
0);
-
-
$this->
order[0] =
array('a',
'a',
'a',
'a',
'a',
'a');
-
$this->
order[1] =
array('a',
'a',
'b',
'a',
'b',
'b');
-
$this->
order[2] =
array('a',
'a',
'b',
'b',
'a',
'b');
-
$this->
order[3] =
array('a',
'a',
'b',
'b',
'b',
'a');
-
$this->
order[4] =
array('a',
'b',
'a',
'a',
'b',
'b');
-
$this->
order[5] =
array('a',
'b',
'b',
'a',
'a',
'b');
-
$this->
order[6] =
array('a',
'b',
'b',
'b',
'a',
'a');
-
$this->
order[7] =
array('a',
'b',
'a',
'b',
'a',
'b');
-
$this->
order[8] =
array('a',
'b',
'a',
'b',
'b',
'a');
-
$this->
order[9] =
array('a',
'b',
'b',
'a',
'b',
'a');
-
}
-
// end class
-
}
-
?>