PHP生成条形码

首先找到强大的开源资料,在barcode官网下载barcodegen.1d-php5.v5.0.1.zip版本,然后解压文件放到你的Apache服务器的根目录下。

 

 1 $text = $_GET['code'];
 2         
 3         require_once('./barcodegen/BCGFontFile.php');
 4         require_once('./barcodegen/BCGColor.php');
 5         require_once('./barcodegen/BCGDrawing.php');
 6         require_once('./barcodegen/BCGcode128.barcode.php');
 7         $font = new BCGFontFile('./barcodegen/font/Arial.ttf', 18);
 8         
 9         $color_black = new BCGColor(0, 0, 0);
10         $color_white = new BCGColor(255, 255, 255);
11         
12         $drawException = null;
13         try {
14             $code = new BCGcode128();
15             $code->setScale(2); // Resolution
16             $code->setThickness(30); // Thickness
17             $code->setForegroundColor($color_black); // Color of bars
18             $code->setBackgroundColor($color_white); // Color of spaces
19             $code->setFont($font); // Font (or 0)
20             $code->parse($text); // Text
21         } catch(Exception $exception) {
22             $drawException = $exception;
23         }
24         
25         
26         $drawing = new BCGDrawing('', $color_white);
27         if($drawException) {
28             $drawing->drawException($drawException);
29         } else {
30             $drawing->setBarcode($code);
31             $drawing->draw();
32         }
33         header('Content-Type: image/png');
34         header('Content-Disposition: inline; filename="barcode.png"');
35         $drawing->finish(BCGDrawing::IMG_FORMAT_PNG);

 

参考 http://www.cnblogs.com/ForEvErNoME/archive/2012/04/21/2460944.html

posted @ 2016-04-15 16:25  贱书生  阅读(759)  评论(0编辑  收藏  举报