A few days ago, I came into contact with a seller's coupon function and needed a bar code. So I sorted out the information again. For more information, see
A few days ago, I came into contact with a seller's coupon function and needed a bar code. So I sorted out the information again. For more information, see
1. What is a bar code?
Baidu encyclopedia definition: barcode is a graphic identifier used to express a set of information by sorting multiple black bars and white spaces with different widths according to certain encoding rules. A common bar code is a parallel line of a black stripe (abbreviated as a bar) and a white stripe (blank for short) with a large difference in reflectivity. In daily life, the bar code can mark the producing country, manufacturer, product name, production date, Book Classification Number, mail place start and end, category, date and many other information. For details about the barcode encoding format, refer
For the printed coupon, the merchant needs to use the validators to read the barcode to obtain its validity.
2. How to generate a barcode?
First, find the strong open source data, download barcodegen.1d-php5.v5.0.1.zip on the barcodeofficial network, and decompress the file to the root directory of your Apache server.
2.1 file structure:
2.2 Specific analysis
(1) the class folder is a class that has encapsulated the generated barcode. You only need to call it.
(2) index. php is a bar code generation function with selectable conditions. It is the entry of the main program, while the html folder provides the referenced code. code39.php refers to the default encoding format.
<? Php header ('location: html/code39.php ');?>
(3) test. php is another example. The Code directly generates a HELLO bar code.
View Code <? Php // reference the class require_once ('class/BCGFontFile) corresponding to the class folder. php '); require_once ('class/BCGColor. php '); require_once ('class/BCGDrawing. php '); // The barcode encoding format require_once ('class/BCGcode39.barcode. php '); // load the font size $ font = new BCGFontFile ('. /class/font/Arial. ttf', 18); // color bar code $ color_black = new BCGColor (0, 0, 0); $ color_white = new BCGColor (255,255,255); $ drawException = null; try {$ code = new BCGcode39 (); $ code- > SetScale (2); $ code-> setThickness (30); // The barcode thickness $ code-> setForegroundColor ($ color_black ); // barcode color $ code-> setBackgroundColor ($ color_white); // blank gap color $ code-> setFont ($ font ); // $ code-> parse ('hello'); // the data content required by the barcode} catch (Exception $ exception) {$ drawException = $ exception ;} // draw a bar code based on the preceding conditions: $ drawing = new BCGDrawing ('', $ color_white); if ($ drawException) {$ drawing-> drawException ($ drawException );} else {$ dr Awing-> setBarcode ($ code); $ drawing-> draw ();} // generate an image header in PNG format ('content-Type: image/png '); $ drawing-> finish (BCGDrawing: IMG_FORMAT_PNG);?>
3. Practical Application
After a rough understanding of the above, we can re-integrate the Code to make it easier to use.
First, create the buildcode. php file and rewrite it according to the test. php file to obtain data from the requested file:
1). barcode encoding format
2). The data content required by the barcode
View Code <? Php // Including all required classes require_once ('class/BCGFontFile. php '); require_once ('class/BCGColor. php '); require_once ('class/BCGDrawing. php '); $ codebar = $ _ REQUEST ['codebar']; // the content of the barcode to be data // Including the barcode technology require_once ('class /'. $ codebar. '. barcode. php '); // Loading Font $ font = new BCGFontFile ('. /class/font/Arial. ttf', 12); // The arguments are R, G, B for color. $ color_bl Ack = new BCGColor (0, 0, 0); $ color_white = new BCGColor (255,255,255); $ drawException = null; try {$ code = new $ codebar (); // instantiate the corresponding encoding format $ code-> setScale (2); // Resolution $ code-> setThickness (23 ); // Thickness $ code-> setForegroundColor ($ color_black); // Color of bars $ code-> setBackgroundColor ($ color_white ); // Color of spaces $ code-> setFont ($ font); // Font (or 0) $ text = $ _ REQUEST ['text']; // The content of the barcode to be data $ c Ode-> parse ($ text);} catch (Exception $ exception) {$ drawException = $ exception;}/* Here is the list of the arguments-Filename (empty: display on screen)-Background color */$ drawing = new BCGDrawing ('', $ color_white); if ($ drawException) {$ drawing-> drawException ($ drawException );} else {$ drawing-> setBarcode ($ code); $ drawing-> draw ();} // Header that says it is an image (remove it if you save t He barcode to a file) header ('content-Type: image/png '); // Draw (or save) the image into PNG format. $ drawing-> finish (BCGDrawing: IMG_FORMAT_PNG);?>
Then, the test.html file is created to request data from buildcode. php.
Test with embedded image
Finally, the browser directly generates a png-format bar code.
The encoding formats supported by codebar can be obtained by user requests:
/* 'Bcgcodabar ', 'bcgcode11', 'bcgcode39extended', 'bcgcode93 ',
'Bcgcode128 ', 'bcgean8', 'bcgean13', 'bcgisbn', 'bcgi25', 'bcgs25', 'bcgmsi ',
'Bcgupca ', 'bcgupce', 'bcgupcext2', 'bcgupcext5', 'bcgpostnet', 'bcgothercode '*/
The rest is verification.
4. Verify
How can we verify that the barcode is valid, that is, whether the content in the barcode can be read.
Save the image, visit the verification function provided on the official website, and upload the image!
Today, I have unveiled how php generates bar codes. I hope you can have a rough understanding of the formation of bar codes and help you learn more in the future.