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