PHP Generated Barcode instance

Source: Internet
Author: User
In addition to the QR code, in the commercial field, one-dimensional code (barcode) application is also very extensive. Because some old sweep gun equipment can not be compatible with the identification of two-dimensional code, so sometimes there will be the need to generate barcodes. So how does PHP generate barcodes? This article explains PHP's example of generating barcodes. We hope to help you.

Pre-array in contact with a merchant coupon function, need to use the barcode, so the data re-organized, the need for friends can refer to the next

1. What is a barcode?

Baidu Encyclopedia definition: bar code (barcode) is the width of a number of black bars and blank, according to a certain coding rules, to express a set of information graphic identifier. A common barcode is a pattern of parallel lines of black strips (short strips) and white bars (short ones) that vary greatly in reflectivity. In daily life, bar code can be marked out of the goods producing countries, manufacturers, product names, production dates, book classification number, mail location, the type, date and many other information. Please refer to the Barcode encoding format for details

Printed coupons, merchants need to use the authenticator to read the barcode, to obtain its validity.

2. How do I generate barcodes?

First find strong open source materials, download the Barcodegen.1d-php5.v5.0.1.zip version on barcode website, and then extract the files to the root directory of your Apache server

2.1 File Structure:

2.2 Specific analysis

(1) Class folders are classes that have been packaged to generate barcodes, and only need to be called.

(2) index.php is an optional condition to generate barcode function, is the main program entrance, and the HTML folder is provided by the referenced code, code39.php refers to the default encoding format.

<?php header (' location:html/code39.php ');?>

(3) test.php is another example of generating a hello barcode directly from the code.

View Code <?php//Refers to Class require_once (' class/bcgfontfile.php ') corresponding to class folder; 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 barcode $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 thickness of the barcode $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 the barcode according to the above conditions $drawing = new Bcgdrawing (", $color _white);   if ($drawException) {$drawing->drawexception ($drawException);} else {$drawing->setbarcode ($code); $drawing->draw ();   }//Generate a PNG-formatted picture header (' content-type:image/png '); $drawingFinish (bcgdrawing::img_format_png);?> 

3. Practical Application

For a general understanding of the above, we can re-integrate the code below, more convenient to use it.

First create a new buildcode.php file, overwrite it according to the test.php file, and fetch the 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 barcode will be the contents of the data//including the Barcode technology require_once (' class/'. $codebar.  Barcode.php ');  Loading Font $font = new Bcgfontfile ('./class/font/arial.ttf ', 12); The arguments is R, G, B for color. $color _black = 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;//Thickn ESS $code->setforegroundcolor ($color _black); Color of Bars $code->setbackgroundcolor ($color _white); Color of Spaces $code->setfont ($font); Font (or 0) $text = $_request[' text ']; The barcode will $code->parse ($text) to the content of the data;  } 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's 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 create a new test.html file and request data to buildcode.php

<! DOCTYPE html> 

Last access, the browser generates a PNG-formatted barcode directly

where Codebar supports encoding formats that can be requested by the user:

/* ' Bcgcodabar ', ' BCGcode11 ', ' BCGcode39 ', ' bcgcode39extended ', ' BCGcode93 ',
' BCGcode128 ', ' BCGean8 ', ' BCGean13 ', ' bcgisbn ', ' BCGi25 ', ' BCGs25 ', ' Bcgmsi ',
' BCGUPCA ', ' bcgupce ', ' BCGupcext2 ', ' BCGupcext5 ', ' bcgpostnet ', ' bcgothercode ' * *
All that's left is to verify

4. Verification

How do we verify that the barcode is valid, that is, the ability to read the contents of the barcode.

First save the picture, and then visit the official website to provide the verification function, upload the image is ok!

Today and everyone together to uncover how PHP generated barcodes, I hope you can have a general understanding of the formation of barcodes, for the future of learning to help.

Related recommendations:

PHP Two-dimensional code generation

PHP Two-dimensional code plus watermark image support Ios,android,win8

PHP image operation class, support to generate thumbnails, add watermarks, upload thumbnails

Related Article

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.