Factor out generating EAN bars
This commit is contained in:
parent
aee3fda3d1
commit
7b59a3ac2b
@ -182,6 +182,36 @@ function barcode_gen_ean_sum($ean)
|
|||||||
return (10 - ((3 * $esum + $osum) % 10)) % 10;
|
return (10 - ((3 * $esum + $osum) % 10)) % 10;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generate EAN bars
|
||||||
|
*
|
||||||
|
* @param string $ean EAN to encode
|
||||||
|
* @return string Encoded EAN
|
||||||
|
*/
|
||||||
|
function barcode_gen_ean_bars($ean)
|
||||||
|
{
|
||||||
|
$digits = array(3211, 2221, 2122, 1411, 1132, 1231, 1114, 1312, 1213, 3112);
|
||||||
|
$mirror = array("000000", "001011", "001101", "001110", "010011", "011001", "011100", "010101", "010110", "011010");
|
||||||
|
$guards = array("9a1a", "1a1a1", "a1a");
|
||||||
|
|
||||||
|
$line = $guards[0];
|
||||||
|
for ($i = 1; $i < 13; $i++) {
|
||||||
|
$str = $digits[$ean[$i]];
|
||||||
|
if ($i < 7 && $mirror[$ean[0]][$i - 1] == 1) {
|
||||||
|
$line .= strrev($str);
|
||||||
|
} else {
|
||||||
|
$line .= $str;
|
||||||
|
}
|
||||||
|
if ($i == 6) {
|
||||||
|
$line .= $guards[1];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
$line .= $guards[2];
|
||||||
|
|
||||||
|
return $line;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Encode EAN
|
* Encode EAN
|
||||||
*
|
*
|
||||||
@ -191,10 +221,6 @@ function barcode_gen_ean_sum($ean)
|
|||||||
*/
|
*/
|
||||||
function barcode_encode_ean($ean, $encoding = "EAN-13")
|
function barcode_encode_ean($ean, $encoding = "EAN-13")
|
||||||
{
|
{
|
||||||
$digits = array(3211, 2221, 2122, 1411, 1132, 1231, 1114, 1312, 1213, 3112);
|
|
||||||
$mirror = array("000000", "001011", "001101", "001110", "010011", "011001", "011100", "010101", "010110", "011010");
|
|
||||||
$guards = array("9a1a", "1a1a1", "a1a");
|
|
||||||
|
|
||||||
$ean = trim($ean);
|
$ean = trim($ean);
|
||||||
if (preg_match("/[^0-9]/i", $ean)) {
|
if (preg_match("/[^0-9]/i", $ean)) {
|
||||||
return array("error"=>"Invalid encoding/code. encoding=".$encoding." code=".$ean." (not a numeric)", "text"=>"Invalid encoding/code. encoding=".$encoding." code=".$ean." (not a numeric)");
|
return array("error"=>"Invalid encoding/code. encoding=".$encoding." code=".$ean." (not a numeric)", "text"=>"Invalid encoding/code. encoding=".$encoding." code=".$ean." (not a numeric)");
|
||||||
@ -215,19 +241,7 @@ function barcode_encode_ean($ean, $encoding = "EAN-13")
|
|||||||
$ean = substr($ean, 0, 12);
|
$ean = substr($ean, 0, 12);
|
||||||
$eansum = barcode_gen_ean_sum($ean);
|
$eansum = barcode_gen_ean_sum($ean);
|
||||||
$ean .= $eansum;
|
$ean .= $eansum;
|
||||||
$line = $guards[0];
|
$bars = barcode_gen_ean_bars($ean);
|
||||||
for ($i = 1; $i < 13; $i++) {
|
|
||||||
$str = $digits[$ean[$i]];
|
|
||||||
if ($i < 7 && $mirror[$ean[0]][$i - 1] == 1) {
|
|
||||||
$line .= strrev($str);
|
|
||||||
} else {
|
|
||||||
$line .= $str;
|
|
||||||
}
|
|
||||||
if ($i == 6) {
|
|
||||||
$line .= $guards[1];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
$line .= $guards[2];
|
|
||||||
|
|
||||||
/* create text */
|
/* create text */
|
||||||
$pos = 0;
|
$pos = 0;
|
||||||
@ -249,7 +263,7 @@ function barcode_encode_ean($ean, $encoding = "EAN-13")
|
|||||||
return array(
|
return array(
|
||||||
"error" => '',
|
"error" => '',
|
||||||
"encoding" => $encoding,
|
"encoding" => $encoding,
|
||||||
"bars" => $line,
|
"bars" => $bars,
|
||||||
"text" => $text
|
"text" => $text
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user