Subversion Repositories Web Services

Rev

Rev 56957 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
56957 fvs 1
<?
2
/*
56961 fvs 3
 * PHP-Barcode 0.4
56957 fvs 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
56961 fvs 16
 *     genbarcode is available at www.ashberg.de/php-barcode
56957 fvs 17
 
56961 fvs 18
 * (C) 2001,2002,2003,2004,2011 by Folke Ashberg <folke@ashberg.de>
56957 fvs 19
 
56961 fvs 20
 * The newest version can be found at http://www.ashberg.de/php-barcode
56957 fvs 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
 
56961 fvs 54
$font_loc=dirname(__FILE__)."/"."FreeSansBold.ttf";
56957 fvs 55
 
56
/* ******************************************************************** */
57
/*                          GENBARCODE                                  */
58
/* ******************************************************************** */
59
/* location of 'genbarcode'
60
 * leave blank if you don't have them :(
61
 * genbarcode is needed to render encodings other than EAN-12/EAN-13/ISBN
62
 */
63
//$genbarcode_loc="c:\winnt\genbarcode.exe";
56961 fvs 64
$genbarcode_loc="/usr/local/bin/genbarcode";
56957 fvs 65
 
66
 
67
/* CONFIGURATION ENDS HERE */
68
 
69
require("encode_bars.php"); /* build-in encoders */
70
 
71
/*
72
 * barcode_outimage(text, bars [, scale [, mode [, total_y [, space ]]]] )
73
 *
74
 *  Outputs an image using libgd
75
 *
76
 *    text   : the text-line (<position>:<font-size>:<character> ...)
77
 *    bars   : where to place the bars  (<space-width><bar-width><space-width><bar-width>...)
78
 *    scale  : scale factor ( 1 < scale < unlimited (scale 50 will produce
79
 *                                                   5400x300 pixels when
80
 *                                                   using EAN-13!!!))
81
 *    mode   : png,gif,jpg, depending on libgd ! (default='png')
82
 *    total_y: the total height of the image ( default: scale * 60 )
83
 *    space  : space
84
 *             default:
85
 *              $space[top]   = 2 * $scale;
86
 *              $space[bottom]= 2 * $scale;
87
 *              $space[left]  = 2 * $scale;
88
 *              $space[right] = 2 * $scale;
89
 */
90
 
91
 
92
function barcode_outimage($text, $bars, $scale = 1, $mode = "png",
93
            $total_y = 0, $space = ''){
94
    global $bar_color, $bg_color, $text_color;
95
    global $font_loc;
96
    /* set defaults */
56961 fvs 97
    if ($scale<1) $scale=2;
56957 fvs 98
    $total_y=(int)($total_y);
99
    if ($total_y<1) $total_y=(int)$scale * 60;
100
    if (!$space)
101
      $space=array('top'=>2*$scale,'bottom'=>2*$scale,'left'=>2*$scale,'right'=>2*$scale);
102
 
103
    /* count total width */
104
    $xpos=0;
105
    $width=true;
106
    for ($i=0;$i<strlen($bars);$i++){
107
        $val=strtolower($bars[$i]);
108
        if ($width){
109
            $xpos+=$val*$scale;
110
            $width=false;
111
            continue;
112
        }
56961 fvs 113
        if (preg_match("#[a-z]#", $val)){
56957 fvs 114
            /* tall bar */
115
            $val=ord($val)-ord('a')+1;
116
        }
117
        $xpos+=$val*$scale;
118
        $width=true;
119
    }
120
 
121
    /* allocate the image */
122
    $total_x=( $xpos )+$space['right']+$space['right'];
123
    $xpos=$space['left'];
124
    if (!function_exists("imagecreate")){
56961 fvs 125
        print "You don't have the gd2 extension enabled<BR>\n";
126
        print "<BR>\n";
127
        print "<BR>\n";
128
        print "Short HOWTO<BR>\n";
129
        print "<BR>\n";
130
        print "Debian: # apt-get install php4-gd2<BR>\n";
131
        print "<BR>\n";
132
        print "SuSE: ask YaST<BR>\n";
133
        print "<BR>\n";
134
        print "OpenBSD: # pkg_add /path/php4-gd-4.X.X.tgz (read output, you have to enable it)<BR>\n";
135
        print "<BR>\n";
136
        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";
137
        print "<BR>\n";
138
        print "<BR>\n";
139
        print "The author of php-barcode will give not support on this topic!<BR>\n";
140
        print "<BR>\n";
141
        print "<BR>\n";
142
        print "<A HREF=\"http://www.ashberg.de/php-barcode/\">Folke Ashberg's OpenSource PHP-Barcode</A><BR>\n";
143
        return "";
56957 fvs 144
    }
145
    $im=imagecreate($total_x, $total_y);
146
    /* create two images */
147
    $col_bg=ImageColorAllocate($im,$bg_color[0],$bg_color[1],$bg_color[2]);
148
    $col_bar=ImageColorAllocate($im,$bar_color[0],$bar_color[1],$bar_color[2]);
149
    $col_text=ImageColorAllocate($im,$text_color[0],$text_color[1],$text_color[2]);
150
    $height=round($total_y-($scale*10));
151
    $height2=round($total_y-$space['bottom']);
152
 
153
 
154
    /* paint the bars */
155
    $width=true;
156
    for ($i=0;$i<strlen($bars);$i++){
157
        $val=strtolower($bars[$i]);
158
        if ($width){
159
            $xpos+=$val*$scale;
160
            $width=false;
161
            continue;
162
        }
56961 fvs 163
        if (preg_match("#[a-z]#", $val)){
56957 fvs 164
            /* tall bar */
165
            $val=ord($val)-ord('a')+1;
166
            $h=$height2;
167
        } else $h=$height;
168
        imagefilledrectangle($im, $xpos, $space['top'], $xpos+($val*$scale)-1, $h, $col_bar);
169
        $xpos+=$val*$scale;
170
        $width=true;
171
    }
172
    /* write out the text */
173
    global $_SERVER;
174
    $chars=explode(" ", $text);
175
    reset($chars);
176
    while (list($n, $v)=each($chars)){
177
        if (trim($v)){
178
            $inf=explode(":", $v);
179
            $fontsize=$scale*($inf[1]/1.8);
180
            $fontheight=$total_y-($fontsize/2.7)+2;
56961 fvs 181
            @imagettftext($im, $fontsize, 0, $space['left']+($scale*$inf[0])+2,
56957 fvs 182
            $fontheight, $col_text, $font_loc, $inf[2]);
183
        }
184
    }
185
 
186
    /* output the image */
187
    $mode=strtolower($mode);
188
    if ($mode=='jpg' || $mode=='jpeg'){
189
        header("Content-Type: image/jpeg; name=\"barcode.jpg\"");
190
        imagejpeg($im);
191
    } else if ($mode=='gif'){
192
        header("Content-Type: image/gif; name=\"barcode.gif\"");
193
        imagegif($im);
194
    } else {
195
        header("Content-Type: image/png; name=\"barcode.png\"");
196
        imagepng($im);
197
    }
198
 
199
}
200
 
201
/*
202
 * barcode_outtext(code, bars)
203
 *
204
 *  Returns (!) a barcode as plain-text
205
 *  ATTENTION: this is very silly!
206
 *
207
 *    text   : the text-line (<position>:<font-size>:<character> ...)
208
 *    bars   : where to place the bars  (<space-width><bar-width><space-width><bar-width>...)
209
 */
210
 
211
function barcode_outtext($code,$bars){
212
    $width=true;
213
    $xpos=$heigh2=0;
214
    $bar_line="";
215
    for ($i=0;$i<strlen($bars);$i++){
216
        $val=strtolower($bars[$i]);
217
        if ($width){
218
            $xpos+=$val;
219
            $width=false;
220
            for ($a=0;$a<$val;$a++) $bar_line.="-";
221
            continue;
222
        }
56961 fvs 223
        if (preg_match("#[a-z]#", $val)){
56957 fvs 224
            $val=ord($val)-ord('a')+1;
225
            $h=$heigh2;
226
            for ($a=0;$a<$val;$a++) $bar_line.="I";
227
        } else for ($a=0;$a<$val;$a++) $bar_line.="#";
228
        $xpos+=$val;
229
        $width=true;
230
    }
231
    return $bar_line;
232
}
233
 
234
/*
235
 * barcode_outhtml(text, bars [, scale [, total_y [, space ]]] )
236
 *
237
 *  returns(!) HTML-Code for barcode-image using html-code (using a table and with black.png and white.png)
238
 *
239
 *    text   : the text-line (<position>:<font-size>:<character> ...)
240
 *    bars   : where to place the bars  (<space-width><bar-width><space-width><bar-width>...)
241
 *    scale  : scale factor ( 1 < scale < unlimited (scale 50 will produce
242
 *                                                   5400x300 pixels when
243
 *                                                   using EAN-13!!!))
244
 *    total_y: the total height of the image ( default: scale * 60 )
245
 *    space  : space
246
 *             default:
247
 *              $space[top]   = 2 * $scale;
248
 *              $space[bottom]= 2 * $scale;
249
 *              $space[left]  = 2 * $scale;
250
 *              $space[right] = 2 * $scale;
251
 */
252
 
253
 
254
 
255
function barcode_outhtml($code, $bars, $scale = 1, $total_y = 0, $space = ''){
256
    /* set defaults */
257
    $total_y=(int)($total_y);
258
    if ($scale<1) $scale=2;
259
    if ($total_y<1) $total_y=(int)$scale * 60;
260
    if (!$space)
261
      $space=array('top'=>2*$scale,'bottom'=>2*$scale,'left'=>2*$scale,'right'=>2*$scale);
262
 
263
 
264
    /* generate html-code */
265
    $height=round($total_y-($scale*10));
266
    $height2=round($total_y)-$space['bottom'];
267
    $out=
56961 fvs 268
      '<table border=0 cellspacing=0 cellpadding=0 bgcolor="white">'."\n".
269
      '<tr><td><img src="white.png" height="'.$space['top'].'" width="1" alt=" "></td></tr>'."\n".
270
      '<tr><td>'."\n".
271
      '<img src="white.png" height="'.$height2.'" width="'.$space['left'].'" alt="#"/>';
56957 fvs 272
 
273
    $width=true;
274
    for ($i=0;$i<strlen($bars);$i++){
275
        $val=strtolower($bars[$i]);
276
        if ($width){
277
            $w=$val*$scale;
56961 fvs 278
            if ($w>0) $out.='<img src="white.png" height="'.$total_y.'" width="'.$w.'" align="top" alt="" />';
56957 fvs 279
            $width=false;
280
            continue;
281
        }
56961 fvs 282
        if (preg_match("#[a-z]#", $val)){
56957 fvs 283
            //hoher strich
284
            $val=ord($val)-ord('a')+1;
285
            $h=$height2;
286
        }else $h=$height;
287
        $w=$val*$scale;
56961 fvs 288
        if ($w>0) $out.='<img src="black.png" height="'.$h.'" width="'.$w.'" align="top" />';
56957 fvs 289
        $width=true;
290
    }
291
    $out.=
56961 fvs 292
      '<img src="white.png" height="'.$height2.'" width=".'.$space['right'].'" />'.
293
      '</td></tr>'."\n".
294
      '<tr><td><img src="white.png" height="'.$space['bottom'].'" width="1"></td></tr>'."\n".
295
      '</table>'."\n";
56957 fvs 296
    //for ($i=0;$i<strlen($bars);$i+=2) print $line[$i]."<B>".$line[$i+1]."</B>&nbsp;";
297
    return $out;
298
}
299
 
300
 
56961 fvs 301
/* barcode_encode_genbarcode(code, encoding)
56957 fvs 302
 *   encodes $code with $encoding using genbarcode
303
 *
304
 *   return:
305
 *    array[encoding] : the encoding which has been used
306
 *    array[bars]     : the bars
307
 *    array[text]     : text-positioning info
308
 */
56961 fvs 309
function barcode_encode_genbarcode($code,$encoding){
56957 fvs 310
    global $genbarcode_loc;
311
    /* delete EAN-13 checksum */
56961 fvs 312
    if (preg_match("#^ean$#i", $encoding) && strlen($code)==13) $code=substr($code,0,12);
56957 fvs 313
    if (!$encoding) $encoding="ANY";
56961 fvs 314
    $encoding=preg_replace("#[|\\\\]#", "_", $encoding);
315
    $code=preg_replace("#[|\\\\]#", "_", $code);
316
    $cmd=$genbarcode_loc." "
317
        .escapeshellarg($code)." "
318
        .escapeshellarg(strtoupper($encoding))."";
319
    //print "'$cmd'<BR>\n";
56957 fvs 320
    $fp=popen($cmd, "r");
321
    if ($fp){
322
        $bars=fgets($fp, 1024);
323
        $text=fgets($fp, 1024);
324
        $encoding=fgets($fp, 1024);
325
        pclose($fp);
326
    } else return false;
327
    $ret=array(
328
                "encoding" => trim($encoding),
329
                "bars" => trim($bars),
330
                "text" => trim($text)
331
              );
332
    if (!$ret['encoding']) return false;
333
    if (!$ret['bars']) return false;
334
    if (!$ret['text']) return false;
335
    return $ret;
336
}
337
 
56961 fvs 338
/* barcode_encode(code, encoding)
56957 fvs 339
 *   encodes $code with $encoding using genbarcode OR built-in encoder
340
 *   if you don't have genbarcode only EAN-13/ISBN is possible
341
 *
342
 * You can use the following encodings (when you have genbarcode):
343
 *   ANY    choose best-fit (default)
344
 *   EAN    8 or 13 EAN-Code
345
 *   UPC    12-digit EAN
346
 *   ISBN   isbn numbers (still EAN-13)
347
 *   39     code 39
348
 *   128    code 128 (a,b,c: autoselection)
349
 *   128C   code 128 (compact form for digits)
350
 *   128B   code 128, full printable ascii
351
 *   I25    interleaved 2 of 5 (only digits)
352
 *   128RAW Raw code 128 (by Leonid A. Broukhis)
353
 *   CBR    Codabar (by Leonid A. Broukhis)
354
 *   MSI    MSI (by Leonid A. Broukhis)
355
 *   PLS    Plessey (by Leonid A. Broukhis)
356
 *
357
 *   return:
358
 *    array[encoding] : the encoding which has been used
359
 *    array[bars]     : the bars
360
 *    array[text]     : text-positioning info
361
 */
56961 fvs 362
function barcode_encode($code,$encoding){
56957 fvs 363
    global $genbarcode_loc;
364
    if (
56961 fvs 365
                ((preg_match("#^ean$#i", $encoding)
56957 fvs 366
                 && ( strlen($code)==12 || strlen($code)==13)))
367
 
56961 fvs 368
                || (($encoding) && (preg_match("#^isbn$#i", $encoding))
56957 fvs 369
                 && (( strlen($code)==9 || strlen($code)==10) ||
56961 fvs 370
                 (((preg_match("#^978#", $code) && strlen($code)==12) ||
56957 fvs 371
                  (strlen($code)==13)))))
372
 
56961 fvs 373
                || (( !isset($encoding) || !$encoding || (preg_match("#^ANY$#i", $encoding) ))
374
                 && (preg_match("#^[0-9]{12,13}$#", $code)))
56957 fvs 375
 
376
                ){
377
        /* use built-in EAN-Encoder */
378
        $bars=barcode_encode_ean($code, $encoding);
56961 fvs 379
    } else if (file_exists($genbarcode_loc)){
380
        /* use genbarcode */
381
        $bars=barcode_encode_genbarcode($code, $encoding);
56957 fvs 382
    } else {
56961 fvs 383
        print "php-barcode needs an external programm for encodings other then EAN/ISBN<BR>\n";
384
        print "<ul>\n";
385
        print "<li>download gnu-barcode from <a href=\"http://www.gnu.org/software/barcode/\">www.gnu.org/software/barcode/</a></li>\n";
386
        print "<li>compile and install them</li>\n";
387
        print "<li>download genbarcode from <a href=\"http://www.ashberg.de/php-barcode/\">www.ashberg.de/php-barcode/</a></li>\n";
388
        print "<li>compile and install them</li>\n";
389
        print "<li>specify path to genbarcode in php-barcode.php</li>\n";
390
        print "</ul>\n";
391
        print "<br />\n";
392
        print "<a href=\"http://www.ashberg.de/php-barcode/\">Folke Ashberg's OpenSource PHP-Barcode</a><br />\n";
393
        return false;
56957 fvs 394
    }
395
    return $bars;
396
}
397
 
398
/* barcode_print(code [, encoding [, scale [, mode ]]] );
399
 *
400
 *  encodes and prints a barcode
401
 *
402
 *   return:
403
 *    array[encoding] : the encoding which has been used
404
 *    array[bars]     : the bars
405
 *    array[text]     : text-positioning info
406
 */
407
 
408
 
56961 fvs 409
function barcode_print($code, $encoding="ANY", $scale = 2 ,$mode = "png" ){
410
    $bars=barcode_encode($code,$encoding);
56957 fvs 411
    if (!$bars) return;
412
    if (!$mode) $mode="png";
56961 fvs 413
    if (preg_match("#^(text|txt|plain)$#i", $mode)) print barcode_outtext($bars['text'],$bars['bars']);
414
    elseif (preg_match("#^(html|htm)$#i", $mode)) print barcode_outhtml($bars['text'],$bars['bars'], $scale,0, 0);
56957 fvs 415
    else barcode_outimage($bars['text'],$bars['bars'],$scale, $mode);
416
    return $bars;
417
}
418
 
419
?>