BarCode 128 generated via PHP and GD library
-
Hello all, I am working on a php script to generate an image with the barcode 128 B (I will not be using the A or C). I have it displaying a barcode in firefox. When I print it it looks close to one generanted by a barcode application. The diference is that mine is slightly longer. Anyways given the same string the app generate code scans and mine does not. I believe everything is in order...but I must be missing something simple here...suggestions? This is the hash_code_128.php script referenced in the main script. The source for the codes came from wikipedia http://en.wikipedia.org/wiki/Code_128[^]
<?php
$b_chr = array(); // character lookup
$b_enc = array(); // encoding lookup
$stop = "11000111010";
$term_bar = "11";
$b_chr['00'] = " ";
$b_chr['01'] = "!";
$b_chr['02'] = '"';
$b_chr['03'] = "#";
$b_chr['04'] = "$";
$b_chr['05'] = "%";
$b_chr['06'] = "&";
$b_chr['07'] = "'";
$b_chr['08'] = "(";
$b_chr['09'] = ")";
$b_chr['10'] = "*";
$b_chr['11'] = "+";
$b_chr['12'] = ",";
$b_chr['13'] = "-";
$b_chr['14'] = ".";
$b_chr['15'] = "/";
$b_chr['16'] = "0";
$b_chr['17'] = "1";
$b_chr['18'] = "2";
$b_chr['19'] = "3";
$b_chr['20'] = "4";
$b_chr['21'] = "5";
$b_chr['22'] = "6";
$b_chr['23'] = "7";
$b_chr['24'] = "8";
$b_chr['25'] = "9";
$b_chr['26'] = ":";
$b_chr['27'] = ";";
$b_chr['28'] = "<";
$b_chr['29'] = "=";
$b_chr['30'] = ">";
$b_chr['31'] = "?";
$b_chr['32'] = "@";
$b_chr['33'] = "A";
$b_chr['34'] = "B";
$b_chr['35'] = "C";
$b_chr['36'] = "D";
$b_chr['37'] = "E";
$b_chr['38'] = "F";
$b_chr['39'] = "G";
$b_chr['40'] = "H";
$b_chr['41'] = "I";
$b_chr['42'] = "J";
$b_chr['43'] = "K";
$b_chr['44'] = "L";
$b_chr['45'] = "M";
$b_chr['46'] = "N";
$b_chr['47'] = "O";
$b_chr['48'] = "P";
$b_chr['49'] = "Q";
$b_chr['50'] = "R";
$b_chr['51'] = "S";
$b_chr['52'] = "T";
$b_chr['53'] = "U";
$b_chr['54'] = "V";
$b_chr['55'] = "W";
$b_chr['56'] = "X";
$b_chr['57'] = "Y";
$b_chr['58'] = "Z";
$b_chr['59'] = "[";
$b_chr['60'] = "\\";
$b_chr['61'] = "]";
$b_chr['62'] = "^";
$b_chr['63'] = "_";
$b_chr['64'] = "`";
$b_chr['65'] = "a";
$b_chr['66'] = "b";
$b_chr['67'] = "c";
$b_chr['68'] = "d";
$b_chr['69'] = "e";
$b_chr['70'] = "f";
$b_chr['71'] = "g";
$b_chr['72'] = "h";
$b_chr['73'] = "i";
$b_chr['74'] = "j"; -
Hello all, I am working on a php script to generate an image with the barcode 128 B (I will not be using the A or C). I have it displaying a barcode in firefox. When I print it it looks close to one generanted by a barcode application. The diference is that mine is slightly longer. Anyways given the same string the app generate code scans and mine does not. I believe everything is in order...but I must be missing something simple here...suggestions? This is the hash_code_128.php script referenced in the main script. The source for the codes came from wikipedia http://en.wikipedia.org/wiki/Code_128[^]
<?php
$b_chr = array(); // character lookup
$b_enc = array(); // encoding lookup
$stop = "11000111010";
$term_bar = "11";
$b_chr['00'] = " ";
$b_chr['01'] = "!";
$b_chr['02'] = '"';
$b_chr['03'] = "#";
$b_chr['04'] = "$";
$b_chr['05'] = "%";
$b_chr['06'] = "&";
$b_chr['07'] = "'";
$b_chr['08'] = "(";
$b_chr['09'] = ")";
$b_chr['10'] = "*";
$b_chr['11'] = "+";
$b_chr['12'] = ",";
$b_chr['13'] = "-";
$b_chr['14'] = ".";
$b_chr['15'] = "/";
$b_chr['16'] = "0";
$b_chr['17'] = "1";
$b_chr['18'] = "2";
$b_chr['19'] = "3";
$b_chr['20'] = "4";
$b_chr['21'] = "5";
$b_chr['22'] = "6";
$b_chr['23'] = "7";
$b_chr['24'] = "8";
$b_chr['25'] = "9";
$b_chr['26'] = ":";
$b_chr['27'] = ";";
$b_chr['28'] = "<";
$b_chr['29'] = "=";
$b_chr['30'] = ">";
$b_chr['31'] = "?";
$b_chr['32'] = "@";
$b_chr['33'] = "A";
$b_chr['34'] = "B";
$b_chr['35'] = "C";
$b_chr['36'] = "D";
$b_chr['37'] = "E";
$b_chr['38'] = "F";
$b_chr['39'] = "G";
$b_chr['40'] = "H";
$b_chr['41'] = "I";
$b_chr['42'] = "J";
$b_chr['43'] = "K";
$b_chr['44'] = "L";
$b_chr['45'] = "M";
$b_chr['46'] = "N";
$b_chr['47'] = "O";
$b_chr['48'] = "P";
$b_chr['49'] = "Q";
$b_chr['50'] = "R";
$b_chr['51'] = "S";
$b_chr['52'] = "T";
$b_chr['53'] = "U";
$b_chr['54'] = "V";
$b_chr['55'] = "W";
$b_chr['56'] = "X";
$b_chr['57'] = "Y";
$b_chr['58'] = "Z";
$b_chr['59'] = "[";
$b_chr['60'] = "\\";
$b_chr['61'] = "]";
$b_chr['62'] = "^";
$b_chr['63'] = "_";
$b_chr['64'] = "`";
$b_chr['65'] = "a";
$b_chr['66'] = "b";
$b_chr['67'] = "c";
$b_chr['68'] = "d";
$b_chr['69'] = "e";
$b_chr['70'] = "f";
$b_chr['71'] = "g";
$b_chr['72'] = "h";
$b_chr['73'] = "i";
$b_chr['74'] = "j";I came across this a while ago - dunno if it'll help:
<?php
function UPCAbarcode($code) {
$lw = 2; $hi = 100;
$Lencode = array('0001101','0011001','0010011','0111101','0100011',
'0110001','0101111','0111011','0110111','0001011');
$Rencode = array('1110010','1100110','1101100','1000010','1011100',
'1001110','1010000','1000100','1001000','1110100');
$ends = '101'; $center = '01010';
/* UPC-A Must be 11 digits, we compute the checksum. */
if ( strlen($code) != 11 ) { die("UPC-A Must be 11 digits."); }
/* Compute the EAN-13 Checksum digit */
$ncode = '0'.$code;
$even = 0; $odd = 0;
for ($x=0;$x<12;$x++) {
if ($x % 2) { $odd += $ncode[$x]; } else { $even += $ncode[$x]; }
}
$code.=(10 - (($odd * 3 + $even) % 10)) % 10;
/* Create the bar encoding using a binary string */
$bars=$ends;
$bars.=$Lencode[$code[0]];
for($x=1;$x<6;$x++) {
$bars.=$Lencode[$code[$x]];
}
$bars.=$center;
for($x=6;$x<12;$x++) {
$bars.=$Rencode[$code[$x]];
}
$bars.=$ends;
/* Generate the Barcode Image */
$img = ImageCreate($lw*95+30,$hi+30);
$fg = ImageColorAllocate($img, 0, 0, 0);
$bg = ImageColorAllocate($img, 255, 255, 255);
ImageFilledRectangle($img, 0, 0, $lw*95+30, $hi+30, $bg);
$shift=10;
for ($x=0;$x=45 && $x<50) || ($x >=85)) { $sh=10; } else { $sh=0; }
if ($bars[$x] == '1') { $color = $fg; } else { $color = $bg; }
ImageFilledRectangle($img, ($x*$lw)+15,5,($x+1)*$lw+14,$hi+5+$sh,$color);
}
/* Add the Human Readable Label */
ImageString($img,4,5,$hi-5,$code[0],$fg);
for ($x=0;$x<5;$x++) {
ImageString($img,5,$lw*(13+$x*6)+15,$hi+5,$code[$x+1],$fg);
ImageString($img,5,$lw*(53+$x*6)+15,$hi+5,$code[$x+6],$fg);
}
ImageString($img,4,$lw*95+17,$hi-5,$code[11],$fg);
/* Output the Header and Content. */
header("Content-Type: image/png");
ImagePNG($img);
}UPCAbarcode('12345678901');
?>
-
Hello all, I am working on a php script to generate an image with the barcode 128 B (I will not be using the A or C). I have it displaying a barcode in firefox. When I print it it looks close to one generanted by a barcode application. The diference is that mine is slightly longer. Anyways given the same string the app generate code scans and mine does not. I believe everything is in order...but I must be missing something simple here...suggestions? This is the hash_code_128.php script referenced in the main script. The source for the codes came from wikipedia http://en.wikipedia.org/wiki/Code_128[^]
<?php
$b_chr = array(); // character lookup
$b_enc = array(); // encoding lookup
$stop = "11000111010";
$term_bar = "11";
$b_chr['00'] = " ";
$b_chr['01'] = "!";
$b_chr['02'] = '"';
$b_chr['03'] = "#";
$b_chr['04'] = "$";
$b_chr['05'] = "%";
$b_chr['06'] = "&";
$b_chr['07'] = "'";
$b_chr['08'] = "(";
$b_chr['09'] = ")";
$b_chr['10'] = "*";
$b_chr['11'] = "+";
$b_chr['12'] = ",";
$b_chr['13'] = "-";
$b_chr['14'] = ".";
$b_chr['15'] = "/";
$b_chr['16'] = "0";
$b_chr['17'] = "1";
$b_chr['18'] = "2";
$b_chr['19'] = "3";
$b_chr['20'] = "4";
$b_chr['21'] = "5";
$b_chr['22'] = "6";
$b_chr['23'] = "7";
$b_chr['24'] = "8";
$b_chr['25'] = "9";
$b_chr['26'] = ":";
$b_chr['27'] = ";";
$b_chr['28'] = "<";
$b_chr['29'] = "=";
$b_chr['30'] = ">";
$b_chr['31'] = "?";
$b_chr['32'] = "@";
$b_chr['33'] = "A";
$b_chr['34'] = "B";
$b_chr['35'] = "C";
$b_chr['36'] = "D";
$b_chr['37'] = "E";
$b_chr['38'] = "F";
$b_chr['39'] = "G";
$b_chr['40'] = "H";
$b_chr['41'] = "I";
$b_chr['42'] = "J";
$b_chr['43'] = "K";
$b_chr['44'] = "L";
$b_chr['45'] = "M";
$b_chr['46'] = "N";
$b_chr['47'] = "O";
$b_chr['48'] = "P";
$b_chr['49'] = "Q";
$b_chr['50'] = "R";
$b_chr['51'] = "S";
$b_chr['52'] = "T";
$b_chr['53'] = "U";
$b_chr['54'] = "V";
$b_chr['55'] = "W";
$b_chr['56'] = "X";
$b_chr['57'] = "Y";
$b_chr['58'] = "Z";
$b_chr['59'] = "[";
$b_chr['60'] = "\\";
$b_chr['61'] = "]";
$b_chr['62'] = "^";
$b_chr['63'] = "_";
$b_chr['64'] = "`";
$b_chr['65'] = "a";
$b_chr['66'] = "b";
$b_chr['67'] = "c";
$b_chr['68'] = "d";
$b_chr['69'] = "e";
$b_chr['70'] = "f";
$b_chr['71'] = "g";
$b_chr['72'] = "h";
$b_chr['73'] = "i";
$b_chr['74'] = "j";I got it working. I had a few issues that needed to be adressed. 1) My checksum made a mistake of just using the modulus value as a lookup, when I really needed to add 32 to the value and find the ascii character it represents. Then I can find the hash code based on that and add it to the bar code string.
$checksum = $checksum + 32;
$checksum2 = chr($checksum);
$key = array_search($checksum2,$b_chr,true);
$bcstring.=$b_enc[$key].$stop.$term_bar;- I did not draw the final termination barcode in my for loop due to a simple logic error. I corrected it with and a "or" to check for the end of the barcode string so I write the last part.
if($bcstring[$i]==0 && $bcstring[$i]!=$last_char || $i == $bc_cnt-1)
{
// stopping new bar
// bool imagefilledrectangle ( resource $image , int $x1 , int $y1 , int $x2 , int $y2 , int $color )
imagefilledrectangle($im, $start_x, $pad_top, $start_x+($stop_x*$size)-1, $height, $black);
$stop_x = 1;
}it is all working. If anyone wants to use this code, enjoy! Of course a mention is always nice ;)
-
I got it working. I had a few issues that needed to be adressed. 1) My checksum made a mistake of just using the modulus value as a lookup, when I really needed to add 32 to the value and find the ascii character it represents. Then I can find the hash code based on that and add it to the bar code string.
$checksum = $checksum + 32;
$checksum2 = chr($checksum);
$key = array_search($checksum2,$b_chr,true);
$bcstring.=$b_enc[$key].$stop.$term_bar;- I did not draw the final termination barcode in my for loop due to a simple logic error. I corrected it with and a "or" to check for the end of the barcode string so I write the last part.
if($bcstring[$i]==0 && $bcstring[$i]!=$last_char || $i == $bc_cnt-1)
{
// stopping new bar
// bool imagefilledrectangle ( resource $image , int $x1 , int $y1 , int $x2 , int $y2 , int $color )
imagefilledrectangle($im, $start_x, $pad_top, $start_x+($stop_x*$size)-1, $height, $black);
$stop_x = 1;
}it is all working. If anyone wants to use this code, enjoy! Of course a mention is always nice ;)
Hello cjoki, I try to explain, but not very practical with English (I'm Italian) and I just started with php ... I am trying your code but you probably have not entered correctly the last few lines of code that you posted, because the barcode is almost always correct but sometimes lack a little bit. Here how I use it, if you can help me. Thanks
<?php
/*
This is a program to generate a code 128B barcode and returns an image.Chris J, 2009-11-04 \*/ $string=urldecode($\_GET\['string'\]); $size=urldecode($\_GET\['size'\]); $height=urldecode($\_GET\['height'\]); // $string is string to convert to bar code // $size is the pixel width of the bar code and is used as a base unit for each barcode/space // based on the # of 0's or 1's in a row. // for example the stop bar code hash is 11000111010. This is drawn as a bar 2 units wide, a space 3 units wide, a bar // 3 units wide, a space 1 unit wide, a bar 1 unit wide followed by a space 1 unit wide. If the size is equal to 3 (for 3 pixels) // this will mean the barcode for the stop is bar 6 pixels, space 9 pixels, bar 9 pixels, space 3 pixels, bar 3 pixels and space // 3 pixels. The entire code will messure 33 pixels in width. of course that is just for the stop (the end of the code 128 barcode). // $height is the height of the barcode in pixels. There must be room for for some padding on the top and bottom and room for // the text of $string to be printed just under the barcode. // at the front and back of a code 128 barcode is a blank space called the quiet zone. It should be 10 times the unit ($size in my function) include('hash\_code\_128.php'); $bcstring = ''; $cnt = strlen($string) ; $quiet\_zone = 10\*$size-1; $pad\_top = 5; $pad\_text = 3; $font\_size = 5; $pad\_bottom = 5; $font\_left = $quiet\_zone; $font\_top = $pad\_text + $height; $ttl\_width = ($quiet\_zone\*2)+($size\*($cnt+3)\*11)+2; // include string length + start + stop + checksum + front and back quiet zones $ttl\_height = $pad\_top + $pad\_text + $font\_size + $pad\_bottom + $height; $running\_checksum = 104; // START B hardcoded... $bcstring.=$b\_enc\['104'\]; // build the barcode for($i=0;$i<$cnt;$i++) { $position = $i+1; $key = array\_search($string\[$i\],$b\_chr,true); $bcstring.=$b\_enc\[$key\]; $running\_checksum+=($position\*$key); } $checksum = $running\_checksum%103; //~ $bcstring.=$b\_enc\[$checksum\].$stop.$term\_bar;
$checksum = $checksum + 32;
$checksum2 = chr($check -
Hello cjoki, I try to explain, but not very practical with English (I'm Italian) and I just started with php ... I am trying your code but you probably have not entered correctly the last few lines of code that you posted, because the barcode is almost always correct but sometimes lack a little bit. Here how I use it, if you can help me. Thanks
<?php
/*
This is a program to generate a code 128B barcode and returns an image.Chris J, 2009-11-04 \*/ $string=urldecode($\_GET\['string'\]); $size=urldecode($\_GET\['size'\]); $height=urldecode($\_GET\['height'\]); // $string is string to convert to bar code // $size is the pixel width of the bar code and is used as a base unit for each barcode/space // based on the # of 0's or 1's in a row. // for example the stop bar code hash is 11000111010. This is drawn as a bar 2 units wide, a space 3 units wide, a bar // 3 units wide, a space 1 unit wide, a bar 1 unit wide followed by a space 1 unit wide. If the size is equal to 3 (for 3 pixels) // this will mean the barcode for the stop is bar 6 pixels, space 9 pixels, bar 9 pixels, space 3 pixels, bar 3 pixels and space // 3 pixels. The entire code will messure 33 pixels in width. of course that is just for the stop (the end of the code 128 barcode). // $height is the height of the barcode in pixels. There must be room for for some padding on the top and bottom and room for // the text of $string to be printed just under the barcode. // at the front and back of a code 128 barcode is a blank space called the quiet zone. It should be 10 times the unit ($size in my function) include('hash\_code\_128.php'); $bcstring = ''; $cnt = strlen($string) ; $quiet\_zone = 10\*$size-1; $pad\_top = 5; $pad\_text = 3; $font\_size = 5; $pad\_bottom = 5; $font\_left = $quiet\_zone; $font\_top = $pad\_text + $height; $ttl\_width = ($quiet\_zone\*2)+($size\*($cnt+3)\*11)+2; // include string length + start + stop + checksum + front and back quiet zones $ttl\_height = $pad\_top + $pad\_text + $font\_size + $pad\_bottom + $height; $running\_checksum = 104; // START B hardcoded... $bcstring.=$b\_enc\['104'\]; // build the barcode for($i=0;$i<$cnt;$i++) { $position = $i+1; $key = array\_search($string\[$i\],$b\_chr,true); $bcstring.=$b\_enc\[$key\]; $running\_checksum+=($position\*$key); } $checksum = $running\_checksum%103; //~ $bcstring.=$b\_enc\[$checksum\].$stop.$term\_bar;
$checksum = $checksum + 32;
$checksum2 = chr($checkhere is the full code that is in production still. maybe this will help. cjoki
<?php
/*
This is a program to generate a code 128B barcode and returns an image.Chris J, 2009-11-04 \*/ function gen\_code128($string, $fid, $size = 1, $height = 50, $show\_text = true) { // $string is string to convert to bar code // $fid is the filename, it will default to the $string if no is provided // $size is the pixel width of the bar code and is used as a base unit for each barcode/space // based on the # of 0's or 1's in a row. // for example the stop bar code hash is 11000111010. This is drawn as a bar 2 units wide, a space 3 units wide, a bar // 3 units wide, a space 1 unit wide, a bar 1 unit wide followed by a space 1 unit wide. If the size is equal to 3 (for 3 pixels) // this will mean the barcode for the stop is bar 6 pixels, space 9 pixels, bar 9 pixels, space 3 pixels, bar 3 pixels and space // 3 pixels. The entire code will messure 33 pixels in width. of course that is just for the stop (the end of the code 128 barcode). // $height is the height of the barcode in pixels. There must be room for for some padding on the top and bottom and room for // the text of $string to be printed just under the barcode. // at the front and back of a code 128 barcode is a blank space called the quiet zone. It should be 10 times the unit ($size in my function) // the $show\_text parameter determines if a string will display under the barcode, this string will be the text version of the barcode. include('hash\_code\_128.php'); $bcstring = ''; $cnt = strlen($string) ; $quiet\_zone = 10\*$size-1; $pad\_top = 5; $pad\_bottom = 5; $ttl\_width = ($quiet\_zone\*2)+($size\*($cnt+3)\*11)+2; // include string length + start + stop + checksum + front and back quiet zones if($show\_text) { $pad\_text = 3; $font\_size = 5; $font\_left = $quiet\_zone; $font\_top = $pad\_text + $height; $ttl\_height = $pad\_top + $pad\_text + $font\_size + $pad\_bottom + $height; } else { $ttl\_height = $pad\_top + $pad\_bottom + $height; } $running\_checksum = 104; // START B hardcoded... $bcstring.=$b\_enc\['104'\]; // build the barcode for($i=0;$i<$cnt;$i++) { $position = $i+1; $key = array\_search($string\[$i\],$b\_chr,true); $bcstring.=$b\_enc\[$key\]; $running\_checksum+=($position\*$key); // echo "<p>pos: ".$position."<br>char: ".$string\[$i\]."<br>key: ".$key."<br&g