Home  >  Article  >  Backend Development  >  A simple example of generating barcodes with PHP5_PHP Tutorial

A simple example of generating barcodes with PHP5_PHP Tutorial

WBOY
WBOYOriginal
2016-07-15 13:26:18921browse

The software supports both PHP4 and PHP5 versions. The PHP5 version is used in this article. Be sure to turn on PHP's GD module before use. This is php_gd2.dll in Windows and gd.so in Linux. Extract the compressed file to Apache and execute http://localhost/barcode/index.php. PHP5 generates barcodes as shown below:

1. Type: Select the barcode type

2. Output: Output image format

3. Thickness: Barcode height

4. Resolution: barcode size

5. Font: text size below the barcode, or no text displayed

6. Text: barcode printed content

barcode

Of course, this PHP5 barcode generation program only generates text into barcodes, but it cannot be flexibly embedded in other PHP programs when used. I will compress the test in the package Some adjustments have been made to .php to make it flexible for use in other programs. When running, just pass the barcode type and text to test.php, for example:
http://localhost/barcode/test.php?codebar=BCGcode39&text=20090729

Or run http://localhost/barcode/mytest.php

mytest.php code:

<span style="COLOR: #0000ff"><pre class="brush:php;toolbar:false"><ol class="dp-xml"><li class="alt">
<span><strong><font color="#006699"><span class="tag"><</SPAN><SPAN class=tag-name>img</SPAN></FONT></STRONG><SPAN><FONT color=#000000> </FONT></SPAN><SPAN class=attribute><FONT color=#ff0000>src</FONT></SPAN><SPAN><FONT color=#000000>=</FONT></SPAN><SPAN class=attribute-value>"test.php?codebar=BCGcode39&text=20090729"</SPAN><SPAN class=tag><STRONG><FONT color=#006699>></span></font></strong></span><span><font color="#000000"> </font></span>
</li></ol>
<span style="COLOR: #0000ff"></span>PHP5生成条形码效果图:

Running

PHP5 generates barcode test.php code:

<span style="COLOR: #0000ff"><pre class="brush:php;toolbar:false"><ol class="dp-c">
<li class="alt"><span><span><font color="#000000"><?php  </FONT></SPAN></SPAN><LI><SPAN></SPAN><SPAN class=comment><FONT color=#008200>// Including all required classes </FONT></SPAN><SPAN><FONT color=#000000> </FONT></SPAN></SPAN><LI class=alt><SPAN></SPAN><SPAN class=keyword><STRONG><FONT color=#006699>require</FONT></STRONG></SPAN><SPAN><FONT color=#000000>(</FONT></SPAN><SPAN class=string>'class/BCGFont.php'</SPAN><SPAN><FONT color=#000000>);  </FONT></SPAN></SPAN><LI><SPAN></SPAN><SPAN class=keyword><STRONG><FONT color=#006699>require</FONT></STRONG></SPAN><SPAN><FONT color=#000000>(</FONT></SPAN><SPAN class=string>'class/BCGColor.php'</SPAN><SPAN><FONT color=#000000>);  </FONT></SPAN></SPAN><LI class=alt><SPAN></SPAN><SPAN class=keyword><STRONG><FONT color=#006699>require</FONT></STRONG></SPAN><SPAN><FONT color=#000000>(</FONT></SPAN><SPAN class=string>'class/BCGDrawing.php'</SPAN><SPAN><FONT color=#000000>);  </FONT></SPAN></SPAN><LI><SPAN></SPAN><SPAN class=comment><FONT color=#008200>/*'BCGcodabar','BCGcode11','BCGcode39','BCGcode39extended','BCGcode93', </FONT></SPAN> </SPAN><LI class=alt><SPAN><SPAN class=comment><FONT color=#008200>'BCGcode128','BCGean8','BCGean13','BCGisbn','BCGi25','BCGs25','BCGmsi', </FONT></SPAN><FONT color=#000000> </FONT></SPAN><LI><SPAN><SPAN class=comment><FONT color=#008200>'BCGupca','BCGupce','BCGupcext2','BCGupcext5','BCGpostnet','BCGothercode'*/</FONT></SPAN><SPAN><FONT color=#000000> </FONT></SPAN></SPAN><LI class=alt><SPAN></SPAN><SPAN class=vars><FONT color=#dd0000>$codebar</FONT></SPAN><SPAN><FONT color=#000000> = </FONT></SPAN><SPAN class=vars><FONT color=#dd0000>$_REQUEST</FONT></SPAN><SPAN><FONT color=#000000>[</FONT></SPAN><SPAN class=string>'codebar'</SPAN><SPAN><FONT color=#000000>]; </FONT></SPAN><SPAN class=comment><FONT color=#008200>//该软件支持的所有编码,只需调整$codebar参数即可。 </FONT></SPAN><SPAN><FONT color=#000000> </FONT></SPAN></SPAN><LI><SPAN></SPAN><SPAN class=comment><FONT color=#008200>// Including the barcode technology </FONT></SPAN><SPAN><FONT color=#000000> </FONT></SPAN></SPAN><LI class=alt><SPAN></SPAN><SPAN class=keyword><STRONG><FONT color=#006699>include</FONT></STRONG></SPAN><SPAN><FONT color=#000000>(</FONT></SPAN><SPAN class=string>'class/'</SPAN><SPAN><FONT color=#000000>.</FONT></SPAN><SPAN class=vars><FONT color=#dd0000>$codebar</FONT></SPAN><SPAN><FONT color=#000000>.</FONT></SPAN><SPAN class=string>'.barcode.php'</SPAN><SPAN><FONT color=#000000>);  </FONT></SPAN></SPAN><LI><SPAN></SPAN><SPAN class=comment><FONT color=#008200>// Loading Font </FONT></SPAN><SPAN><FONT color=#000000> </FONT></SPAN></SPAN><LI class=alt><SPAN></SPAN><SPAN class=vars><FONT color=#dd0000>$font</FONT></SPAN><SPAN><FONT color=#000000> = </FONT></SPAN><SPAN class=keyword><STRONG><FONT color=#006699>new</FONT></STRONG></SPAN><SPAN><FONT color=#000000> BCGFont(</FONT></SPAN><SPAN class=string>'./class/font/Arial.ttf'</SPAN><SPAN><FONT color=#000000>, 10);  </FONT></SPAN></SPAN><LI><SPAN></SPAN><SPAN class=comment><FONT color=#008200>// The arguments are R, G, B for color. </FONT></SPAN><SPAN><FONT color=#000000> </FONT></SPAN></SPAN><LI class=alt><SPAN></SPAN><SPAN class=vars><FONT color=#dd0000>$color_black</FONT></SPAN><SPAN><FONT color=#000000> = </FONT></SPAN><SPAN class=keyword><STRONG><FONT color=#006699>new</FONT></STRONG></SPAN><SPAN><FONT color=#000000> BCGColor(0, 0, 0);  </FONT></SPAN></SPAN><LI><SPAN></SPAN><SPAN class=vars><FONT color=#dd0000>$color_white</FONT></SPAN><SPAN><FONT color=#000000> = </FONT></SPAN><SPAN class=keyword><STRONG><FONT color=#006699>new</FONT></STRONG></SPAN><SPAN><FONT color=#000000> BCGColor(255, 255, 255);  </FONT></SPAN></SPAN><LI class=alt><SPAN></SPAN><SPAN class=vars><FONT color=#dd0000>$code</FONT></SPAN><SPAN><FONT color=#000000> = </FONT></SPAN><SPAN class=keyword><STRONG><FONT color=#006699>new</FONT></STRONG></SPAN><SPAN><FONT color=#000000> </FONT></SPAN><SPAN class=vars><FONT color=#dd0000>$codebar</FONT></SPAN><SPAN><FONT color=#000000>();  </FONT></SPAN></SPAN><LI><SPAN></SPAN><SPAN class=vars><FONT color=#dd0000>$code</FONT></SPAN><SPAN><FONT color=#000000>->setScale(2); </font></span><span class="comment"><font color="#008200">// Resolution </font></span><span><font color="#000000"> </font></span></span></li>
<li class="alt">
<span></span><span class="vars"><font color="#dd0000">$code</font></span><span><font color="#000000">->setThickness(30); </font></span><span class="comment"><font color="#008200">// Thickness </font></span><span><font color="#000000"> </font></span>
</li>
</ol>
  • $code->setForegroundColor($color_black); // Color of bars  
  • $code->setBackgroundColor($color_white); // Color of spaces  
  • $code->setFont($font); // Font (or 0)  
  • $text = $_REQUEST['text']; //PHP5生成条形码将要数据的内容  
  • $code->parse($text);  
  • /* Here is the list of the arguments  
  • 1 - Filename (empty : display on screen)  
  • 2 - Background color */ 
  • $drawing = new BCGDrawing(''$color_white);  
  • $drawing->setBarcode($code);  
  • $drawing->draw();  
  • // Header that says it is an image (remove it if you save the barcode to a file)  
  • header('Content-Type: image/png');  
  • // Draw (or save) the image into PNG format.  
  • $drawing->finish(BCGDrawing::IMG_FORMAT_PNG);  
  • ?> 
  • Exception occurred during operation";
    throw $e; //Rethrow exception
    }
    }
    ?﹥
     


    www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/446609.htmlTechArticleThe software supports both PHP4 and PHP5 versions. The PHP5 version is used in this article. Be sure to turn on PHP's GD module before use. This is php_gd2.dll in Windows and gd.so in Linux. Will compress...
    Statement:
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn