Simple Example of generating a barcode in PHP5

Source: Internet
Author: User

This software supports two versions: PHP4 and PHP5. The PHP5 version is used in this article. Before using the function, you must enable the GD module of PHP. In Windows, it is php_gd2.dll, and in Linux it is gd. so. Decompress the compressed file to Apache and executeHttp: // localhost/barcode/index. php. For more information about generating the PHP5 barcode, see:

1. Type: select the barcode Type

2. Output: Output image format

3. Thickness: bar code height

4. Resolution: bar code size

5. Font: the text at the bottom of the bar code cannot be displayed.

6. Text: bar code printed content

498) this. width = 498; 'onmousewheel = 'javascript: return big (this) 'alt = bar code effect src = "http://www.bkjia.com/uploadfile/2013/0904/20130904094617982.jpg" width = 615 height = 557>

Of course, this PHP5 generated barcode program only generates text as a barcode, but it cannot be flexibly embedded into other PHP programs during use. I will compress the test in the package. php has made some adjustments so that it can be used flexibly in other programs. You only need to pass the barcode type and text to test. php at runtime, for example:
Http: // localhost/barcode/test. php? Codebar = BCGcode39 & text = 20090729

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

Mytest. php code:

   
  
  1. <img src="test.php?codebar=BCGcode39&text=20090729"> 
PHP5 generates a barcode:

498) this. width = 498; 'onmousewheel = 'javascript: return big (this) 'alt = run instance src = "http://www.bkjia.com/uploadfile/2013/0904/20130904094617379.jpg" width = 665 height = 186>

PHP5 generates the code test. php:

   
  
  1. <? Php
  2. // Including all required classes 
  3. Require('Class/BCGFont. php');
  4. Require('Class/BCGColor. php');
  5. Require('Class/BCGDrawing. php');
  6. /* 'Bcgcodabar ', 'bcgcode11', 'bcgcode39extended', 'bcgcode93 ', 
  7. 'Bcgcode128 ', 'bcgean8', 'bcgean13', 'bcgisbn', 'bcgi25', 'bcgs25', 'bcgmsi ', 
  8. 'Bcgupca ', 'bcgupce', 'bcgupcext2', 'bcgupcext5', 'bcgpostnet', 'bcgothercode '*/ 
  9. $ Codebar=$ _ REQUEST['Codebar'];// All codes supported by the Software. You only need to adjust the $ codebar parameter. 
  10. // Including the barcode technology 
  11. Include('Class /'.$ Codebar.'. Barcode. php');
  12. // Loading Font 
  13. $ Font=NewBCGFont ('./Class/font/Arial. ttf', 10 );
  14. // The arguments are R, G, B for color. 
  15. $ Color_black=NewBCGColor (0, 0, 0 );
  16. $ Color_white=NewBCGColor (1, 255,255,255 );
  17. $ Code=New $ Codebar();
  18. $ Code-> SetScale (2 );// Resolution 
  19. $ Code-> SetThickness (30 );// 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'];// PHP5 generates the content of the barcode to be data 
  24. $ Code-> Parse ($ Text);
  25. /* Here is the list of the arguments 
  26. 1-Filename (empty: display on screen) 
  27. 2-Background color */ 
  28. $ Drawing=NewBCGDrawing ('',$ Color_white);
  29. $ Drawing-> SetBarcode ($ Code);
  30. $ Drawing-> Draw ();
  31. // Header that says it is an image (remove it if you save the barcode to a file) 
  32. Header ('Content-Type: image/png');
  33. // Draw (or save) the image into PNG format. 
  34. $ Drawing-> Finish (BCGDrawing: IMG_FORMAT_PNG );
  35. ?>
An exception occurs during running ";
Throw $ e; // throwing an exception
}
}
? >
 


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.