/
I2of5.php
107 lines (88 loc) · 2.36 KB
/
I2of5.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
<?php
/**
* PHP Interleaved 2 of 5 Barcode
*/
class I2of5
{
// barcode sequence settings
private static $start = '0000';
private static $end = '100';
// barcode sequences from 0 to 9
private static $seqs = array(
'00110',
'10001',
'01001',
'11000',
'00101',
'10100',
'01100',
'00011',
'10010',
'01010'
);
// picture height in pixels
public static $height = 50;
// bars weight in pixels
public static $thin = 1;
public static $wide = 3;
/**
* Returns the interleaved 2 of 5 sequence
* 0 = thin line
* 1 = wide line
*
* @param string $num
*/
private static function sequence($num)
{
$num = trim($num);
// we need an even length number
if (strlen($num) %2 != 0) {
$num = '0' . $num;
}
// create the sequence
$seq = '';
for ($i = 0, $n = strlen($num); $i < $n; $i++) {
$seq .= self::$seqs[$num[$i]];
}
// interleave the sequence
$int = '';
for ($i = 0, $n = strlen($seq); $i < $n - 5; $i++) {
$int .= $seq[$i] . $seq[$i + 5];
if (($i + 1) % 5 == 0) {
$i += 5;
}
}
return self::$start . $int . self::$end;
}
/**
* Creates the barcode png picture
*
* @param string $num
*/
public static function picture($num)
{
$seq = self::sequence($num);
// determine the image width
$width = 0;
for ($i = 0, $n = strlen($seq); $i < $n; $i++) {
$width += $seq[$i] == '0' ? self::$thin : self::$wide;
}
$img = imagecreate($width, self::$height);
$black = imagecolorallocate($img, 0, 0, 0);
$white = imagecolorallocate($img, 255, 255, 255);
$x = 0;
for ($i = 0, $n = strlen($seq); $i < $n; $i++) {
// line weight
$w = $seq[$i] == '0' ? self::$thin : self::$wide;
// line x position
$x += $w;
// line color
$color = $i % 2 == 0 ? $black : $white;
// draw the line
imagefilledrectangle($img, $x - $w, 0, $x, self::$height, $color);
}
// send the png image
header("Content-Type: image/png");
imagepng($img);
}
}