cli-barcode PHP
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 

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