1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
| function Barcode($barcode)
{
//Ajoute des 0 si nécessaire
$len = 12;
$barcode=str_pad($barcode,$len-1,'0',STR_PAD_LEFT);
if($len==12)
$barcode='0'.$barcode;
//Ajoute ou teste le chiffre de contrôle
if(strlen($barcode)==12)
$barcode.=GetCheckDigit($barcode);
elseif(!TestCheckDigit($barcode))
die('erreur code barre');
//Convertit les chiffres en barres
$codes=array(
'A'=>array(
'0'=>'0001101','1'=>'0011001','2'=>'0010011','3'=>'0111101','4'=>'0100011',
'5'=>'0110001','6'=>'0101111','7'=>'0111011','8'=>'0110111','9'=>'0001011'),
'B'=>array(
'0'=>'0100111','1'=>'0110011','2'=>'0011011','3'=>'0100001','4'=>'0011101',
'5'=>'0111001','6'=>'0000101','7'=>'0010001','8'=>'0001001','9'=>'0010111'),
'C'=>array(
'0'=>'1110010','1'=>'1100110','2'=>'1101100','3'=>'1000010','4'=>'1011100',
'5'=>'1001110','6'=>'1010000','7'=>'1000100','8'=>'1001000','9'=>'1110100')
);
$parities=array(
'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')
);
$code='101';
$p=$parities[$barcode{0}];
for($i=1;$i<=6;$i++)
$code.=$codes[$p[$i-1]][$barcode{$i}];
$code.='01010';
for($i=7;$i<=12;$i++)
$code.=$codes['C'][$barcode{$i}];
$code.='101';
return $barcode;
} |
Partager