PHP generated barcode method code sharing

Source: Internet
Author: User
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.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.

 
  

(3) test.php is another example of generating a hello barcode directly from the 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 '); $drawing->finish (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

 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 barcode will $code->parse ($text) The contents of the data;} catch (Exception $exception) {$drawException = $exception;}/* Here is the list of the Arguments-filename (empty:disp Lay on screen)-Background color */$drawing = new Bcgdrawing (", $color _white), if ($drawException) {$drawing->drawexc Eption ($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

Test with embedded image

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!

  • 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.