1414
1515require_once 'vendor/autoload.php ' ;
1616
17- define ("_BC_VERSION " , "1.0.5 " );
17+ define ("_BC_VERSION " , "1.0.6 " );
1818
1919# default padding for cli messages
20- define ("_BC_PADDING " , 30 );
20+ define ("_BC_PADDING " , 30 );
21+ define ("_BC_HELP_NEWLINE " , "\n " );
2122
2223$ verbose = false ;
2324$ quiet = false ;
7778);
7879sort ($ formats_list );
7980
81+ # Converts Hex Color to array(r, g, b, [a])
82+ function hexToRgb ($ hex ) {
83+ $ hex = str_replace ('# ' , '' , $ hex );
84+ $ length = strlen ($ hex );
85+ $ rgb [] = hexdec ($ length == 6 ? substr ($ hex , 0 , 2 ) : ($ length == 3 ? str_repeat (substr ($ hex , 0 , 1 ), 2 ) : 0 ));
86+ $ rgb [] = hexdec ($ length == 6 ? substr ($ hex , 2 , 2 ) : ($ length == 3 ? str_repeat (substr ($ hex , 1 , 1 ), 2 ) : 0 ));
87+ $ rgb [] = hexdec ($ length == 6 ? substr ($ hex , 4 , 2 ) : ($ length == 3 ? str_repeat (substr ($ hex , 2 , 1 ), 2 ) : 0 ));
88+ return $ rgb ;
89+ }
90+
8091
8192/////////////////// PRINT HELP
8293
94+ function getVersionString ($ suffix ='' ) {
95+ return "PHP-CLI Barcode v " ._BC_VERSION .$ suffix ;
96+ }
97+
98+ function printVersion ($ suffix ='' ) {
99+ echo getVersionString ($ suffix )."\n" ;
100+ }
101+
83102// prints help information
84103function print_help ($ getopt ) {
85104 global $ encodings_list ;
@@ -106,9 +125,9 @@ function print_help($getopt) {
106125}
107126
108127
109- /////////////////// CREATE BASH SCRIPT
128+ /////////////////// CREATE SH SCRIPT
110129
111- // creates a bash script named barcode that will run this script
130+ // creates a shell script named barcode that will run this script
112131// from anywhere on the system. Assumes barcode.php is running
113132// on its final installation location
114133function create_bash_script () {
@@ -117,7 +136,7 @@ function create_bash_script() {
117136 $ bash_path = dirname ($ bc_path ) . DIRECTORY_SEPARATOR . "barcode " ;
118137
119138 $ bash_script = <<<EOF
120- #!/usr/bin/env bash
139+ #!/usr/bin/env sh
121140
122141##################################################
123142# Gustavo Arnosti Neves - 2016 Jul 11
@@ -177,13 +196,26 @@ function not_empty($str) {
177196 return (!empty ($ str ));
178197}
179198
199+ // check if empty callback
200+ function isHexColor ($ str ) {
201+ $ str = str_replace ('# ' , '' , $ str );
202+ if (! ctype_alnum ($ str ) || (strlen ($ str ) !== 6 && strlen ($ str ) !== 3 )) return false ;
203+ return true ;
204+ }
205+
206+ function checkQuietExit ($ exitStat =0 ) {
207+ global $ quiet ;
208+ if ($ quiet ) ob_end_clean ();
209+ exit ($ exitStat );
210+ }
211+
180212// define and configure options
181213$ getopt = new Getopt (array (
182214 (new Option ('e ' , 'encoding ' , Getopt::REQUIRED_ARGUMENT ))
183- ->setDescription ('Barcode encoding type selection ' )
215+ ->setDescription ('Barcode encoding type selection, listed below ' )
184216 ->setArgument (new Argument (null , 'isEncoding ' , 'bar-type ' )),
185217 (new Option ('f ' , 'format ' , Getopt::REQUIRED_ARGUMENT ))
186- ->setDescription ('Output format for the barcode ' )
218+ ->setDescription ('Output format for the barcode, listed below ' )
187219 ->setArgument (new Argument (null , 'isFormat ' , 'file-type ' )),
188220 (new Option ('w ' , 'width ' , Getopt::REQUIRED_ARGUMENT ))
189221 ->setDescription ('Width factor for bars to make wider, defaults to 2 ' )
@@ -192,20 +224,20 @@ function not_empty($str) {
192224 ->setDescription ('Total height of the barcode, defaults to 30 ' )
193225 ->setArgument (new Argument (30 , 'is_numeric ' , 'points ' )),
194226 (new Option ('c ' , 'color ' , Getopt::REQUIRED_ARGUMENT ))
195- ->setDescription ('Hex code of the foreground color, defaults to black ' )
196- ->setArgument (new Argument ('#000000 ' , 'not_empty ' , 'hex-color ' )),
227+ ->setDescription ('Hex code of the foreground color, defaults to black ' . _BC_HELP_NEWLINE . " Eg. -c 54863b, or -c '#000' " )
228+ ->setArgument (new Argument ('#000000 ' , 'isHexColor ' , 'hex-color ' )),
197229 (new Option ('v ' , 'verbose ' ))
198- ->setDescription ('Display extra information' )
230+ ->setDescription ('Prints verbose information to screen ' . _BC_HELP_NEWLINE . " Use twice for timestamp " )
199231 ->setDefaultValue (false ),
200232 (new Option ('q ' , 'quiet ' ))
201- ->setDescription ('Supress all messages ' )
233+ ->setDescription ('Supress all messages, even errors ' )
202234 ->setDefaultValue (false ),
203235 (new Option (null , 'help ' ))
204236 ->setDescription ('Help Information, including encodings and formats ' ),
205237 (new Option (null , 'version ' ))
206238 ->setDescription ('Display version information and exits ' ),
207239 (new Option (null , 'create-bash ' ))
208- ->setDescription ('Creates a bash script named barcode that can call this script ' )
240+ ->setDescription ('Creates a shell script named \' barcode \' that can call this script ' )
209241));
210242
211243$ getopt ->setBanner ("Usage: barcode -e <encoding> -f <output_format> [options] <barcode string> <output file> \n" );
@@ -214,7 +246,7 @@ function not_empty($str) {
214246 $ getopt ->parse ();
215247
216248 if ($ getopt ['version ' ]) {
217- echo " PHP-CLI Barcode v " . _BC_VERSION . "\n" ;
249+ printVersion () ;
218250 exit (0 );
219251 }
220252
@@ -236,7 +268,7 @@ function not_empty($str) {
236268
237269 $ width = $ getopt ['width ' ];
238270 $ height = $ getopt ['height ' ];
239- $ color = $ getopt ['color ' ];
271+ $ color = ' # ' . str_replace ( ' # ' , '' , $ getopt ['color ' ]) ;
240272
241273 $ bc_string = $ getopt ->getOperand (0 );
242274 $ bc_file = $ getopt ->getOperand (1 );
@@ -254,14 +286,53 @@ function not_empty($str) {
254286 exit (1 );
255287}
256288
289+ // check output directory
290+ $ tgtDir = dirname ($ bc_file );
291+ if (! is_dir ($ tgtDir )) {
292+ echo "Error: Could not locate target directory! \nTarget Dir: $ tgtDir \n" ;
293+ exit (1 );
294+ }
295+
257296// Match case
258297$ encoding = strtoupper ($ encoding );
259298$ format = strtoupper ($ format );
260299
261300
301+
302+ /////////////////// QUIET EXECUTION STARTS
303+
304+ // From here on use checkQuietExit() to Exit
305+ if ($ quiet ) {
306+ ob_start ();
307+ }
308+
262309/////////////////// GETOPT ENDS
263310
264311
312+ /////////////////// PRINT VERBOSE INFO
313+
314+ vPrint (getVersionString (" - Verbose Execution " ));
315+ vPrint (array ("Output File " , "$ bc_file " ));
316+ vPrint (array ("Barcode String " , "$ bc_string " ));
317+ vPrint (array ("Barcode Encoding " , "$ encoding " ));
318+ vPrint (array ("Output Format " , "$ format " ));
319+ vPrint (array ("Width Factor " , "$ width " ));
320+ vPrint (array ("Height of Barcode " , "$ height " ));
321+ vPrint (array ("Hex Color " , "$ color " ));
322+
323+ function vPrint ($ input ) {
324+ global $ verbose ;
325+ if ($ verbose ) {
326+ if ($ verbose > 1 ) echo date (DATE_ATOM ) . " | " ;
327+ if (is_array ($ input ) && count ($ input ) > 1 ) {
328+ printf ("%-17s : %-30s \n" , $ input [0 ], $ input [1 ]);
329+ } else if (is_string ($ input )) {
330+ echo "$ input \n" ;
331+ }
332+ }
333+ }
334+
335+
265336/////////////////// CREATE BARCODE STARTS
266337
267338// get actual barcode type string
@@ -276,34 +347,30 @@ function not_empty($str) {
276347if ($ format === 'SVG ' ) {
277348 $ generator = new Picqer \Barcode \BarcodeGeneratorSVG ();
278349} else if ($ format === 'PNG ' ) {
350+ $ color = hexToRgb ($ color );
279351 $ generator = new Picqer \Barcode \BarcodeGeneratorPNG ();
280352} else if ($ format === 'JPG ' ) {
353+ $ color = hexToRgb ($ color );
281354 $ generator = new Picqer \Barcode \BarcodeGeneratorJPG ();
282355} else if ($ format === 'HTML ' ) {
283356 $ generator = new Picqer \Barcode \BarcodeGeneratorHTML ();
284357} else {
285- exit (1 );
358+ echo "Error: Invalid taget format: $ format \n" ;
359+ checkQuietExit (1 );
286360}
287361
288362// generate de barcode
289363try {
290364 $ bc_data = $ generator ->getBarcode ($ bc_string , $ bc_type , $ width , $ height , $ color );
291365} catch (Exception $ e ) {
292366 echo "Error: " .$ e ->getMessage ()."\n" ;
293- exit (1 );
294- }
295-
296- // sanity check
297- $ tgtDir = dirname ($ bc_file );
298- if (! is_dir ($ tgtDir )) {
299- echo "Error: Could not locate target directory! \nTarget Dir: $ tgtDir \n" ;
300- exit (1 );
367+ checkQuietExit (1 );
301368}
302369
303370// save to file
304371if (@file_put_contents ($ bc_file , $ bc_data ) === false ) {
305372 echo "Error: could not save file $ bc_file! \n" ;
306- exit (1 );
373+ checkQuietExit (1 );
307374}
308375
309376// set permissions and group
@@ -312,7 +379,10 @@ function not_empty($str) {
312379
313380/////////////////// CREATE BARCODE ENDS
314381
315- // done
316- exit (0 );
382+
383+ /////////////////// PRINT VERBOSE INFO ENDS
384+
385+ vprint (array ("Final Status " , "Success " ));
386+ checkQuietExit (0 );
317387
318388?>
0 commit comments