#!/usr/bin/env php gmail < d o t > com # # PHP cli-barcode-generator # # Most of the work here is for option / argument parsing. # Picqer's barcode framework makes it east on the bar coding generation. # While ulrichsg's getopt-php helps with the cli part. # ########################################################################## require_once 'vendor/autoload.php'; define("_BC_VERSION", "1.0.2"); # permission to set to barcode files define("_BC_PERMISSION", 0644); # group to set to barcode files (disabled at bot) define("_BC_SYSGROUP", "yourGrpHere"); # default padding for barcode define("_BC_PADDING", 30); $verbose = false; $quiet = false; $encoding = null; $format = null; $bc_string = null; $bc_file = null; $width = 2; $height = 30; $color = '#000000'; $encodings_list = array( 'CODE_39', 'CODE_39_CHECKSUM', 'CODE_39E', 'CODE_39E_CHECKSUM', 'CODE_93', 'STANDARD_2_5', 'STANDARD_2_5_CHECKSUM', 'INTERLEAVED_2_5', 'INTERLEAVED_2_5_CHECKSUM', 'CODE_128', 'CODE_128_A', 'CODE_128_B', 'CODE_128_C', 'EAN_2', 'EAN_5', 'EAN_8', 'EAN_13', 'UPC_A', 'UPC_E', 'MSI', 'MSI_CHECKSUM', 'POSTNET', 'PLANET', 'RMS4CC', 'KIX', 'IMB', 'CODABAR', 'CODE_11', 'PHARMA_CODE', 'PHARMA_CODE_TWO_TRACKS' ); sort($encodings_list); $formats_list = array( 'SVG', 'PNG', 'JPG', 'HTML' ); sort($formats_list); /////////////////// GETOPT STARTS use Ulrichsg\Getopt\Getopt; use Ulrichsg\Getopt\Option; use Ulrichsg\Getopt\Argument; // define and configure options $getopt = new Getopt(array( (new Option('e', 'encoding', Getopt::REQUIRED_ARGUMENT)) ->setDescription('Barcode encoding type selection') ->setValidation(function($value) { global $encodings_list; return in_array(strtoupper($value), $encodings_list); }) ->setArgument(new Argument(null, null, 'bar-type')), (new Option('f', 'format', Getopt::REQUIRED_ARGUMENT)) ->setDescription('Output format for the barcode') ->setValidation(function($value, $formats_list) { global $formats_list; return in_array(strtoupper($value), $formats_list); }) ->setArgument(new Argument(null, null, 'file-type')), (new Option('w', 'width', Getopt::REQUIRED_ARGUMENT)) ->setDescription('Width factor for bars to make wider, defaults to 2') ->setArgument(new Argument(2, 'is_numeric', 'points')), (new Option('h', 'height', Getopt::REQUIRED_ARGUMENT)) ->setDescription('Total height of the barcode, defaults to 30') ->setArgument(new Argument(30, 'is_numeric', 'points')), (new Option('c', 'color', Getopt::REQUIRED_ARGUMENT)) ->setDescription('Hex code of the foreground color, defaults to black') ->setArgument(new Argument('#000000', 'not_empty', 'hex-color')), (new Option('v', 'verbose')) ->setDescription('Display extra information') ->setDefaultValue(false), (new Option('q', 'quiet')) ->setDescription('Supress all messages') ->setDefaultValue(false), (new Option(null, 'help')) ->setDescription('Help Information, including encodings and formats'), (new Option(null, 'version')) ->setDescription('Display version information and exits'), (new Option(null, 'create-bash')) ->setDescription('Creates a bash script named barcode that can call this script') )); $getopt->setBanner("Usage: barcode -e -f [options] \n"); try { $getopt->parse(); if ($getopt['version']) { echo "PHP-CLI Barcode v"._BC_VERSION."\n"; exit(0); } if ($getopt['help']) { print_help($getopt); exit(0); } if ($getopt['create-bash']) { create_bash_script(); exit(1); } $verbose = $getopt['verbose']; $quiet = $getopt['quiet']; $encoding = $getopt['encoding']; $format = $getopt['format']; $width = $getopt['width']; $height = $getopt['height']; $color = $getopt['color']; $bc_string = $getopt->getOperand(0); $bc_file = $getopt->getOperand(1); } catch (UnexpectedValueException $e) { echo "Error: ".$e->getMessage()."\n"; echo $getopt->getHelpText(_BC_PADDING); exit(1); } // check if we can proceed if (empty($encoding) || empty($format) || empty($bc_string) || empty($bc_file)) { echo "Error: Invalid parameters or options.\n"; echo $getopt->getHelpText(_BC_PADDING); exit(1); } // Match case $encoding = strtoupper($encoding); $format = strtoupper($format); /////////////////// GETOPT ENDS /////////////////// CREATE BARCODE // creates a bash script named barcode that will run this script // from anywhere on the system. Assumes barcode.php is running // on its final installation location function create_bash_script() { $error = true; $bc_path = __FILE__; $bash_path = dirname($bc_path) . DIRECTORY_SEPARATOR . "barcode"; $bash_script = <<getBarcode($bc_string, $bc_type, $width, $height, $color); // save to file if (file_put_contents($bc_file, $bc_data) === false) { echo "Error: could not save file $bc_file!\n"; exit(1); } // set permissions and group chmod($bc_file, _BC_PERMISSION); #chgrp($bc_file, _BC_SYSGROUP); // prints help information function print_help($getopt) { global $encodings_list; global $formats_list; echo $getopt->getHelpText(_BC_PADDING); echo "\nRequired Options and Parameters:\n"; echo " -e \n"; echo " -f \n"; echo " \n"; echo " \n"; echo "\nOutput Formats:\n"; foreach($formats_list as $for) { echo " $for\n"; } echo "\nEncodings:\n"; foreach($encodings_list as $enc) { echo " $enc\n"; } echo "\nExamples:\n"; echo " barcode -f HTML -e CODE_39 \"1234567890\" \"/tmp/1234567890.html\"\n"; echo " barcode -e CODE_128 -f PNG -c \"#888\" -w 3 -h 50 \"AGREATBAR\" \"/tmp/AGREATBAR.png\"\n"; echo " barcode \"1234567890\" \"/tmp/mybar.svg\" --encoding EAN_13 --format SVG\n"; } // check if empty (callback) function not_empty($str) { return (!empty($str)); } // done exit(0); ?>