Php generates EAN_13 standard barcode instance _ PHP Tutorial

Source: Internet
Author: User
Php generates an EAN_13 standard barcode instance. The following is the PHP method for generating the EAN_13 standard bar code. the following code needs to be copied in the php + gd environment :? FunctionEAN_13 ($ code) {the width of a unit $ lw2; bar code height $ hi is the PHP method for generating the EAN_13 standard bar code, which requires the php + gd environment

The code is as follows:

Function EAN_13 ($ code ){
// The width of a unit
$ Lw = 2;
// High bar code
$ Hi = 100;
// The guide code is no coding, is used to show the left part coding type //
// Array guide is used to record the EAN_13 is left part coding type //
$ Guide = array (1 => 'aaaaa', 'aababab', 'aabbab', 'ababab', 'ababab', 'ababbaa', 'ababab', 'ababba ', 'abbaba ');
$ Lstart = '123 ';
$ Lencode = array ("A" => array ('20160301', '20160301', '20160301', '20160301', '20160301', '20160301 ', '123', '123', '123 '),
"B" => array ('000000', '000000', '000000', '000000', '000000', '000000', '000000', '000000 ', '123', '123 '));
$ Rencode = array ('20140901', '20160901', '20160901', '20160301', '20160301 ',
'123', '123', '123', '123', '123 ');

$ Center = '000000 ';

$ Ends = '20180101 ';
If (strlen ($ code )! = 13)
{Die ("UPC-A Must be 13 digits .");}
$ Lsum = 0;
$ Rsum = 0;
For ($ I = 0; $ I <(strlen ($ code)-1); $ I ++)
{
If ($ I % 2)
{
// $ Odd + = $ ncode [$ x]
$ Lsum + = (int) $ code [$ I];
} Else {
$ Rsum + = (int) $ code [$ I];
}

}
$ Tsum = $ lsum * 3 + $ rsum;
If ($ code [12]! = (10-($ tsum % 10 )))
{
Die ("the code is bad! ");
}

// Echo $ Guide [$ code [0];
$ Barcode = $ Lstart;
For ($ I = 1; $ I <= 6; $ I ++)
{
$ Barcode. = $ Lencode [$ Guide [$ code [0] [($ i-1)] [$ code [$ I];
}
$ Barcode. = $ center;

For ($ I = 7; $ I <13; $ I ++)
{
$ Barcode. = $ Rencode [$ code [($ I)];
}
$ Barcode. = $ ends;

$ Img = ImageCreate ($ lw * 95 + 60, $ hi + 30 );
$ Fg = ImageColorAllocate ($ img, 0, 0, 0 );
$ Bg = ImageColorAllocate ($ img, 255,255,255 );
ImageFilledRectangle ($ img, 0, 0, $ lw * 95 + 60, $ hi + 30, $ bg );
$ Shift = 10;
For ($ x = 0; $ x If ($ x <4) | ($ x >=45 & $ x <50) | ($ x> = 92 ))
{
$ Sh = 10;
} Else {
$ Sh = 0;
}
If ($ barcode [$ x] = '1 ')
{
$ Color = $ fg;
} Else {
$ Color = $ bg;
}
ImageFilledRectangle ($ img, ($ x * $ lw) + 30,5, ($ x + 1) * $ lw + 29, $ hi + 5 + $ sh, $ color );
}
/* Add the Human Readable Label */
ImageString ($ img, 5, 20, $ hi + 5, $ code [0], $ fg );
For ($ x = 0; $ x <6; $ x ++ ){
ImageString ($ img, 5, $ lw * (8 + $ x * 6) + 30, $ hi + 5, $ code [$ x + 1], $ fg );
ImageString ($ img, 5, $ lw * (53 + $ x * 6) + 30, $ hi + 5, $ code [$ x + 7], $ fg );
}
// ImageString ($ img, 4, $ lw * 95 + 17, $ hi-5, $ code [12], $ fg );
/* Output the Header and Content .*/
Header ("Content-Type: image/png ");
ImagePNG ($ img );

}

EAN_13 ('20140901 ');

?>

The runtime environment code is as follows :? Function EAN_13 ($ code) {// The width of a unit $ lw = 2; // The Bar code height $ hi =...

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.