PHP头条
热点:

3.实际应用

对于上面有个大概的了解后,下面我们可以重新整合下代码,更加方便的使用它。

首先新建buildcode.php文件中,根据test.php文件进行改写,从请求的文件中获取数据:

1.条形码的编码格式

2.条形码需要的数据内容

  1. View Code    
  2. <?php  
  3. // Including all required classes  
  4. require_once('class/BCGFontFile.php');  
  5. require_once('class/BCGColor.php');  
  6. require_once('class/BCGDrawing.php');   
  7. $codebar = $_REQUEST['codebar']; //条形码将要数据的内容   
  8. // Including the barcode technology  
  9. require_once('class/'.$codebar.'.barcode.php');   
  10. // Loading Font  
  11. $font = new BCGFontFile('./class/font/Arial.ttf', 12);   
  12. // The arguments are R, G, B for color.  
  13. $color_black = new BCGColor(0, 0, 0);  
  14. $color_white = new BCGColor(255, 255, 255);   
  15. $drawException = null;  
  16. try {  
  17.     $code = new $codebar();//实例化对应的编码格式  
  18.     $code->setScale(2); // Resolution  
  19.     $code->setThickness(23); // Thickness  
  20.     $code->setForegroundColor($color_black); // Color of bars  
  21.     $code->setBackgroundColor($color_white); // Color of spaces  
  22.     $code->setFont($font); // Font (or 0)  
  23.     $text = $_REQUEST['text']; //条形码将要数据的内容  
  24.     $code->parse($text);  
  25. } catch(Exception $exception) {  
  26.     $drawException = $exception;  
  27. }   
  28. /* Here is the list of the arguments  
  29.  - Filename (empty : display on screen)  
  30.  - Background color */ 
  31. $drawing = new BCGDrawing(''$color_white);  
  32. if($drawException) {  
  33.     $drawing->drawException($drawException);  
  34. else {  
  35.     $drawing->setBarcode($code);  
  36.     $drawing->draw();  
  37. }   
  38. // Header that says it is an image (remove it if you save the barcode to a file)  
  39. header('Content-Type: image/png');   
  40. // Draw (or save) the image into PNG format.  
  41. $drawing->finish(BCGDrawing::IMG_FORMAT_PNG);  
  42. ?> 

然后新建test.html文件,向buildcode.php请求数据

  1. <!DOCTYPE html> 
  2. <html> 
  3. <head> 
  4. <title>Test with embedded image</title> 
  5. </head> 
  6. <body> 
  7.   <img src="buildcode.php?codebar=BCGcode39&text=abc123"/> 
  8. </body> 
  9. </html> 

最后访问http://localhost/barcodegen/test.html或访问http://localhost/barcodegen/buildcode.php?codebar=BCGcode39&text=abc123,浏览器直接生成png格式的条形码

其中codebar支持的编码格式可以由用户请求所得:

  1. /*'BCGcodabar','BCGcode11','BCGcode39','BCGcode39extended','BCGcode93',   
  2. 'BCGcode128','BCGean8','BCGean13','BCGisbn','BCGi25','BCGs25','BCGmsi',   
  3. 'BCGupca','BCGupce','BCGupcext2','BCGupcext5','BCGpostnet','BCGothercode'*/  

剩下的就是验证了


www.phpzy.comtrue/php/3412.htmlTechArticle3.实际应用 对于上面有个大概的了解后,下面我们可以重新整合下代码,更加方便的使用它。 首先新建buildcode.php文件中,根据test.php文件进行改写,从请...

相关文章

相关频道:

PHP之友评论

今天推荐