cli-barcode PHP
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 

298 行
8.2 KiB

  1. #!/usr/bin/env php
  2. <?php
  3. ##########################################################################
  4. # Gustavo Arnosti Neves - 2016 Jul 11
  5. # guneves < a t > gmail < d o t > com
  6. #
  7. # PHP cli-barcode-generator
  8. #
  9. # Most of the work here is for option / argument parsing.
  10. # Picqer's barcode framework makes it east on the bar coding generation.
  11. # While ulrichsg's getopt-php helps with the cli part.
  12. #
  13. ##########################################################################
  14. require_once 'vendor/autoload.php';
  15. define("_BC_VERSION", "1.0.2");
  16. # permission to set to barcode files
  17. define("_BC_PERMISSION", 0644);
  18. # group to set to barcode files (disabled at bot)
  19. define("_BC_SYSGROUP", "yourGrpHere");
  20. # default padding for barcode
  21. define("_BC_PADDING", 30);
  22. $verbose = false;
  23. $quiet = false;
  24. $encoding = null;
  25. $format = null;
  26. $bc_string = null;
  27. $bc_file = null;
  28. $width = 2;
  29. $height = 30;
  30. $color = '#000000';
  31. $encodings_list = array(
  32. 'CODE_39',
  33. 'CODE_39_CHECKSUM',
  34. 'CODE_39E',
  35. 'CODE_39E_CHECKSUM',
  36. 'CODE_93',
  37. 'STANDARD_2_5',
  38. 'STANDARD_2_5_CHECKSUM',
  39. 'INTERLEAVED_2_5',
  40. 'INTERLEAVED_2_5_CHECKSUM',
  41. 'CODE_128',
  42. 'CODE_128_A',
  43. 'CODE_128_B',
  44. 'CODE_128_C',
  45. 'EAN_2',
  46. 'EAN_5',
  47. 'EAN_8',
  48. 'EAN_13',
  49. 'UPC_A',
  50. 'UPC_E',
  51. 'MSI',
  52. 'MSI_CHECKSUM',
  53. 'POSTNET',
  54. 'PLANET',
  55. 'RMS4CC',
  56. 'KIX',
  57. 'IMB',
  58. 'CODABAR',
  59. 'CODE_11',
  60. 'PHARMA_CODE',
  61. 'PHARMA_CODE_TWO_TRACKS'
  62. );
  63. sort($encodings_list);
  64. $formats_list = array(
  65. 'SVG',
  66. 'PNG',
  67. 'JPG',
  68. 'HTML'
  69. );
  70. sort($formats_list);
  71. /////////////////// GETOPT STARTS
  72. use Ulrichsg\Getopt\Getopt;
  73. use Ulrichsg\Getopt\Option;
  74. use Ulrichsg\Getopt\Argument;
  75. // define and configure options
  76. $getopt = new Getopt(array(
  77. (new Option('e', 'encoding', Getopt::REQUIRED_ARGUMENT))
  78. ->setDescription('Barcode encoding type selection')
  79. ->setValidation(function($value) {
  80. global $encodings_list;
  81. return in_array(strtoupper($value), $encodings_list);
  82. })
  83. ->setArgument(new Argument(null, null, 'bar-type')),
  84. (new Option('f', 'format', Getopt::REQUIRED_ARGUMENT))
  85. ->setDescription('Output format for the barcode')
  86. ->setValidation(function($value, $formats_list) {
  87. global $formats_list;
  88. return in_array(strtoupper($value), $formats_list);
  89. })
  90. ->setArgument(new Argument(null, null, 'file-type')),
  91. (new Option('w', 'width', Getopt::REQUIRED_ARGUMENT))
  92. ->setDescription('Width factor for bars to make wider, defaults to 2')
  93. ->setArgument(new Argument(2, 'is_numeric', 'points')),
  94. (new Option('h', 'height', Getopt::REQUIRED_ARGUMENT))
  95. ->setDescription('Total height of the barcode, defaults to 30')
  96. ->setArgument(new Argument(30, 'is_numeric', 'points')),
  97. (new Option('c', 'color', Getopt::REQUIRED_ARGUMENT))
  98. ->setDescription('Hex code of the foreground color, defaults to black')
  99. ->setArgument(new Argument('#000000', 'not_empty', 'hex-color')),
  100. (new Option('v', 'verbose'))
  101. ->setDescription('Display extra information')
  102. ->setDefaultValue(false),
  103. (new Option('q', 'quiet'))
  104. ->setDescription('Supress all messages')
  105. ->setDefaultValue(false),
  106. (new Option(null, 'help'))
  107. ->setDescription('Help Information, including encodings and formats'),
  108. (new Option(null, 'version'))
  109. ->setDescription('Display version information and exits'),
  110. (new Option(null, 'create-bash'))
  111. ->setDescription('Creates a bash script named barcode that can call this script')
  112. ));
  113. $getopt->setBanner("Usage: barcode -e <encoding> -f <output_format> [options] <barcode string> <output file>\n");
  114. try {
  115. $getopt->parse();
  116. if ($getopt['version']) {
  117. echo "PHP-CLI Barcode v"._BC_VERSION."\n";
  118. exit(0);
  119. }
  120. if ($getopt['help']) {
  121. print_help($getopt);
  122. exit(0);
  123. }
  124. if ($getopt['create-bash']) {
  125. create_bash_script();
  126. exit(1);
  127. }
  128. $verbose = $getopt['verbose'];
  129. $quiet = $getopt['quiet'];
  130. $encoding = $getopt['encoding'];
  131. $format = $getopt['format'];
  132. $width = $getopt['width'];
  133. $height = $getopt['height'];
  134. $color = $getopt['color'];
  135. $bc_string = $getopt->getOperand(0);
  136. $bc_file = $getopt->getOperand(1);
  137. } catch (UnexpectedValueException $e) {
  138. echo "Error: ".$e->getMessage()."\n";
  139. echo $getopt->getHelpText(_BC_PADDING);
  140. exit(1);
  141. }
  142. // check if we can proceed
  143. if (empty($encoding) || empty($format) || empty($bc_string) || empty($bc_file)) {
  144. echo "Error: Invalid parameters or options.\n";
  145. echo $getopt->getHelpText(_BC_PADDING);
  146. exit(1);
  147. }
  148. // Match case
  149. $encoding = strtoupper($encoding);
  150. $format = strtoupper($format);
  151. /////////////////// GETOPT ENDS
  152. /////////////////// CREATE BARCODE
  153. // creates a bash script named barcode that will run this script
  154. // from anywhere on the system. Assumes barcode.php is running
  155. // on its final installation location
  156. function create_bash_script() {
  157. $error = true;
  158. $bc_path = __FILE__;
  159. $bash_path = dirname($bc_path) . DIRECTORY_SEPARATOR . "barcode";
  160. $bash_script = <<<EOF
  161. #!/usr/bin/env bash
  162. ##################################################
  163. # Gustavo Arnosti Neves - 2016 Jul 11
  164. # Simple bash script for global barcode executable
  165. # PHP cli-barcode-generator
  166. #
  167. # Please run "./barcode.php --create-bash" to update this script
  168. # You can then use sudo make install to copy it to /usr/local/bin
  169. #
  170. # This won't work on windows
  171. #
  172. BARCODE_LOCATION="$bc_path" # enter full path here
  173. /usr/bin/env php "\$BARCODE_LOCATION" "\$@"
  174. EOF;
  175. if (file_exists($bash_path)) {
  176. unlink($bash_path) or die("Could not remove old barcode script, are you running from it?");
  177. }
  178. $error = file_put_contents($bash_path, $bash_script) === true ? false : true;
  179. $error = chmod($bash_path, 0755) === true ? false : true;
  180. if ($error) {
  181. echo "\nAn error was detected during the process.\n";
  182. echo "Please check for permissions and try again.\n\n";
  183. exit(2);
  184. }
  185. echo "\nThe file \"$bash_path\" was successfully created.\n";
  186. echo "You may perform a system install to /usr/local/bin by issuing\n";
  187. echo "the command \"sudo make install\"\n\n";
  188. exit(0);
  189. }
  190. // get actual barcode type string
  191. $bc_type = constant('Picqer\Barcode\BarcodeGenerator::TYPE_'.$encoding);
  192. // add trailling zero if odd digits on Code128C
  193. $bc_string = strlen($bc_string) % 2 == 0 && $encoding === 'CODE_128_C'?
  194. $bc_string : '0'.$bc_string;
  195. // create appropriate generator
  196. $generator = null;
  197. if ($format === 'SVG') {
  198. $generator = new Picqer\Barcode\BarcodeGeneratorSVG();
  199. } else if ($format === 'PNG') {
  200. $generator = new Picqer\Barcode\BarcodeGeneratorPNG();
  201. } else if ($format === 'JPG') {
  202. $generator = new Picqer\Barcode\BarcodeGeneratorJPG();
  203. } else if ($format === 'HTML') {
  204. $generator = new Picqer\Barcode\BarcodeGeneratorHTML();
  205. } else {
  206. exit(1);
  207. }
  208. // generate de barcode
  209. $bc_data = $generator->getBarcode($bc_string, $bc_type, $width, $height, $color);
  210. // save to file
  211. if (file_put_contents($bc_file, $bc_data) === false) {
  212. echo "Error: could not save file $bc_file!\n";
  213. exit(1);
  214. }
  215. // set permissions and group
  216. chmod($bc_file, _BC_PERMISSION);
  217. #chgrp($bc_file, _BC_SYSGROUP);
  218. // prints help information
  219. function print_help($getopt) {
  220. global $encodings_list;
  221. global $formats_list;
  222. echo $getopt->getHelpText(_BC_PADDING);
  223. echo "\nRequired Options and Parameters:\n";
  224. echo " -e <encoding>\n";
  225. echo " -f <output format>\n";
  226. echo " <input string>\n";
  227. echo " <output file>\n";
  228. echo "\nOutput Formats:\n";
  229. foreach($formats_list as $for) {
  230. echo " $for\n";
  231. }
  232. echo "\nEncodings:\n";
  233. foreach($encodings_list as $enc) {
  234. echo " $enc\n";
  235. }
  236. echo "\nExamples:\n";
  237. echo " barcode -f HTML -e CODE_39 \"1234567890\" \"/tmp/1234567890.html\"\n";
  238. echo " barcode -e CODE_128 -f PNG -c \"#888\" -w 3 -h 50 \"AGREATBAR\" \"/tmp/AGREATBAR.png\"\n";
  239. echo " barcode \"1234567890\" \"/tmp/mybar.svg\" --encoding EAN_13 --format SVG\n";
  240. }
  241. // check if empty (callback)
  242. function not_empty($str) {
  243. return (!empty($str));
  244. }
  245. // done
  246. exit(0);
  247. ?>