Blame view

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