Source for file barcodes.php
Documentation is available at barcodes.php
//============================================================+
// File name : barcodes.php
// Last Update : 2009-08-26
// License : GNU LGPL (http://www.gnu.org/copyleft/lesser.html)
// ----------------------------------------------------------------------------
// Copyright (C) 2008-2009 Nicola Asuni - Tecnick.com S.r.l.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 2.1 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
// You should have received a copy of the GNU Lesser General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
// See LICENSE.TXT file for more information.
// ----------------------------------------------------------------------------
// Description : PHP class to creates array representations for
// common 1D barcodes to be used with TCPDF.
//============================================================+
* PHP class to creates array representations for common 1D barcodes to be used with TCPDF.
* @package com.tecnick.tcpdf
* @abstract Functions for generating string representation of common 1D barcodes.
* @copyright 2008-2009 Nicola Asuni - Tecnick.com S.r.l (www.tecnick.com) Via Della Pace, 11 - 09044 - Quartucciu (CA) - ITALY - www.tecnick.com - info@tecnick.com
* @link http://www.tcpdf.org
* @license http://www.gnu.org/copyleft/lesser.html LGPL
* PHP class to creates array representations for common 1D barcodes to be used with TCPDF (http://www.tcpdf.org).<br>
* @package com.tecnick.tcpdf
* @link http://www.tcpdf.org
* @license http://www.gnu.org/copyleft/lesser.html LGPL
* @var array representation of barcode.
* This is the class constructor.
* Return an array representations for common 1D barcodes:<ul>
* <li>$arrcode['code'] code to be printed on text label</li>
* <li>$arrcode['maxh'] max bar height</li>
* <li>$arrcode['maxw'] max bar width</li>
* <li>$arrcode['bcode'][$k] single bar or space in $k position</li>
* <li>$arrcode['bcode'][$k]['t'] bar type: true = bar, false = space.</li>
* <li>$arrcode['bcode'][$k]['w'] bar width in units.</li>
* <li>$arrcode['bcode'][$k]['h'] bar height in units.</li>
* <li>$arrcode['bcode'][$k]['p'] bar top position (0 = top, 1 = middle)</li></ul>
* @param string $code code to print
* @param string $type type of barcode: <ul><li>C39 : CODE 39 - ANSI MH10.8M-1983 - USD-3 - 3 of 9.</li><li>C39+ : CODE 39 with checksum</li><li>C39E : CODE 39 EXTENDED</li><li>C39E+ : CODE 39 EXTENDED + CHECKSUM</li><li>C93 : CODE 93 - USS-93</li><li>S25 : Standard 2 of 5</li><li>S25+ : Standard 2 of 5 + CHECKSUM</li><li>I25 : Interleaved 2 of 5</li><li>I25+ : Interleaved 2 of 5 + CHECKSUM</li><li>C128A : CODE 128 A</li><li>C128B : CODE 128 B</li><li>C128C : CODE 128 C</li><li>EAN2 : 2-Digits UPC-Based Extention</li><li>EAN5 : 5-Digits UPC-Based Extention</li><li>EAN8 : EAN 8</li><li>EAN13 : EAN 13</li><li>UPCA : UPC-A</li><li>UPCE : UPC-E</li><li>MSI : MSI (Variation of Plessey code)</li><li>MSI+ : MSI + CHECKSUM (modulo 11)</li><li>POSTNET : POSTNET</li><li>PLANET : PLANET</li><li>RMS4CC : RMS4CC (Royal Mail 4-state Customer Code) - CBC (Customer Bar Code)</li><li>KIX : KIX (Klant index - Customer index)</li><li>IMB: Intelligent Mail Barcode - Onecode - USPS-B-3200</li><li>CODABAR : CODABAR</li><li>CODE11 : CODE 11</li><li>PHARMA : PHARMACODE</li><li>PHARMA2T : PHARMACODE TWO-TRACKS</li></ul>
* Return an array representations of barcode.
* @param string $code code to print
* @param string $type type of barcode: <ul><li>C39 : CODE 39 - ANSI MH10.8M-1983 - USD-3 - 3 of 9.</li><li>C39+ : CODE 39 with checksum</li><li>C39E : CODE 39 EXTENDED</li><li>C39E+ : CODE 39 EXTENDED + CHECKSUM</li><li>C93 : CODE 93 - USS-93</li><li>S25 : Standard 2 of 5</li><li>S25+ : Standard 2 of 5 + CHECKSUM</li><li>I25 : Interleaved 2 of 5</li><li>I25+ : Interleaved 2 of 5 + CHECKSUM</li><li>C128A : CODE 128 A</li><li>C128B : CODE 128 B</li><li>C128C : CODE 128 C</li><li>EAN2 : 2-Digits UPC-Based Extention</li><li>EAN5 : 5-Digits UPC-Based Extention</li><li>EAN8 : EAN 8</li><li>EAN13 : EAN 13</li><li>UPCA : UPC-A</li><li>UPCE : UPC-E</li><li>MSI : MSI (Variation of Plessey code)</li><li>MSI+ : MSI + CHECKSUM (modulo 11)</li><li>POSTNET : POSTNET</li><li>PLANET : PLANET</li><li>RMS4CC : RMS4CC (Royal Mail 4-state Customer Code) - CBC (Customer Bar Code)</li><li>KIX : KIX (Klant index - Customer index)</li><li>IMB: Intelligent Mail Barcode - Onecode - USPS-B-3200</li><li>CODABAR : CODABAR</li><li>CODE11 : CODE 11</li><li>PHARMA : PHARMACODE</li><li>PHARMA2T : PHARMACODE TWO-TRACKS</li></ul>
case 'C39': { // CODE 39 - ANSI MH10.8M-1983 - USD-3 - 3 of 9.
case 'C39+': { // CODE 39 with checksum
case 'C39E': { // CODE 39 EXTENDED
case 'C39E+': { // CODE 39 EXTENDED + CHECKSUM
case 'C93': { // CODE 93 - USS-93
case 'S25': { // Standard 2 of 5
case 'S25+': { // Standard 2 of 5 + CHECKSUM
case 'I25': { // Interleaved 2 of 5
case 'I25+': { // Interleaved 2 of 5 + CHECKSUM
case 'C128A': { // CODE 128 A
case 'C128B': { // CODE 128 B
case 'C128C': { // CODE 128 C
case 'EAN2': { // 2-Digits UPC-Based Extention
case 'EAN5': { // 5-Digits UPC-Based Extention
case 'EAN13': { // EAN 13
case 'MSI': { // MSI (Variation of Plessey code)
case 'MSI+': { // MSI + CHECKSUM (modulo 11)
case 'POSTNET': { // POSTNET
case 'PLANET': { // PLANET
case 'RMS4CC': { // RMS4CC (Royal Mail 4-state Customer Code) - CBC (Customer Bar Code)
case 'KIX': { // KIX (Klant index - Customer index)
case 'IMB': { // IMB - Intelligent Mail Barcode - Onecode - USPS-B-3200
case 'CODABAR': { // CODABAR
case 'CODE11': { // CODE 11
case 'PHARMA': { // PHARMACODE
case 'PHARMA2T': { // PHARMACODE TWO-TRACKS
* CODE 39 - ANSI MH10.8M-1983 - USD-3 - 3 of 9.
* General-purpose code in very wide use world-wide
* @param string $code code to represent.
* @param boolean $checksum if true add a checksum to the code
* @return array barcode representation.
protected function barcode_code39($code, $extended= false, $checksum= false) {
// add start and stop codes
$bararray = array('code' => $code, 'maxw' => 0, 'maxh' => 1, 'bcode' => array());
for ($i = 0; $i < $clen; ++ $i) {
if(!isset ($chr[$char])) {
for ($j = 0; $j < 9; ++ $j) {
$bararray['bcode'][$k] = array('t' => $t, 'w' => $w, 'h' => 1, 'p' => 0);
$bararray['bcode'][$k] = array('t' => false, 'w' => 1, 'h' => 1, 'p' => 0);
* Encode a string to be used for CODE 39 Extended mode.
* @param string $code code to represent.
* @return encoded string.
chr(0) => '%U', chr(1) => '$A', chr(2) => '$B', chr(3) => '$C',
chr(4) => '$D', chr(5) => '$E', chr(6) => '$F', chr(7) => '$G',
chr(8) => '$H', chr(9) => '$I', chr(10) => '$J', chr(11) => '£K',
chr(12) => '$L', chr(13) => '$M', chr(14) => '$N', chr(15) => '$O',
chr(16) => '$P', chr(17) => '$Q', chr(18) => '$R', chr(19) => '$S',
chr(20) => '$T', chr(21) => '$U', chr(22) => '$V', chr(23) => '$W',
chr(24) => '$X', chr(25) => '$Y', chr(26) => '$Z', chr(27) => '%A',
chr(28) => '%B', chr(29) => '%C', chr(30) => '%D', chr(31) => '%E',
chr(32) => ' ', chr(33) => '/A', chr(34) => '/B', chr(35) => '/C',
chr(36) => '/D', chr(37) => '/E', chr(38) => '/F', chr(39) => '/G',
chr(40) => '/H', chr(41) => '/I', chr(42) => '/J', chr(43) => '/K',
chr(44) => '/L', chr(45) => '-', chr(46) => '.', chr(47) => '/O',
chr(48) => '0', chr(49) => '1', chr(50) => '2', chr(51) => '3',
chr(52) => '4', chr(53) => '5', chr(54) => '6', chr(55) => '7',
chr(56) => '8', chr(57) => '9', chr(58) => '/Z', chr(59) => '%F',
chr(60) => '%G', chr(61) => '%H', chr(62) => '%I', chr(63) => '%J',
chr(64) => '%V', chr(65) => 'A', chr(66) => 'B', chr(67) => 'C',
chr(68) => 'D', chr(69) => 'E', chr(70) => 'F', chr(71) => 'G',
chr(72) => 'H', chr(73) => 'I', chr(74) => 'J', chr(75) => 'K',
chr(76) => 'L', chr(77) => 'M', chr(78) => 'N', chr(79) => 'O',
chr(80) => 'P', chr(81) => 'Q', chr(82) => 'R', chr(83) => 'S',
chr(84) => 'T', chr(85) => 'U', chr(86) => 'V', chr(87) => 'W',
chr(88) => 'X', chr(89) => 'Y', chr(90) => 'Z', chr(91) => '%K',
chr(92) => '%L', chr(93) => '%M', chr(94) => '%N', chr(95) => '%O',
chr(96) => '%W', chr(97) => '+A', chr(98) => '+B', chr(99) => '+C',
chr(100) => '+D', chr(101) => '+E', chr(102) => '+F', chr(103) => '+G',
chr(104) => '+H', chr(105) => '+I', chr(106) => '+J', chr(107) => '+K',
chr(108) => '+L', chr(109) => '+M', chr(110) => '+N', chr(111) => '+O',
chr(112) => '+P', chr(113) => '+Q', chr(114) => '+R', chr(115) => '+S',
chr(116) => '+T', chr(117) => '+U', chr(118) => '+V', chr(119) => '+W',
chr(120) => '+X', chr(121) => '+Y', chr(122) => '+Z', chr(123) => '%P',
chr(124) => '%Q', chr(125) => '%R', chr(126) => '%S', chr(127) => '%T');
for ($i = 0 ; $i < $clen; ++ $i) {
if (ord($code{$i}) > 127) {
$code_ext .= $encode[$code{$i}];
* Calculate CODE 39 checksum (modulo 43).
* @param string $code code to represent.
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K',
'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V',
'W', 'X', 'Y', 'Z', '-', '.', ' ', '$', '/', '+', '%');
for ($i = 0 ; $i < $clen; ++ $i) {
* Compact code similar to Code 39
* @param string $code code to represent.
* @param boolean $checksum if true add a checksum to the code
* @return array barcode representation.
$chr[128] = '121221'; // ($)
$chr[129] = '311121'; // (/)
$chr[130] = '122211'; // (+)
$chr[131] = '312111'; // (%)
chr(48) => '0', chr(49) => '1', chr(50) => '2', chr(51) => '3',
chr(52) => '4', chr(53) => '5', chr(54) => '6', chr(55) => '7',
chr(68) => 'D', chr(69) => 'E', chr(70) => 'F', chr(71) => 'G',
chr(72) => 'H', chr(73) => 'I', chr(74) => 'J', chr(75) => 'K',
chr(76) => 'L', chr(77) => 'M', chr(78) => 'N', chr(79) => 'O',
chr(80) => 'P', chr(81) => 'Q', chr(82) => 'R', chr(83) => 'S',
chr(84) => 'T', chr(85) => 'U', chr(86) => 'V', chr(87) => 'W',
for ($i = 0 ; $i < $clen; ++ $i) {
if (ord($code{$i}) > 127) {
$code_ext .= $encode[$code{$i}];
// add start and stop codes
$bararray = array('code' => $code, 'maxw' => 0, 'maxh' => 1, 'bcode' => array());
for ($i = 0; $i < $clen; ++ $i) {
if(!isset ($chr[$char])) {
for ($j = 0; $j < 6; ++ $j) {
$bararray['bcode'][$k] = array('t' => $t, 'w' => $w, 'h' => 1, 'p' => 0);
$bararray['bcode'][$k] = array('t' => true, 'w' => 1, 'h' => 1, 'p' => 0);
* Calculate CODE 93 checksum (modulo 47).
* @param string $code code to represent.
* @return string checksum code.
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K',
'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V',
'W', 'X', 'Y', 'Z', '-', '.', ' ', '$', '/', '+', '%');
// translate special characters
// calculate check digit C
for ($i = ($len - 1); $i >= 0; -- $i) {
// calculate check digit K
for ($i = $len; $i >= 0; -- $i) {
* Checksum for standard 2 of 5 barcodes.
* @param string $code code to process.
for ($i = 0; $i < $len; $i+= 2) {
for ($i = 1; $i < $len; $i+= 2) {
* Variation of Plessey code, with similar applications
* Contains digits (0 to 9) and encodes the data only in the width of bars.
* @param string $code code to represent.
* @param boolean $checksum if true add a checksum to the code (modulo 11)
* @return array barcode representation.
protected function barcode_msi($code, $checksum= false) {
$chr['0'] = '100100100100';
$chr['1'] = '100100100110';
$chr['2'] = '100100110100';
$chr['3'] = '100100110110';
$chr['4'] = '100110100100';
$chr['5'] = '100110100110';
$chr['6'] = '100110110100';
$chr['7'] = '100110110110';
$chr['8'] = '110100100100';
$chr['9'] = '110100100110';
$chr['A'] = '110100110100';
$chr['B'] = '110100110110';
$chr['C'] = '110110100100';
$chr['D'] = '110110100110';
$chr['E'] = '110110110100';
$chr['F'] = '110110110110';
for ($i = ($clen - 1); $i >= 0; -- $i) {
$check += (hexdec($code{$i}) * $p);
$seq = '110'; // left guard
for ($i = 0; $i < $clen; ++ $i) {
if (!isset ($chr[$digit])) {
$seq .= '1001'; // right guard
$bararray = array('code' => $code, 'maxw' => 0, 'maxh' => 1, 'bcode' => array());
* Standard 2 of 5 barcodes.
* Used in airline ticket marking, photofinishing
* Contains digits (0 to 9) and encodes the data only in the width of bars.
* @param string $code code to represent.
* @param boolean $checksum if true add a checksum to the code
* @return array barcode representation.
protected function barcode_s25($code, $checksum= false) {
$chr['0'] = '10101110111010';
$chr['1'] = '11101010101110';
$chr['2'] = '10111010101110';
$chr['3'] = '11101110101010';
$chr['4'] = '10101110101110';
$chr['5'] = '11101011101010';
$chr['6'] = '10111011101010';
$chr['7'] = '10101011101110';
$chr['8'] = '10101110111010';
$chr['9'] = '10111010111010';
if((strlen($code) % 2) != 0) {
// add leading zero if code-length is odd
for ($i = 0; $i < $clen; ++ $i) {
if (!isset ($chr[$digit])) {
$bararray = array('code' => $code, 'maxw' => 0, 'maxh' => 1, 'bcode' => array());
* Convert binary barcode sequence to TCPDF barcode array
* @param string $seq barcode as binary sequence
* òparam array $bararray TCPDF barcode array to fill up
* @return array barcode representation.
for ($i = 0; $i < $len; ++ $i) {
if (($i == ($len - 1)) OR (($i < ($len - 1)) AND ($seq{$i} != $seq{($i+ 1)}))) {
$bararray['bcode'][$k] = array('t' => $t, 'w' => $w, 'h' => 1, 'p' => 0);
* Interleaved 2 of 5 barcodes.
* Compact numeric code, widely used in industry, air cargo
* Contains digits (0 to 9) and encodes the data in the width of both bars and spaces.
* @param string $code code to represent.
* @param boolean $checksum if true add a checksum to the code
* @return array barcode representation.
protected function barcode_i25($code, $checksum= false) {
if((strlen($code) % 2) != 0) {
// add leading zero if code-length is odd
// add start and stop codes
$bararray = array('code' => $code, 'maxw' => 0, 'maxh' => 1, 'bcode' => array());
for ($i = 0; $i < $clen; $i = ($i + 2)) {
$char_space = $code{$i+ 1};
if((!isset ($chr[$char_bar])) OR (!isset ($chr[$char_space]))) {
// create a bar-space sequence
$chrlen = strlen($chr[$char_bar]);
for ($s = 0; $s < $chrlen; $s++ ){
$seq .= $chr[$char_bar]{$s} . $chr[$char_space]{$s};
for ($j = 0; $j < $seqlen; ++ $j) {
$bararray['bcode'][$k] = array('t' => $t, 'w' => $w, 'h' => 1, 'p' => 0);
* Very capable code, excellent density, high reliability; in very wide use world-wide
* @param string $code code to represent.
* @param string $type barcode type: A, B or C
* @return array barcode representation.
'211412', /* 103 START A */
'211214', /* 104 START B */
'211232', /* 105 START C */
$keys = ' !"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_';
for ($i = 0; $i < 32; ++ $i) {
$keys = ' !"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~'. chr(127);
if ((strlen($code) % 2) != 0) {
// The length of barcode value must be even ($code). You must pad the number with zeros
for ($i = 0; $i <= 99; ++ $i) {
for ($i = 0; $i < $hclen; ++ $i) {
$new_code .= chr(intval($code{(2 * $i)}. $code{(2 * $i + 1)}));
// calculate check character
for ($i = 0; $i < $clen; ++ $i) {
$sum += (strpos($keys, $code{$i}) * ($i+ 1));
// add start, check and stop codes
$bararray = array('code' => $code, 'maxw' => 0, 'maxh' => 1, 'bcode' => array());
for ($i = 0; $i < $len; ++ $i) {
$ck = strpos($keys, $code{$i});
if (($i == 0) OR ($i > ($len- 4))) {
$char_num = ord($code{$i});
} elseif(($ck >= 0) AND isset ($chr[$ck])) {
for ($j = 0; $j < 6; ++ $j) {
$bararray['bcode'][$k] = array('t' => $t, 'w' => $w, 'h' => 1, 'p' => 0);
* EAN13 and UPC-A barcodes.
* EAN13: European Article Numbering international retail product code
* UPC-A: Universal product code seen on almost all retail products in the USA and Canada
* UPC-E: Short version of UPC symbol
* @param string $code code to represent.
* @param string $len barcode type: 6 = UPC-E, 8 = EAN8, 13 = EAN13, 12 = UPC-A
* @return array barcode representation.
$upce = true; // UPC-E mode
$code = str_pad($code, $data_len, '0', STR_PAD_LEFT);
for ($i = 1; $i < $data_len; $i+= 2) {
for ($i = 0; $i < $data_len; $i+= 2) {
$r = ($sum_a + $sum_b) % 10;
if ($code_len == $data_len) {
} elseif ($r !== intval($code{$data_len})) {
// convert UPC-A to UPC-E
if (($tmp == '000') OR ($tmp == '100') OR ($tmp == '200')) {
// manufacturer code ends in 000, 100, or 200
// manufacturer code ends in 00
// manufacturer code ends in 0
// manufacturer code does not end in zero
'A'=> array( // left odd parity
'B'=> array( // left even parity
'0'=> array('A','A','A','A','A','A'),
'1'=> array('A','A','B','A','B','B'),
'2'=> array('A','A','B','B','A','B'),
'3'=> array('A','A','B','B','B','A'),
'4'=> array('A','B','A','A','B','B'),
'5'=> array('A','B','B','A','A','B'),
'6'=> array('A','B','B','B','A','A'),
'7'=> array('A','B','A','B','A','B'),
'8'=> array('A','B','A','B','B','A'),
'9'=> array('A','B','B','A','B','A')
$upce_parities = array();
$upce_parities[0] = array(
'0'=> array('B','B','B','A','A','A'),
'1'=> array('B','B','A','B','A','A'),
'2'=> array('B','B','A','A','B','A'),
'3'=> array('B','B','A','A','A','B'),
'4'=> array('B','A','B','B','A','A'),
'5'=> array('B','A','A','B','B','A'),
'6'=> array('B','A','A','A','B','B'),
'7'=> array('B','A','B','A','B','A'),
'8'=> array('B','A','B','A','A','B'),
'9'=> array('B','A','A','B','A','B')
$upce_parities[1] = array(
'0'=> array('A','A','A','B','B','B'),
'1'=> array('A','A','B','A','B','B'),
'2'=> array('A','A','B','B','A','B'),
'3'=> array('A','A','B','B','B','A'),
'4'=> array('A','B','A','A','B','B'),
'5'=> array('A','B','B','A','A','B'),
'6'=> array('A','B','B','B','A','A'),
'7'=> array('A','B','A','B','A','B'),
'8'=> array('A','B','A','B','B','A'),
'9'=> array('A','B','B','A','B','A')
$seq = '101'; // left guard bar
$bararray = array('code' => $upce_code, 'maxw' => 0, 'maxh' => 1, 'bcode' => array());
$p = $upce_parities[$code{1}][$r];
for ($i = 0; $i < 6; ++ $i) {
$seq .= $codes[$p[$i]][$upce_code{$i}];
$seq .= '010101'; // right guard bar
$bararray = array('code' => $code, 'maxw' => 0, 'maxh' => 1, 'bcode' => array());
$half_len = ceil($len / 2);
for ($i = 0; $i < $half_len; ++ $i) {
$seq .= $codes['A'][$code{$i}];
$p = $parities[$code{0}];
for ($i = 1; $i < $half_len; ++ $i) {
$seq .= $codes[$p[$i- 1]][$code{$i}];
$seq .= '01010'; // center guard bar
for ($i = $half_len; $i < $len; ++ $i) {
$seq .= $codes['C'][$code{$i}];
$seq .= '101'; // right guard bar
for ($i = 0; $i < $clen; ++ $i) {
if (($i == ($clen - 1)) OR (($i < ($clen - 1)) AND ($seq{$i} != $seq{($i+ 1)}))) {
$bararray['bcode'][$k] = array('t' => $t, 'w' => $w, 'h' => 1, 'p' => 0);
* 2-Digit Ext.: Used to indicate magazines and newspaper issue numbers
* 5-Digit Ext.: Used to mark suggested retail price of books
* @param string $code code to represent.
* @param string $len barcode type: 2 = 2-Digit, 5 = 5-Digit
* @return array barcode representation.
$code = str_pad($code, $len, '0', STR_PAD_LEFT);
$r = (3 * ($code{0} + $code{2} + $code{4})) + (9 * ($code{1} + $code{3}));
'A'=> array( // left odd parity
'B'=> array( // left even parity
'0'=> array('B','B','A','A','A'),
'1'=> array('B','A','B','A','A'),
'2'=> array('B','A','A','B','A'),
'3'=> array('B','A','A','A','B'),
'4'=> array('A','B','B','A','A'),
'5'=> array('A','A','B','B','A'),
'6'=> array('A','A','A','B','B'),
'7'=> array('A','B','A','B','A'),
'8'=> array('A','B','A','A','B'),
'9'=> array('A','A','B','A','B')
$p = $parities[$len][$r];
$seq = '1011'; // left guard bar
$seq .= $codes[$p[0]][$code{0}];
for ($i = 1; $i < $len; ++ $i) {
$seq .= '01'; // separator
$seq .= $codes[$p[$i]][$code{$i}];
$bararray = array('code' => $code, 'maxw' => 0, 'maxh' => 1, 'bcode' => array());
* POSTNET and PLANET barcodes.
* Used by U.S. Postal Service for automated mail sorting
* @param string $code zip code to represent. Must be a string containing a zip code of the form DDDDD or DDDDD-DDDD.
* @param boolean $planet if true print the PLANET barcode, otherwise print POSTNET
* @return array barcode representation.
$bararray = array('code' => $code, 'maxw' => 0, 'maxh' => 2, 'bcode' => array());
for ($i = 0; $i < $len; ++ $i) {
$bararray['bcode'][$k++ ] = array('t' => 1, 'w' => 1, 'h' => 2, 'p' => 0);
$bararray['bcode'][$k++ ] = array('t' => 0, 'w' => 1, 'h' => 2, 'p' => 0);
for ($i = 0; $i < $len; ++ $i) {
for ($j = 0; $j < 5; ++ $j) {
$h = $barlen[$code{$i}][$j];
$bararray['bcode'][$k++ ] = array('t' => 1, 'w' => 1, 'h' => $h, 'p' => $p);
$bararray['bcode'][$k++ ] = array('t' => 0, 'w' => 1, 'h' => 2, 'p' => 0);
$bararray['bcode'][$k++ ] = array('t' => 1, 'w' => 1, 'h' => 2, 'p' => 0);
* RMS4CC (Royal Mail 4-state Customer Code) - CBC (Customer Bar Code) - KIX (Klant index - Customer index)
* RM4SCC is the name of the barcode symbology used by the Royal Mail for its Cleanmail service.
* @param string $code code to print
* @param boolean $kix if true prints the KIX variation (doesn't use the start and end symbols, and the checksum) - in this case the house number must be sufficed with an X and placed at the end of the code.
* @return array barcode representation.
$bararray = array('code' => $code, 'maxw' => 0, 'maxh' => 3, 'bcode' => array());
// table for checksum calculation (row,col)
for ($i = 0; $i < $len; ++ $i) {
$row += $checktable[$code{$i}][0];
$col += $checktable[$code{$i}][1];
$bararray['bcode'][$k++ ] = array('t' => 1, 'w' => 1, 'h' => 2, 'p' => 0);
$bararray['bcode'][$k++ ] = array('t' => 0, 'w' => 1, 'h' => 2, 'p' => 0);
for ($i = 0; $i < $len; ++ $i) {
for ($j = 0; $j < 4; ++ $j) {
switch ($barmode[$code{$i}][$j]) {
$bararray['bcode'][$k++ ] = array('t' => 1, 'w' => 1, 'h' => $h, 'p' => $p);
$bararray['bcode'][$k++ ] = array('t' => 0, 'w' => 1, 'h' => 2, 'p' => 0);
$bararray['bcode'][$k++ ] = array('t' => 1, 'w' => 1, 'h' => 3, 'p' => 0);
* Older code often used in library systems, sometimes in blood banks
* @param string $code code to represent.
* @return array barcode representation.
$bararray = array('code' => $code, 'maxw' => 0, 'maxh' => 1, 'bcode' => array());
for ($i = 0; $i < $len; ++ $i) {
if (!isset ($chr[$code{$i}])) {
for ($j = 0; $j < 8; ++ $j) {
$bararray['bcode'][$k] = array('t' => $t, 'w' => $w, 'h' => 1, 'p' => 0);
* Used primarily for labeling telecommunications equipment
* @param string $code code to represent.
* @return array barcode representation.
$bararray = array('code' => $code, 'maxw' => 0, 'maxh' => 1, 'bcode' => array());
// calculate check digit C
for ($i = ($len - 1); $i >= 0; -- $i) {
// calculate check digit K
for ($i = $len; $i >= 0; -- $i) {
for ($i = 0; $i < $len; ++ $i) {
if (!isset ($chr[$code{$i}])) {
for ($j = 0; $j < 6; ++ $j) {
$bararray['bcode'][$k] = array('t' => $t, 'w' => $w, 'h' => 1, 'p' => 0);
* Contains digits (0 to 9)
* @param string $code code to represent.
* @return array barcode representation.
$bararray = array('code' => $code, 'maxw' => 0, 'maxh' => 1, 'bcode' => array());
* Contains digits (0 to 9)
* @param string $code code to represent.
* @return array barcode representation.
$bararray = array('code' => $code, 'maxw' => 0, 'maxh' => 2, 'bcode' => array());
for ($i = 0; $i < $len; ++ $i) {
$bararray['bcode'][$k++ ] = array('t' => 1, 'w' => 1, 'h' => $h, 'p' => $p);
$bararray['bcode'][$k++ ] = array('t' => 0, 'w' => 1, 'h' => 2, 'p' => 0);
unset ($bararray['bcode'][($k - 1)]);
* IMB - Intelligent Mail Barcode - Onecode - USPS-B-3200
* (requires PHP bcmath extension)
* Intelligent Mail barcode is a 65-bar code for use on mail in the United States.
* The fields are described as follows:<ul><li>The Barcode Identifier shall be assigned by USPS to encode the presort identification that is currently printed in human readable form on the optional endorsement line (OEL) as well as for future USPS use. This shall be two digits, with the second digit in the range of 0–4. The allowable encoding ranges shall be 00–04, 10–14, 20–24, 30–34, 40–44, 50–54, 60–64, 70–74, 80–84, and 90–94.</li><li>The Service Type Identifier shall be assigned by USPS for any combination of services requested on the mailpiece. The allowable encoding range shall be 000http://it2.php.net/manual/en/function.dechex.php–999. Each 3-digit value shall correspond to a particular mail class with a particular combination of service(s). Each service program, such as OneCode Confirm and OneCode ACS, shall provide the list of Service Type Identifier values.</li><li>The Mailer or Customer Identifier shall be assigned by USPS as a unique, 6 or 9 digit number that identifies a business entity. The allowable encoding range for the 6 digit Mailer ID shall be 000000- 899999, while the allowable encoding range for the 9 digit Mailer ID shall be 900000000-999999999.</li><li>The Serial or Sequence Number shall be assigned by the mailer for uniquely identifying and tracking mailpieces. The allowable encoding range shall be 000000000–999999999 when used with a 6 digit Mailer ID and 000000-999999 when used with a 9 digit Mailer ID. e. The Delivery Point ZIP Code shall be assigned by the mailer for routing the mailpiece. This shall replace POSTNET for routing the mailpiece to its final delivery point. The length may be 0, 5, 9, or 11 digits. The allowable encoding ranges shall be no ZIP Code, 00000–99999, 000000000–999999999, and 00000000000–99999999999.</li></ul>
* @param string $code code to print, separate the ZIP (routing code) from the rest using a minus char '-' (BarcodeID_ServiceTypeID_MailerID_SerialNumber-RoutingCode)
* @return array barcode representation.
$asc_chr = array(4,0,2,6,3,5,1,9,8,7,1,2,0,6,4,8,2,9,5,3,0,1,3,7,4,6,8,9,2,0,5,1,9,4,3,8,6,7,1,2,4,3,9,5,7,8,3,0,2,1,4,0,9,1,7,0,2,4,6,3,7,1,9,5,8);
$dsc_chr = array(7,1,9,5,8,0,2,4,6,3,5,8,9,7,3,0,6,1,7,4,6,8,9,2,5,1,7,5,4,3,8,7,6,0,2,5,4,9,3,0,1,6,8,2,0,4,5,9,6,7,5,2,6,3,8,5,1,9,8,7,4,0,2,6,3);
$asc_pos = array(3,0,8,11,1,12,8,11,10,6,4,12,2,7,9,6,7,9,2,8,4,0,12,7,10,9,0,7,10,5,7,9,6,8,2,12,1,4,2,0,1,5,4,6,12,1,0,9,4,7,5,10,2,6,9,11,2,12,6,7,5,11,0,3,2);
$dsc_pos = array(2,10,12,5,9,1,5,4,3,9,11,5,10,1,6,3,4,1,10,0,2,11,8,6,1,12,3,8,6,4,4,11,0,6,1,9,11,5,3,7,3,10,7,11,8,2,10,3,5,8,0,3,12,11,8,4,5,1,3,0,7,12,9,8,10);
$tracking_number = $code_arr[0];
if (isset ($code_arr[1])) {
$routing_code = $code_arr[1];
// Conversion of Routing Code
switch (strlen($routing_code)) {
$binary_code = bcadd($routing_code, '1');
$binary_code = bcadd($routing_code, '100001');
$binary_code = bcadd($routing_code, '1000100001');
$binary_code = bcmul($binary_code, 10);
$binary_code = bcadd($binary_code, $tracking_number{0});
$binary_code = bcmul($binary_code, 5);
$binary_code = bcadd($binary_code, $tracking_number{1});
$binary_code .= substr($tracking_number, 2, 18);
// convert to hexadecimal
$binary_code = str_pad($binary_code, 26, '0', STR_PAD_LEFT);
// convert string to array of bytes
$binary_code_arr = substr($binary_code_arr, 0, - 1);
$binary_code_arr = explode("\r", $binary_code_arr);
// calculate frame check sequence
// exclude first 2 bits from first byte
$binary_code_102bit = $first_byte. substr($binary_code, 2);
// convert binary data to codewords
$codewords[0] = bcmod($data, 636) * 2;
$data = bcdiv($data, 636);
for ($i = 1; $i < 9; ++ $i) {
$codewords[$i] = bcmod($data, 1365);
$data = bcdiv($data, 1365);
// generate lookup tables
// convert codewords to characters
foreach($codewords as $k => $val) {
$chrcode = $table5of13[$val];
$chrcode = $table2of13[($val - 1287)];
if (($fcs & $bitmask) > 0) {
$chrcode = ((~ $chrcode) & 8191);
$characters[] = $chrcode;
$bararray = array('code' => $code, 'maxw' => 0, 'maxh' => 3, 'bcode' => array());
for ($i = 0; $i < 65; ++ $i) {
$asc = (($characters[$asc_chr[$i]] & pow(2, $asc_pos[$i])) > 0);
$dsc = (($characters[$dsc_chr[$i]] & pow(2, $dsc_pos[$i])) > 0);
$bararray['bcode'][$k++ ] = array('t' => 1, 'w' => 1, 'h' => $h, 'p' => $p);
$bararray['bcode'][$k++ ] = array('t' => 0, 'w' => 1, 'h' => 2, 'p' => 0);
unset ($bararray['bcode'][($k - 1)]);
* Convert large integer number to hexadecimal representation.
* (requires PHP bcmath extension)
* @param string $number number to convert specified as a string
* @return string hexadecimal representation
$number = bcdiv($number, '16', 0);
* Convert large hexadecimal number to decimal representation (string).
* (requires PHP bcmath extension)
* @param string $hex hexadecimal number to convert specified as a string
* @return string hexadecimal representation
for($pos = ($len - 1); $pos >= 0; -- $pos) {
$bitval = bcmul($bitval, 16);
* Intelligent Mail Barcode calculation of Frame Check Sequence
* @param string $code_arr array of hexadecimal values (13 bytes holding 102 bits right justified).
* @return int 11 bit Frame Check Sequence as integer (decimal base)
$genpoly = 0x0F35; // generator polynomial
$fcs = 0x07FF; // Frame Check Sequence
// do most significant byte skipping the 2 most significant bits
$data = hexdec($code_arr[0]) << 5;
for ($bit = 2; $bit < 8; ++ $bit) {
if (($fcs ^ $data) & 0x400) {
$fcs = ($fcs << 1) ^ $genpoly;
for ($byte = 1; $byte < 13; ++ $byte) {
$data = hexdec($code_arr[$byte]) << 3;
for ($bit = 0; $bit < 8; ++ $bit) {
if (($fcs ^ $data) & 0x400) {
$fcs = ($fcs << 1) ^ $genpoly;
* Reverse unsigned short value
* @param int $num value to reversr
* @return int reversed value
for ($i = 0; $i < 16; ++ $i) {
* generate Nof13 tables used for Intelligent Mail Barcode
* @param int $n is the type of table: 2 for 2of13 table, 5 for 5of13table
* @param int $size size of table (78 for n=2 and 1287 for n=5)
* @return array requested table
$lli = 0; // LUT lower index
$lui = $size - 1; // LUT upper index
for ($count = 0; $count < 8192; ++ $count) {
for ($bit_index = 0; $bit_index < 13; ++ $bit_index) {
$bit_count += intval(($count & (1 << $bit_index)) != 0);
// if we don't have the right number of bits on, go on to the next value
// if the reverse is less than count, we have already visited this pair before
if ($reverse >= $count) {
// If count is symmetric, place it at the first free slot from the end of the list.
// Otherwise, place it at the first free slot from the beginning of the list AND place $reverse ath the next free slot from the beginning of the list
if ($reverse == $count) {
//============================================================+
//============================================================+
|