40097-vm/includes/barcode_generator.php
2026-05-21 21:32:30 +00:00

119 lines
5.3 KiB
PHP

<?php
// Simplified Barcode Generator for Code 128
// Generates SVG output.
class BarcodeGenerator
{
private $barcode_codes;
public function __construct()
{
$this->barcode_codes = $this->getCode128Map();
}
public function getBarcode($code, $widthFactor = 1, $height = 50, $foregroundColor = 'black', $barReduction = 0)
{
$barcodeData = $this->getBarcodeData($code);
if (!$barcodeData) return '';
$bars = '';
$quietZone = 10;
$x = $quietZone;
$barArray = str_split($barcodeData['bars']);
for ($i = 0; $i < count($barArray); $i++) {
$val = (int)$barArray[$i];
$width = $widthFactor * $val;
if ($i % 2 == 0) { // Even indices are bars, odd are spaces
// Apply bar reduction to increase white space between bars
$drawWidth = max(0.4, $width - $barReduction);
$bars .= '<rect x="' . $x . '" y="0" width="' . $drawWidth . '" height="' . $height . '" fill="' . $foregroundColor . '" />';
}
$x += $width;
}
$totalWidth = $x + $quietZone;
$svg = '<svg version="1.1" xmlns="http://www.w3.org/2000/svg" width="' . $totalWidth . '" height="' . $height . '" viewBox="0 0 ' . $totalWidth . ' ' . $height . '" shape-rendering="crispEdges">';
$svg .= '<rect width="' . $totalWidth . '" height="' . $height . '" fill="white" />';
$svg .= $bars;
$svg .= '</svg>';
return $svg;
}
private function getBarcodeData($code)
{
// Remove any accidental spaces and trim
$code = str_replace(' ', '', trim($code));
$len = strlen($code);
$indices = [104]; // Start B (Standard Set B)
for ($i = 0; $i < $len; $i++) {
$char = $code[$i];
$val = $this->getCharValue('B', $char);
if ($val !== null) {
$indices[] = $val;
}
}
// Checksum calculation
$sum = $indices[0];
for ($i = 1; $i < count($indices); $i++) {
$sum += ($indices[$i] * $i);
}
$checksum = $sum % 103;
$indices[] = $checksum;
$indices[] = 106; // Stop
$bars = '';
foreach ($indices as $index) {
if (isset($this->barcode_codes['bars'][$index])) {
$bars .= $this->barcode_codes['bars'][$index];
}
}
return ['bars' => $bars];
}
private function getCharValue($set, $char)
{
if (isset($this->barcode_codes[$set][$char])) {
return $this->barcode_codes[$set][$char];
}
return null;
}
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,
'`' => 64, 'a' => 65, 'b' => 66, 'c' => 67, 'd' => 68, 'e' => 69, 'f' => 70, 'g' => 71,
'h' => 72, 'i' => 73, 'j' => 74, 'k' => 75, 'l' => 76, 'm' => 77, 'n' => 78, 'o' => 79,
'p' => 80, 'q' => 81, 'r' => 82, 's' => 83, 't' => 84, 'u' => 85, 'v' => 86, 'w' => 87,
'x' => 88, 'y' => 89, 'z' => 90, '{' => 91, '|' => 92, '}' => 93, '~' => 94
],
'bars' => [
'212222', '222122', '222221', '121223', '121322', '131222', '122213', '122312', '132212', '221213', // 0-9
'221312', '231212', '112232', '122132', '122231', '113222', '123122', '123221', '223211', '221132', // 10-19
'221231', '213212', '223112', '312131', '311222', '321122', '321221', '312212', '322112', '322211', // 20-29
'212123', '212321', '232121', '111323', '131123', '131321', '112313', '132113', '132311', '211313', // 30-39
'231113', '231311', '112133', '112331', '132131', '113123', '113321', '133121', '313121', '211331', // 40-49
'231131', '213113', '213311', '213131', '311123', '311321', '331121', '312113', '312311', '332111', // 50-59
'314111', '221411', '413111', '111224', '111421', '121124', '121421', '141122', '141221', '112214', // 60-69
'112412', '122114', '122411', '142112', '142211', '241211', '221114', '411122', '411221', '421121', // 70-79
'421211', '212141', '214121', '412121', '111143', '111341', '131141', '114113', '114311', '411113', // 80-89
'411311', '113141', '114131', '311141', '411131', '232131', '233131', '132131', '113131', '311131', // 90-99
'311311', '331131', '313111', '211412', '211214', '211232', '2331112' // 100-106
]
];
}
}