A while ago, I was working on a seller's coupon function and needed a bar code. So I sorted out the information again.
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 more information about the barcode encoding format, seeReference
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 powerful open-source materials.BarcodeDownload from official websiteBarcodegen.1d-php5.v5.0.1.zip version, and then unzip 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 an optional bar code generation function, which is the mainProgramWhile the HTML folder provides the referencedCode, Code39.php refers to the default encoding format.
<?PHPHeader('Location: HTML/code39.php');?>
When direct accessHttp: // localhost/barcodegen/index. phpYou can use this function to select any item and generate the corresponding barcode. If necessary, you can change it to a module.
(3) test. php is another example. The Code directly generates a hello bar code.
When http: // localhost/barcodegen/test. php is accessed, the hello. PNG image is generated.
View code
<? PHP // Reference the class corresponding to the class folder Require_once ('Class/bcgfontfile. php' ); Require_once ('Class/bcgcolor. php' ); Require_once ('Class/bcgdrawing. php' ); // Barcode encoding format Require_once ('Class/bcgcode39.barcode. php' ); // Load font size $ Font = New Bcgfontfile ('./class/font/Arial. ttf', 18 ); // Color Bar Code $ Color_black = New Bcgcolor (0, 0, 0 ); $ Color_white =New Bcgcolor (1, 255,255,255 ); $ Drawexception = Null ; Try { $ Code = New Bcgcode39 (); $ Code -> Setscale (2 ); $ Code -> Setthickness (30 ); // Barcode Thickness $ Code -> Setforegroundcolor ( $ Color_black ); // Barcode color $ Code -> Setbackgroundcolor ( $ Color_white ); // Blank gap color $ Code -> Setfont ( $ Font ); // $ Code -> Parse ('hello '); // Data content required by the barcode } Catch ( Exception $ Exception ){ $ Drawexception = $ Exception ;} // Create a bar code based on the preceding conditions. $ Drawing = New Bcgdrawing ('', $ Color_white ); If ( $ Drawexception ){ $ Drawing -> Drawexception ( $ Drawexception );} Else { $ Drawing -> Setbarcode ( $ Code ); $ Drawing -> Draw ();} // Generate images in PNG format Header ('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. 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']; // 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_black = New Bcgcolor (0, 0, 0 ); $ Color_white = New Bcgcolor (1, 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 $ Code -> Parse ( $ Text );} Catch ( Exception $ Exception ){ $ Drawexception = $ Exception ;} /* Here is the list of the arguments1-filename (empty: display on screen) 2-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 the 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.
View code
<! Doctype html > < Html > < Head > < Title > Test with Embedded Image</ Title > </ Head > < Body > < IMG SRC = "Buildcode. php? Codebar = bcgcode39 & text = ABC123" /> </ Body > </ Html >
Finally, access http: // localhost/barcodegen/test.html or http: // localhost/barcodegen/buildcode. php? Codebar = bcgcode39 & text = ABC123. the browser directly generates a PNG barcode.
The encoding formats supported by codebar can be obtained by user requests:
/*'Bcgcodabar ', 'bcgcode11', 'bcgcode39', 'hangzhou', 'bcgcode93', 'bcgcode128', 'bcgean8', 'bcgean13', 'bcgisbn ', 'bcgi25 ', 'bcgs25', 'bcgmsi ', 'bcgupca', 'bcgupce', 'bcgupcext2', 'bcgupcext5 ', 'bcgpostnet', 'bcgothercode'*/
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 and then accessOfficial WebsiteThe verification function is provided to upload images!
Author: forevernome
Source: http://www.cnblogs.com/ForEvErNoME/
You are welcome to repost or share it, but be sure to declare it Article Source. If the article is helpful to you, I hope you can Recommendation Or Follow