1 | <?php
|
---|
2 | require_once('jpgraph/jpgraph_barcode.php');
|
---|
3 |
|
---|
4 | /*=======================================================================
|
---|
5 | // File: MKBARCODE.PHP
|
---|
6 | // Description: Comman line tool to generate linear barcodes
|
---|
7 | // Created: 2009-06-20
|
---|
8 | // Ver: $Id: mkbarcode.php 1455 2009-07-03 18:52:25Z ljp $
|
---|
9 | //
|
---|
10 | // Copyright (c) Asial Corporation. All rights reserved.
|
---|
11 | //=======================================================================
|
---|
12 | */
|
---|
13 |
|
---|
14 | //----------------------------------------------------------------------
|
---|
15 | // CLASS ParseArgs
|
---|
16 | // Parse command line arguments and make sanity checks
|
---|
17 | //----------------------------------------------------------------------
|
---|
18 | class ParseArgs {
|
---|
19 | var $argc,$argv;
|
---|
20 |
|
---|
21 | function ParseArgs() {
|
---|
22 | // Get command line argument
|
---|
23 | $this->argv = ($_SERVER['argv']);
|
---|
24 | $this->argc = ($_SERVER['argc']);
|
---|
25 | }
|
---|
26 |
|
---|
27 | function PrintUsage() {
|
---|
28 | $n = $this->argv[0];
|
---|
29 | echo "$n -b <symbology> [-r -h -c -o <output format> -m <width> -s <scale> -y <height> -f <filename> ] datastring \n".
|
---|
30 | "Create the specified barcode\n".
|
---|
31 | "-b What symbology to use, one of the following strings (case insensitive)\n".
|
---|
32 | " UPCA \n".
|
---|
33 | " UPCE \n".
|
---|
34 | " EAN128 \n".
|
---|
35 | " EAN13 \n".
|
---|
36 | " EAN8 \n".
|
---|
37 | " CODE11 \n".
|
---|
38 | " CODE39 \n".
|
---|
39 | " CODE128 \n".
|
---|
40 | " CODE25 \n".
|
---|
41 | " CODEI25 \n".
|
---|
42 | " CODABAR \n".
|
---|
43 | " BOOKLAND \n".
|
---|
44 | "-c Add checkdigit for symbologies where this is optional\n".
|
---|
45 | "-o Output format. 0=Image, 1=PS, 2=EPS\n".
|
---|
46 | "-m Module width\n".
|
---|
47 | "-s Scale factor\n".
|
---|
48 | "-h Show this help\n".
|
---|
49 | "-f Filename to write to\n".
|
---|
50 | "-r Rotate barcode 90 degrees\n".
|
---|
51 | "-y height Set height in pixels\n".
|
---|
52 | "-x Hide the human readable text\n".
|
---|
53 | "--silent Silent. Don't give any error mesages\n";
|
---|
54 | exit(1);
|
---|
55 | }
|
---|
56 |
|
---|
57 | function Get() {
|
---|
58 | $barcode='code39';
|
---|
59 | $hide=false;
|
---|
60 | $checkdigit=false;
|
---|
61 | $modulewidth=2;
|
---|
62 | $scale=1;
|
---|
63 | $output=0;
|
---|
64 | $filename='';
|
---|
65 | $data = '';
|
---|
66 | $rotate = false;
|
---|
67 | $silent=false;
|
---|
68 | $height = 70;
|
---|
69 | if( ($n=$this->GetNum()) > 0 ) {
|
---|
70 | $i=1;
|
---|
71 | while( $i <= $n ) {
|
---|
72 | switch( $this->argv[$i] ) {
|
---|
73 | case '-h':
|
---|
74 | $this->PrintUsage();
|
---|
75 | exit(0);
|
---|
76 | break;
|
---|
77 | case '-b':
|
---|
78 | $barcode = $this->argv[++$i];
|
---|
79 | break;
|
---|
80 | case '-o':
|
---|
81 | $output = (int)$this->argv[++$i];
|
---|
82 | break;
|
---|
83 | case '-y':
|
---|
84 | $height = (int)$this->argv[++$i];
|
---|
85 | break;
|
---|
86 | case '-x':
|
---|
87 | $hide=true;
|
---|
88 | break;
|
---|
89 | case '-r':
|
---|
90 | $rotate=true;
|
---|
91 | break;
|
---|
92 | case '-c':
|
---|
93 | $checkdigit=true;
|
---|
94 | break;
|
---|
95 | case '--silent':
|
---|
96 | $silent=true;
|
---|
97 | break;
|
---|
98 | case '-s':
|
---|
99 | $scale = (float)$this->argv[++$i];
|
---|
100 | break;
|
---|
101 | case '-m':
|
---|
102 | $modulewidth = (float)$this->argv[++$i];
|
---|
103 | break;
|
---|
104 | case '-f':
|
---|
105 | $filename = $this->argv[++$i];
|
---|
106 | break;
|
---|
107 | default:
|
---|
108 | if( $data == '' ) {
|
---|
109 | $data = $this->argv[$i];
|
---|
110 | }
|
---|
111 | else {
|
---|
112 | $this->PrintUsage();
|
---|
113 | die("Illegal specified parameters");
|
---|
114 | }
|
---|
115 | break;
|
---|
116 | }
|
---|
117 | ++$i;
|
---|
118 | }
|
---|
119 |
|
---|
120 | }
|
---|
121 |
|
---|
122 | if( $output < 0 || $output > 2 ) {
|
---|
123 | fwrite(STDERR,"Unkown output format ($output)\n");
|
---|
124 | exit(1);
|
---|
125 | }
|
---|
126 |
|
---|
127 | if( $output === 0 ) {
|
---|
128 | $modulewidth = floor($modulewidth);
|
---|
129 | }
|
---|
130 |
|
---|
131 | // Sanity check
|
---|
132 | if( $modulewidth > 15 ) {
|
---|
133 | fwrite(STDERR,"Too large modulewidth\n");
|
---|
134 | exit(1);
|
---|
135 | }
|
---|
136 |
|
---|
137 | // Sanity check
|
---|
138 | if( $height > 1000 ) {
|
---|
139 | fwrite(STDERR,"Too large height\n");
|
---|
140 | exit(1);
|
---|
141 | }
|
---|
142 |
|
---|
143 | // Sanity check
|
---|
144 | if( $scale > 15 ) {
|
---|
145 | fwrite(STDERR,"Too large scale factor\n");
|
---|
146 | exit(1);
|
---|
147 | }
|
---|
148 |
|
---|
149 | if( strlen($filename) > 256 ) {
|
---|
150 | fwrite(STDERR,"Too long filename\n");
|
---|
151 | exit(1);
|
---|
152 | }
|
---|
153 |
|
---|
154 | if( trim($data) == '' ) {
|
---|
155 | fwrite(STDERR,"No input data specified\n");
|
---|
156 | exit(1);
|
---|
157 | }
|
---|
158 |
|
---|
159 | $barcodes = array(
|
---|
160 | 'UPCA' => ENCODING_UPCA,
|
---|
161 | 'UPCE' => ENCODING_UPCE,
|
---|
162 | 'EAN128' => ENCODING_EAN128,
|
---|
163 | 'EAN13' => ENCODING_EAN13,
|
---|
164 | 'EAN8' => ENCODING_EAN8,
|
---|
165 | 'CODE11' => ENCODING_CODE11,
|
---|
166 | 'CODE39' => ENCODING_CODE39,
|
---|
167 | 'CODE128' => ENCODING_CODE128,
|
---|
168 | 'CODE25' => ENCODING_CODE25,
|
---|
169 | 'CODEI25' => ENCODING_CODEI25,
|
---|
170 | 'CODABAR' => ENCODING_CODABAR,
|
---|
171 | 'BOOKLAND' => ENCODING_BOOKLAND,
|
---|
172 | );
|
---|
173 | $barcode = strtoupper($barcode);
|
---|
174 | if( key_exists($barcode,$barcodes) ) {
|
---|
175 | $barcode = $barcodes[$barcode];
|
---|
176 | }
|
---|
177 | else {
|
---|
178 | fwrite(STDERR,'Specified barcode symbology ('.$barcode.") is not supported\n");
|
---|
179 | exit(1);
|
---|
180 | }
|
---|
181 |
|
---|
182 | $ret = array(
|
---|
183 | 'barcode' => $barcode,
|
---|
184 | 'hide' => $hide,
|
---|
185 | 'modulewidth' => $modulewidth,
|
---|
186 | 'scale' => $scale,
|
---|
187 | 'output' => $output,
|
---|
188 | 'data' => $data,
|
---|
189 | 'silent' => $silent,
|
---|
190 | 'rotate' => $rotate,
|
---|
191 | 'height' => $height,
|
---|
192 | 'checkdigit' => $checkdigit,
|
---|
193 | 'filename' => $filename
|
---|
194 | );
|
---|
195 |
|
---|
196 | return $ret;
|
---|
197 | }
|
---|
198 |
|
---|
199 | function _Dump() {
|
---|
200 | var_dump($this->argv);
|
---|
201 | }
|
---|
202 |
|
---|
203 | function GetNum() {
|
---|
204 | return $this->argc-1;
|
---|
205 | }
|
---|
206 | }
|
---|
207 |
|
---|
208 | //----------------------------------------------------------------------
|
---|
209 | // CLASS Driver
|
---|
210 | // Main driver class to create barcodes with the parmeters specified on
|
---|
211 | // the command line.
|
---|
212 | //----------------------------------------------------------------------
|
---|
213 | class Driver {
|
---|
214 |
|
---|
215 | private $iParams;
|
---|
216 | static public $silent=false;
|
---|
217 |
|
---|
218 | static public function ErrHandlerPS(Exception $e) {
|
---|
219 | if( !Driver::$silent )
|
---|
220 | fwrite(STDERR,$e->getMessage()."\n");
|
---|
221 | exit(1);
|
---|
222 | }
|
---|
223 |
|
---|
224 | static public function ErrHandlerImg(Exception $e) {
|
---|
225 | if( !Driver::$silent )
|
---|
226 | fwrite(STDERR,$e->getMessage()."\n");
|
---|
227 | $errobj = new JpGraphErrObjectImg();
|
---|
228 | $errobj->Raise($e->getMessage());
|
---|
229 | exit(1);
|
---|
230 | }
|
---|
231 |
|
---|
232 | function Run($aParams) {
|
---|
233 |
|
---|
234 | $this->iParams = $aParams;
|
---|
235 |
|
---|
236 | Driver::$silent = $aParams['silent'];
|
---|
237 |
|
---|
238 | $encoder = BarcodeFactory::Create($aParams['barcode']);
|
---|
239 | $encoder->AddChecksum($aParams['checkdigit']);
|
---|
240 | switch( $aParams['output'] ) {
|
---|
241 | case 0:
|
---|
242 | $e = BackendFactory::Create(BACKEND_IMAGE,$encoder);
|
---|
243 | set_exception_handler(array('Driver','ErrHandlerImg'));
|
---|
244 | break;
|
---|
245 | case 1:
|
---|
246 | $e = BackendFactory::Create(BACKEND_PS,$encoder);
|
---|
247 | set_exception_handler(array('Driver','ErrHandlerPS'));
|
---|
248 | break;
|
---|
249 | case 2:
|
---|
250 | $e = BackendFactory::Create(BACKEND_PS,$encoder);
|
---|
251 | $e->SetEPS();
|
---|
252 | set_exception_handler(array('Driver','ErrHandlerPS'));
|
---|
253 | break;
|
---|
254 | }
|
---|
255 | $e->SetHeight($aParams['height']);
|
---|
256 | $e->SetVertical($aParams['rotate']);
|
---|
257 | $e->SetModuleWidth($aParams['modulewidth']);
|
---|
258 | $e->SetScale($aParams['scale']);
|
---|
259 | $e->HideText($aParams['hide']);
|
---|
260 | if( $aParams['output'] === 0 ) {
|
---|
261 | $err = $e->Stroke($aParams['data'], $aParams['filename']);
|
---|
262 | }
|
---|
263 | else {
|
---|
264 | $s = $e->Stroke($aParams['data'], $aParams['filename']);
|
---|
265 | if( $aParams['filename'] == '' ) {
|
---|
266 | // If no filename specified then return the generated postscript
|
---|
267 | echo $s;
|
---|
268 | }
|
---|
269 | }
|
---|
270 | }
|
---|
271 | }
|
---|
272 |
|
---|
273 | $pa = new ParseArgs();
|
---|
274 | $params = $pa->Get();
|
---|
275 | $driver = new Driver();
|
---|
276 | $driver->Run($params);
|
---|
277 |
|
---|
278 | // Successfull termination
|
---|
279 | exit(0);
|
---|
280 |
|
---|
281 | ?>
|
---|