/[cvs]/nfo/php/libs/de.ashberg.www/barcode/php-barcode.php
ViewVC logotype

Contents of /nfo/php/libs/de.ashberg.www/barcode/php-barcode.php

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.1 - (show annotations)
Thu Aug 11 19:19:57 2005 UTC (18 years, 10 months ago) by jonen
Branch: MAIN
CVS Tags: HEAD
+ initial commit

1 <?
2 /*
3 * PHP-Barcode 0.3pl1
4
5 * PHP-Barcode generates
6 * - Barcode-Images using libgd2 (png, jpg, gif)
7 * - HTML-Images (using 1x1 pixel and html-table)
8 * - silly Text-Barcodes
9 *
10 * PHP-Barcode encodes using
11 * - a built-in EAN-13/ISBN Encoder
12 * - genbarcode (by Folke Ashberg), a command line
13 * barcode-encoder which uses GNU-Barcode
14 * genbarcode can encode EAN-13, EAN-8, UPC, ISBN, 39, 128(a,b,c),
15 * I25, 128RAW, CBR, MSI, PLS
16 * genbarcode is available at www.ashberg.de/bar
17
18 * (C) 2001,2002,2003,2004 by Folke Ashberg <folke@ashberg.de>
19
20 * The newest version can be found at http://www.ashberg.de/bar
21
22 * This program is free software; you can redistribute it and/or modify
23 * it under the terms of the GNU General Public License as published by
24 * the Free Software Foundation; either version 2 of the License, or
25 * (at your option) any later version.
26
27 * This program is distributed in the hope that it will be useful,
28 * but WITHOUT ANY WARRANTY; without even the implied warranty of
29 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30 * GNU General Public License for more details.
31
32 * You should have received a copy of the GNU General Public License
33 * along with this program; if not, write to the Free Software
34 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
35
36 */
37
38
39 /* CONFIGURATION */
40
41 /* ******************************************************************** */
42 /* COLORS */
43 /* ******************************************************************** */
44 $bar_color=Array(0,0,0);
45 $bg_color=Array(255,255,255);
46 $text_color=Array(0,0,0);
47
48
49 /* ******************************************************************** */
50 /* FONT FILE */
51 /* ******************************************************************** */
52 /* location the the ttf-font */
53 /* the file arialbd.ttf isn't included! */
54
55 /* SAMPLE1 :
56 * use arialbd.ttf located in same directory like the script
57 * which includes/requires php-barcode.php
58 */
59 $font_loc=dirname($_SERVER["PATH_TRANSLATED"])."/inc/fonts/"."arialbd.ttf";
60
61 /* SAMPLE2 :
62 * use font specified by full-path
63 */
64 //$font_loc="/path/font.ttf"
65
66 /* Automatic-Detection of Font if running Windows
67 * kick this lines if you don't need them! */
68 //if (isset($_ENV['windir']) && file_exists($_ENV['windir'])){
69 // $font_loc=$_ENV['windir']."\Fonts\arialbd.ttf";
70 //}
71
72 /* ******************************************************************** */
73 /* GENBARCODE */
74 /* ******************************************************************** */
75 /* location of 'genbarcode'
76 * leave blank if you don't have them :(
77 * genbarcode is needed to render encodings other than EAN-12/EAN-13/ISBN
78 */
79 //$genbarcode_loc="c:\winnt\genbarcode.exe";
80 $genbarcode_loc="/usr/local/bin/genbarcode";
81
82
83 /* CONFIGURATION ENDS HERE */
84
85 require("encode_bars.php"); /* build-in encoders */
86
87 /*
88 * barcode_outimage(text, bars [, scale [, mode [, total_y [, space ]]]] )
89 *
90 * Outputs an image using libgd
91 *
92 * text : the text-line (<position>:<font-size>:<character> ...)
93 * bars : where to place the bars (<space-width><bar-width><space-width><bar-width>...)
94 * scale : scale factor ( 1 < scale < unlimited (scale 50 will produce
95 * 5400x300 pixels when
96 * using EAN-13!!!))
97 * mode : png,gif,jpg, depending on libgd ! (default='png')
98 * total_y: the total height of the image ( default: scale * 60 )
99 * space : space
100 * default:
101 * $space[top] = 2 * $scale;
102 * $space[bottom]= 2 * $scale;
103 * $space[left] = 2 * $scale;
104 * $space[right] = 2 * $scale;
105 */
106
107
108 function barcode_outimage($text, $bars, $scale = 1, $mode = "png",
109 $total_y = 0, $space = '', $img_file = ''){
110 global $bar_color, $bg_color, $text_color;
111 global $font_loc;
112 /* set defaults */
113 if ($scale<1) $scale=2;
114 $total_y=(int)($total_y);
115 if ($total_y<1) $total_y=(int)$scale * 60;
116 if (!$space)
117 $space=array('top'=>2*$scale,'bottom'=>2*$scale,'left'=>2*$scale,'right'=>2*$scale);
118
119 /* count total width */
120 $xpos=0;
121 $width=true;
122 for ($i=0;$i<strlen($bars);$i++){
123 $val=strtolower($bars[$i]);
124 if ($width){
125 $xpos+=$val*$scale;
126 $width=false;
127 continue;
128 }
129 if (ereg("[a-z]", $val)){
130 /* tall bar */
131 $val=ord($val)-ord('a')+1;
132 }
133 $xpos+=$val*$scale;
134 $width=true;
135 }
136
137 /* allocate the image */
138 $total_x=( $xpos )+$space['right']+$space['right'];
139 $xpos=$space['left'];
140 if (!function_exists("imagecreate")){
141 print "You don't have the gd2 extension enabled<BR>\n";
142 print "<BR>\n";
143 print "<BR>\n";
144 print "Short HOWTO<BR>\n";
145 print "<BR>\n";
146 print "Debian: # apt-get install php4-gd2<BR>\n";
147 print "<BR>\n";
148 print "SuSE: ask YaST<BR>\n";
149 print "<BR>\n";
150 print "OpenBSD: # pkg_add /path/php4-gd-4.X.X.tgz (read output, you have to enable it)<BR>\n";
151 print "<BR>\n";
152 print "Windows: Download the PHP zip package from <A href=\"http://www.php.net/downloads.php\">php.net</A>, NOT the windows-installer, unzip the php_gd2.dll to C:\PHP (this is the default install dir) and uncomment 'extension=php_gd2.dll' in C:\WINNT\php.ini (or where ever your os is installed)<BR>\n";
153 print "<BR>\n";
154 print "<BR>\n";
155 print "The author of php-barcode will give not support on this topic!<BR>\n";
156 print "<BR>\n";
157 print "<BR>\n";
158 print "<A HREF=\"http://www.ashberg.de/bar/\">Folke Ashberg's OpenSource PHP-Barcode</A><BR>\n";
159 return "";
160 }
161 $im=imagecreate($total_x, $total_y);
162 /* create two images */
163 $col_bg=ImageColorAllocate($im,$bg_color[0],$bg_color[1],$bg_color[2]);
164 $col_bar=ImageColorAllocate($im,$bar_color[0],$bar_color[1],$bar_color[2]);
165 $col_text=ImageColorAllocate($im,$text_color[0],$text_color[1],$text_color[2]);
166 $height=round($total_y-($scale*10));
167 $height2=round($total_y-$space['bottom']);
168
169
170 /* paint the bars */
171 $width=true;
172 for ($i=0;$i<strlen($bars);$i++){
173 $val=strtolower($bars[$i]);
174 if ($width){
175 $xpos+=$val*$scale;
176 $width=false;
177 continue;
178 }
179 if (ereg("[a-z]", $val)){
180 /* tall bar */
181 $val=ord($val)-ord('a')+1;
182 $h=$height2;
183 } else $h=$height;
184 imagefilledrectangle($im, $xpos, $space['top'], $xpos+($val*$scale)-1, $h, $col_bar);
185 $xpos+=$val*$scale;
186 $width=true;
187 }
188 /* write out the text */
189 global $_SERVER;
190 $chars=explode(" ", $text);
191 reset($chars);
192 while (list($n, $v)=each($chars)){
193 if (trim($v)){
194 $inf=explode(":", $v);
195 $fontsize=$scale*($inf[1]/1.8);
196 $fontheight=$total_y-($fontsize/2.7)+2;
197 @imagettftext($im, $fontsize, 0, $space['left']+($scale*$inf[0])+2,
198 $fontheight, $col_text, $font_loc, $inf[2]);
199 }
200 }
201
202 /* output the image */
203 $mode=strtolower($mode);
204 if ($mode=='jpg' || $mode=='jpeg'){
205 if($img_file) {
206 imagejpeg($im, $img_file);
207 } else {
208 header("Content-Type: image/jpeg; name=\"barcode.jpg\"");
209 imagejpeg($im);
210 }
211 } else if ($mode=='gif'){
212 if($img_file) {
213 imagegif($im, $img_file);
214 } else {
215 header("Content-Type: image/gif; name=\"barcode.gif\"");
216 imagegif($im);
217 }
218 } else {
219 if($img_file) {
220 imagepng($im, $img_file);
221 } else {
222 header("Content-Type: image/png; name=\"barcode.png\"");
223 imagepng($im);
224 }
225 }
226
227 }
228
229 /*
230 * barcode_outtext(code, bars)
231 *
232 * Returns (!) a barcode as plain-text
233 * ATTENTION: this is very silly!
234 *
235 * text : the text-line (<position>:<font-size>:<character> ...)
236 * bars : where to place the bars (<space-width><bar-width><space-width><bar-width>...)
237 */
238
239 function barcode_outtext($code,$bars){
240 $width=true;
241 $xpos=$heigh2=0;
242 $bar_line="";
243 for ($i=0;$i<strlen($bars);$i++){
244 $val=strtolower($bars[$i]);
245 if ($width){
246 $xpos+=$val;
247 $width=false;
248 for ($a=0;$a<$val;$a++) $bar_line.="-";
249 continue;
250 }
251 if (ereg("[a-z]", $val)){
252 $val=ord($val)-ord('a')+1;
253 $h=$heigh2;
254 for ($a=0;$a<$val;$a++) $bar_line.="I";
255 } else for ($a=0;$a<$val;$a++) $bar_line.="#";
256 $xpos+=$val;
257 $width=true;
258 }
259 return $bar_line;
260 }
261
262 /*
263 * barcode_outhtml(text, bars [, scale [, total_y [, space ]]] )
264 *
265 * returns(!) HTML-Code for barcode-image using html-code (using a table and with black.png and white.png)
266 *
267 * text : the text-line (<position>:<font-size>:<character> ...)
268 * bars : where to place the bars (<space-width><bar-width><space-width><bar-width>...)
269 * scale : scale factor ( 1 < scale < unlimited (scale 50 will produce
270 * 5400x300 pixels when
271 * using EAN-13!!!))
272 * total_y: the total height of the image ( default: scale * 60 )
273 * space : space
274 * default:
275 * $space[top] = 2 * $scale;
276 * $space[bottom]= 2 * $scale;
277 * $space[left] = 2 * $scale;
278 * $space[right] = 2 * $scale;
279 */
280
281
282
283 function barcode_outhtml($code, $bars, $scale = 1, $total_y = 0, $space = ''){
284 /* set defaults */
285 $total_y=(int)($total_y);
286 if ($scale<1) $scale=2;
287 if ($total_y<1) $total_y=(int)$scale * 60;
288 if (!$space)
289 $space=array('top'=>2*$scale,'bottom'=>2*$scale,'left'=>2*$scale,'right'=>2*$scale);
290
291
292 /* generate html-code */
293 $height=round($total_y-($scale*10));
294 $height2=round($total_y)-$space['bottom'];
295 $out=
296 '<Table border=0 cellspacing=0 cellpadding=0 bgcolor="white">'."\n".
297 '<TR><TD><img src=white.png height="'.$space['top'].'" width=1></TD></TR>'."\n".
298 '<TR><TD>'."\n".
299 '<IMG src=white.png height="'.$height2.'" width="'.$space['left'].'">';
300
301 $width=true;
302 for ($i=0;$i<strlen($bars);$i++){
303 $val=strtolower($bars[$i]);
304 if ($width){
305 $w=$val*$scale;
306 if ($w>0) $out.="<IMG src=white.png height=\"$total_y\" width=\"$w\" align=top>";
307 $width=false;
308 continue;
309 }
310 if (ereg("[a-z]", $val)){
311 //hoher strich
312 $val=ord($val)-ord('a')+1;
313 $h=$height2;
314 }else $h=$height;
315 $w=$val*$scale;
316 if ($w>0) $out.='<IMG src="black.png" height="'.$h.'" width="'.$w.'" align=top>';
317 $width=true;
318 }
319 $out.=
320 '<IMG src=white.png height="'.$height2.'" width=".'.$space['right'].'">'.
321 '</TD></TR>'."\n".
322 '<TR><TD><img src="white.png" height="'.$space['bottom'].'" width="1"></TD></TR>'."\n".
323 '</TABLE>'."\n";
324 //for ($i=0;$i<strlen($bars);$i+=2) print $line[$i]."<B>".$line[$i+1]."</B>&nbsp;";
325 return $out;
326 }
327
328
329 /* barcode_encode_genbarcode(code, encoding)
330 * encodes $code with $encoding using genbarcode
331 *
332 * return:
333 * array[encoding] : the encoding which has been used
334 * array[bars] : the bars
335 * array[text] : text-positioning info
336 */
337 function barcode_encode_genbarcode($code,$encoding){
338 global $genbarcode_loc;
339 /* delete EAN-13 checksum */
340 if (eregi("^ean$", $encoding) && strlen($code)==13) $code=substr($code,0,12);
341 if (!$encoding) $encoding="ANY";
342 $encoding=ereg_replace("[|\\]", "_", $encoding);
343 $code=ereg_replace("[|\\]", "_", $code);
344 $cmd=$genbarcode_loc." \""
345 .str_replace("\"", "\\\"",$code)."\" \""
346 .str_replace("\"", "\\\"",strtoupper($encoding))."\"";
347 //print "'$cmd'<BR>\n";
348 $fp=popen($cmd, "r");
349 if ($fp){
350 $bars=fgets($fp, 1024);
351 $text=fgets($fp, 1024);
352 $encoding=fgets($fp, 1024);
353 pclose($fp);
354 } else return false;
355 $ret=array(
356 "encoding" => trim($encoding),
357 "bars" => trim($bars),
358 "text" => trim($text)
359 );
360 if (!$ret['encoding']) return false;
361 if (!$ret['bars']) return false;
362 if (!$ret['text']) return false;
363 return $ret;
364 }
365
366 /* barcode_encode(code, encoding)
367 * encodes $code with $encoding using genbarcode OR built-in encoder
368 * if you don't have genbarcode only EAN-13/ISBN is possible
369 *
370 * You can use the following encodings (when you have genbarcode):
371 * ANY choose best-fit (default)
372 * EAN 8 or 13 EAN-Code
373 * UPC 12-digit EAN
374 * ISBN isbn numbers (still EAN-13)
375 * 39 code 39
376 * 128 code 128 (a,b,c: autoselection)
377 * 128C code 128 (compact form for digits)
378 * 128B code 128, full printable ascii
379 * I25 interleaved 2 of 5 (only digits)
380 * 128RAW Raw code 128 (by Leonid A. Broukhis)
381 * CBR Codabar (by Leonid A. Broukhis)
382 * MSI MSI (by Leonid A. Broukhis)
383 * PLS Plessey (by Leonid A. Broukhis)
384 *
385 * return:
386 * array[encoding] : the encoding which has been used
387 * array[bars] : the bars
388 * array[text] : text-positioning info
389 */
390 function barcode_encode($code,$encoding){
391 global $genbarcode_loc;
392 if (
393 ((eregi("^ean$", $encoding)
394 && ( strlen($code)==12 || strlen($code)==13)))
395
396 || (($encoding) && (eregi("^isbn$", $encoding))
397 && (( strlen($code)==9 || strlen($code)==10) ||
398 (((ereg("^978", $code) && strlen($code)==12) ||
399 (strlen($code)==13)))))
400
401 || (( !isset($encoding) || !$encoding || (eregi("^ANY$", $encoding) ))
402 && (ereg("^[0-9]{12,13}$", $code)))
403
404 ){
405 /* use built-in EAN-Encoder */
406 $bars=barcode_encode_ean($code, $encoding);
407 } else if (file_exists($genbarcode_loc)){
408 /* use genbarcode */
409 $bars=barcode_encode_genbarcode($code, $encoding);
410 } else {
411 print "php-barcode needs an external programm for encodings other then EAN/ISBN<BR>\n";
412 print "<UL>\n";
413 print "<LI>download gnu-barcode from <A href=\"http://www.gnu.org/software/barcode/\">www.gnu.org/software/barcode/</A>\n";
414 print "<LI>compile and install them\n";
415 print "<LI>download genbarcode from <A href=\"http://www.ashberg.de/bar/\">www.ashberg.de/bar/</A>\n";
416 print "<LI>compile and install them\n";
417 print "<LI>specify path the genbarcode in php-barcode.php\n";
418 print "</UL>\n";
419 print "<BR>\n";
420 print "<A HREF=\"http://www.ashberg.de/bar/\">Folke Ashberg's OpenSource PHP-Barcode</A><BR>\n";
421 return false;
422 }
423 return $bars;
424 }
425
426 /* barcode_print(code [, encoding [, scale [, mode ]]] );
427 *
428 * encodes and prints a barcode
429 *
430 * return:
431 * array[encoding] : the encoding which has been used
432 * array[bars] : the bars
433 * array[text] : text-positioning info
434 */
435
436
437 function barcode_print($code, $encoding="ANY", $scale = 2 ,$mode = "png" ){
438 $bars=barcode_encode($code,$encoding);
439 if (!$bars) return;
440 if (!$mode) $mode="png";
441 if (eregi($mode,"^(text|txt|plain)$")) print barcode_outtext($bars['text'],$bars['bars']);
442 elseif (eregi($mode,"^(html|htm)$")) print barcode_outhtml($bars['text'],$bars['bars'], $scale,0, 0);
443 else barcode_outimage($bars['text'],$bars['bars'],$scale, $mode);
444 return $bars;
445 }
446
447 ?>

MailToCvsAdmin">MailToCvsAdmin
ViewVC Help
Powered by ViewVC 1.1.26 RSS 2.0 feed