| 1 |
lars |
1 |
<?php
|
|
|
2 |
/* vim: set expandtab tabstop=4 softtabstop=4 shiftwidth=4: */
|
|
|
3 |
|
|
|
4 |
/**
|
|
|
5 |
* Image_Barcode_Code39 class
|
|
|
6 |
*
|
|
|
7 |
* Image_Barcode_Code39 creates Code 3 of 9 ( Code39 ) barcode images. It's
|
|
|
8 |
* implementation borrows heavily for the perl module GD::Barcode::Code39
|
|
|
9 |
*
|
|
|
10 |
* PHP versions 4
|
|
|
11 |
*
|
|
|
12 |
* LICENSE: This source file is subject to version 3.0 of the PHP license
|
|
|
13 |
* that is available through the world-wide-web at the following URI:
|
|
|
14 |
* http://www.php.net/license/3_0.txt. If you did not receive a copy of
|
|
|
15 |
* the PHP License and are unable to obtain it through the web, please
|
|
|
16 |
* send a note to license@php.net so we can mail you a copy immediately.
|
|
|
17 |
*
|
|
|
18 |
* @category Image
|
|
|
19 |
* @package Image_Barcode
|
|
|
20 |
* @author Ryan Briones <ryanbriones@webxdesign.org>
|
|
|
21 |
* @copyright 2005 The PHP Group
|
|
|
22 |
* @license http://www.php.net/license/3_0.txt PHP License 3.0
|
|
|
23 |
* @version CVS: $Id: Code39.php 304741 2010-10-25 09:14:17Z clockwerx $
|
|
|
24 |
* @link http://pear.php.net/package/Image_Barcode
|
|
|
25 |
*/
|
|
|
26 |
|
|
|
27 |
|
|
|
28 |
require_once "Image/Barcode.php";
|
|
|
29 |
|
|
|
30 |
|
|
|
31 |
if (!function_exists('str_split')) {
|
|
|
32 |
require_once 'PHP/Compat.php';
|
|
|
33 |
PHP_Compat::loadFunction('str_split');
|
|
|
34 |
}
|
|
|
35 |
|
|
|
36 |
/**
|
|
|
37 |
* Image_Barcode_Code39 class
|
|
|
38 |
*
|
|
|
39 |
* Package which provides a method to create Code39 using GD library.
|
|
|
40 |
*
|
|
|
41 |
* @category Image
|
|
|
42 |
* @package Image_Barcode
|
|
|
43 |
* @author Ryan Briones <ryanbriones@webxdesign.org>
|
|
|
44 |
* @copyright 2005 The PHP Group
|
|
|
45 |
* @license http://www.php.net/license/3_0.txt PHP License 3.0
|
|
|
46 |
* @version Release: @package_version@
|
|
|
47 |
* @link http://pear.php.net/package/Image_Barcode
|
|
|
48 |
* @since Image_Barcode 0.5
|
|
|
49 |
*/
|
|
|
50 |
class Image_Barcode_Code39 extends Image_Barcode
|
|
|
51 |
{
|
|
|
52 |
/**
|
|
|
53 |
* Barcode type
|
|
|
54 |
* @var string
|
|
|
55 |
*/
|
|
|
56 |
var $_type = 'Code39';
|
|
|
57 |
|
|
|
58 |
/**
|
|
|
59 |
* Barcode height
|
|
|
60 |
*
|
|
|
61 |
* @var integer
|
|
|
62 |
*/
|
|
|
63 |
var $_barcodeheight = 50;
|
|
|
64 |
|
|
|
65 |
/**
|
|
|
66 |
* Bar thin width
|
|
|
67 |
*
|
|
|
68 |
* @var integer
|
|
|
69 |
*/
|
|
|
70 |
var $_barthinwidth = 1;
|
|
|
71 |
|
|
|
72 |
/**
|
|
|
73 |
* Bar thick width
|
|
|
74 |
*
|
|
|
75 |
* @var integer
|
|
|
76 |
*/
|
|
|
77 |
var $_barthickwidth = 3;
|
|
|
78 |
|
|
|
79 |
/**
|
|
|
80 |
* Coding map
|
|
|
81 |
* @var array
|
|
|
82 |
*/
|
|
|
83 |
var $_coding_map = array(
|
|
|
84 |
'0' => '000110100',
|
|
|
85 |
'1' => '100100001',
|
|
|
86 |
'2' => '001100001',
|
|
|
87 |
'3' => '101100000',
|
|
|
88 |
'4' => '000110001',
|
|
|
89 |
'5' => '100110000',
|
|
|
90 |
'6' => '001110000',
|
|
|
91 |
'7' => '000100101',
|
|
|
92 |
'8' => '100100100',
|
|
|
93 |
'9' => '001100100',
|
|
|
94 |
'A' => '100001001',
|
|
|
95 |
'B' => '001001001',
|
|
|
96 |
'C' => '101001000',
|
|
|
97 |
'D' => '000011001',
|
|
|
98 |
'E' => '100011000',
|
|
|
99 |
'F' => '001011000',
|
|
|
100 |
'G' => '000001101',
|
|
|
101 |
'H' => '100001100',
|
|
|
102 |
'I' => '001001100',
|
|
|
103 |
'J' => '000011100',
|
|
|
104 |
'K' => '100000011',
|
|
|
105 |
'L' => '001000011',
|
|
|
106 |
'M' => '101000010',
|
|
|
107 |
'N' => '000010011',
|
|
|
108 |
'O' => '100010010',
|
|
|
109 |
'P' => '001010010',
|
|
|
110 |
'Q' => '000000111',
|
|
|
111 |
'R' => '100000110',
|
|
|
112 |
'S' => '001000110',
|
|
|
113 |
'T' => '000010110',
|
|
|
114 |
'U' => '110000001',
|
|
|
115 |
'V' => '011000001',
|
|
|
116 |
'W' => '111000000',
|
|
|
117 |
'X' => '010010001',
|
|
|
118 |
'Y' => '110010000',
|
|
|
119 |
'Z' => '011010000',
|
|
|
120 |
'-' => '010000101',
|
|
|
121 |
'*' => '010010100',
|
|
|
122 |
'+' => '010001010',
|
|
|
123 |
'$' => '010101000',
|
|
|
124 |
'%' => '000101010',
|
|
|
125 |
'/' => '010100010',
|
|
|
126 |
'.' => '110000100',
|
|
|
127 |
' ' => '011000100'
|
|
|
128 |
);
|
|
|
129 |
|
|
|
130 |
/**
|
|
|
131 |
* Constructor
|
|
|
132 |
*
|
|
|
133 |
* @param string $text A text that should be in the image barcode
|
|
|
134 |
* @param int $wThin Width of the thin lines on the barcode
|
|
|
135 |
* @param int $wThick Width of the thick lines on the barcode
|
|
|
136 |
*
|
|
|
137 |
* @author Ryan Briones <ryanbriones@webxdesign.org>
|
|
|
138 |
*
|
|
|
139 |
*/
|
|
|
140 |
function Image_Barcode_Code39( $text = '', $wThin = 0, $wThick = 0 )
|
|
|
141 |
{
|
|
|
142 |
// Check $text for invalid characters
|
|
|
143 |
if ( $this->checkInvalid( $text ) ) {
|
|
|
144 |
return false;
|
|
|
145 |
}
|
|
|
146 |
|
|
|
147 |
$this->text = $text;
|
|
|
148 |
if ( $wThin > 0 ) $this->_barthinwidth = $wThin;
|
|
|
149 |
if ( $wThick > 0 ) $this->_barthickwidth = $wThick;
|
|
|
150 |
|
|
|
151 |
return true;
|
|
|
152 |
}
|
|
|
153 |
|
|
|
154 |
/**
|
|
|
155 |
* Make an image resource using the GD image library
|
|
|
156 |
*
|
|
|
157 |
* @param bool $noText Set to true if you'd like your barcode to be sans text
|
|
|
158 |
* @param int $bHeight height of the barcode image including text
|
|
|
159 |
* @return resource The Barcode Image (TM)
|
|
|
160 |
*
|
|
|
161 |
* @author Ryan Briones <ryanbriones@webxdesign.org>
|
|
|
162 |
*
|
|
|
163 |
*/
|
|
|
164 |
function plot($noText = false, $bHeight = 0)
|
|
|
165 |
{
|
|
|
166 |
// add start and stop * characters
|
|
|
167 |
$final_text = '*' . $this->text . '*';
|
|
|
168 |
|
|
|
169 |
if ( $bHeight > 0 ) {
|
|
|
170 |
$this->_barcodeheight = $bHeight;
|
|
|
171 |
}
|
|
|
172 |
|
|
|
173 |
$barcode = '';
|
|
|
174 |
foreach ( str_split( $final_text ) as $character ) {
|
|
|
175 |
$barcode .= $this->_dumpCode( $this->_coding_map[$character] . '0' );
|
|
|
176 |
}
|
|
|
177 |
|
|
|
178 |
$barcode_len = strlen( $barcode );
|
|
|
179 |
|
|
|
180 |
// Create GD image object
|
|
|
181 |
$img = imagecreate( $barcode_len, $this->_barcodeheight );
|
|
|
182 |
|
|
|
183 |
// Allocate black and white colors to the image
|
|
|
184 |
$black = imagecolorallocate( $img, 0, 0, 0 );
|
|
|
185 |
$white = imagecolorallocate( $img, 255, 255, 255 );
|
|
|
186 |
$font_height = ( $noText ? 0 : imagefontheight( "gdFontSmall" ) );
|
|
|
187 |
$font_width = imagefontwidth( "gdFontSmall" );
|
|
|
188 |
|
|
|
189 |
// fill background with white color
|
|
|
190 |
imagefill( $img, 0, 0, $white );
|
|
|
191 |
|
|
|
192 |
// Initialize X position
|
|
|
193 |
$xpos = 0;
|
|
|
194 |
|
|
|
195 |
// draw barcode bars to image
|
|
|
196 |
if ( $noText ) {
|
|
|
197 |
foreach (str_split($barcode) as $character_code ) {
|
|
|
198 |
if ($character_code == 0 ) {
|
|
|
199 |
imageline($img, $xpos, 0, $xpos, $this->_barcodeheight, $white);
|
|
|
200 |
} else {
|
|
|
201 |
imageline($img, $xpos, 0, $xpos, $this->_barcodeheight, $black);
|
|
|
202 |
}
|
|
|
203 |
|
|
|
204 |
$xpos++;
|
|
|
205 |
}
|
|
|
206 |
} else {
|
|
|
207 |
foreach (str_split($barcode) as $character_code ) {
|
|
|
208 |
if ($character_code == 0) {
|
|
|
209 |
imageline($img, $xpos, 0, $xpos, $this->_barcodeheight - $font_height - 1, $white);
|
|
|
210 |
} else {
|
|
|
211 |
imageline($img, $xpos, 0, $xpos, $this->_barcodeheight - $font_height - 1, $black);
|
|
|
212 |
}
|
|
|
213 |
|
|
|
214 |
$xpos++;
|
|
|
215 |
}
|
|
|
216 |
|
|
|
217 |
// draw text under barcode
|
|
|
218 |
imagestring(
|
|
|
219 |
$img,
|
|
|
220 |
'gdFontSmall',
|
|
|
221 |
( $barcode_len - $font_width * strlen( $this->text ) )/2,
|
|
|
222 |
$this->_barcodeheight - $font_height,
|
|
|
223 |
$this->text,
|
|
|
224 |
$black
|
|
|
225 |
);
|
|
|
226 |
}
|
|
|
227 |
|
|
|
228 |
return $img;
|
|
|
229 |
}
|
|
|
230 |
|
|
|
231 |
/**
|
|
|
232 |
* Send image to the browser; for Image_Barcode compaitbility
|
|
|
233 |
*
|
|
|
234 |
* @param string $text
|
|
|
235 |
* @param string $imgtype Image type; accepts jpg, png, and gif, but gif only works if you've payed for licensing
|
|
|
236 |
* @param bool $noText Set to true if you'd like your barcode to be sans text
|
|
|
237 |
* @param int $bHeight height of the barcode image including text
|
|
|
238 |
* @return gd_image GD image object
|
|
|
239 |
*
|
|
|
240 |
* @author Ryan Briones <ryanbriones@webxdesign.org>
|
|
|
241 |
*
|
|
|
242 |
*/
|
|
|
243 |
function &draw($text, $imgtype = 'png', $noText = false, $bHeight = 0)
|
|
|
244 |
{
|
|
|
245 |
// Check $text for invalid characters
|
|
|
246 |
if ($this->checkInvalid($text)) {
|
|
|
247 |
return PEAR::raiseError('Invalid text');
|
|
|
248 |
}
|
|
|
249 |
|
|
|
250 |
$this->text = $text;
|
|
|
251 |
$img = &$this->plot($noText, $bHeight);
|
|
|
252 |
|
|
|
253 |
return $img;
|
|
|
254 |
}
|
|
|
255 |
|
|
|
256 |
/**
|
|
|
257 |
* _dumpCode is a PHP implementation of dumpCode from the Perl module
|
|
|
258 |
* GD::Barcode::Code39. I royally screwed up when trying to do the thing
|
|
|
259 |
* my own way the first time. This way works.
|
|
|
260 |
*
|
|
|
261 |
* @param string $code Code39 barcode code
|
|
|
262 |
* @return string $result barcode line code
|
|
|
263 |
*
|
|
|
264 |
* @access private
|
|
|
265 |
*
|
|
|
266 |
* @author Ryan Briones <ryanbriones@webxdesign.org>
|
|
|
267 |
*
|
|
|
268 |
*
|
|
|
269 |
*/
|
|
|
270 |
function _dumpCode($code)
|
|
|
271 |
{
|
|
|
272 |
$result = '';
|
|
|
273 |
$color = 1; // 1: Black, 0: White
|
|
|
274 |
|
|
|
275 |
// if $bit is 1, line is wide; if $bit is 0 line is thin
|
|
|
276 |
foreach ( str_split( $code ) as $bit ) {
|
|
|
277 |
$result .= ( ( $bit == 1 ) ? str_repeat( "$color", $this->_barthickwidth ) : str_repeat( "$color", $this->_barthinwidth ) );
|
|
|
278 |
$color = ( ( $color == 0 ) ? 1 : 0 );
|
|
|
279 |
}
|
|
|
280 |
|
|
|
281 |
return $result;
|
|
|
282 |
}
|
|
|
283 |
|
|
|
284 |
/**
|
|
|
285 |
* Check for invalid characters
|
|
|
286 |
*
|
|
|
287 |
* @param string $text text to be ckecked
|
|
|
288 |
* @return bool returns true when invalid characters have been found
|
|
|
289 |
*
|
|
|
290 |
* @author Ryan Briones <ryanbriones@webxdesign.org>
|
|
|
291 |
*
|
|
|
292 |
*/
|
|
|
293 |
function checkInvalid($text)
|
|
|
294 |
{
|
|
|
295 |
return preg_match( "/[^0-9A-Z\-*+\$%\/. ]/", $text );
|
|
|
296 |
}
|
|
|
297 |
}
|
|
|
298 |
?>
|