Php generates the EAN_13 standard Barcode instance. The following is the PHP method for generating the EAN_13 standard Barcode. The php + gd environment is required.
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 ');
?>