cli-barcode PHP
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 

310 řádky
8.4 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.4");
  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. ->setArgument(new Argument(null, 'isEncoding', 'bar-type')),
  80. (new Option('f', 'format', Getopt::REQUIRED_ARGUMENT))
  81. ->setDescription('Output format for the barcode')
  82. ->setArgument(new Argument(null, 'isFormat', 'file-type')),
  83. (new Option('w', 'width', Getopt::REQUIRED_ARGUMENT))
  84. ->setDescription('Width factor for bars to make wider, defaults to 2')
  85. ->setArgument(new Argument(2, 'is_numeric', 'points')),
  86. (new Option('h', 'height', Getopt::REQUIRED_ARGUMENT))
  87. ->setDescription('Total height of the barcode, defaults to 30')
  88. ->setArgument(new Argument(30, 'is_numeric', 'points')),
  89. (new Option('c', 'color', Getopt::REQUIRED_ARGUMENT))
  90. ->setDescription('Hex code of the foreground color, defaults to black')
  91. ->setArgument(new Argument('#000000', 'not_empty', 'hex-color')),
  92. (new Option('v', 'verbose'))
  93. ->setDescription('Display extra information')
  94. ->setDefaultValue(false),
  95. (new Option('q', 'quiet'))
  96. ->setDescription('Supress all messages')
  97. ->setDefaultValue(false),
  98. (new Option(null, 'help'))
  99. ->setDescription('Help Information, including encodings and formats'),
  100. (new Option(null, 'version'))
  101. ->setDescription('Display version information and exits'),
  102. (new Option(null, 'create-bash'))
  103. ->setDescription('Creates a bash script named barcode that can call this script')
  104. ));
  105. $getopt->setBanner("Usage: barcode -e <encoding> -f <output_format> [options] <barcode string> <output file>\n");
  106. try {
  107. $getopt->parse();
  108. if ($getopt['version']) {
  109. echo "PHP-CLI Barcode v"._BC_VERSION."\n";
  110. exit(0);
  111. }
  112. if ($getopt['help']) {
  113. print_help($getopt);
  114. exit(0);
  115. }
  116. if ($getopt['create-bash']) {
  117. create_bash_script();
  118. exit(1);
  119. }
  120. $verbose = $getopt['verbose'];
  121. $quiet = $getopt['quiet'];
  122. $encoding = $getopt['encoding'];
  123. $format = $getopt['format'];
  124. $width = $getopt['width'];
  125. $height = $getopt['height'];
  126. $color = $getopt['color'];
  127. $bc_string = $getopt->getOperand(0);
  128. $bc_file = $getopt->getOperand(1);
  129. } catch (UnexpectedValueException $e) {
  130. echo "Error: ".$e->getMessage()."\n";
  131. echo $getopt->getHelpText(_BC_PADDING);
  132. exit(1);
  133. }
  134. // check if we can proceed
  135. if (empty($encoding) || empty($format) || empty($bc_string) || empty($bc_file)) {
  136. echo "Error: Invalid parameters or options.\n";
  137. echo $getopt->getHelpText(_BC_PADDING);
  138. exit(1);
  139. }
  140. // Match case
  141. $encoding = strtoupper($encoding);
  142. $format = strtoupper($format);
  143. /////////////////// GETOPT ENDS
  144. /////////////////// CREATE BARCODE
  145. // creates a bash script named barcode that will run this script
  146. // from anywhere on the system. Assumes barcode.php is running
  147. // on its final installation location
  148. function create_bash_script() {
  149. $error = true;
  150. $bc_path = __FILE__;
  151. $bash_path = dirname($bc_path) . DIRECTORY_SEPARATOR . "barcode";
  152. $bash_script = <<<EOF
  153. #!/usr/bin/env bash
  154. ##################################################
  155. # Gustavo Arnosti Neves - 2016 Jul 11
  156. # Simple bash script for global barcode executable
  157. # PHP cli-barcode-generator
  158. #
  159. # Please run "./barcode.php --create-bash" to update this script
  160. # You can then use sudo make install to copy it to /usr/local/bin
  161. #
  162. # This won't work on windows
  163. #
  164. BARCODE_LOCATION="$bc_path" # enter full path here
  165. /usr/bin/env php "\$BARCODE_LOCATION" "\$@"
  166. EOF;
  167. if (file_exists($bash_path)) {
  168. unlink($bash_path) or die("Could not remove old barcode script, are you running from it?");
  169. }
  170. $error = file_put_contents($bash_path, $bash_script) === true ? false : true;
  171. $error = chmod($bash_path, 0755) === true ? false : true;
  172. if ($error) {
  173. echo "\nAn error was detected during the process.\n";
  174. echo "Please check for permissions and try again.\n\n";
  175. exit(2);
  176. }
  177. echo "\nThe file \"$bash_path\" was successfully created.\n";
  178. echo "You may perform a system install to /usr/local/bin by issuing\n";
  179. echo "the command \"sudo make install\"\n\n";
  180. exit(0);
  181. }
  182. // get actual barcode type string
  183. $bc_type = constant('Picqer\Barcode\BarcodeGenerator::TYPE_'.$encoding);
  184. // add trailling zero if odd digits on Code128C
  185. $bc_string = strlen($bc_string) % 2 == 0 && $encoding === 'CODE_128_C'?
  186. $bc_string : '0'.$bc_string;
  187. // create appropriate generator
  188. $generator = null;
  189. if ($format === 'SVG') {
  190. $generator = new Picqer\Barcode\BarcodeGeneratorSVG();
  191. } else if ($format === 'PNG') {
  192. $generator = new Picqer\Barcode\BarcodeGeneratorPNG();
  193. } else if ($format === 'JPG') {
  194. $generator = new Picqer\Barcode\BarcodeGeneratorJPG();
  195. } else if ($format === 'HTML') {
  196. $generator = new Picqer\Barcode\BarcodeGeneratorHTML();
  197. } else {
  198. exit(1);
  199. }
  200. // generate de barcode
  201. $bc_data = $generator->getBarcode($bc_string, $bc_type, $width, $height, $color);
  202. // sanity check
  203. $tgtDir = dirname($bc_file);
  204. if (! is_dir($tgtDir)) {
  205. echo "Error: Could not locate target directory!\nTarget Dir: $tgtDir\n";
  206. exit(1);
  207. }
  208. // save to file
  209. if (@file_put_contents($bc_file, $bc_data) === false) {
  210. echo "Error: could not save file $bc_file!\n";
  211. exit(1);
  212. }
  213. // set permissions and group
  214. chmod($bc_file, _BC_PERMISSION);
  215. #chgrp($bc_file, _BC_SYSGROUP);
  216. // prints help information
  217. function print_help($getopt) {
  218. global $encodings_list;
  219. global $formats_list;
  220. echo $getopt->getHelpText(_BC_PADDING);
  221. echo "\nRequired Options and Parameters:\n";
  222. echo " -e <encoding>\n";
  223. echo " -f <output format>\n";
  224. echo " <input string>\n";
  225. echo " <output file>\n";
  226. echo "\nOutput Formats:\n";
  227. foreach($formats_list as $for) {
  228. echo " $for\n";
  229. }
  230. echo "\nEncodings:\n";
  231. foreach($encodings_list as $enc) {
  232. echo " $enc\n";
  233. }
  234. echo "\nExamples:\n";
  235. echo " barcode -f HTML -e CODE_39 \"1234567890\" \"/tmp/1234567890.html\"\n";
  236. echo " barcode -e CODE_128 -f PNG -c \"#888\" -w 3 -h 50 \"AGREATBAR\" \"/tmp/AGREATBAR.png\"\n";
  237. echo " barcode \"1234567890\" \"/tmp/mybar.svg\" --encoding EAN_13 --format SVG\n";
  238. }
  239. // check if encoding callback
  240. function isEncoding($enc=null) {
  241. global $encodings_list;
  242. return in_array(strtoupper($enc), $encodings_list);
  243. }
  244. // check if format callback
  245. function isFormat($format=null) {
  246. global $formats_list;
  247. return in_array(strtoupper($format), $formats_list);
  248. }
  249. // check if empty callback
  250. function not_empty($str) {
  251. return (!empty($str));
  252. }
  253. // done
  254. exit(0);
  255. ?>