Using PHP to generate EAN

Source: Internet
Author: User
Tags array coding standards header strlen
Standard | Bar code This sky saw someone fateh discuss such as using PHP to generate barcodes, so it took a little time to learn the EAN_13 standard. EAN_13 standard is widely used barcode coding standards, mainly used in the commodity circulation, such as we see the bar code is this code standard, as well as books on the coding standard is this coding standard.
The following is my section of code generated is EAN_13 standard barcode, need PHP+GD environment
[Code:1:dfc50fedcf]<?
function Ean_13 ($code) {
The width of a cell
$LW = 2;
Barcode High
$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 are left part coding type//
$Guide = Array (1=> ' aaaaaa ', ' aababb ', ' aabbab ', ' abaabb ', ' Abbaab ', ' Abbbaa ', ' ababab ', ' Ababba ', ' Abbaba ');
$Lstart = ' 101 ';
$Lencode = Array ("A" => Array (' 0001101 ', ' 0011001 ', ' 0010011 ', ' 0111101 ', ' 0100011 ', ' 0110001 ', ' 0101111 ', ' 0111011 ', ') ' 0110111 ', ' 0001011 '),
"B" => Array (' 0100111 ', ' 0110011 ', ' 0011011 ', ' 0100001 ', ' 0011101 ', ' 0111001 ', ' 0000101 ', ' 0010001 ', ' 0001001 ', ') ', ' 0010111 '));
$Rencode = Array (' 1110010 ', ' 1100110 ', ' 1101100 ', ' 1000010 ', ' 1011100 ',
' 1001110 ', ' 1010000 ', ' 1000100 ', ' 1001000 ', ' 1110100 ');

$center = ' 01010 ';

$ends = ' 101 ';
if (strlen ($code)!= 13)
{die ("upc-a must be 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 [a]!= (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 <strlen ($barcode); $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 [a], $FG);
/* Output the Header and Content. */
Header ("Content-type:image/png");
Imagepng ($IMG);

}

Ean_13 (' 6901028055048 ');

?>


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.