barcode_codes = $this->getCode128Map();
}
public function getBarcode($code, $widthFactor = 2, $height = 50, $foregroundColor = 'black')
{
$barcodeData = $this->getBarcodeData($code);
$bars = '';
$x = 0;
// Generate SVG bars
foreach (str_split($barcodeData['bars']) as $bar) {
$width = $widthFactor * (int)$bar;
if ($x % 2 == 0) { // 0, 2, 4... are black bars
$bars .= '';
}
$x += $width;
}
$svg = '';
return $svg;
}
private function getBarcodeData($code)
{
// For simplicity, we'll stick to Code Set B
$codeSet = 'B';
$data = '';
$sum = 104; // Start code B
$mult = 1;
foreach (str_split($code) as $char) {
$value = $this->getCharValue($codeSet, $char);
if ($value === null) {
// Fallback for unsupported characters, or switch sets (more complex)
// For now, we'll just skip them to avoid errors
continue;
}
$data .= str_pad($value, 2, '0', STR_PAD_LEFT);
$sum += ($value * $mult);
$mult++;
}
$checksum = $sum % 103;
$data .= str_pad($checksum, 2, '0', STR_PAD_LEFT);
// Start, data, checksum, stop
$full_code = '104' . $data . '106';
$bars = '';
for ($i = 0; $i < strlen($full_code); $i += 2) {
$index = (int)substr($full_code, $i, 2);
$bars .= $this->barcode_codes['bars'][$index];
}
return ['bars' => $bars, 'code' => $full_code];
}
private function getCharValue($set, $char)
{
if (isset($this->barcode_codes[$set][$char])) {
return $this->barcode_codes[$set][$char];
}
return null; // Character not in set
}
private function getCode128Map()
{
return [
'B' => [
' ' => 0, '!' => 1, '"' => 2, '#' => 3, '$' => 4, '%' => 5, '&' => 6, '\'' => 7,
'(' => 8, ')' => 9, '*' => 10, '+' => 11, ',' => 12, '-' => 13, '.' => 14, '/' => 15,
'0' => 16, '1' => 17, '2' => 18, '3' => 19, '4' => 20, '5' => 21, '6' => 22, '7' => 23,
'8' => 24, '9' => 25, ':' => 26, ';' => 27, '<' => 28, '=' => 29, '>' => 30, '?' => 31,
'@' => 32, 'A' => 33, 'B' => 34, 'C' => 35, 'D' => 36, 'E' => 37, 'F' => 38, 'G' => 39,
'H' => 40, 'I' => 41, 'J' => 42, 'K' => 43, 'L' => 44, 'M' => 45, 'N' => 46, 'O' => 47,
'P' => 48, 'Q' => 49, 'R' => 50, 'S' => 51, 'T' => 52, 'U' => 53, 'V' => 54, 'W' => 55,
'X' => 56, 'Y' => 57, 'Z' => 58, '[' => 59, '\\' => 60, ']' => 61, '^' => 62, '_' => 63
],
'bars' => [
'212222', '222122', '222221', '121223', '121322', '131222', '122213', '122312', '132212', '221213', '221312', '231212', '112232', '122132', '122231', '113222', '123122', '123221', '223211', '221132', '221231', '213212', '223112', '312131', '311222', '321122', '321221', '312212', '322112', '322211', '212123', '212321', '232121', '111323', '131123', '131321', '112313', '132113', '132311', '211313', '231113', '231311', '112133', '112331', '132131', '113123', '113321', '133121', '313121', '211331', '231131', '213113', '213311', '213131', '311123', '311321', '331121', '312113', '312311', '332111', '314111', '221411', '413111', '111224', '111421', '121124', '121421', '141122', '141221', '112214', '112412', '122114', '122411', '142112', '142211', '241211', '221114', '411122', '411221', '421121', '421211', '212141', '214121', '412121', '111143', '111341', '131141', '114113', '114311', '411113', '411311', '113141', '114131', '311141', '411131', '211412', '211214', '211232', '2331112'
]
];
}
}
?>