diff --git a/imprimir_etiquetas_termica.php b/imprimir_etiquetas_termica.php index f9c4191e..e477d550 100644 --- a/imprimir_etiquetas_termica.php +++ b/imprimir_etiquetas_termica.php @@ -16,7 +16,7 @@ if (empty($codes)) { Imprimir Etiquetas Térmicas @@ -137,8 +142,8 @@ if (empty($codes)) {
getBarcode($code, 1.0, 100, 'black', 0); + // Narrow bar: 0.25mm (2 dots), Height: 11.5mm, Quiet Zone: 4mm + echo $generator->getBarcode($code, 0.25, 11.5, 'black', 4); ?>
diff --git a/includes/barcode_generator.php b/includes/barcode_generator.php index bedd3119..6a90abea 100644 --- a/includes/barcode_generator.php +++ b/includes/barcode_generator.php @@ -1,5 +1,5 @@ barcode_codes = $this->getCode128Map(); } - public function getBarcode($code, $widthFactor = 1, $height = 50, $foregroundColor = 'black', $barReduction = 0) + public function getBarcode($code, $narrowBarWidth = 0.25, $height = 11.5, $foregroundColor = 'black', $quietZoneMM = 4, $type = 'C128') { - $barcodeData = $this->getBarcodeData($code); + $barcodeData = ($type === 'C39') ? $this->getBarcodeData39($code) : $this->getBarcodeData($code); if (!$barcodeData) return ''; $bars = ''; - $quietZone = 10; - $x = $quietZone; + $x = $quietZoneMM; $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 .= ''; + $width = $narrowBarWidth * $val; + if ($i % 2 == 0) { // Even indices are bars + $bars .= ''; } $x += $width; } - $totalWidth = $x + $quietZone; - $svg = ''; + $totalWidth = $x + $quietZoneMM; + // Using crispEdges for maximum sharpness on thermal printers + $svg = ''; $svg .= ''; $svg .= $bars; $svg .= ''; @@ -43,10 +41,9 @@ class BarcodeGenerator private function getBarcodeData($code) { - // Remove any accidental spaces and trim - $code = str_replace(' ', '', trim($code)); + $code = str_replace(' ', '', strtoupper(trim($code))); $len = strlen($code); - $indices = [104]; // Start B (Standard Set B) + $indices = [104]; // Start B for ($i = 0; $i < $len; $i++) { $char = $code[$i]; @@ -56,7 +53,6 @@ class BarcodeGenerator } } - // Checksum calculation $sum = $indices[0]; for ($i = 1; $i < count($indices); $i++) { $sum += ($indices[$i] * $i); @@ -75,6 +71,33 @@ class BarcodeGenerator return ['bars' => $bars]; } + private function getBarcodeData39($code) + { + $code = '*' . strtoupper(trim($code)) . '*'; + $map = [ + '0' => '111221211', '1' => '211211112', '2' => '112211112', '3' => '212211111', + '4' => '111221112', '5' => '211221111', '6' => '112221111', '7' => '111211212', + '8' => '211211211', '9' => '112211211', 'A' => '211112112', 'B' => '112112112', + 'C' => '212112111', 'D' => '111122112', 'E' => '211122111', 'F' => '112122111', + 'G' => '111112212', 'H' => '211112211', 'I' => '112112211', 'J' => '111122211', + 'K' => '211111122', 'L' => '112111122', 'M' => '212111121', 'N' => '111121122', + 'O' => '211121121', 'P' => '112121121', 'Q' => '111111222', 'R' => '211111221', + 'S' => '112111221', 'T' => '111121221', 'U' => '221111112', 'V' => '122111112', + 'W' => '222111111', 'X' => '121121112', 'Y' => '221121111', 'Z' => '122121111', + '-' => '121111212', '.' => '221111211', ' ' => '122111211', '*' => '121121211', + '$' => '121212111', '/' => '121211121', '+' => '121112121', '%' => '111212121' + ]; + + $bars = ''; + for ($i = 0; $i < strlen($code); $i++) { + $char = $code[$i]; + if (isset($map[$char])) { + $bars .= $map[$char] . '1'; // Add a narrow space between characters + } + } + return ['bars' => rtrim($bars, '1')]; + } + private function getCharValue($set, $char) { if (isset($this->barcode_codes[$set][$char])) { @@ -107,12 +130,12 @@ class BarcodeGenerator '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 + '314111', '221411', '431111', '111224', '111413', '121124', '121421', '141122', '141221', '112214', // 60-69 + '112412', '122114', '122411', '142112', '142211', '241211', '221114', '413111', '241112', '134111', // 70-79 + '111242', '121142', '121241', '114212', '124112', '124211', '411212', '421112', '421211', '212141', // 80-89 + '214121', '412121', '111143', '111341', '131141', '114113', '114311', '411113', '411311', '113141', // 90-99 + '114131', '311141', '411131', '211412', '211214', '211232', '2331112' // 100-106 ] ]; } -} +} \ No newline at end of file diff --git a/test_etiquetas.php b/test_etiquetas.php index 704166f9..14163fc0 100644 --- a/test_etiquetas.php +++ b/test_etiquetas.php @@ -1,16 +1,20 @@ - Prueba Etiquetas - Opción 3 (Finas) + Prueba de Etiquetas - REDPOS 4B-2054K