| 1 |
lars |
1 |
<?php
|
|
|
2 |
/* vim: set expandtab tabstop=4 softtabstop=4 shiftwidth=4: */
|
|
|
3 |
|
|
|
4 |
/**
|
|
|
5 |
* Image_Barcode_code128 class
|
|
|
6 |
*
|
|
|
7 |
* Renders Code128 barcodes
|
|
|
8 |
* Code128 is a high density encoding for alphanumeric strings.
|
|
|
9 |
* This module prints the Code128B representation of the most common
|
|
|
10 |
* ASCII characters (32 to 134).
|
|
|
11 |
*
|
|
|
12 |
* These are the components of a Code128 Bar code:
|
|
|
13 |
* - 10 Unit Quiet Zone
|
|
|
14 |
* - 6 Unit Start Character
|
|
|
15 |
* - (n * 6) Unit Message
|
|
|
16 |
* - 6 Unit "Check Digit" Character
|
|
|
17 |
* - 7 Unit Stop Character
|
|
|
18 |
* - 10 Unit Quiet Zone
|
|
|
19 |
*
|
|
|
20 |
* I originally wrote this algorithm in Visual Basic 6 for a Rapid
|
|
|
21 |
* Software Development class, where we printed Code128 B bar codes
|
|
|
22 |
* to read using Cue Cat bar code readers. I rewrote the algorithm
|
|
|
23 |
* using PHP for inclusion in the PEAR Image_Barcode project.
|
|
|
24 |
*
|
|
|
25 |
* The Code128B bar codes produced by the algorithm have been validated
|
|
|
26 |
* using my trusty Cue-Cat bar code reader.
|
|
|
27 |
*
|
|
|
28 |
* PHP versions 4
|
|
|
29 |
*
|
|
|
30 |
* LICENSE: This source file is subject to version 3.0 of the PHP license
|
|
|
31 |
* that is available through the world-wide-web at the following URI:
|
|
|
32 |
* http://www.php.net/license/3_0.txt. If you did not receive a copy of
|
|
|
33 |
* the PHP License and are unable to obtain it through the web, please
|
|
|
34 |
* send a note to license@php.net so we can mail you a copy immediately.
|
|
|
35 |
*
|
|
|
36 |
* @category Image
|
|
|
37 |
* @package Image_Barcode
|
|
|
38 |
* @author Jeffrey K. Brown <jkb@darkfantastic.net>
|
|
|
39 |
* @copyright 2005 The PHP Group
|
|
|
40 |
* @license http://www.php.net/license/3_0.txt PHP License 3.0
|
|
|
41 |
* @version CVS: $Id: code128.php 304741 2010-10-25 09:14:17Z clockwerx $
|
|
|
42 |
* @link http://pear.php.net/package/Image_Barcode
|
|
|
43 |
*/
|
|
|
44 |
|
|
|
45 |
require_once "Image/Barcode.php";
|
|
|
46 |
|
|
|
47 |
class Image_Barcode_code128 extends Image_Barcode
|
|
|
48 |
{
|
|
|
49 |
var $_type = 'code128';
|
|
|
50 |
var $_barcodeheight = 60;
|
|
|
51 |
var $_font = 2;
|
|
|
52 |
var $_barwidth = 1;
|
|
|
53 |
var $code;
|
|
|
54 |
|
|
|
55 |
|
|
|
56 |
/**
|
|
|
57 |
* Draws a Code128 image barcode
|
|
|
58 |
*
|
|
|
59 |
* @param string $text A text that should be in the image barcode
|
|
|
60 |
* @param string $imgtype The image type that will be generated
|
|
|
61 |
*
|
|
|
62 |
* @return image The corresponding interleaved 2 of 5 image barcode
|
|
|
63 |
*
|
|
|
64 |
* @access public
|
|
|
65 |
*
|
|
|
66 |
* @author Jeffrey K. Brown <jkb@darkfantastic.net>
|
|
|
67 |
*
|
|
|
68 |
* @internal
|
|
|
69 |
* The draw() method is broken into three sections. First, we take
|
|
|
70 |
* the input string and convert it to a string of barcode widths.
|
|
|
71 |
* Then, we size and allocate the image. Finally, we print the bars to
|
|
|
72 |
* the image along with the barcode text and display it to the beholder.
|
|
|
73 |
*
|
|
|
74 |
*/
|
|
|
75 |
function &draw($text, $imgtype = 'png')
|
|
|
76 |
{
|
|
|
77 |
|
|
|
78 |
// We start with the Code128 Start Code character. We
|
|
|
79 |
// initialize checksum to 104, rather than calculate it.
|
|
|
80 |
// We then add the startcode to $allbars, the main string
|
|
|
81 |
// containing the bar sizes for the entire code.
|
|
|
82 |
$startcode= $this->getStartCode();
|
|
|
83 |
$checksum = 104;
|
|
|
84 |
$allbars = $startcode;
|
|
|
85 |
|
|
|
86 |
|
|
|
87 |
// Next, we read the $text string that was passed to the
|
|
|
88 |
// method and for each character, we determine the bar
|
|
|
89 |
// pattern and add it to the end of the $allbars string.
|
|
|
90 |
// In addition, we continually add the character's value
|
|
|
91 |
// to the checksum
|
|
|
92 |
$bars = '';
|
|
|
93 |
for ($i=0; $i < strlen($text); ++$i) {
|
|
|
94 |
$char = $text[$i];
|
|
|
95 |
$val = $this->getCharNumber($char);
|
|
|
96 |
|
|
|
97 |
$checksum += ($val * ($i + 1));
|
|
|
98 |
|
|
|
99 |
$bars = $this->getCharCode($char);
|
|
|
100 |
$allbars = $allbars . $bars;
|
|
|
101 |
}
|
|
|
102 |
|
|
|
103 |
|
|
|
104 |
// Then, Take the Mod 103 of the total to get the index
|
|
|
105 |
// of the Code128 Check Character. We get its bar
|
|
|
106 |
// pattern and add it to $allbars in the next section.
|
|
|
107 |
$checkdigit = $checksum % 103;
|
|
|
108 |
$bars = $this->getNumCode($checkdigit);
|
|
|
109 |
|
|
|
110 |
|
|
|
111 |
// Finally, we get the Stop Code pattern and put the
|
|
|
112 |
// remaining pieces together. We are left with the
|
|
|
113 |
// string $allbars containing all of the bar widths
|
|
|
114 |
// and can now think about writing it to the image.
|
|
|
115 |
|
|
|
116 |
$stopcode = $this->getStopCode();
|
|
|
117 |
$allbars = $allbars . $bars . $stopcode;
|
|
|
118 |
|
|
|
119 |
//------------------------------------------------------//
|
|
|
120 |
// Next, we will calculate the width of the resulting
|
|
|
121 |
// bar code and size the image accordingly.
|
|
|
122 |
|
|
|
123 |
// 10 Pixel "Quiet Zone" in front, and 10 Pixel
|
|
|
124 |
// "Quiet Zone" at the end.
|
|
|
125 |
$barcodewidth = 20;
|
|
|
126 |
|
|
|
127 |
|
|
|
128 |
// We will read each of the characters (1,2,3,or 4) in
|
|
|
129 |
// the $allbars string and add its width to the running
|
|
|
130 |
// total $barcodewidth. The height of the barcode is
|
|
|
131 |
// calculated by taking the bar height plus the font height.
|
|
|
132 |
|
|
|
133 |
for ($i=0; $i < strlen($allbars); ++$i) {
|
|
|
134 |
$nval = $allbars[$i];
|
|
|
135 |
$barcodewidth += ($nval * $this->_barwidth);
|
|
|
136 |
}
|
|
|
137 |
$barcodelongheight = (int) (imagefontheight($this->_font) / 2) + $this->_barcodeheight;
|
|
|
138 |
|
|
|
139 |
|
|
|
140 |
// Then, we create the image, allocate the colors, and fill
|
|
|
141 |
// the image with a nice, white background, ready for printing
|
|
|
142 |
// our black bars and the text.
|
|
|
143 |
|
|
|
144 |
$img = ImageCreate($barcodewidth, $barcodelongheight+ imagefontheight($this->_font)+1);
|
|
|
145 |
$black = ImageColorAllocate($img, 0, 0, 0);
|
|
|
146 |
$white = ImageColorAllocate($img, 255, 255, 255);
|
|
|
147 |
imagefill($img, 0, 0, $white);
|
|
|
148 |
|
|
|
149 |
|
|
|
150 |
//------------------------------------------------------//
|
|
|
151 |
// Finally, we write our text line centered across the
|
|
|
152 |
// bottom and the bar patterns and display the image.
|
|
|
153 |
|
|
|
154 |
|
|
|
155 |
// First, print the image, centered across the bottom.
|
|
|
156 |
imagestring(
|
|
|
157 |
$img,
|
|
|
158 |
$this->_font,
|
|
|
159 |
$barcodewidth / 2 - strlen($text) / 2 * (imagefontwidth($this->_font)),
|
|
|
160 |
$this->_barcodeheight + imagefontheight($this->_font) / 2,
|
|
|
161 |
$text,
|
|
|
162 |
$black
|
|
|
163 |
);
|
|
|
164 |
|
|
|
165 |
// We set $xpos to 10 so we start bar printing after
|
|
|
166 |
// position 10 to simulate the 10 pixel "Quiet Zone"
|
|
|
167 |
$xpos = 10;
|
|
|
168 |
|
|
|
169 |
// We will now process each of the characters in the $allbars
|
|
|
170 |
// array. The number in each position is read and then alternating
|
|
|
171 |
// black bars and spaces are drawn with the corresponding width.
|
|
|
172 |
$bar = 1;
|
|
|
173 |
for ($i=0; $i < strlen($allbars); ++$i) {
|
|
|
174 |
$nval = $allbars[$i];
|
|
|
175 |
$width = $nval * $this->_barwidth;
|
|
|
176 |
|
|
|
177 |
if ($bar==1) {
|
|
|
178 |
imagefilledrectangle($img, $xpos, 0, $xpos + $width-1, $barcodelongheight, $black);
|
|
|
179 |
$xpos += $width;
|
|
|
180 |
$bar = 0;
|
|
|
181 |
} else {
|
|
|
182 |
$xpos += $width;
|
|
|
183 |
$bar = 1;
|
|
|
184 |
}
|
|
|
185 |
}
|
|
|
186 |
|
|
|
187 |
return $img;
|
|
|
188 |
} // function draw()
|
|
|
189 |
|
|
|
190 |
|
|
|
191 |
/**
|
|
|
192 |
* @internal
|
|
|
193 |
* In the Image_Barcode_code128 constructor, we initialize
|
|
|
194 |
* the $code array, containing the bar and space pattern
|
|
|
195 |
* for the Code128 B character set.
|
|
|
196 |
*/
|
|
|
197 |
function Image_Barcode_code128()
|
|
|
198 |
{
|
|
|
199 |
$this->code[0] = "212222"; // " "
|
|
|
200 |
$this->code[1] = "222122"; // "!"
|
|
|
201 |
$this->code[2] = "222221"; // "{QUOTE}"
|
|
|
202 |
$this->code[3] = "121223"; // "#"
|
|
|
203 |
$this->code[4] = "121322"; // "$"
|
|
|
204 |
$this->code[5] = "131222"; // "%"
|
|
|
205 |
$this->code[6] = "122213"; // "&"
|
|
|
206 |
$this->code[7] = "122312"; // "'"
|
|
|
207 |
$this->code[8] = "132212"; // "("
|
|
|
208 |
$this->code[9] = "221213"; // ")"
|
|
|
209 |
$this->code[10] = "221312"; // "*"
|
|
|
210 |
$this->code[11] = "231212"; // "+"
|
|
|
211 |
$this->code[12] = "112232"; // ","
|
|
|
212 |
$this->code[13] = "122132"; // "-"
|
|
|
213 |
$this->code[14] = "122231"; // "."
|
|
|
214 |
$this->code[15] = "113222"; // "/"
|
|
|
215 |
$this->code[16] = "123122"; // "0"
|
|
|
216 |
$this->code[17] = "123221"; // "1"
|
|
|
217 |
$this->code[18] = "223211"; // "2"
|
|
|
218 |
$this->code[19] = "221132"; // "3"
|
|
|
219 |
$this->code[20] = "221231"; // "4"
|
|
|
220 |
$this->code[21] = "213212"; // "5"
|
|
|
221 |
$this->code[22] = "223112"; // "6"
|
|
|
222 |
$this->code[23] = "312131"; // "7"
|
|
|
223 |
$this->code[24] = "311222"; // "8"
|
|
|
224 |
$this->code[25] = "321122"; // "9"
|
|
|
225 |
$this->code[26] = "321221"; // ":"
|
|
|
226 |
$this->code[27] = "312212"; // ";"
|
|
|
227 |
$this->code[28] = "322112"; // "<"
|
|
|
228 |
$this->code[29] = "322211"; // "="
|
|
|
229 |
$this->code[30] = "212123"; // ">"
|
|
|
230 |
$this->code[31] = "212321"; // "?"
|
|
|
231 |
$this->code[32] = "232121"; // "@"
|
|
|
232 |
$this->code[33] = "111323"; // "A"
|
|
|
233 |
$this->code[34] = "131123"; // "B"
|
|
|
234 |
$this->code[35] = "131321"; // "C"
|
|
|
235 |
$this->code[36] = "112313"; // "D"
|
|
|
236 |
$this->code[37] = "132113"; // "E"
|
|
|
237 |
$this->code[38] = "132311"; // "F"
|
|
|
238 |
$this->code[39] = "211313"; // "G"
|
|
|
239 |
$this->code[40] = "231113"; // "H"
|
|
|
240 |
$this->code[41] = "231311"; // "I"
|
|
|
241 |
$this->code[42] = "112133"; // "J"
|
|
|
242 |
$this->code[43] = "112331"; // "K"
|
|
|
243 |
$this->code[44] = "132131"; // "L"
|
|
|
244 |
$this->code[45] = "113123"; // "M"
|
|
|
245 |
$this->code[46] = "113321"; // "N"
|
|
|
246 |
$this->code[47] = "133121"; // "O"
|
|
|
247 |
$this->code[48] = "313121"; // "P"
|
|
|
248 |
$this->code[49] = "211331"; // "Q"
|
|
|
249 |
$this->code[50] = "231131"; // "R"
|
|
|
250 |
$this->code[51] = "213113"; // "S"
|
|
|
251 |
$this->code[52] = "213311"; // "T"
|
|
|
252 |
$this->code[53] = "213131"; // "U"
|
|
|
253 |
$this->code[54] = "311123"; // "V"
|
|
|
254 |
$this->code[55] = "311321"; // "W"
|
|
|
255 |
$this->code[56] = "331121"; // "X"
|
|
|
256 |
$this->code[57] = "312113"; // "Y"
|
|
|
257 |
$this->code[58] = "312311"; // "Z"
|
|
|
258 |
$this->code[59] = "332111"; // "["
|
|
|
259 |
$this->code[60] = "314111"; // "\"
|
|
|
260 |
$this->code[61] = "221411"; // "]"
|
|
|
261 |
$this->code[62] = "431111"; // "^"
|
|
|
262 |
$this->code[63] = "111224"; // "_"
|
|
|
263 |
$this->code[64] = "111422"; // "`"
|
|
|
264 |
$this->code[65] = "121124"; // "a"
|
|
|
265 |
$this->code[66] = "121421"; // "b"
|
|
|
266 |
$this->code[67] = "141122"; // "c"
|
|
|
267 |
$this->code[68] = "141221"; // "d"
|
|
|
268 |
$this->code[69] = "112214"; // "e"
|
|
|
269 |
$this->code[70] = "112412"; // "f"
|
|
|
270 |
$this->code[71] = "122114"; // "g"
|
|
|
271 |
$this->code[72] = "122411"; // "h"
|
|
|
272 |
$this->code[73] = "142112"; // "i"
|
|
|
273 |
$this->code[74] = "142211"; // "j"
|
|
|
274 |
$this->code[75] = "241211"; // "k"
|
|
|
275 |
$this->code[76] = "221114"; // "l"
|
|
|
276 |
$this->code[77] = "413111"; // "m"
|
|
|
277 |
$this->code[78] = "241112"; // "n"
|
|
|
278 |
$this->code[79] = "134111"; // "o"
|
|
|
279 |
$this->code[80] = "111242"; // "p"
|
|
|
280 |
$this->code[81] = "121142"; // "q"
|
|
|
281 |
$this->code[82] = "121241"; // "r"
|
|
|
282 |
$this->code[83] = "114212"; // "s"
|
|
|
283 |
$this->code[84] = "124112"; // "t"
|
|
|
284 |
$this->code[85] = "124211"; // "u"
|
|
|
285 |
$this->code[86] = "411212"; // "v"
|
|
|
286 |
$this->code[87] = "421112"; // "w"
|
|
|
287 |
$this->code[88] = "421211"; // "x"
|
|
|
288 |
$this->code[89] = "212141"; // "y"
|
|
|
289 |
$this->code[90] = "214121"; // "z"
|
|
|
290 |
$this->code[91] = "412121"; // "{"
|
|
|
291 |
$this->code[92] = "111143"; // "|"
|
|
|
292 |
$this->code[93] = "111341"; // "}"
|
|
|
293 |
$this->code[94] = "131141"; // "~"
|
|
|
294 |
$this->code[95] = "114113"; // 95
|
|
|
295 |
$this->code[96] = "114311"; // 96
|
|
|
296 |
$this->code[97] = "411113"; // 97
|
|
|
297 |
$this->code[98] = "411311"; // 98
|
|
|
298 |
$this->code[99] = "113141"; // 99
|
|
|
299 |
$this->code[100] = "114131"; // 100
|
|
|
300 |
$this->code[101] = "311141"; // 101
|
|
|
301 |
$this->code[102] = "411131"; // 102
|
|
|
302 |
}
|
|
|
303 |
|
|
|
304 |
/**
|
|
|
305 |
* Return the Code128 code for a character
|
|
|
306 |
*/
|
|
|
307 |
function getCharCode($c) {
|
|
|
308 |
$retval = $this->code[ord($c) - 32];
|
|
|
309 |
return $retval;
|
|
|
310 |
}
|
|
|
311 |
|
|
|
312 |
/**
|
|
|
313 |
* Return the Start Code for Code128
|
|
|
314 |
*/
|
|
|
315 |
function getStartCode() {
|
|
|
316 |
return '211214';
|
|
|
317 |
}
|
|
|
318 |
|
|
|
319 |
/**
|
|
|
320 |
* Return the Stop Code for Code128
|
|
|
321 |
*/
|
|
|
322 |
function getStopCode() {
|
|
|
323 |
return '2331112';
|
|
|
324 |
}
|
|
|
325 |
|
|
|
326 |
/**
|
|
|
327 |
* Return the Code128 code equivalent of a character number
|
|
|
328 |
*/
|
|
|
329 |
function getNumCode($index) {
|
|
|
330 |
$retval = $this->code[$index];
|
|
|
331 |
return $retval;
|
|
|
332 |
}
|
|
|
333 |
|
|
|
334 |
/**
|
|
|
335 |
* Return the Code128 numerical equivalent of a character.
|
|
|
336 |
*/
|
|
|
337 |
function getCharNumber($c) {
|
|
|
338 |
$retval = ord($c) - 32;
|
|
|
339 |
return $retval;
|
|
|
340 |
}
|
|
|
341 |
|
|
|
342 |
} // class
|
|
|
343 |
?>
|