PageRenderTime 33ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/protected/vendors/jpgraph/barcode/mkbarcode.php

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