From e7e17b7237e3b3ce45422377cd8a10a6e3e64f56 Mon Sep 17 00:00:00 2001 From: Philip Hofstetter Date: Fri, 6 Sep 2019 15:07:03 +0200 Subject: [PATCH 001/155] update for PHP 7.4 PHP 7.4 deprecates the use of {} for index access on array and strings. See https://wiki.php.net/rfc/deprecate_curly_braces_array_access --- src/BarcodeGenerator.php | 116 +++++++++++++++++++-------------------- 1 file changed, 58 insertions(+), 58 deletions(-) diff --git a/src/BarcodeGenerator.php b/src/BarcodeGenerator.php index 33e6fa0..8d93a3c 100644 --- a/src/BarcodeGenerator.php +++ b/src/BarcodeGenerator.php @@ -290,7 +290,7 @@ protected function barcode_code39($code, $extended = false, $checksum = false) $k = 0; $clen = strlen($code); for ($i = 0; $i < $clen; ++$i) { - $char = $code{$i}; + $char = $code[$i]; if ( ! isset($chr[$char])) { throw new InvalidCharacterException('Char ' . $char . ' is unsupported'); } @@ -300,7 +300,7 @@ protected function barcode_code39($code, $extended = false, $checksum = false) } else { $t = false; // space } - $w = $chr[$char]{$j}; + $w = $chr[$char][$j]; $bararray['bcode'][$k] = array('t' => $t, 'w' => $w, 'h' => 1, 'p' => 0); $bararray['maxw'] += $w; ++$k; @@ -456,10 +456,10 @@ protected function encode_code39_ext($code) $code_ext = ''; $clen = strlen($code); for ($i = 0; $i < $clen; ++$i) { - if (ord($code{$i}) > 127) { + if (ord($code[$i]) > 127) { throw new InvalidCharacterException('Only supports till char 127'); } - $code_ext .= $encode[$code{$i}]; + $code_ext .= $encode[$code[$i]]; } return $code_ext; @@ -522,7 +522,7 @@ protected function checksum_code39($code) $sum = 0; $codelength = strlen($code); for ($i = 0; $i < $codelength; ++$i) { - $k = array_keys($chars, $code{$i}); + $k = array_keys($chars, $code[$i]); $sum += $k[0]; } $j = ($sum % 43); @@ -723,10 +723,10 @@ protected function barcode_code93($code) $code_ext = ''; $clen = strlen($code); for ($i = 0; $i < $clen; ++$i) { - if (ord($code{$i}) > 127) { + if (ord($code[$i]) > 127) { throw new InvalidCharacterException('Only supports till char 127'); } - $code_ext .= $encode[$code{$i}]; + $code_ext .= $encode[$code[$i]]; } // checksum $code_ext .= $this->checksum_code93($code_ext); @@ -736,7 +736,7 @@ protected function barcode_code93($code) $k = 0; $clen = strlen($code); for ($i = 0; $i < $clen; ++$i) { - $char = ord($code{$i}); + $char = ord($code[$i]); if ( ! isset($chr[$char])) { throw new InvalidCharacterException('Char ' . $char . ' is unsupported'); } @@ -746,7 +746,7 @@ protected function barcode_code93($code) } else { $t = false; // space } - $w = $chr[$char]{$j}; + $w = $chr[$char][$j]; $bararray['bcode'][$k] = array('t' => $t, 'w' => $w, 'h' => 1, 'p' => 0); $bararray['maxw'] += $w; ++$k; @@ -823,7 +823,7 @@ protected function checksum_code93($code) $p = 1; $check = 0; for ($i = ($len - 1); $i >= 0; --$i) { - $k = array_keys($chars, $code{$i}); + $k = array_keys($chars, $code[$i]); $check += ($k[0] * $p); ++$p; if ($p > 20) { @@ -837,7 +837,7 @@ protected function checksum_code93($code) $p = 1; $check = 0; for ($i = $len; $i >= 0; --$i) { - $k = array_keys($chars, $code{$i}); + $k = array_keys($chars, $code[$i]); $check += ($k[0] * $p); ++$p; if ($p > 15) { @@ -865,11 +865,11 @@ protected function checksum_s25($code) $len = strlen($code); $sum = 0; for ($i = 0; $i < $len; $i += 2) { - $sum += $code{$i}; + $sum += $code[$i]; } $sum *= 3; for ($i = 1; $i < $len; $i += 2) { - $sum += ($code{$i}); + $sum += ($code[$i]); } $r = $sum % 10; if ($r > 0) { @@ -913,7 +913,7 @@ protected function barcode_msi($code, $checksum = false) $p = 2; $check = 0; for ($i = ($clen - 1); $i >= 0; --$i) { - $check += (hexdec($code{$i}) * $p); + $check += (hexdec($code[$i]) * $p); ++$p; if ($p > 7) { $p = 2; @@ -928,7 +928,7 @@ protected function barcode_msi($code, $checksum = false) $seq = '110'; // left guard $clen = strlen($code); for ($i = 0; $i < $clen; ++$i) { - $digit = $code{$i}; + $digit = $code[$i]; if ( ! isset($chr[$digit])) { throw new InvalidCharacterException('Char ' . $digit . ' is unsupported'); } @@ -973,7 +973,7 @@ protected function barcode_s25($code, $checksum = false) $seq = '11011010'; $clen = strlen($code); for ($i = 0; $i < $clen; ++$i) { - $digit = $code{$i}; + $digit = $code[$i]; if ( ! isset($chr[$digit])) { throw new InvalidCharacterException('Char ' . $digit . ' is unsupported'); } @@ -1001,8 +1001,8 @@ protected function binseq_to_array($seq, $bararray) $k = 0; for ($i = 0; $i < $len; ++$i) { $w += 1; - if (($i == ($len - 1)) OR (($i < ($len - 1)) AND ($seq{$i} != $seq{($i + 1)}))) { - if ($seq{$i} == '1') { + if (($i == ($len - 1)) OR (($i < ($len - 1)) AND ($seq[$i] != $seq[($i + 1)]))) { + if ($seq[$i] == '1') { $t = true; // bar } else { $t = false; // space @@ -1056,8 +1056,8 @@ protected function barcode_i25($code, $checksum = false) $k = 0; $clen = strlen($code); for ($i = 0; $i < $clen; $i = ($i + 2)) { - $char_bar = $code{$i}; - $char_space = $code{$i + 1}; + $char_bar = $code[$i]; + $char_space = $code[$i + 1]; if ( ! isset($chr[$char_bar]) || ! isset($chr[$char_space])) { throw new InvalidCharacterException(); } @@ -1065,7 +1065,7 @@ protected function barcode_i25($code, $checksum = false) $seq = ''; $chrlen = strlen($chr[$char_bar]); for ($s = 0; $s < $chrlen; $s++) { - $seq .= $chr[$char_bar]{$s} . $chr[$char_space]{$s}; + $seq .= $chr[$char_bar][$s] . $chr[$char_space][$s]; } $seqlen = strlen($seq); for ($j = 0; $j < $seqlen; ++$j) { @@ -1074,7 +1074,7 @@ protected function barcode_i25($code, $checksum = false) } else { $t = false; // space } - $w = $seq{$j}; + $w = $seq[$j]; $bararray['bcode'][$k] = array('t' => $t, 'w' => $w, 'h' => 1, 'p' => 0); $bararray['maxw'] += $w; ++$k; @@ -1224,7 +1224,7 @@ protected function barcode_c128($code, $type = '') case 'A': { // MODE A $startid = 103; for ($i = 0; $i < $len; ++$i) { - $char = $code{$i}; + $char = $code[$i]; $char_id = ord($char); if (($char_id >= 241) AND ($char_id <= 244)) { $code_data[] = $fnc_a[$char_id]; @@ -1239,7 +1239,7 @@ protected function barcode_c128($code, $type = '') case 'B': { // MODE B $startid = 104; for ($i = 0; $i < $len; ++$i) { - $char = $code{$i}; + $char = $code[$i]; $char_id = ord($char); if (($char_id >= 241) AND ($char_id <= 244)) { $code_data[] = $fnc_b[$char_id]; @@ -1262,7 +1262,7 @@ protected function barcode_c128($code, $type = '') throw new InvalidLengthException('Length must be even'); } for ($i = 0; $i < $len; $i += 2) { - $chrnum = $code{$i} . $code{$i + 1}; + $chrnum = $code[$i] . $code[$i + 1]; if (preg_match('/([0-9]{2})/', $chrnum) > 0) { $code_data[] = intval($chrnum); } else { @@ -1328,7 +1328,7 @@ protected function barcode_c128($code, $type = '') } } for ($i = 0; $i < $seq[2]; ++$i) { - $char = $seq[1]{$i}; + $char = $seq[1][$i]; $char_id = ord($char); if (($char_id >= 241) AND ($char_id <= 244)) { $code_data[] = $fnc_a[$char_id]; @@ -1371,7 +1371,7 @@ protected function barcode_c128($code, $type = '') } } for ($i = 0; $i < $seq[2]; ++$i) { - $char = $seq[1]{$i}; + $char = $seq[1][$i]; $char_id = ord($char); if (($char_id >= 241) AND ($char_id <= 244)) { $code_data[] = $fnc_b[$char_id]; @@ -1388,7 +1388,7 @@ protected function barcode_c128($code, $type = '') $code_data[] = 99; } for ($i = 0; $i < $seq[2]; $i += 2) { - $chrnum = $seq[1]{$i} . $seq[1]{$i + 1}; + $chrnum = $seq[1][$i] . $seq[1][$i + 1]; $code_data[] = intval($chrnum); } break; @@ -1419,7 +1419,7 @@ protected function barcode_c128($code, $type = '') } else { $t = false; // space } - $w = $seq{$j}; + $w = $seq[$j]; $bararray['bcode'][] = array('t' => $t, 'w' => $w, 'h' => 1, 'p' => 0); $bararray['maxw'] += $w; } @@ -1495,14 +1495,14 @@ protected function barcode_eanupc($code, $len = 13) // calculate check digit $sum_a = 0; for ($i = 1; $i < $data_len; $i += 2) { - $sum_a += $code{$i}; + $sum_a += $code[$i]; } if ($len > 12) { $sum_a *= 3; } $sum_b = 0; for ($i = 0; $i < $data_len; $i += 2) { - $sum_b += ($code{$i}); + $sum_b += ($code[$i]); } if ($len < 13) { $sum_b *= 3; @@ -1514,7 +1514,7 @@ protected function barcode_eanupc($code, $len = 13) if ($code_len == $data_len) { // add check digit $code .= $r; - } elseif ($r !== intval($code{$data_len})) { + } elseif ($r !== intval($code[$data_len])) { throw new InvalidCheckDigitException(); } if ($len == 12) { @@ -1627,7 +1627,7 @@ protected function barcode_eanupc($code, $len = 13) $bararray = array('code' => $upce_code, 'maxw' => 0, 'maxh' => 1, 'bcode' => array()); $p = $upce_parities[$code[1]][$r]; for ($i = 0; $i < 6; ++$i) { - $seq .= $codes[$p[$i]][$upce_code{$i}]; + $seq .= $codes[$p[$i]][$upce_code[$i]]; } $seq .= '010101'; // right guard bar } else { @@ -1635,20 +1635,20 @@ protected function barcode_eanupc($code, $len = 13) $half_len = intval(ceil($len / 2)); if ($len == 8) { for ($i = 0; $i < $half_len; ++$i) { - $seq .= $codes['A'][$code{$i}]; + $seq .= $codes['A'][$code[$i]]; } } else { $p = $parities[$code[0]]; for ($i = 1; $i < $half_len; ++$i) { - $seq .= $codes[$p[$i - 1]][$code{$i}]; + $seq .= $codes[$p[$i - 1]][$code[$i]]; } } $seq .= '01010'; // center guard bar for ($i = $half_len; $i < $len; ++$i) { - if ( ! isset($codes['C'][$code{$i}])) { - throw new InvalidCharacterException('Char ' . $code{$i} . ' not allowed'); + if ( ! isset($codes['C'][$code[$i]])) { + throw new InvalidCharacterException('Char ' . $code[$i] . ' not allowed'); } - $seq .= $codes['C'][$code{$i}]; + $seq .= $codes['C'][$code[$i]]; } $seq .= '101'; // right guard bar } @@ -1656,8 +1656,8 @@ protected function barcode_eanupc($code, $len = 13) $w = 0; for ($i = 0; $i < $clen; ++$i) { $w += 1; - if (($i == ($clen - 1)) OR (($i < ($clen - 1)) AND ($seq{$i} != $seq{($i + 1)}))) { - if ($seq{$i} == '1') { + if (($i == ($clen - 1)) OR (($i < ($clen - 1)) AND ($seq[$i] != $seq[($i + 1)]))) { + if ($seq[$i] == '1') { $t = true; // bar } else { $t = false; // space @@ -1746,7 +1746,7 @@ protected function barcode_eanext($code, $len = 5) $seq .= $codes[$p[0]][$code[0]]; for ($i = 1; $i < $len; ++$i) { $seq .= '01'; // separator - $seq .= $codes[$p[$i]][$code{$i}]; + $seq .= $codes[$p[$i]][$code[$i]]; } $bararray = array('code' => $code, 'maxw' => 0, 'maxh' => 1, 'bcode' => array()); @@ -1801,7 +1801,7 @@ protected function barcode_postnet($code, $planet = false) // calculate checksum $sum = 0; for ($i = 0; $i < $len; ++$i) { - $sum += intval($code{$i}); + $sum += intval($code[$i]); } $chkd = ($sum % 10); if ($chkd > 0) { @@ -1815,7 +1815,7 @@ protected function barcode_postnet($code, $planet = false) $bararray['maxw'] += 2; for ($i = 0; $i < $len; ++$i) { for ($j = 0; $j < 5; ++$j) { - $h = $barlen[$code{$i}][$j]; + $h = $barlen[$code[$i]][$j]; $p = floor(1 / $h); $bararray['bcode'][$k++] = array('t' => 1, 'w' => 1, 'h' => $h, 'p' => $p); $bararray['bcode'][$k++] = array('t' => 0, 'w' => 1, 'h' => 2, 'p' => 0); @@ -1932,8 +1932,8 @@ protected function barcode_rms4cc($code, $kix = false) $row = 0; $col = 0; for ($i = 0; $i < $len; ++$i) { - $row += $checktable[$code{$i}][0]; - $col += $checktable[$code{$i}][1]; + $row += $checktable[$code[$i]][0]; + $col += $checktable[$code[$i]][1]; } $row %= 6; $col %= 6; @@ -1950,7 +1950,7 @@ protected function barcode_rms4cc($code, $kix = false) } for ($i = 0; $i < $len; ++$i) { for ($j = 0; $j < 4; ++$j) { - switch ($barmode[$code{$i}][$j]) { + switch ($barmode[$code[$i]][$j]) { case 1: { $p = 0; $h = 2; @@ -2025,17 +2025,17 @@ protected function barcode_codabar($code) $code = 'A' . strtoupper($code) . 'A'; $len = strlen($code); for ($i = 0; $i < $len; ++$i) { - if ( ! isset($chr[$code{$i}])) { - throw new InvalidCharacterException('Char ' . $code{$i} . ' is unsupported'); + if ( ! isset($chr[$code[$i]])) { + throw new InvalidCharacterException('Char ' . $code[$i] . ' is unsupported'); } - $seq = $chr[$code{$i}]; + $seq = $chr[$code[$i]]; for ($j = 0; $j < 8; ++$j) { if (($j % 2) == 0) { $t = true; // bar } else { $t = false; // space } - $w = $seq{$j}; + $w = $seq[$i]; $bararray['bcode'][$k] = array('t' => $t, 'w' => $w, 'h' => 1, 'p' => 0); $bararray['maxw'] += $w; ++$k; @@ -2078,7 +2078,7 @@ protected function barcode_code11($code) $p = 1; $check = 0; for ($i = ($len - 1); $i >= 0; --$i) { - $digit = $code{$i}; + $digit = $code[$i]; if ($digit == '-') { $dval = 10; } else { @@ -2100,7 +2100,7 @@ protected function barcode_code11($code) $p = 1; $check = 0; for ($i = $len; $i >= 0; --$i) { - $digit = $code{$i}; + $digit = $code[$i]; if ($digit == '-') { $dval = 10; } else { @@ -2119,17 +2119,17 @@ protected function barcode_code11($code) $code = 'S' . $code . 'S'; $len += 3; for ($i = 0; $i < $len; ++$i) { - if ( ! isset($chr[$code{$i}])) { - throw new InvalidCharacterException('Char ' . $code{$i} . ' is unsupported'); + if ( ! isset($chr[$code[$i]])) { + throw new InvalidCharacterException('Char ' . $code[$i] . ' is unsupported'); } - $seq = $chr[$code{$i}]; + $seq = $chr[$code[$i]]; for ($j = 0; $j < 6; ++$j) { if (($j % 2) == 0) { $t = true; // bar } else { $t = false; // space } - $w = $seq{$j}; + $w = $seq[$j]; $bararray['bcode'][$k] = array('t' => $t, 'w' => $w, 'h' => 1, 'p' => 0); $bararray['maxw'] += $w; ++$k; @@ -2204,7 +2204,7 @@ protected function barcode_pharmacode2t($code) $bararray = array('code' => $code, 'maxw' => 0, 'maxh' => 2, 'bcode' => array()); $len = strlen($seq); for ($i = 0; $i < $len; ++$i) { - switch ($seq{$i}) { + switch ($seq[$i]) { case '1': { $p = 1; $h = 1; @@ -2732,7 +2732,7 @@ protected function hex_to_dec($hex) $bitval = 1; $len = strlen($hex); for ($pos = ($len - 1); $pos >= 0; --$pos) { - $dec = bcadd($dec, bcmul(hexdec($hex{$pos}), $bitval)); + $dec = bcadd($dec, bcmul(hexdec($hex[$pos]), $bitval)); $bitval = bcmul($bitval, 16); } From 6bab859b944a91d954c5fc801b063958ede6eb5b Mon Sep 17 00:00:00 2001 From: Philip Hofstetter Date: Fri, 6 Sep 2019 15:09:05 +0200 Subject: [PATCH 002/155] update for PHP 7.1 this fixes the "A non-numeric value encountered" warning introduced in PHP 7.1. This fixes the test ean13_generator_throws_exception_if_invalid_chars_are_used --- src/BarcodeGenerator.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/BarcodeGenerator.php b/src/BarcodeGenerator.php index 8d93a3c..252a727 100644 --- a/src/BarcodeGenerator.php +++ b/src/BarcodeGenerator.php @@ -1502,7 +1502,7 @@ protected function barcode_eanupc($code, $len = 13) } $sum_b = 0; for ($i = 0; $i < $data_len; $i += 2) { - $sum_b += ($code[$i]); + $sum_b += intval(($code[$i])); } if ($len < 13) { $sum_b *= 3; From 28ebddc582ca5e3f9821b3bcc3d78a9ac5d8c367 Mon Sep 17 00:00:00 2001 From: Casper Bakker Date: Wed, 18 Dec 2019 17:01:07 +0100 Subject: [PATCH 003/155] Only test supported PHP versions --- .travis.yml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/.travis.yml b/.travis.yml index 93e4bfa..2e9486a 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,12 +1,12 @@ language: php php: - - 5.6 - - 7.0 - - hhvm + - 7.2 + - 7.3 + - 7.4 sudo: false install: composer install --no-interaction --prefer-source -script: vendor/bin/phpunit \ No newline at end of file +script: vendor/bin/phpunit From 891f53f3fc7944c821ffccc1f73476b5a445085f Mon Sep 17 00:00:00 2001 From: Casper Bakker Date: Mon, 23 Dec 2019 13:36:20 +0100 Subject: [PATCH 004/155] Update license according to https://spdx.org/licenses/ --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 22f4c42..8e2724f 100644 --- a/composer.json +++ b/composer.json @@ -4,7 +4,7 @@ "description": "An easy to use, non-bloated, barcode generator in PHP. Creates SVG, PNG, JPG and HTML images from the most used 1D barcode standards.", "keywords": [ "php", "barcode", "barcode generator", "EAN", "EAN13", "UPC", "Code39", "Code128", "Code93", "Standard 2 of 5", "MSI", "POSTNET", "KIX", "KIXCODE", "CODABAR", "PHARMA", "Code11", "SVG", "PNG", "HTML", "JPG", "JPEG" ], "homepage": "http://github.com/picqer/php-barcode-generator", - "license": "LGPLv3", + "license": "LGPL-3.0-or-later", "authors": [ { "name": "Nicola Asuni", From 6495fecafda0455ac4d4d1883ebe8813d1653d3e Mon Sep 17 00:00:00 2001 From: Casper Bakker Date: Tue, 31 Dec 2019 21:06:24 +0100 Subject: [PATCH 005/155] Updated minimum PHP version to 7.2 --- composer.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index 8e2724f..97987cb 100644 --- a/composer.json +++ b/composer.json @@ -3,7 +3,7 @@ "type": "library", "description": "An easy to use, non-bloated, barcode generator in PHP. Creates SVG, PNG, JPG and HTML images from the most used 1D barcode standards.", "keywords": [ "php", "barcode", "barcode generator", "EAN", "EAN13", "UPC", "Code39", "Code128", "Code93", "Standard 2 of 5", "MSI", "POSTNET", "KIX", "KIXCODE", "CODABAR", "PHARMA", "Code11", "SVG", "PNG", "HTML", "JPG", "JPEG" ], - "homepage": "http://github.com/picqer/php-barcode-generator", + "homepage": "https://github.com/picqer/php-barcode-generator", "license": "LGPL-3.0-or-later", "authors": [ { @@ -17,7 +17,7 @@ } ], "require": { - "php": ">=5.4.0" + "php": ">=7.2.0" }, "require-dev": { "phpunit/phpunit": "^5.3" From 73044f13a2880c1e938852ce22628d3f0be9d8d7 Mon Sep 17 00:00:00 2001 From: Casper Bakker Date: Tue, 31 Dec 2019 21:17:03 +0100 Subject: [PATCH 006/155] Create phpunit.yml --- .github/workflows/phpunit.yml | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 .github/workflows/phpunit.yml diff --git a/.github/workflows/phpunit.yml b/.github/workflows/phpunit.yml new file mode 100644 index 0000000..5a2fb7f --- /dev/null +++ b/.github/workflows/phpunit.yml @@ -0,0 +1,20 @@ +name: PHP Composer + +on: [push] + +jobs: + build: + + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v2 + + - name: Validate composer.json and composer.lock + run: composer validate + + - name: Install dependencies + run: composer install --prefer-dist --no-progress --no-suggest + + - name: Run test suite + run: composer run-script test From 833b6f7138aebe02f7e1fe95881c7db41f0d6bcb Mon Sep 17 00:00:00 2001 From: Casper Bakker Date: Tue, 31 Dec 2019 21:24:25 +0100 Subject: [PATCH 007/155] Add composer test command --- .gitignore | 3 ++- composer.json | 3 +++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 2e3beb6..06b5400 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ .idea .DS_Store vendor -composer.lock \ No newline at end of file +composer.lock +composer.phar \ No newline at end of file diff --git a/composer.json b/composer.json index 97987cb..9a85d0d 100644 --- a/composer.json +++ b/composer.json @@ -26,5 +26,8 @@ "psr-4": { "Picqer\\Barcode\\": "src" } + }, + "scripts": { + "test": "vendor/bin/phpunit" } } From 04401df55a4def2fc347fd669481abfe1aa3b94b Mon Sep 17 00:00:00 2001 From: Casper Bakker Date: Tue, 31 Dec 2019 21:36:43 +0100 Subject: [PATCH 008/155] Update to phpunit 8.5 --- composer.json | 2 +- phpunit.xml | 1 - tests/BarcodeTest.php | 67 ++++++++++++++----------------------------- 3 files changed, 23 insertions(+), 47 deletions(-) diff --git a/composer.json b/composer.json index 9a85d0d..4730c16 100644 --- a/composer.json +++ b/composer.json @@ -20,7 +20,7 @@ "php": ">=7.2.0" }, "require-dev": { - "phpunit/phpunit": "^5.3" + "phpunit/phpunit": "^8.5" }, "autoload": { "psr-4": { diff --git a/phpunit.xml b/phpunit.xml index 0d549dc..ea81c2b 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -8,7 +8,6 @@ processIsolation="false" stopOnError="false" stopOnFailure="false" - syntaxCheck="true" verbose="true" > diff --git a/tests/BarcodeTest.php b/tests/BarcodeTest.php index 8f532b8..337f357 100644 --- a/tests/BarcodeTest.php +++ b/tests/BarcodeTest.php @@ -1,11 +1,10 @@ getBarcode('081231723897', $generator::TYPE_CODE_128); @@ -13,10 +12,7 @@ public function png_barcode_generator_can_generate_code_128_barcode() $this->assertEquals('PNG', substr($generated, 1, 3)); } - /** - * @test - */ - public function svg_barcode_generator_can_generate_ean_13_barcode() + public function test_svg_barcode_generator_can_generate_ean_13_barcode() { $generator = new Picqer\Barcode\BarcodeGeneratorSVG(); $generated = $generator->getBarcode('081231723897', $generator::TYPE_EAN_13); @@ -24,10 +20,7 @@ public function svg_barcode_generator_can_generate_ean_13_barcode() $this->assertStringEqualsFile('tests/verified-files/081231723897-ean13.svg', $generated); } - /** - * @test - */ - public function html_barcode_generator_can_generate_code_128_barcode() + public function test_html_barcode_generator_can_generate_code_128_barcode() { $generator = new Picqer\Barcode\BarcodeGeneratorHTML(); $generated = $generator->getBarcode('081231723897', $generator::TYPE_CODE_128); @@ -35,29 +28,23 @@ public function html_barcode_generator_can_generate_code_128_barcode() $this->assertStringEqualsFile('tests/verified-files/081231723897-code128.html', $generated); } - /** - * @test - */ - public function jpg_barcode_generator_can_generate_code_128_barcode() + public function test_jpg_barcode_generator_can_generate_code_128_barcode() { $generator = new Picqer\Barcode\BarcodeGeneratorJPG(); - $generator->getBarcode('081231723897', $generator::TYPE_CODE_128); + $result = $generator->getBarcode('081231723897', $generator::TYPE_CODE_128); + + $this->assertGreaterThan(1000, strlen($result)); } - /** - * @test - * @expectedException \Picqer\Barcode\Exceptions\InvalidCharacterException - */ - public function ean13_generator_throws_exception_if_invalid_chars_are_used() + public function test_ean13_generator_throws_exception_if_invalid_chars_are_used() { + $this->expectException(Picqer\Barcode\Exceptions\InvalidCharacterException::class); + $generator = new Picqer\Barcode\BarcodeGeneratorSVG(); $generator->getBarcode('A123', $generator::TYPE_EAN_13); } - /** - * @test - */ - public function ean13_generator_accepting_13_chars() + public function test_ean13_generator_accepting_13_chars() { $generator = new Picqer\Barcode\BarcodeGeneratorSVG(); $generated = $generator->getBarcode('0049000004632', $generator::TYPE_EAN_13); @@ -65,10 +52,7 @@ public function ean13_generator_accepting_13_chars() $this->assertStringEqualsFile('tests/verified-files/0049000004632-ean13.svg', $generated); } - /** - * @test - */ - public function ean13_generator_accepting_12_chars_and_generates_13th_check_digit() + public function test_ean13_generator_accepting_12_chars_and_generates_13th_check_digit() { $generator = new Picqer\Barcode\BarcodeGeneratorSVG(); $generated = $generator->getBarcode('004900000463', $generator::TYPE_EAN_13); @@ -76,10 +60,7 @@ public function ean13_generator_accepting_12_chars_and_generates_13th_check_digi $this->assertStringEqualsFile('tests/verified-files/0049000004632-ean13.svg', $generated); } - /** - * @test - */ - public function ean13_generator_accepting_11_chars_and_generates_13th_check_digit_and_adds_leading_zero() + public function test_ean13_generator_accepting_11_chars_and_generates_13th_check_digit_and_adds_leading_zero() { $generator = new Picqer\Barcode\BarcodeGeneratorSVG(); $generated = $generator->getBarcode('04900000463', $generator::TYPE_EAN_13); @@ -87,22 +68,18 @@ public function ean13_generator_accepting_11_chars_and_generates_13th_check_digi $this->assertStringEqualsFile('tests/verified-files/0049000004632-ean13.svg', $generated); } - /** - * @test - * @expectedException \Picqer\Barcode\Exceptions\InvalidCheckDigitException - */ - public function ean13_generator_throws_exception_when_wrong_check_digit_is_given() + public function test_ean13_generator_throws_exception_when_wrong_check_digit_is_given() { + $this->expectException(Picqer\Barcode\Exceptions\InvalidCheckDigitException::class); + $generator = new Picqer\Barcode\BarcodeGeneratorSVG(); $generator->getBarcode('0049000004633', $generator::TYPE_EAN_13); } - /** - * @test - * @expectedException \Picqer\Barcode\Exceptions\UnknownTypeException - */ - public function generator_throws_unknown_type_exceptions() + public function test_generator_throws_unknown_type_exceptions() { + $this->expectException(Picqer\Barcode\Exceptions\UnknownTypeException::class); + $generator = new Picqer\Barcode\BarcodeGeneratorSVG(); $generator->getBarcode('0049000004633', 'vladimir'); } From 8b9b66b88c591da3997e61340887909a13aea695 Mon Sep 17 00:00:00 2001 From: Casper Bakker Date: Tue, 31 Dec 2019 21:47:42 +0100 Subject: [PATCH 009/155] Test multiple php versions with Github Actions --- .github/workflows/phpunit.yml | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/.github/workflows/phpunit.yml b/.github/workflows/phpunit.yml index 5a2fb7f..4362064 100644 --- a/.github/workflows/phpunit.yml +++ b/.github/workflows/phpunit.yml @@ -7,10 +7,19 @@ jobs: runs-on: ubuntu-latest + strategy: + matrix: + php-versions: ['7.2', '7.3', '7.4'] + steps: - uses: actions/checkout@v2 - - name: Validate composer.json and composer.lock + - name: Setup PHP + uses: shivammathur/setup-php@v1 + with: + php-version: ${{ matrix.php-versions }} + + - name: Validate composer.json run: composer validate - name: Install dependencies From d23401d6b858e6231eb463dc628aaf56214a875b Mon Sep 17 00:00:00 2001 From: Casper Bakker Date: Tue, 31 Dec 2019 21:49:41 +0100 Subject: [PATCH 010/155] Add mbstring to Github Actions --- .github/workflows/phpunit.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.github/workflows/phpunit.yml b/.github/workflows/phpunit.yml index 4362064..5a1aa87 100644 --- a/.github/workflows/phpunit.yml +++ b/.github/workflows/phpunit.yml @@ -16,8 +16,9 @@ jobs: - name: Setup PHP uses: shivammathur/setup-php@v1 - with: + with: php-version: ${{ matrix.php-versions }} + extensions: mbstring - name: Validate composer.json run: composer validate From 1d453be66170547a06b636e80f50627a963e2f88 Mon Sep 17 00:00:00 2001 From: Casper Bakker Date: Tue, 31 Dec 2019 21:53:15 +0100 Subject: [PATCH 011/155] Add gd --- .github/workflows/phpunit.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/phpunit.yml b/.github/workflows/phpunit.yml index 5a1aa87..4c8243d 100644 --- a/.github/workflows/phpunit.yml +++ b/.github/workflows/phpunit.yml @@ -18,7 +18,7 @@ jobs: uses: shivammathur/setup-php@v1 with: php-version: ${{ matrix.php-versions }} - extensions: mbstring + extensions: mbstring, gd - name: Validate composer.json run: composer validate From 3bc3a2387299f1d55703344e2d319f3fd2da6784 Mon Sep 17 00:00:00 2001 From: Casper Bakker Date: Tue, 31 Dec 2019 22:36:28 +0100 Subject: [PATCH 012/155] Add tests for all barcode types --- tests/BarcodeTest.php | 233 ++++++++++++++++++ tests/verified-files/1234567890-code128a.svg | 49 ++++ tests/verified-files/1234567890-code128c.svg | 34 +++ .../1234567890abcABC-283*33-code128.svg | 76 ++++++ .../1234567890abcABC-283*33-code128b.svg | 88 +++++++ 5 files changed, 480 insertions(+) create mode 100644 tests/verified-files/1234567890-code128a.svg create mode 100644 tests/verified-files/1234567890-code128c.svg create mode 100644 tests/verified-files/1234567890abcABC-283*33-code128.svg create mode 100644 tests/verified-files/1234567890abcABC-283*33-code128b.svg diff --git a/tests/BarcodeTest.php b/tests/BarcodeTest.php index 337f357..92e1a30 100644 --- a/tests/BarcodeTest.php +++ b/tests/BarcodeTest.php @@ -83,4 +83,237 @@ public function test_generator_throws_unknown_type_exceptions() $generator = new Picqer\Barcode\BarcodeGeneratorSVG(); $generator->getBarcode('0049000004633', 'vladimir'); } + + public function test_generator_can_generate_code_39_barcode() + { + $generator = new Picqer\Barcode\BarcodeGeneratorSVG(); + $result = $generator->getBarcode('1234567890abcABC', $generator::TYPE_CODE_39); + + $this->assertGreaterThan(100, strlen($result)); + } + + public function test_generator_can_generate_code_39_checksum_barcode() + { + $generator = new Picqer\Barcode\BarcodeGeneratorSVG(); + $result = $generator->getBarcode('1234567890abcABC', $generator::TYPE_CODE_39_CHECKSUM); + + $this->assertGreaterThan(100, strlen($result)); + } + + public function test_generator_can_generate_code_39_extended_checksum_barcode() + { + $generator = new Picqer\Barcode\BarcodeGeneratorSVG(); + $result = $generator->getBarcode('1234567890abcABC', $generator::TYPE_CODE_39E_CHECKSUM); + + $this->assertGreaterThan(100, strlen($result)); + } + + public function test_generator_can_generate_code_93_barcode() + { + $generator = new Picqer\Barcode\BarcodeGeneratorSVG(); + $result = $generator->getBarcode('1234567890abcABC', $generator::TYPE_CODE_93); + + $this->assertGreaterThan(100, strlen($result)); + } + + public function test_generator_can_generate_standard_2_5_barcode() + { + $generator = new Picqer\Barcode\BarcodeGeneratorSVG(); + $result = $generator->getBarcode('1234567890', $generator::TYPE_STANDARD_2_5); + + $this->assertGreaterThan(100, strlen($result)); + } + + public function test_generator_can_generate_standard_2_5_checksum_barcode() + { + $generator = new Picqer\Barcode\BarcodeGeneratorSVG(); + $result = $generator->getBarcode('1234567890', $generator::TYPE_STANDARD_2_5_CHECKSUM); + + $this->assertGreaterThan(100, strlen($result)); + } + + public function test_generator_can_generate_interleaved_2_5_barcode() + { + $generator = new Picqer\Barcode\BarcodeGeneratorSVG(); + $result = $generator->getBarcode('1234567890', $generator::TYPE_INTERLEAVED_2_5); + + $this->assertGreaterThan(100, strlen($result)); + } + + public function test_generator_can_generate_interleaved_2_5_checksum_barcode() + { + $generator = new Picqer\Barcode\BarcodeGeneratorSVG(); + $result = $generator->getBarcode('1234567890', $generator::TYPE_INTERLEAVED_2_5_CHECKSUM); + + $this->assertGreaterThan(100, strlen($result)); + } + + public function test_generator_can_generate_code_128_barcode() + { + $generator = new Picqer\Barcode\BarcodeGeneratorSVG(); + $result = $generator->getBarcode('1234567890abcABC-283*33', $generator::TYPE_CODE_128); + + $this->assertStringEqualsFile('tests/verified-files/1234567890abcABC-283*33-code128.svg', $result); + } + + public function test_generator_can_generate_code_128_a_barcode() + { + $generator = new Picqer\Barcode\BarcodeGeneratorSVG(); + $result = $generator->getBarcode('1234567890', $generator::TYPE_CODE_128_A); + + $this->assertStringEqualsFile('tests/verified-files/1234567890-code128a.svg', $result); + } + + public function test_generator_can_generate_code_128_b_barcode() + { + $generator = new Picqer\Barcode\BarcodeGeneratorSVG(); + $result = $generator->getBarcode('1234567890abcABC-283*33', $generator::TYPE_CODE_128_B); + + $this->assertStringEqualsFile('tests/verified-files/1234567890abcABC-283*33-code128b.svg', $result); + } + + public function test_generator_can_generate_code_128_c_barcode() + { + $generator = new Picqer\Barcode\BarcodeGeneratorSVG(); + $result = $generator->getBarcode('1234567890', $generator::TYPE_CODE_128_C); + + $this->assertStringEqualsFile('tests/verified-files/1234567890-code128c.svg', $result); + } + + public function test_generator_can_generate_ean_2_barcode() + { + $generator = new Picqer\Barcode\BarcodeGeneratorSVG(); + $result = $generator->getBarcode('1234567890abcABC-283*33', $generator::TYPE_EAN_2); + + $this->assertGreaterThan(100, strlen($result)); + } + + public function test_generator_can_generate_ean_5_barcode() + { + $generator = new Picqer\Barcode\BarcodeGeneratorSVG(); + $result = $generator->getBarcode('1234567890abcABC-283*33', $generator::TYPE_EAN_5); + + $this->assertGreaterThan(100, strlen($result)); + } + + public function test_generator_can_generate_ean_8_barcode() + { + $generator = new Picqer\Barcode\BarcodeGeneratorSVG(); + $result = $generator->getBarcode('1234568', $generator::TYPE_EAN_8); + + $this->assertGreaterThan(100, strlen($result)); + } + + public function test_generator_can_generate_ean_13_barcode() + { + $generator = new Picqer\Barcode\BarcodeGeneratorSVG(); + $result = $generator->getBarcode('1234567890', $generator::TYPE_EAN_13); + + $this->assertGreaterThan(100, strlen($result)); + } + + public function test_generator_can_generate_upc_a_barcode() + { + $generator = new Picqer\Barcode\BarcodeGeneratorSVG(); + $result = $generator->getBarcode('123456789', $generator::TYPE_UPC_A); + + $this->assertGreaterThan(100, strlen($result)); + } + + public function test_generator_can_generate_upc_e_barcode() + { + $generator = new Picqer\Barcode\BarcodeGeneratorSVG(); + $result = $generator->getBarcode('123456789', $generator::TYPE_UPC_E); + + $this->assertGreaterThan(100, strlen($result)); + } + + public function test_generator_can_generate_msi_barcode() + { + $generator = new Picqer\Barcode\BarcodeGeneratorSVG(); + $result = $generator->getBarcode('123456789', $generator::TYPE_MSI); + + $this->assertGreaterThan(100, strlen($result)); + } + + public function test_generator_can_generate_msi_checksum_barcode() + { + $generator = new Picqer\Barcode\BarcodeGeneratorSVG(); + $result = $generator->getBarcode('123456789', $generator::TYPE_MSI_CHECKSUM); + + $this->assertGreaterThan(100, strlen($result)); + } + + public function test_generator_can_generate_postnet_barcode() + { + $generator = new Picqer\Barcode\BarcodeGeneratorSVG(); + $result = $generator->getBarcode('123456789', $generator::TYPE_POSTNET); + + $this->assertGreaterThan(100, strlen($result)); + } + + public function test_generator_can_generate_planet_barcode() + { + $generator = new Picqer\Barcode\BarcodeGeneratorSVG(); + $result = $generator->getBarcode('123456789', $generator::TYPE_PLANET); + + $this->assertGreaterThan(100, strlen($result)); + } + + public function test_generator_can_generate_rms4cc_barcode() + { + $generator = new Picqer\Barcode\BarcodeGeneratorSVG(); + $result = $generator->getBarcode('123456789', $generator::TYPE_RMS4CC); + + $this->assertGreaterThan(100, strlen($result)); + } + + public function test_generator_can_generate_kix_barcode() + { + $generator = new Picqer\Barcode\BarcodeGeneratorSVG(); + $result = $generator->getBarcode('123456789', $generator::TYPE_KIX); + + $this->assertGreaterThan(100, strlen($result)); + } + + public function test_generator_can_generate_imb_barcode() + { + $generator = new Picqer\Barcode\BarcodeGeneratorSVG(); + $result = $generator->getBarcode('123456789', $generator::TYPE_IMB); + + $this->assertGreaterThan(100, strlen($result)); + } + + public function test_generator_can_generate_codabar_barcode() + { + $generator = new Picqer\Barcode\BarcodeGeneratorSVG(); + $result = $generator->getBarcode('123456789', $generator::TYPE_CODABAR); + + $this->assertGreaterThan(100, strlen($result)); + } + + public function test_generator_can_generate_code_11_barcode() + { + $generator = new Picqer\Barcode\BarcodeGeneratorSVG(); + $result = $generator->getBarcode('123456789', $generator::TYPE_CODE_11); + + $this->assertGreaterThan(100, strlen($result)); + } + + public function test_generator_can_generate_pharma_code_barcode() + { + $generator = new Picqer\Barcode\BarcodeGeneratorSVG(); + $result = $generator->getBarcode('123456789', $generator::TYPE_PHARMA_CODE); + + $this->assertGreaterThan(100, strlen($result)); + } + + public function test_generator_can_generate_pharma_code_2_tracks_barcode() + { + $generator = new Picqer\Barcode\BarcodeGeneratorSVG(); + $result = $generator->getBarcode('123456789', $generator::TYPE_PHARMA_CODE_TWO_TRACKS); + + $this->assertGreaterThan(100, strlen($result)); + } + } \ No newline at end of file diff --git a/tests/verified-files/1234567890-code128a.svg b/tests/verified-files/1234567890-code128a.svg new file mode 100644 index 0000000..0ca2f72 --- /dev/null +++ b/tests/verified-files/1234567890-code128a.svg @@ -0,0 +1,49 @@ + + + + 1234567890 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/tests/verified-files/1234567890-code128c.svg b/tests/verified-files/1234567890-code128c.svg new file mode 100644 index 0000000..b4d8b1d --- /dev/null +++ b/tests/verified-files/1234567890-code128c.svg @@ -0,0 +1,34 @@ + + + + 1234567890 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/tests/verified-files/1234567890abcABC-283*33-code128.svg b/tests/verified-files/1234567890abcABC-283*33-code128.svg new file mode 100644 index 0000000..8184e9b --- /dev/null +++ b/tests/verified-files/1234567890abcABC-283*33-code128.svg @@ -0,0 +1,76 @@ + + + + 1234567890abcABC-283*33 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/tests/verified-files/1234567890abcABC-283*33-code128b.svg b/tests/verified-files/1234567890abcABC-283*33-code128b.svg new file mode 100644 index 0000000..2a1f71e --- /dev/null +++ b/tests/verified-files/1234567890abcABC-283*33-code128b.svg @@ -0,0 +1,88 @@ + + + + 1234567890abcABC-283*33 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + From 2d1128f5222d9368fc6151d2b51801ea29ba1052 Mon Sep 17 00:00:00 2001 From: Casper Bakker Date: Tue, 31 Dec 2019 22:36:45 +0100 Subject: [PATCH 013/155] Bugfix in codabar --- src/BarcodeGenerator.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/BarcodeGenerator.php b/src/BarcodeGenerator.php index 252a727..33e4979 100644 --- a/src/BarcodeGenerator.php +++ b/src/BarcodeGenerator.php @@ -2035,7 +2035,7 @@ protected function barcode_codabar($code) } else { $t = false; // space } - $w = $seq[$i]; + $w = $seq[$j]; $bararray['bcode'][$k] = array('t' => $t, 'w' => $w, 'h' => 1, 'p' => 0); $bararray['maxw'] += $w; ++$k; From a6892d75305c8a6708ee47f7047154f37730cd27 Mon Sep 17 00:00:00 2001 From: Casper Bakker Date: Tue, 31 Dec 2019 23:08:06 +0100 Subject: [PATCH 014/155] Move some barcode types to there own classes --- src/BarcodeGenerator.php | 558 ++++-------------------- src/Types/Type.php | 8 + src/Types/TypeCode93.php | 329 ++++++++++++++ src/Types/TypeInterleaved25.php | 17 + src/Types/TypeInterleaved25Checksum.php | 90 ++++ 5 files changed, 530 insertions(+), 472 deletions(-) create mode 100644 src/Types/Type.php create mode 100644 src/Types/TypeCode93.php create mode 100644 src/Types/TypeInterleaved25.php create mode 100644 src/Types/TypeInterleaved25Checksum.php diff --git a/src/BarcodeGenerator.php b/src/BarcodeGenerator.php index 33e4979..80766a7 100644 --- a/src/BarcodeGenerator.php +++ b/src/BarcodeGenerator.php @@ -35,35 +35,38 @@ use Picqer\Barcode\Exceptions\InvalidFormatException; use Picqer\Barcode\Exceptions\InvalidLengthException; use Picqer\Barcode\Exceptions\UnknownTypeException; +use Picqer\Barcode\Types\TypeCode93; +use Picqer\Barcode\Types\TypeInterleaved25; +use Picqer\Barcode\Types\TypeInterleaved25Checksum; abstract class BarcodeGenerator { - const TYPE_CODE_39 = 'C39'; - const TYPE_CODE_39_CHECKSUM = 'C39+'; - const TYPE_CODE_39E = 'C39E'; - const TYPE_CODE_39E_CHECKSUM = 'C39E+'; - const TYPE_CODE_93 = 'C93'; - const TYPE_STANDARD_2_5 = 'S25'; - const TYPE_STANDARD_2_5_CHECKSUM = 'S25+'; - const TYPE_INTERLEAVED_2_5 = 'I25'; - const TYPE_INTERLEAVED_2_5_CHECKSUM = 'I25+'; + const TYPE_CODE_39 = 'C39'; // CODE 39 - ANSI MH10.8M-1983 - USD-3 - 3 of 9. + const TYPE_CODE_39_CHECKSUM = 'C39+'; // CODE 39 with checksum + const TYPE_CODE_39E = 'C39E'; // CODE 39 EXTENDED + const TYPE_CODE_39E_CHECKSUM = 'C39E+'; // CODE 39 EXTENDED + CHECKSUM + const TYPE_CODE_93 = 'C93'; // CODE 93 - USS-93 + const TYPE_STANDARD_2_5 = 'S25'; // Standard 2 of 5 + const TYPE_STANDARD_2_5_CHECKSUM = 'S25+'; // Standard 2 of 5 + CHECKSUM + const TYPE_INTERLEAVED_2_5 = 'I25'; // Interleaved 2 of 5 + const TYPE_INTERLEAVED_2_5_CHECKSUM = 'I25+'; // Interleaved 2 of 5 + CHECKSUM const TYPE_CODE_128 = 'C128'; const TYPE_CODE_128_A = 'C128A'; const TYPE_CODE_128_B = 'C128B'; const TYPE_CODE_128_C = 'C128C'; - const TYPE_EAN_2 = 'EAN2'; - const TYPE_EAN_5 = 'EAN5'; + const TYPE_EAN_2 = 'EAN2'; // 2-Digits UPC-Based Extention + const TYPE_EAN_5 = 'EAN5'; // 5-Digits UPC-Based Extention const TYPE_EAN_8 = 'EAN8'; const TYPE_EAN_13 = 'EAN13'; const TYPE_UPC_A = 'UPCA'; const TYPE_UPC_E = 'UPCE'; - const TYPE_MSI = 'MSI'; - const TYPE_MSI_CHECKSUM = 'MSI+'; + const TYPE_MSI = 'MSI'; // MSI (Variation of Plessey code) + const TYPE_MSI_CHECKSUM = 'MSI+'; // MSI + CHECKSUM (modulo 11) const TYPE_POSTNET = 'POSTNET'; const TYPE_PLANET = 'PLANET'; - const TYPE_RMS4CC = 'RMS4CC'; - const TYPE_KIX = 'KIX'; - const TYPE_IMB = 'IMB'; + const TYPE_RMS4CC = 'RMS4CC'; // RMS4CC (Royal Mail 4-state Customer Code) - CBC (Customer Bar Code) + const TYPE_KIX = 'KIX'; // KIX (Klant index - Customer index) + const TYPE_IMB = 'IMB'; // IMB - Intelligent Mail Barcode - Onecode - USPS-B-3200 const TYPE_CODABAR = 'CODABAR'; const TYPE_CODE_11 = 'CODE11'; const TYPE_PHARMA_CODE = 'PHARMA'; @@ -80,130 +83,131 @@ abstract class BarcodeGenerator protected function getBarcodeData($code, $type) { switch (strtoupper($type)) { - case self::TYPE_CODE_39: { // CODE 39 - ANSI MH10.8M-1983 - USD-3 - 3 of 9. + case self::TYPE_CODE_39: $arrcode = $this->barcode_code39($code, false, false); break; - } - case self::TYPE_CODE_39_CHECKSUM: { // CODE 39 with checksum + + case self::TYPE_CODE_39_CHECKSUM: $arrcode = $this->barcode_code39($code, false, true); break; - } - case self::TYPE_CODE_39E: { // CODE 39 EXTENDED + + case self::TYPE_CODE_39E: $arrcode = $this->barcode_code39($code, true, false); break; - } - case self::TYPE_CODE_39E_CHECKSUM: { // CODE 39 EXTENDED + CHECKSUM + + case self::TYPE_CODE_39E_CHECKSUM: $arrcode = $this->barcode_code39($code, true, true); break; - } - case self::TYPE_CODE_93: { // CODE 93 - USS-93 - $arrcode = $this->barcode_code93($code); + + case self::TYPE_CODE_93: + $barcodeDataBuilder = new TypeCode93(); + $arrcode = $barcodeDataBuilder->getBarcodeData($code); break; - } - case self::TYPE_STANDARD_2_5: { // Standard 2 of 5 + + case self::TYPE_STANDARD_2_5: $arrcode = $this->barcode_s25($code, false); break; - } - case self::TYPE_STANDARD_2_5_CHECKSUM: { // Standard 2 of 5 + CHECKSUM + + case self::TYPE_STANDARD_2_5_CHECKSUM: $arrcode = $this->barcode_s25($code, true); break; - } - case self::TYPE_INTERLEAVED_2_5: { // Interleaved 2 of 5 - $arrcode = $this->barcode_i25($code, false); + + case self::TYPE_INTERLEAVED_2_5: + $barcodeDataBuilder = new TypeInterleaved25(); + $arrcode = $barcodeDataBuilder->getBarcodeData($code); break; - } - case self::TYPE_INTERLEAVED_2_5_CHECKSUM: { // Interleaved 2 of 5 + CHECKSUM - $arrcode = $this->barcode_i25($code, true); + + case self::TYPE_INTERLEAVED_2_5_CHECKSUM: + $barcodeDataBuilder = new TypeInterleaved25Checksum(); + $arrcode = $barcodeDataBuilder->getBarcodeData($code); break; - } - case self::TYPE_CODE_128: { // CODE 128 + + case self::TYPE_CODE_128: $arrcode = $this->barcode_c128($code, ''); break; - } - case self::TYPE_CODE_128_A: { // CODE 128 A + + case self::TYPE_CODE_128_A: $arrcode = $this->barcode_c128($code, 'A'); break; - } - case self::TYPE_CODE_128_B: { // CODE 128 B + + case self::TYPE_CODE_128_B: $arrcode = $this->barcode_c128($code, 'B'); break; - } - case self::TYPE_CODE_128_C: { // CODE 128 C + + case self::TYPE_CODE_128_C: $arrcode = $this->barcode_c128($code, 'C'); break; - } - case self::TYPE_EAN_2: { // 2-Digits UPC-Based Extention + + case self::TYPE_EAN_2: $arrcode = $this->barcode_eanext($code, 2); break; - } - case self::TYPE_EAN_5: { // 5-Digits UPC-Based Extention + + case self::TYPE_EAN_5: $arrcode = $this->barcode_eanext($code, 5); break; - } - case self::TYPE_EAN_8: { // EAN 8 + + case self::TYPE_EAN_8: $arrcode = $this->barcode_eanupc($code, 8); break; - } - case self::TYPE_EAN_13: { // EAN 13 + + case self::TYPE_EAN_13: $arrcode = $this->barcode_eanupc($code, 13); break; - } - case self::TYPE_UPC_A: { // UPC-A + + case self::TYPE_UPC_A: $arrcode = $this->barcode_eanupc($code, 12); break; - } - case self::TYPE_UPC_E: { // UPC-E + + case self::TYPE_UPC_E: $arrcode = $this->barcode_eanupc($code, 6); break; - } - case self::TYPE_MSI: { // MSI (Variation of Plessey code) + + case self::TYPE_MSI: $arrcode = $this->barcode_msi($code, false); break; - } - case self::TYPE_MSI_CHECKSUM: { // MSI + CHECKSUM (modulo 11) + + case self::TYPE_MSI_CHECKSUM: $arrcode = $this->barcode_msi($code, true); break; - } - case self::TYPE_POSTNET: { // POSTNET + + case self::TYPE_POSTNET: $arrcode = $this->barcode_postnet($code, false); break; - } - case self::TYPE_PLANET: { // PLANET + + case self::TYPE_PLANET: $arrcode = $this->barcode_postnet($code, true); break; - } - case self::TYPE_RMS4CC: { // RMS4CC (Royal Mail 4-state Customer Code) - CBC (Customer Bar Code) + + case self::TYPE_RMS4CC: $arrcode = $this->barcode_rms4cc($code, false); break; - } - case self::TYPE_KIX: { // KIX (Klant index - Customer index) + + case self::TYPE_KIX: $arrcode = $this->barcode_rms4cc($code, true); break; - } - case self::TYPE_IMB: { // IMB - Intelligent Mail Barcode - Onecode - USPS-B-3200 + + case self::TYPE_IMB: $arrcode = $this->barcode_imb($code); break; - } - case self::TYPE_CODABAR: { // CODABAR + + case self::TYPE_CODABAR: $arrcode = $this->barcode_codabar($code); break; - } - case self::TYPE_CODE_11: { // CODE 11 + + case self::TYPE_CODE_11: $arrcode = $this->barcode_code11($code); break; - } - case self::TYPE_PHARMA_CODE: { // PHARMACODE + + case self::TYPE_PHARMA_CODE: $arrcode = $this->barcode_pharmacode($code); break; - } - case self::TYPE_PHARMA_CODE_TWO_TRACKS: { // PHARMACODE TWO-TRACKS + + case self::TYPE_PHARMA_CODE_TWO_TRACKS: $arrcode = $this->barcode_pharmacode2t($code); break; - } - default: { + + default: throw new UnknownTypeException(); - break; - } } if ( ! isset($arrcode['maxWidth'])) { @@ -530,329 +534,6 @@ protected function checksum_code39($code) return $chars[$j]; } - /** - * CODE 93 - USS-93 - * Compact code similar to Code 39 - * - * @param $code (string) code to represent. - * @return array barcode representation. - * @protected - */ - protected function barcode_code93($code) - { - $chr = []; - $chr[48] = '131112'; // 0 - $chr[49] = '111213'; // 1 - $chr[50] = '111312'; // 2 - $chr[51] = '111411'; // 3 - $chr[52] = '121113'; // 4 - $chr[53] = '121212'; // 5 - $chr[54] = '121311'; // 6 - $chr[55] = '111114'; // 7 - $chr[56] = '131211'; // 8 - $chr[57] = '141111'; // 9 - $chr[65] = '211113'; // A - $chr[66] = '211212'; // B - $chr[67] = '211311'; // C - $chr[68] = '221112'; // D - $chr[69] = '221211'; // E - $chr[70] = '231111'; // F - $chr[71] = '112113'; // G - $chr[72] = '112212'; // H - $chr[73] = '112311'; // I - $chr[74] = '122112'; // J - $chr[75] = '132111'; // K - $chr[76] = '111123'; // L - $chr[77] = '111222'; // M - $chr[78] = '111321'; // N - $chr[79] = '121122'; // O - $chr[80] = '131121'; // P - $chr[81] = '212112'; // Q - $chr[82] = '212211'; // R - $chr[83] = '211122'; // S - $chr[84] = '211221'; // T - $chr[85] = '221121'; // U - $chr[86] = '222111'; // V - $chr[87] = '112122'; // W - $chr[88] = '112221'; // X - $chr[89] = '122121'; // Y - $chr[90] = '123111'; // Z - $chr[45] = '121131'; // - - $chr[46] = '311112'; // . - $chr[32] = '311211'; // - $chr[36] = '321111'; // $ - $chr[47] = '112131'; // / - $chr[43] = '113121'; // + - $chr[37] = '211131'; // % - $chr[128] = '121221'; // ($) - $chr[129] = '311121'; // (/) - $chr[130] = '122211'; // (+) - $chr[131] = '312111'; // (%) - $chr[42] = '111141'; // start-stop - $code = strtoupper($code); - $encode = array( - chr(0) => chr(131) . 'U', - chr(1) => chr(128) . 'A', - chr(2) => chr(128) . 'B', - chr(3) => chr(128) . 'C', - chr(4) => chr(128) . 'D', - chr(5) => chr(128) . 'E', - chr(6) => chr(128) . 'F', - chr(7) => chr(128) . 'G', - chr(8) => chr(128) . 'H', - chr(9) => chr(128) . 'I', - chr(10) => chr(128) . 'J', - chr(11) => '£K', - chr(12) => chr(128) . 'L', - chr(13) => chr(128) . 'M', - chr(14) => chr(128) . 'N', - chr(15) => chr(128) . 'O', - chr(16) => chr(128) . 'P', - chr(17) => chr(128) . 'Q', - chr(18) => chr(128) . 'R', - chr(19) => chr(128) . 'S', - chr(20) => chr(128) . 'T', - chr(21) => chr(128) . 'U', - chr(22) => chr(128) . 'V', - chr(23) => chr(128) . 'W', - chr(24) => chr(128) . 'X', - chr(25) => chr(128) . 'Y', - chr(26) => chr(128) . 'Z', - chr(27) => chr(131) . 'A', - chr(28) => chr(131) . 'B', - chr(29) => chr(131) . 'C', - chr(30) => chr(131) . 'D', - chr(31) => chr(131) . 'E', - chr(32) => ' ', - chr(33) => chr(129) . 'A', - chr(34) => chr(129) . 'B', - chr(35) => chr(129) . 'C', - chr(36) => chr(129) . 'D', - chr(37) => chr(129) . 'E', - chr(38) => chr(129) . 'F', - chr(39) => chr(129) . 'G', - chr(40) => chr(129) . 'H', - chr(41) => chr(129) . 'I', - chr(42) => chr(129) . 'J', - chr(43) => chr(129) . 'K', - chr(44) => chr(129) . 'L', - chr(45) => '-', - chr(46) => '.', - chr(47) => chr(129) . 'O', - chr(48) => '0', - chr(49) => '1', - chr(50) => '2', - chr(51) => '3', - chr(52) => '4', - chr(53) => '5', - chr(54) => '6', - chr(55) => '7', - chr(56) => '8', - chr(57) => '9', - chr(58) => chr(129) . 'Z', - chr(59) => chr(131) . 'F', - chr(60) => chr(131) . 'G', - chr(61) => chr(131) . 'H', - chr(62) => chr(131) . 'I', - chr(63) => chr(131) . 'J', - chr(64) => chr(131) . 'V', - chr(65) => 'A', - chr(66) => 'B', - chr(67) => 'C', - chr(68) => 'D', - chr(69) => 'E', - chr(70) => 'F', - chr(71) => 'G', - chr(72) => 'H', - chr(73) => 'I', - chr(74) => 'J', - chr(75) => 'K', - chr(76) => 'L', - chr(77) => 'M', - chr(78) => 'N', - chr(79) => 'O', - chr(80) => 'P', - chr(81) => 'Q', - chr(82) => 'R', - chr(83) => 'S', - chr(84) => 'T', - chr(85) => 'U', - chr(86) => 'V', - chr(87) => 'W', - chr(88) => 'X', - chr(89) => 'Y', - chr(90) => 'Z', - chr(91) => chr(131) . 'K', - chr(92) => chr(131) . 'L', - chr(93) => chr(131) . 'M', - chr(94) => chr(131) . 'N', - chr(95) => chr(131) . 'O', - chr(96) => chr(131) . 'W', - chr(97) => chr(130) . 'A', - chr(98) => chr(130) . 'B', - chr(99) => chr(130) . 'C', - chr(100) => chr(130) . 'D', - chr(101) => chr(130) . 'E', - chr(102) => chr(130) . 'F', - chr(103) => chr(130) . 'G', - chr(104) => chr(130) . 'H', - chr(105) => chr(130) . 'I', - chr(106) => chr(130) . 'J', - chr(107) => chr(130) . 'K', - chr(108) => chr(130) . 'L', - chr(109) => chr(130) . 'M', - chr(110) => chr(130) . 'N', - chr(111) => chr(130) . 'O', - chr(112) => chr(130) . 'P', - chr(113) => chr(130) . 'Q', - chr(114) => chr(130) . 'R', - chr(115) => chr(130) . 'S', - chr(116) => chr(130) . 'T', - chr(117) => chr(130) . 'U', - chr(118) => chr(130) . 'V', - chr(119) => chr(130) . 'W', - chr(120) => chr(130) . 'X', - chr(121) => chr(130) . 'Y', - chr(122) => chr(130) . 'Z', - chr(123) => chr(131) . 'P', - chr(124) => chr(131) . 'Q', - chr(125) => chr(131) . 'R', - chr(126) => chr(131) . 'S', - chr(127) => chr(131) . 'T' - ); - $code_ext = ''; - $clen = strlen($code); - for ($i = 0; $i < $clen; ++$i) { - if (ord($code[$i]) > 127) { - throw new InvalidCharacterException('Only supports till char 127'); - } - $code_ext .= $encode[$code[$i]]; - } - // checksum - $code_ext .= $this->checksum_code93($code_ext); - // add start and stop codes - $code = '*' . $code_ext . '*'; - $bararray = array('code' => $code, 'maxw' => 0, 'maxh' => 1, 'bcode' => array()); - $k = 0; - $clen = strlen($code); - for ($i = 0; $i < $clen; ++$i) { - $char = ord($code[$i]); - if ( ! isset($chr[$char])) { - throw new InvalidCharacterException('Char ' . $char . ' is unsupported'); - } - for ($j = 0; $j < 6; ++$j) { - if (($j % 2) == 0) { - $t = true; // bar - } else { - $t = false; // space - } - $w = $chr[$char][$j]; - $bararray['bcode'][$k] = array('t' => $t, 'w' => $w, 'h' => 1, 'p' => 0); - $bararray['maxw'] += $w; - ++$k; - } - } - $bararray['bcode'][$k] = array('t' => true, 'w' => 1, 'h' => 1, 'p' => 0); - $bararray['maxw'] += 1; - - return $bararray; - } - - /** - * Calculate CODE 93 checksum (modulo 47). - * - * @param $code (string) code to represent. - * @return string checksum code. - * @protected - */ - protected function checksum_code93($code) - { - $chars = array( - '0', - '1', - '2', - '3', - '4', - '5', - '6', - '7', - '8', - '9', - 'A', - 'B', - 'C', - 'D', - 'E', - 'F', - 'G', - 'H', - 'I', - 'J', - 'K', - 'L', - 'M', - 'N', - 'O', - 'P', - 'Q', - 'R', - 'S', - 'T', - 'U', - 'V', - 'W', - 'X', - 'Y', - 'Z', - '-', - '.', - ' ', - '$', - '/', - '+', - '%', - '<', - '=', - '>', - '?' - ); - // translate special characters - $code = strtr($code, chr(128) . chr(131) . chr(129) . chr(130), '<=>?'); - $len = strlen($code); - // calculate check digit C - $p = 1; - $check = 0; - for ($i = ($len - 1); $i >= 0; --$i) { - $k = array_keys($chars, $code[$i]); - $check += ($k[0] * $p); - ++$p; - if ($p > 20) { - $p = 1; - } - } - $check %= 47; - $c = $chars[$check]; - $code .= $c; - // calculate check digit K - $p = 1; - $check = 0; - for ($i = $len; $i >= 0; --$i) { - $k = array_keys($chars, $code[$i]); - $check += ($k[0] * $p); - ++$p; - if ($p > 15) { - $p = 1; - } - } - $check %= 47; - $k = $chars[$check]; - $checksum = $c . $k; - // resto respecial characters - $checksum = strtr($checksum, '<=>?', chr(128) . chr(131) . chr(129) . chr(130)); - - return $checksum; - } - /** * Checksum for standard 2 of 5 barcodes. * @@ -1017,73 +698,6 @@ protected function binseq_to_array($seq, $bararray) return $bararray; } - /** - * Interleaved 2 of 5 barcodes. - * Compact numeric code, widely used in industry, air cargo - * Contains digits (0 to 9) and encodes the data in the width of both bars and spaces. - * - * @param $code (string) code to represent. - * @param $checksum (boolean) if true add a checksum to the code - * @return array barcode representation. - * @protected - */ - protected function barcode_i25($code, $checksum = false) - { - $chr['0'] = '11221'; - $chr['1'] = '21112'; - $chr['2'] = '12112'; - $chr['3'] = '22111'; - $chr['4'] = '11212'; - $chr['5'] = '21211'; - $chr['6'] = '12211'; - $chr['7'] = '11122'; - $chr['8'] = '21121'; - $chr['9'] = '12121'; - $chr['A'] = '11'; - $chr['Z'] = '21'; - if ($checksum) { - // add checksum - $code .= $this->checksum_s25($code); - } - if ((strlen($code) % 2) != 0) { - // add leading zero if code-length is odd - $code = '0' . $code; - } - // add start and stop codes - $code = 'AA' . strtolower($code) . 'ZA'; - - $bararray = array('code' => $code, 'maxw' => 0, 'maxh' => 1, 'bcode' => array()); - $k = 0; - $clen = strlen($code); - for ($i = 0; $i < $clen; $i = ($i + 2)) { - $char_bar = $code[$i]; - $char_space = $code[$i + 1]; - if ( ! isset($chr[$char_bar]) || ! isset($chr[$char_space])) { - throw new InvalidCharacterException(); - } - // create a bar-space sequence - $seq = ''; - $chrlen = strlen($chr[$char_bar]); - for ($s = 0; $s < $chrlen; $s++) { - $seq .= $chr[$char_bar][$s] . $chr[$char_space][$s]; - } - $seqlen = strlen($seq); - for ($j = 0; $j < $seqlen; ++$j) { - if (($j % 2) == 0) { - $t = true; // bar - } else { - $t = false; // space - } - $w = $seq[$j]; - $bararray['bcode'][$k] = array('t' => $t, 'w' => $w, 'h' => 1, 'p' => 0); - $bararray['maxw'] += $w; - ++$k; - } - } - - return $bararray; - } - /** * C128 barcodes. * Very capable code, excellent density, high reliability; in very wide use world-wide diff --git a/src/Types/Type.php b/src/Types/Type.php new file mode 100644 index 0000000..e093a20 --- /dev/null +++ b/src/Types/Type.php @@ -0,0 +1,8 @@ + chr(131) . 'U', + chr(1) => chr(128) . 'A', + chr(2) => chr(128) . 'B', + chr(3) => chr(128) . 'C', + chr(4) => chr(128) . 'D', + chr(5) => chr(128) . 'E', + chr(6) => chr(128) . 'F', + chr(7) => chr(128) . 'G', + chr(8) => chr(128) . 'H', + chr(9) => chr(128) . 'I', + chr(10) => chr(128) . 'J', + chr(11) => '£K', + chr(12) => chr(128) . 'L', + chr(13) => chr(128) . 'M', + chr(14) => chr(128) . 'N', + chr(15) => chr(128) . 'O', + chr(16) => chr(128) . 'P', + chr(17) => chr(128) . 'Q', + chr(18) => chr(128) . 'R', + chr(19) => chr(128) . 'S', + chr(20) => chr(128) . 'T', + chr(21) => chr(128) . 'U', + chr(22) => chr(128) . 'V', + chr(23) => chr(128) . 'W', + chr(24) => chr(128) . 'X', + chr(25) => chr(128) . 'Y', + chr(26) => chr(128) . 'Z', + chr(27) => chr(131) . 'A', + chr(28) => chr(131) . 'B', + chr(29) => chr(131) . 'C', + chr(30) => chr(131) . 'D', + chr(31) => chr(131) . 'E', + chr(32) => ' ', + chr(33) => chr(129) . 'A', + chr(34) => chr(129) . 'B', + chr(35) => chr(129) . 'C', + chr(36) => chr(129) . 'D', + chr(37) => chr(129) . 'E', + chr(38) => chr(129) . 'F', + chr(39) => chr(129) . 'G', + chr(40) => chr(129) . 'H', + chr(41) => chr(129) . 'I', + chr(42) => chr(129) . 'J', + chr(43) => chr(129) . 'K', + chr(44) => chr(129) . 'L', + chr(45) => '-', + chr(46) => '.', + chr(47) => chr(129) . 'O', + chr(48) => '0', + chr(49) => '1', + chr(50) => '2', + chr(51) => '3', + chr(52) => '4', + chr(53) => '5', + chr(54) => '6', + chr(55) => '7', + chr(56) => '8', + chr(57) => '9', + chr(58) => chr(129) . 'Z', + chr(59) => chr(131) . 'F', + chr(60) => chr(131) . 'G', + chr(61) => chr(131) . 'H', + chr(62) => chr(131) . 'I', + chr(63) => chr(131) . 'J', + chr(64) => chr(131) . 'V', + chr(65) => 'A', + chr(66) => 'B', + chr(67) => 'C', + chr(68) => 'D', + chr(69) => 'E', + chr(70) => 'F', + chr(71) => 'G', + chr(72) => 'H', + chr(73) => 'I', + chr(74) => 'J', + chr(75) => 'K', + chr(76) => 'L', + chr(77) => 'M', + chr(78) => 'N', + chr(79) => 'O', + chr(80) => 'P', + chr(81) => 'Q', + chr(82) => 'R', + chr(83) => 'S', + chr(84) => 'T', + chr(85) => 'U', + chr(86) => 'V', + chr(87) => 'W', + chr(88) => 'X', + chr(89) => 'Y', + chr(90) => 'Z', + chr(91) => chr(131) . 'K', + chr(92) => chr(131) . 'L', + chr(93) => chr(131) . 'M', + chr(94) => chr(131) . 'N', + chr(95) => chr(131) . 'O', + chr(96) => chr(131) . 'W', + chr(97) => chr(130) . 'A', + chr(98) => chr(130) . 'B', + chr(99) => chr(130) . 'C', + chr(100) => chr(130) . 'D', + chr(101) => chr(130) . 'E', + chr(102) => chr(130) . 'F', + chr(103) => chr(130) . 'G', + chr(104) => chr(130) . 'H', + chr(105) => chr(130) . 'I', + chr(106) => chr(130) . 'J', + chr(107) => chr(130) . 'K', + chr(108) => chr(130) . 'L', + chr(109) => chr(130) . 'M', + chr(110) => chr(130) . 'N', + chr(111) => chr(130) . 'O', + chr(112) => chr(130) . 'P', + chr(113) => chr(130) . 'Q', + chr(114) => chr(130) . 'R', + chr(115) => chr(130) . 'S', + chr(116) => chr(130) . 'T', + chr(117) => chr(130) . 'U', + chr(118) => chr(130) . 'V', + chr(119) => chr(130) . 'W', + chr(120) => chr(130) . 'X', + chr(121) => chr(130) . 'Y', + chr(122) => chr(130) . 'Z', + chr(123) => chr(131) . 'P', + chr(124) => chr(131) . 'Q', + chr(125) => chr(131) . 'R', + chr(126) => chr(131) . 'S', + chr(127) => chr(131) . 'T' + ); + $code_ext = ''; + $clen = strlen($code); + for ($i = 0; $i < $clen; ++$i) { + if (ord($code[$i]) > 127) { + throw new InvalidCharacterException('Only supports till char 127'); + } + $code_ext .= $encode[$code[$i]]; + } + // checksum + $code_ext .= $this->checksum_code93($code_ext); + // add start and stop codes + $code = '*' . $code_ext . '*'; + $bararray = array('code' => $code, 'maxw' => 0, 'maxh' => 1, 'bcode' => array()); + $k = 0; + $clen = strlen($code); + for ($i = 0; $i < $clen; ++$i) { + $char = ord($code[$i]); + if ( ! isset($chr[$char])) { + throw new InvalidCharacterException('Char ' . $char . ' is unsupported'); + } + for ($j = 0; $j < 6; ++$j) { + if (($j % 2) == 0) { + $t = true; // bar + } else { + $t = false; // space + } + $w = $chr[$char][$j]; + $bararray['bcode'][$k] = array('t' => $t, 'w' => $w, 'h' => 1, 'p' => 0); + $bararray['maxw'] += $w; + ++$k; + } + } + $bararray['bcode'][$k] = array('t' => true, 'w' => 1, 'h' => 1, 'p' => 0); + $bararray['maxw'] += 1; + + return $bararray; + } + + + /** + * Calculate CODE 93 checksum (modulo 47). + * + * @param $code (string) code to represent. + * @return string checksum code. + * @protected + */ + protected function checksum_code93($code) + { + $chars = array( + '0', + '1', + '2', + '3', + '4', + '5', + '6', + '7', + '8', + '9', + 'A', + 'B', + 'C', + 'D', + 'E', + 'F', + 'G', + 'H', + 'I', + 'J', + 'K', + 'L', + 'M', + 'N', + 'O', + 'P', + 'Q', + 'R', + 'S', + 'T', + 'U', + 'V', + 'W', + 'X', + 'Y', + 'Z', + '-', + '.', + ' ', + '$', + '/', + '+', + '%', + '<', + '=', + '>', + '?' + ); + // translate special characters + $code = strtr($code, chr(128) . chr(131) . chr(129) . chr(130), '<=>?'); + $len = strlen($code); + // calculate check digit C + $p = 1; + $check = 0; + for ($i = ($len - 1); $i >= 0; --$i) { + $k = array_keys($chars, $code[$i]); + $check += ($k[0] * $p); + ++$p; + if ($p > 20) { + $p = 1; + } + } + $check %= 47; + $c = $chars[$check]; + $code .= $c; + // calculate check digit K + $p = 1; + $check = 0; + for ($i = $len; $i >= 0; --$i) { + $k = array_keys($chars, $code[$i]); + $check += ($k[0] * $p); + ++$p; + if ($p > 15) { + $p = 1; + } + } + $check %= 47; + $k = $chars[$check]; + $checksum = $c . $k; + // resto respecial characters + $checksum = strtr($checksum, '<=>?', chr(128) . chr(131) . chr(129) . chr(130)); + + return $checksum; + } +} diff --git a/src/Types/TypeInterleaved25.php b/src/Types/TypeInterleaved25.php new file mode 100644 index 0000000..18d9ce4 --- /dev/null +++ b/src/Types/TypeInterleaved25.php @@ -0,0 +1,17 @@ +getChecksum($code); + + if ((strlen($code) % 2) != 0) { + // add leading zero if code-length is odd + $code = '0' . $code; + } + // add start and stop codes + $code = 'AA' . strtolower($code) . 'ZA'; + + $bararray = array('code' => $code, 'maxw' => 0, 'maxh' => 1, 'bcode' => array()); + $k = 0; + $clen = strlen($code); + for ($i = 0; $i < $clen; $i = ($i + 2)) { + $char_bar = $code[$i]; + $char_space = $code[$i + 1]; + if ( ! isset($chr[$char_bar]) || ! isset($chr[$char_space])) { + throw new InvalidCharacterException(); + } + // create a bar-space sequence + $seq = ''; + $chrlen = strlen($chr[$char_bar]); + for ($s = 0; $s < $chrlen; $s++) { + $seq .= $chr[$char_bar][$s] . $chr[$char_space][$s]; + } + $seqlen = strlen($seq); + for ($j = 0; $j < $seqlen; ++$j) { + if (($j % 2) == 0) { + $t = true; // bar + } else { + $t = false; // space + } + $w = $seq[$j]; + $bararray['bcode'][$k] = array('t' => $t, 'w' => $w, 'h' => 1, 'p' => 0); + $bararray['maxw'] += $w; + ++$k; + } + } + + return $bararray; + } + + protected function getChecksum(string $code): string + { + $len = strlen($code); + $sum = 0; + for ($i = 0; $i < $len; $i += 2) { + $sum += $code[$i]; + } + $sum *= 3; + for ($i = 1; $i < $len; $i += 2) { + $sum += ($code[$i]); + } + $r = $sum % 10; + if ($r > 0) { + $r = (10 - $r); + } + + return $r; + } +} From 5ccfd8090e52c33092fe355b60e49f6936e7f075 Mon Sep 17 00:00:00 2001 From: Casper Bakker Date: Wed, 1 Jan 2020 11:17:56 +0100 Subject: [PATCH 015/155] Add tests for output of all supported barcodes --- .phpunit.result.cache | 1 + generate-verified-files.php | 18 +++- src/BarcodeGenerator.php | 2 +- tests/BarcodeTest.php | 2 +- tests/VerifiedBarcodeTest.php | 58 ++++++++++ tests/verified-files/C128-081231723897.svg | 37 +++++++ .../C128-1234567890abcABC-283*33.svg | 76 +++++++++++++ tests/verified-files/C128A-1234567890.svg | 49 +++++++++ tests/verified-files/C128B-081231723897.svg | 55 ++++++++++ .../C128B-1234567890abcABC-283*33.svg | 88 +++++++++++++++ .../verified-files/C39+-1234567890abcABC.svg | 102 ++++++++++++++++++ tests/verified-files/C39-1234567890abcABC.svg | 97 +++++++++++++++++ .../verified-files/C39E+-1234567890abcABC.svg | 102 ++++++++++++++++++ tests/verified-files/C93-1234567890abcABC.svg | 68 ++++++++++++ tests/verified-files/CODABAR-123456789.svg | 51 +++++++++ tests/verified-files/CODE11-123456789.svg | 43 ++++++++ tests/verified-files/EAN13-004900000463.svg | 37 +++++++ tests/verified-files/EAN13-0049000004632.svg | 37 +++++++ tests/verified-files/EAN13-081231723897.svg | 37 +++++++ .../EAN2-1234567890abcABC-283*33.svg | 14 +++ .../EAN5-1234567890abcABC-283*33.svg | 23 ++++ tests/verified-files/EAN8-1234568.svg | 29 +++++ tests/verified-files/I25+-1234567890.svg | 41 +++++++ tests/verified-files/I25-1234567890.svg | 36 +++++++ tests/verified-files/IMB-123456789.svg | 72 +++++++++++++ tests/verified-files/KIX-123456789.svg | 43 ++++++++ tests/verified-files/MSI+-123456789.svg | 50 +++++++++ tests/verified-files/MSI-123456789.svg | 46 ++++++++ tests/verified-files/PHARMA-123456789.svg | 33 ++++++ tests/verified-files/PHARMA2T-123456789.svg | 24 +++++ tests/verified-files/PLANET-123456789.svg | 59 ++++++++++ tests/verified-files/POSTNET-123456789.svg | 59 ++++++++++ tests/verified-files/RMS4CC-123456789.svg | 49 +++++++++ tests/verified-files/S25+-1234567890.svg | 73 +++++++++++++ tests/verified-files/S25-1234567890.svg | 63 +++++++++++ tests/verified-files/UPCA-123456789.svg | 37 +++++++ tests/verified-files/UPCE-123456789.svg | 24 +++++ 37 files changed, 1728 insertions(+), 7 deletions(-) create mode 100644 .phpunit.result.cache create mode 100644 tests/VerifiedBarcodeTest.php create mode 100644 tests/verified-files/C128-081231723897.svg create mode 100644 tests/verified-files/C128-1234567890abcABC-283*33.svg create mode 100644 tests/verified-files/C128A-1234567890.svg create mode 100644 tests/verified-files/C128B-081231723897.svg create mode 100644 tests/verified-files/C128B-1234567890abcABC-283*33.svg create mode 100644 tests/verified-files/C39+-1234567890abcABC.svg create mode 100644 tests/verified-files/C39-1234567890abcABC.svg create mode 100644 tests/verified-files/C39E+-1234567890abcABC.svg create mode 100644 tests/verified-files/C93-1234567890abcABC.svg create mode 100644 tests/verified-files/CODABAR-123456789.svg create mode 100644 tests/verified-files/CODE11-123456789.svg create mode 100644 tests/verified-files/EAN13-004900000463.svg create mode 100644 tests/verified-files/EAN13-0049000004632.svg create mode 100644 tests/verified-files/EAN13-081231723897.svg create mode 100644 tests/verified-files/EAN2-1234567890abcABC-283*33.svg create mode 100644 tests/verified-files/EAN5-1234567890abcABC-283*33.svg create mode 100644 tests/verified-files/EAN8-1234568.svg create mode 100644 tests/verified-files/I25+-1234567890.svg create mode 100644 tests/verified-files/I25-1234567890.svg create mode 100644 tests/verified-files/IMB-123456789.svg create mode 100644 tests/verified-files/KIX-123456789.svg create mode 100644 tests/verified-files/MSI+-123456789.svg create mode 100644 tests/verified-files/MSI-123456789.svg create mode 100644 tests/verified-files/PHARMA-123456789.svg create mode 100644 tests/verified-files/PHARMA2T-123456789.svg create mode 100644 tests/verified-files/PLANET-123456789.svg create mode 100644 tests/verified-files/POSTNET-123456789.svg create mode 100644 tests/verified-files/RMS4CC-123456789.svg create mode 100644 tests/verified-files/S25+-1234567890.svg create mode 100644 tests/verified-files/S25-1234567890.svg create mode 100644 tests/verified-files/UPCA-123456789.svg create mode 100644 tests/verified-files/UPCE-123456789.svg diff --git a/.phpunit.result.cache b/.phpunit.result.cache new file mode 100644 index 0000000..bdee210 --- /dev/null +++ b/.phpunit.result.cache @@ -0,0 +1 @@ +C:37:"PHPUnit\Runner\DefaultTestResultCache":3096:{a:2:{s:7:"defects";a:1:{s:56:"BarcodeTest::test_generator_can_generate_code_39_barcode";i:3;}s:5:"times";a:40:{s:69:"BarcodeTest::test_png_barcode_generator_can_generate_code_128_barcode";d:0.003;s:67:"BarcodeTest::test_svg_barcode_generator_can_generate_ean_13_barcode";d:0;s:70:"BarcodeTest::test_html_barcode_generator_can_generate_code_128_barcode";d:0;s:69:"BarcodeTest::test_jpg_barcode_generator_can_generate_code_128_barcode";d:0;s:76:"BarcodeTest::test_ean13_generator_throws_exception_if_invalid_chars_are_used";d:0.001;s:52:"BarcodeTest::test_ean13_generator_accepting_13_chars";d:0;s:83:"BarcodeTest::test_ean13_generator_accepting_12_chars_and_generates_13th_check_digit";d:0;s:105:"BarcodeTest::test_ean13_generator_accepting_11_chars_and_generates_13th_check_digit_and_adds_leading_zero";d:0;s:82:"BarcodeTest::test_ean13_generator_throws_exception_when_wrong_check_digit_is_given";d:0;s:58:"BarcodeTest::test_generator_throws_unknown_type_exceptions";d:0;s:56:"BarcodeTest::test_generator_can_generate_code_39_barcode";d:0;s:65:"BarcodeTest::test_generator_can_generate_code_39_checksum_barcode";d:0;s:74:"BarcodeTest::test_generator_can_generate_code_39_extended_checksum_barcode";d:0;s:56:"BarcodeTest::test_generator_can_generate_code_93_barcode";d:0.001;s:61:"BarcodeTest::test_generator_can_generate_standard_2_5_barcode";d:0;s:70:"BarcodeTest::test_generator_can_generate_standard_2_5_checksum_barcode";d:0;s:64:"BarcodeTest::test_generator_can_generate_interleaved_2_5_barcode";d:0;s:73:"BarcodeTest::test_generator_can_generate_interleaved_2_5_checksum_barcode";d:0;s:57:"BarcodeTest::test_generator_can_generate_code_128_barcode";d:0;s:59:"BarcodeTest::test_generator_can_generate_code_128_a_barcode";d:0;s:59:"BarcodeTest::test_generator_can_generate_code_128_b_barcode";d:0;s:59:"BarcodeTest::test_generator_can_generate_code_128_c_barcode";d:0;s:54:"BarcodeTest::test_generator_can_generate_ean_2_barcode";d:0;s:54:"BarcodeTest::test_generator_can_generate_ean_5_barcode";d:0;s:54:"BarcodeTest::test_generator_can_generate_ean_8_barcode";d:0;s:55:"BarcodeTest::test_generator_can_generate_ean_13_barcode";d:0;s:54:"BarcodeTest::test_generator_can_generate_upc_a_barcode";d:0;s:54:"BarcodeTest::test_generator_can_generate_upc_e_barcode";d:0;s:52:"BarcodeTest::test_generator_can_generate_msi_barcode";d:0;s:61:"BarcodeTest::test_generator_can_generate_msi_checksum_barcode";d:0;s:56:"BarcodeTest::test_generator_can_generate_postnet_barcode";d:0;s:55:"BarcodeTest::test_generator_can_generate_planet_barcode";d:0;s:55:"BarcodeTest::test_generator_can_generate_rms4cc_barcode";d:0;s:52:"BarcodeTest::test_generator_can_generate_kix_barcode";d:0;s:52:"BarcodeTest::test_generator_can_generate_imb_barcode";d:0.009;s:56:"BarcodeTest::test_generator_can_generate_codabar_barcode";d:0;s:56:"BarcodeTest::test_generator_can_generate_code_11_barcode";d:0;s:60:"BarcodeTest::test_generator_can_generate_pharma_code_barcode";d:0;s:69:"BarcodeTest::test_generator_can_generate_pharma_code_2_tracks_barcode";d:0;s:49:"VerifiedBarcodeTest::testAllSupportedBarcodeTypes";d:0.014;}}} \ No newline at end of file diff --git a/generate-verified-files.php b/generate-verified-files.php index b035d87..174767d 100644 --- a/generate-verified-files.php +++ b/generate-verified-files.php @@ -1,10 +1,6 @@ getBarcode('081231723897', $generatorSVG::TYPE_EAN_13)); @@ -14,3 +10,15 @@ $generatorSVG = new Picqer\Barcode\BarcodeGeneratorSVG(); file_put_contents('tests/verified-files/0049000004632-ean13.svg', $generatorSVG->getBarcode('0049000004632', $generatorSVG::TYPE_EAN_13)); + + +// New style of verified files +require('tests/VerifiedBarcodeTest.php'); +$verifiedFiles = VerifiedBarcodeTest::$supportedBarcodes; + +$generatorSVG = new Picqer\Barcode\BarcodeGeneratorSVG(); +foreach ($verifiedFiles as $verifiedFile) { + foreach ($verifiedFile['barcodes'] as $barcode) { + file_put_contents('tests/verified-files/' . $verifiedFile['type'] . '-' . $barcode . '.svg', $generatorSVG->getBarcode($barcode, $verifiedFile['type'])); + } +} diff --git a/src/BarcodeGenerator.php b/src/BarcodeGenerator.php index 80766a7..fd19c60 100644 --- a/src/BarcodeGenerator.php +++ b/src/BarcodeGenerator.php @@ -9,7 +9,7 @@ // Copyright (C) 2002-2015 Nicola Asuni - Tecnick.com LTD // -// This file is part of TCPDF software library. +// This file was part of TCPDF software library. // // TCPDF is free software: you can redistribute it and/or modify it // under the terms of the GNU Lesser General Public License as diff --git a/tests/BarcodeTest.php b/tests/BarcodeTest.php index 92e1a30..ad8f917 100644 --- a/tests/BarcodeTest.php +++ b/tests/BarcodeTest.php @@ -89,7 +89,7 @@ public function test_generator_can_generate_code_39_barcode() $generator = new Picqer\Barcode\BarcodeGeneratorSVG(); $result = $generator->getBarcode('1234567890abcABC', $generator::TYPE_CODE_39); - $this->assertGreaterThan(100, strlen($result)); + $this->assertStringEqualsFile('tests/verified-files/C39-1234567890abcABC.svg', $result); } public function test_generator_can_generate_code_39_checksum_barcode() diff --git a/tests/VerifiedBarcodeTest.php b/tests/VerifiedBarcodeTest.php new file mode 100644 index 0000000..e644708 --- /dev/null +++ b/tests/VerifiedBarcodeTest.php @@ -0,0 +1,58 @@ +getBarcode($barcode, $barcodeTestSet['type']); + + $this->assertStringEqualsFile(sprintf('tests/verified-files/%s-%s.svg', $barcodeTestSet['type'], $barcode), $result); + } + } + } + + public static $supportedBarcodes = [ + ['type' => \Picqer\Barcode\BarcodeGenerator::TYPE_CODE_39, 'barcodes' => ['1234567890abcABC']], + ['type' => \Picqer\Barcode\BarcodeGenerator::TYPE_CODE_39_CHECKSUM, 'barcodes' => ['1234567890abcABC']], + ['type' => \Picqer\Barcode\BarcodeGenerator::TYPE_CODE_39E_CHECKSUM, 'barcodes' => ['1234567890abcABC']], + ['type' => \Picqer\Barcode\BarcodeGenerator::TYPE_CODE_93, 'barcodes' => ['1234567890abcABC']], + ['type' => \Picqer\Barcode\BarcodeGenerator::TYPE_STANDARD_2_5, 'barcodes' => ['1234567890']], + ['type' => \Picqer\Barcode\BarcodeGenerator::TYPE_STANDARD_2_5_CHECKSUM, 'barcodes' => ['1234567890']], + ['type' => \Picqer\Barcode\BarcodeGenerator::TYPE_INTERLEAVED_2_5, 'barcodes' => ['1234567890']], + ['type' => \Picqer\Barcode\BarcodeGenerator::TYPE_INTERLEAVED_2_5_CHECKSUM, 'barcodes' => ['1234567890']], + ['type' => \Picqer\Barcode\BarcodeGenerator::TYPE_EAN_13, 'barcodes' => ['081231723897', '0049000004632', '004900000463']], + ['type' => \Picqer\Barcode\BarcodeGenerator::TYPE_CODE_128, 'barcodes' => ['081231723897', '1234567890abcABC-283*33']], + ['type' => \Picqer\Barcode\BarcodeGenerator::TYPE_CODE_128_A, 'barcodes' => ['1234567890']], + ['type' => \Picqer\Barcode\BarcodeGenerator::TYPE_CODE_128_B, 'barcodes' => ['081231723897', '1234567890abcABC-283*33']], + ['type' => \Picqer\Barcode\BarcodeGenerator::TYPE_EAN_2, 'barcodes' => ['1234567890abcABC-283*33']], + ['type' => \Picqer\Barcode\BarcodeGenerator::TYPE_EAN_5, 'barcodes' => ['1234567890abcABC-283*33']], + ['type' => \Picqer\Barcode\BarcodeGenerator::TYPE_EAN_8, 'barcodes' => ['1234568']], + ['type' => \Picqer\Barcode\BarcodeGenerator::TYPE_UPC_A, 'barcodes' => ['123456789']], + ['type' => \Picqer\Barcode\BarcodeGenerator::TYPE_UPC_E, 'barcodes' => ['123456789']], + ['type' => \Picqer\Barcode\BarcodeGenerator::TYPE_MSI, 'barcodes' => ['123456789']], + ['type' => \Picqer\Barcode\BarcodeGenerator::TYPE_MSI_CHECKSUM, 'barcodes' => ['123456789']], + ['type' => \Picqer\Barcode\BarcodeGenerator::TYPE_POSTNET, 'barcodes' => ['123456789']], + ['type' => \Picqer\Barcode\BarcodeGenerator::TYPE_PLANET, 'barcodes' => ['123456789']], + ['type' => \Picqer\Barcode\BarcodeGenerator::TYPE_RMS4CC, 'barcodes' => ['123456789']], + ['type' => \Picqer\Barcode\BarcodeGenerator::TYPE_KIX, 'barcodes' => ['123456789']], + ['type' => \Picqer\Barcode\BarcodeGenerator::TYPE_IMB, 'barcodes' => ['123456789']], + ['type' => \Picqer\Barcode\BarcodeGenerator::TYPE_CODABAR, 'barcodes' => ['123456789']], + ['type' => \Picqer\Barcode\BarcodeGenerator::TYPE_CODE_11, 'barcodes' => ['123456789']], + ['type' => \Picqer\Barcode\BarcodeGenerator::TYPE_PHARMA_CODE, 'barcodes' => ['123456789']], + ['type' => \Picqer\Barcode\BarcodeGenerator::TYPE_PHARMA_CODE_TWO_TRACKS, 'barcodes' => ['123456789']], + ]; +} \ No newline at end of file diff --git a/tests/verified-files/C128-081231723897.svg b/tests/verified-files/C128-081231723897.svg new file mode 100644 index 0000000..7d5486f --- /dev/null +++ b/tests/verified-files/C128-081231723897.svg @@ -0,0 +1,37 @@ + + + + 081231723897 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/tests/verified-files/C128-1234567890abcABC-283*33.svg b/tests/verified-files/C128-1234567890abcABC-283*33.svg new file mode 100644 index 0000000..8184e9b --- /dev/null +++ b/tests/verified-files/C128-1234567890abcABC-283*33.svg @@ -0,0 +1,76 @@ + + + + 1234567890abcABC-283*33 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/tests/verified-files/C128A-1234567890.svg b/tests/verified-files/C128A-1234567890.svg new file mode 100644 index 0000000..0ca2f72 --- /dev/null +++ b/tests/verified-files/C128A-1234567890.svg @@ -0,0 +1,49 @@ + + + + 1234567890 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/tests/verified-files/C128B-081231723897.svg b/tests/verified-files/C128B-081231723897.svg new file mode 100644 index 0000000..3af5645 --- /dev/null +++ b/tests/verified-files/C128B-081231723897.svg @@ -0,0 +1,55 @@ + + + + 081231723897 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/tests/verified-files/C128B-1234567890abcABC-283*33.svg b/tests/verified-files/C128B-1234567890abcABC-283*33.svg new file mode 100644 index 0000000..2a1f71e --- /dev/null +++ b/tests/verified-files/C128B-1234567890abcABC-283*33.svg @@ -0,0 +1,88 @@ + + + + 1234567890abcABC-283*33 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/tests/verified-files/C39+-1234567890abcABC.svg b/tests/verified-files/C39+-1234567890abcABC.svg new file mode 100644 index 0000000..f5ae78d --- /dev/null +++ b/tests/verified-files/C39+-1234567890abcABC.svg @@ -0,0 +1,102 @@ + + + + *1234567890ABCABCP* + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/tests/verified-files/C39-1234567890abcABC.svg b/tests/verified-files/C39-1234567890abcABC.svg new file mode 100644 index 0000000..a1b9792 --- /dev/null +++ b/tests/verified-files/C39-1234567890abcABC.svg @@ -0,0 +1,97 @@ + + + + *1234567890ABCABC* + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/tests/verified-files/C39E+-1234567890abcABC.svg b/tests/verified-files/C39E+-1234567890abcABC.svg new file mode 100644 index 0000000..f5ae78d --- /dev/null +++ b/tests/verified-files/C39E+-1234567890abcABC.svg @@ -0,0 +1,102 @@ + + + + *1234567890ABCABCP* + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/tests/verified-files/C93-1234567890abcABC.svg b/tests/verified-files/C93-1234567890abcABC.svg new file mode 100644 index 0000000..2e18423 --- /dev/null +++ b/tests/verified-files/C93-1234567890abcABC.svg @@ -0,0 +1,68 @@ + + + + *1234567890ABCABC2N* + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/tests/verified-files/CODABAR-123456789.svg b/tests/verified-files/CODABAR-123456789.svg new file mode 100644 index 0000000..0d9606a --- /dev/null +++ b/tests/verified-files/CODABAR-123456789.svg @@ -0,0 +1,51 @@ + + + + 123456789 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/tests/verified-files/CODE11-123456789.svg b/tests/verified-files/CODE11-123456789.svg new file mode 100644 index 0000000..f30d919 --- /dev/null +++ b/tests/verified-files/CODE11-123456789.svg @@ -0,0 +1,43 @@ + + + + 123456789 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/tests/verified-files/EAN13-004900000463.svg b/tests/verified-files/EAN13-004900000463.svg new file mode 100644 index 0000000..427ff40 --- /dev/null +++ b/tests/verified-files/EAN13-004900000463.svg @@ -0,0 +1,37 @@ + + + + 0049000004632 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/tests/verified-files/EAN13-0049000004632.svg b/tests/verified-files/EAN13-0049000004632.svg new file mode 100644 index 0000000..427ff40 --- /dev/null +++ b/tests/verified-files/EAN13-0049000004632.svg @@ -0,0 +1,37 @@ + + + + 0049000004632 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/tests/verified-files/EAN13-081231723897.svg b/tests/verified-files/EAN13-081231723897.svg new file mode 100644 index 0000000..96e1619 --- /dev/null +++ b/tests/verified-files/EAN13-081231723897.svg @@ -0,0 +1,37 @@ + + + + 0812317238973 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/tests/verified-files/EAN2-1234567890abcABC-283*33.svg b/tests/verified-files/EAN2-1234567890abcABC-283*33.svg new file mode 100644 index 0000000..74b92de --- /dev/null +++ b/tests/verified-files/EAN2-1234567890abcABC-283*33.svg @@ -0,0 +1,14 @@ + + + + 1234567890abcABC-283*33 + + + + + + + + + + diff --git a/tests/verified-files/EAN5-1234567890abcABC-283*33.svg b/tests/verified-files/EAN5-1234567890abcABC-283*33.svg new file mode 100644 index 0000000..df18eae --- /dev/null +++ b/tests/verified-files/EAN5-1234567890abcABC-283*33.svg @@ -0,0 +1,23 @@ + + + + 1234567890abcABC-283*33 + + + + + + + + + + + + + + + + + + + diff --git a/tests/verified-files/EAN8-1234568.svg b/tests/verified-files/EAN8-1234568.svg new file mode 100644 index 0000000..9c4e210 --- /dev/null +++ b/tests/verified-files/EAN8-1234568.svg @@ -0,0 +1,29 @@ + + + + 12345687 + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/tests/verified-files/I25+-1234567890.svg b/tests/verified-files/I25+-1234567890.svg new file mode 100644 index 0000000..55f32b0 --- /dev/null +++ b/tests/verified-files/I25+-1234567890.svg @@ -0,0 +1,41 @@ + + + + AA012345678905ZA + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/tests/verified-files/I25-1234567890.svg b/tests/verified-files/I25-1234567890.svg new file mode 100644 index 0000000..77af1b4 --- /dev/null +++ b/tests/verified-files/I25-1234567890.svg @@ -0,0 +1,36 @@ + + + + AA1234567890ZA + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/tests/verified-files/IMB-123456789.svg b/tests/verified-files/IMB-123456789.svg new file mode 100644 index 0000000..a82dcd9 --- /dev/null +++ b/tests/verified-files/IMB-123456789.svg @@ -0,0 +1,72 @@ + + + + 123456789 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/tests/verified-files/KIX-123456789.svg b/tests/verified-files/KIX-123456789.svg new file mode 100644 index 0000000..0c05177 --- /dev/null +++ b/tests/verified-files/KIX-123456789.svg @@ -0,0 +1,43 @@ + + + + 123456789 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/tests/verified-files/MSI+-123456789.svg b/tests/verified-files/MSI+-123456789.svg new file mode 100644 index 0000000..de5eae8 --- /dev/null +++ b/tests/verified-files/MSI+-123456789.svg @@ -0,0 +1,50 @@ + + + + 1234567892 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/tests/verified-files/MSI-123456789.svg b/tests/verified-files/MSI-123456789.svg new file mode 100644 index 0000000..89c0090 --- /dev/null +++ b/tests/verified-files/MSI-123456789.svg @@ -0,0 +1,46 @@ + + + + 123456789 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/tests/verified-files/PHARMA-123456789.svg b/tests/verified-files/PHARMA-123456789.svg new file mode 100644 index 0000000..f3561bb --- /dev/null +++ b/tests/verified-files/PHARMA-123456789.svg @@ -0,0 +1,33 @@ + + + + 0 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/tests/verified-files/PHARMA2T-123456789.svg b/tests/verified-files/PHARMA2T-123456789.svg new file mode 100644 index 0000000..6f9368f --- /dev/null +++ b/tests/verified-files/PHARMA2T-123456789.svg @@ -0,0 +1,24 @@ + + + + 0 + + + + + + + + + + + + + + + + + + + + diff --git a/tests/verified-files/PLANET-123456789.svg b/tests/verified-files/PLANET-123456789.svg new file mode 100644 index 0000000..51de3aa --- /dev/null +++ b/tests/verified-files/PLANET-123456789.svg @@ -0,0 +1,59 @@ + + + + 123456789 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/tests/verified-files/POSTNET-123456789.svg b/tests/verified-files/POSTNET-123456789.svg new file mode 100644 index 0000000..ca895ec --- /dev/null +++ b/tests/verified-files/POSTNET-123456789.svg @@ -0,0 +1,59 @@ + + + + 123456789 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/tests/verified-files/RMS4CC-123456789.svg b/tests/verified-files/RMS4CC-123456789.svg new file mode 100644 index 0000000..ee91be7 --- /dev/null +++ b/tests/verified-files/RMS4CC-123456789.svg @@ -0,0 +1,49 @@ + + + + 123456789 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/tests/verified-files/S25+-1234567890.svg b/tests/verified-files/S25+-1234567890.svg new file mode 100644 index 0000000..4271d9f --- /dev/null +++ b/tests/verified-files/S25+-1234567890.svg @@ -0,0 +1,73 @@ + + + + 012345678905 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/tests/verified-files/S25-1234567890.svg b/tests/verified-files/S25-1234567890.svg new file mode 100644 index 0000000..2a6051d --- /dev/null +++ b/tests/verified-files/S25-1234567890.svg @@ -0,0 +1,63 @@ + + + + 1234567890 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/tests/verified-files/UPCA-123456789.svg b/tests/verified-files/UPCA-123456789.svg new file mode 100644 index 0000000..d7ed6dc --- /dev/null +++ b/tests/verified-files/UPCA-123456789.svg @@ -0,0 +1,37 @@ + + + + 0001234567895 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/tests/verified-files/UPCE-123456789.svg b/tests/verified-files/UPCE-123456789.svg new file mode 100644 index 0000000..4347e92 --- /dev/null +++ b/tests/verified-files/UPCE-123456789.svg @@ -0,0 +1,24 @@ + + + + 012349 + + + + + + + + + + + + + + + + + + + + From f1cb0f94ce0a4e360f212ce56882593ea87def33 Mon Sep 17 00:00:00 2001 From: Casper Bakker Date: Wed, 1 Jan 2020 11:24:51 +0100 Subject: [PATCH 016/155] Fix test for EAN 2 --- tests/BarcodeTest.php | 2 +- tests/VerifiedBarcodeTest.php | 2 +- tests/verified-files/1234567890-code128a.svg | 49 ----------- tests/verified-files/1234567890-code128c.svg | 34 ------- .../1234567890abcABC-283*33-code128.svg | 76 ---------------- .../1234567890abcABC-283*33-code128b.svg | 88 ------------------- ...234567890abcABC-283*33.svg => EAN2-22.svg} | 4 +- 7 files changed, 4 insertions(+), 251 deletions(-) delete mode 100644 tests/verified-files/1234567890-code128a.svg delete mode 100644 tests/verified-files/1234567890-code128c.svg delete mode 100644 tests/verified-files/1234567890abcABC-283*33-code128.svg delete mode 100644 tests/verified-files/1234567890abcABC-283*33-code128b.svg rename tests/verified-files/{EAN2-1234567890abcABC-283*33.svg => EAN2-22.svg} (87%) diff --git a/tests/BarcodeTest.php b/tests/BarcodeTest.php index ad8f917..b235cae 100644 --- a/tests/BarcodeTest.php +++ b/tests/BarcodeTest.php @@ -183,7 +183,7 @@ public function test_generator_can_generate_code_128_c_barcode() public function test_generator_can_generate_ean_2_barcode() { $generator = new Picqer\Barcode\BarcodeGeneratorSVG(); - $result = $generator->getBarcode('1234567890abcABC-283*33', $generator::TYPE_EAN_2); + $result = $generator->getBarcode('22', $generator::TYPE_EAN_2); $this->assertGreaterThan(100, strlen($result)); } diff --git a/tests/VerifiedBarcodeTest.php b/tests/VerifiedBarcodeTest.php index e644708..7f118f6 100644 --- a/tests/VerifiedBarcodeTest.php +++ b/tests/VerifiedBarcodeTest.php @@ -38,7 +38,7 @@ public function testAllSupportedBarcodeTypes() ['type' => \Picqer\Barcode\BarcodeGenerator::TYPE_CODE_128, 'barcodes' => ['081231723897', '1234567890abcABC-283*33']], ['type' => \Picqer\Barcode\BarcodeGenerator::TYPE_CODE_128_A, 'barcodes' => ['1234567890']], ['type' => \Picqer\Barcode\BarcodeGenerator::TYPE_CODE_128_B, 'barcodes' => ['081231723897', '1234567890abcABC-283*33']], - ['type' => \Picqer\Barcode\BarcodeGenerator::TYPE_EAN_2, 'barcodes' => ['1234567890abcABC-283*33']], + ['type' => \Picqer\Barcode\BarcodeGenerator::TYPE_EAN_2, 'barcodes' => ['22']], ['type' => \Picqer\Barcode\BarcodeGenerator::TYPE_EAN_5, 'barcodes' => ['1234567890abcABC-283*33']], ['type' => \Picqer\Barcode\BarcodeGenerator::TYPE_EAN_8, 'barcodes' => ['1234568']], ['type' => \Picqer\Barcode\BarcodeGenerator::TYPE_UPC_A, 'barcodes' => ['123456789']], diff --git a/tests/verified-files/1234567890-code128a.svg b/tests/verified-files/1234567890-code128a.svg deleted file mode 100644 index 0ca2f72..0000000 --- a/tests/verified-files/1234567890-code128a.svg +++ /dev/null @@ -1,49 +0,0 @@ - - - - 1234567890 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/tests/verified-files/1234567890-code128c.svg b/tests/verified-files/1234567890-code128c.svg deleted file mode 100644 index b4d8b1d..0000000 --- a/tests/verified-files/1234567890-code128c.svg +++ /dev/null @@ -1,34 +0,0 @@ - - - - 1234567890 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/tests/verified-files/1234567890abcABC-283*33-code128.svg b/tests/verified-files/1234567890abcABC-283*33-code128.svg deleted file mode 100644 index 8184e9b..0000000 --- a/tests/verified-files/1234567890abcABC-283*33-code128.svg +++ /dev/null @@ -1,76 +0,0 @@ - - - - 1234567890abcABC-283*33 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/tests/verified-files/1234567890abcABC-283*33-code128b.svg b/tests/verified-files/1234567890abcABC-283*33-code128b.svg deleted file mode 100644 index 2a1f71e..0000000 --- a/tests/verified-files/1234567890abcABC-283*33-code128b.svg +++ /dev/null @@ -1,88 +0,0 @@ - - - - 1234567890abcABC-283*33 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/tests/verified-files/EAN2-1234567890abcABC-283*33.svg b/tests/verified-files/EAN2-22.svg similarity index 87% rename from tests/verified-files/EAN2-1234567890abcABC-283*33.svg rename to tests/verified-files/EAN2-22.svg index 74b92de..d7f285d 100644 --- a/tests/verified-files/EAN2-1234567890abcABC-283*33.svg +++ b/tests/verified-files/EAN2-22.svg @@ -1,11 +1,11 @@ - 1234567890abcABC-283*33 + 22 - + From 5100eb84462ed854cab95691a4f913d2bc083063 Mon Sep 17 00:00:00 2001 From: Casper Bakker Date: Wed, 1 Jan 2020 11:30:42 +0100 Subject: [PATCH 017/155] Fix tests wiith new verified files --- .phpunit.result.cache | 1 - tests/BarcodeTest.php | 14 ++------ tests/VerifiedBarcodeTest.php | 65 ++++++++++++++++++----------------- 3 files changed, 36 insertions(+), 44 deletions(-) delete mode 100644 .phpunit.result.cache diff --git a/.phpunit.result.cache b/.phpunit.result.cache deleted file mode 100644 index bdee210..0000000 --- a/.phpunit.result.cache +++ /dev/null @@ -1 +0,0 @@ -C:37:"PHPUnit\Runner\DefaultTestResultCache":3096:{a:2:{s:7:"defects";a:1:{s:56:"BarcodeTest::test_generator_can_generate_code_39_barcode";i:3;}s:5:"times";a:40:{s:69:"BarcodeTest::test_png_barcode_generator_can_generate_code_128_barcode";d:0.003;s:67:"BarcodeTest::test_svg_barcode_generator_can_generate_ean_13_barcode";d:0;s:70:"BarcodeTest::test_html_barcode_generator_can_generate_code_128_barcode";d:0;s:69:"BarcodeTest::test_jpg_barcode_generator_can_generate_code_128_barcode";d:0;s:76:"BarcodeTest::test_ean13_generator_throws_exception_if_invalid_chars_are_used";d:0.001;s:52:"BarcodeTest::test_ean13_generator_accepting_13_chars";d:0;s:83:"BarcodeTest::test_ean13_generator_accepting_12_chars_and_generates_13th_check_digit";d:0;s:105:"BarcodeTest::test_ean13_generator_accepting_11_chars_and_generates_13th_check_digit_and_adds_leading_zero";d:0;s:82:"BarcodeTest::test_ean13_generator_throws_exception_when_wrong_check_digit_is_given";d:0;s:58:"BarcodeTest::test_generator_throws_unknown_type_exceptions";d:0;s:56:"BarcodeTest::test_generator_can_generate_code_39_barcode";d:0;s:65:"BarcodeTest::test_generator_can_generate_code_39_checksum_barcode";d:0;s:74:"BarcodeTest::test_generator_can_generate_code_39_extended_checksum_barcode";d:0;s:56:"BarcodeTest::test_generator_can_generate_code_93_barcode";d:0.001;s:61:"BarcodeTest::test_generator_can_generate_standard_2_5_barcode";d:0;s:70:"BarcodeTest::test_generator_can_generate_standard_2_5_checksum_barcode";d:0;s:64:"BarcodeTest::test_generator_can_generate_interleaved_2_5_barcode";d:0;s:73:"BarcodeTest::test_generator_can_generate_interleaved_2_5_checksum_barcode";d:0;s:57:"BarcodeTest::test_generator_can_generate_code_128_barcode";d:0;s:59:"BarcodeTest::test_generator_can_generate_code_128_a_barcode";d:0;s:59:"BarcodeTest::test_generator_can_generate_code_128_b_barcode";d:0;s:59:"BarcodeTest::test_generator_can_generate_code_128_c_barcode";d:0;s:54:"BarcodeTest::test_generator_can_generate_ean_2_barcode";d:0;s:54:"BarcodeTest::test_generator_can_generate_ean_5_barcode";d:0;s:54:"BarcodeTest::test_generator_can_generate_ean_8_barcode";d:0;s:55:"BarcodeTest::test_generator_can_generate_ean_13_barcode";d:0;s:54:"BarcodeTest::test_generator_can_generate_upc_a_barcode";d:0;s:54:"BarcodeTest::test_generator_can_generate_upc_e_barcode";d:0;s:52:"BarcodeTest::test_generator_can_generate_msi_barcode";d:0;s:61:"BarcodeTest::test_generator_can_generate_msi_checksum_barcode";d:0;s:56:"BarcodeTest::test_generator_can_generate_postnet_barcode";d:0;s:55:"BarcodeTest::test_generator_can_generate_planet_barcode";d:0;s:55:"BarcodeTest::test_generator_can_generate_rms4cc_barcode";d:0;s:52:"BarcodeTest::test_generator_can_generate_kix_barcode";d:0;s:52:"BarcodeTest::test_generator_can_generate_imb_barcode";d:0.009;s:56:"BarcodeTest::test_generator_can_generate_codabar_barcode";d:0;s:56:"BarcodeTest::test_generator_can_generate_code_11_barcode";d:0;s:60:"BarcodeTest::test_generator_can_generate_pharma_code_barcode";d:0;s:69:"BarcodeTest::test_generator_can_generate_pharma_code_2_tracks_barcode";d:0;s:49:"VerifiedBarcodeTest::testAllSupportedBarcodeTypes";d:0.014;}}} \ No newline at end of file diff --git a/tests/BarcodeTest.php b/tests/BarcodeTest.php index b235cae..3198e29 100644 --- a/tests/BarcodeTest.php +++ b/tests/BarcodeTest.php @@ -153,7 +153,7 @@ public function test_generator_can_generate_code_128_barcode() $generator = new Picqer\Barcode\BarcodeGeneratorSVG(); $result = $generator->getBarcode('1234567890abcABC-283*33', $generator::TYPE_CODE_128); - $this->assertStringEqualsFile('tests/verified-files/1234567890abcABC-283*33-code128.svg', $result); + $this->assertStringEqualsFile('tests/verified-files/C128-1234567890abcABC-283*33.svg', $result); } public function test_generator_can_generate_code_128_a_barcode() @@ -161,7 +161,7 @@ public function test_generator_can_generate_code_128_a_barcode() $generator = new Picqer\Barcode\BarcodeGeneratorSVG(); $result = $generator->getBarcode('1234567890', $generator::TYPE_CODE_128_A); - $this->assertStringEqualsFile('tests/verified-files/1234567890-code128a.svg', $result); + $this->assertStringEqualsFile('tests/verified-files/C128A-1234567890.svg', $result); } public function test_generator_can_generate_code_128_b_barcode() @@ -169,15 +169,7 @@ public function test_generator_can_generate_code_128_b_barcode() $generator = new Picqer\Barcode\BarcodeGeneratorSVG(); $result = $generator->getBarcode('1234567890abcABC-283*33', $generator::TYPE_CODE_128_B); - $this->assertStringEqualsFile('tests/verified-files/1234567890abcABC-283*33-code128b.svg', $result); - } - - public function test_generator_can_generate_code_128_c_barcode() - { - $generator = new Picqer\Barcode\BarcodeGeneratorSVG(); - $result = $generator->getBarcode('1234567890', $generator::TYPE_CODE_128_C); - - $this->assertStringEqualsFile('tests/verified-files/1234567890-code128c.svg', $result); + $this->assertStringEqualsFile('tests/verified-files/C128B-1234567890abcABC-283*33.svg', $result); } public function test_generator_can_generate_ean_2_barcode() diff --git a/tests/VerifiedBarcodeTest.php b/tests/VerifiedBarcodeTest.php index 7f118f6..02d9d6f 100644 --- a/tests/VerifiedBarcodeTest.php +++ b/tests/VerifiedBarcodeTest.php @@ -1,6 +1,7 @@ BarcodeGenerator::TYPE_CODE_39, 'barcodes' => ['1234567890abcABC']], + ['type' => BarcodeGenerator::TYPE_CODE_39_CHECKSUM, 'barcodes' => ['1234567890abcABC']], + ['type' => BarcodeGenerator::TYPE_CODE_39E_CHECKSUM, 'barcodes' => ['1234567890abcABC']], + ['type' => BarcodeGenerator::TYPE_CODE_93, 'barcodes' => ['1234567890abcABC']], + ['type' => BarcodeGenerator::TYPE_STANDARD_2_5, 'barcodes' => ['1234567890']], + ['type' => BarcodeGenerator::TYPE_STANDARD_2_5_CHECKSUM, 'barcodes' => ['1234567890']], + ['type' => BarcodeGenerator::TYPE_INTERLEAVED_2_5, 'barcodes' => ['1234567890']], + ['type' => BarcodeGenerator::TYPE_INTERLEAVED_2_5_CHECKSUM, 'barcodes' => ['1234567890']], + ['type' => BarcodeGenerator::TYPE_EAN_13, 'barcodes' => ['081231723897', '0049000004632', '004900000463']], + ['type' => BarcodeGenerator::TYPE_CODE_128, 'barcodes' => ['081231723897', '1234567890abcABC-283*33']], + ['type' => BarcodeGenerator::TYPE_CODE_128_A, 'barcodes' => ['1234567890']], + ['type' => BarcodeGenerator::TYPE_CODE_128_B, 'barcodes' => ['081231723897', '1234567890abcABC-283*33']], + ['type' => BarcodeGenerator::TYPE_EAN_2, 'barcodes' => ['22']], + ['type' => BarcodeGenerator::TYPE_EAN_5, 'barcodes' => ['1234567890abcABC-283*33']], + ['type' => BarcodeGenerator::TYPE_EAN_8, 'barcodes' => ['1234568']], + ['type' => BarcodeGenerator::TYPE_UPC_A, 'barcodes' => ['123456789']], + ['type' => BarcodeGenerator::TYPE_UPC_E, 'barcodes' => ['123456789']], + ['type' => BarcodeGenerator::TYPE_MSI, 'barcodes' => ['123456789']], + ['type' => BarcodeGenerator::TYPE_MSI_CHECKSUM, 'barcodes' => ['123456789']], + ['type' => BarcodeGenerator::TYPE_POSTNET, 'barcodes' => ['123456789']], + ['type' => BarcodeGenerator::TYPE_PLANET, 'barcodes' => ['123456789']], + ['type' => BarcodeGenerator::TYPE_RMS4CC, 'barcodes' => ['123456789']], + ['type' => BarcodeGenerator::TYPE_KIX, 'barcodes' => ['123456789']], + ['type' => BarcodeGenerator::TYPE_IMB, 'barcodes' => ['123456789']], + ['type' => BarcodeGenerator::TYPE_CODABAR, 'barcodes' => ['123456789']], + ['type' => BarcodeGenerator::TYPE_CODE_11, 'barcodes' => ['123456789']], + ['type' => BarcodeGenerator::TYPE_PHARMA_CODE, 'barcodes' => ['123456789']], + ['type' => BarcodeGenerator::TYPE_PHARMA_CODE_TWO_TRACKS, 'barcodes' => ['123456789']], + ]; + public function testAllSupportedBarcodeTypes() { $generator = new Picqer\Barcode\BarcodeGeneratorSVG(); @@ -24,35 +56,4 @@ public function testAllSupportedBarcodeTypes() } } } - - public static $supportedBarcodes = [ - ['type' => \Picqer\Barcode\BarcodeGenerator::TYPE_CODE_39, 'barcodes' => ['1234567890abcABC']], - ['type' => \Picqer\Barcode\BarcodeGenerator::TYPE_CODE_39_CHECKSUM, 'barcodes' => ['1234567890abcABC']], - ['type' => \Picqer\Barcode\BarcodeGenerator::TYPE_CODE_39E_CHECKSUM, 'barcodes' => ['1234567890abcABC']], - ['type' => \Picqer\Barcode\BarcodeGenerator::TYPE_CODE_93, 'barcodes' => ['1234567890abcABC']], - ['type' => \Picqer\Barcode\BarcodeGenerator::TYPE_STANDARD_2_5, 'barcodes' => ['1234567890']], - ['type' => \Picqer\Barcode\BarcodeGenerator::TYPE_STANDARD_2_5_CHECKSUM, 'barcodes' => ['1234567890']], - ['type' => \Picqer\Barcode\BarcodeGenerator::TYPE_INTERLEAVED_2_5, 'barcodes' => ['1234567890']], - ['type' => \Picqer\Barcode\BarcodeGenerator::TYPE_INTERLEAVED_2_5_CHECKSUM, 'barcodes' => ['1234567890']], - ['type' => \Picqer\Barcode\BarcodeGenerator::TYPE_EAN_13, 'barcodes' => ['081231723897', '0049000004632', '004900000463']], - ['type' => \Picqer\Barcode\BarcodeGenerator::TYPE_CODE_128, 'barcodes' => ['081231723897', '1234567890abcABC-283*33']], - ['type' => \Picqer\Barcode\BarcodeGenerator::TYPE_CODE_128_A, 'barcodes' => ['1234567890']], - ['type' => \Picqer\Barcode\BarcodeGenerator::TYPE_CODE_128_B, 'barcodes' => ['081231723897', '1234567890abcABC-283*33']], - ['type' => \Picqer\Barcode\BarcodeGenerator::TYPE_EAN_2, 'barcodes' => ['22']], - ['type' => \Picqer\Barcode\BarcodeGenerator::TYPE_EAN_5, 'barcodes' => ['1234567890abcABC-283*33']], - ['type' => \Picqer\Barcode\BarcodeGenerator::TYPE_EAN_8, 'barcodes' => ['1234568']], - ['type' => \Picqer\Barcode\BarcodeGenerator::TYPE_UPC_A, 'barcodes' => ['123456789']], - ['type' => \Picqer\Barcode\BarcodeGenerator::TYPE_UPC_E, 'barcodes' => ['123456789']], - ['type' => \Picqer\Barcode\BarcodeGenerator::TYPE_MSI, 'barcodes' => ['123456789']], - ['type' => \Picqer\Barcode\BarcodeGenerator::TYPE_MSI_CHECKSUM, 'barcodes' => ['123456789']], - ['type' => \Picqer\Barcode\BarcodeGenerator::TYPE_POSTNET, 'barcodes' => ['123456789']], - ['type' => \Picqer\Barcode\BarcodeGenerator::TYPE_PLANET, 'barcodes' => ['123456789']], - ['type' => \Picqer\Barcode\BarcodeGenerator::TYPE_RMS4CC, 'barcodes' => ['123456789']], - ['type' => \Picqer\Barcode\BarcodeGenerator::TYPE_KIX, 'barcodes' => ['123456789']], - ['type' => \Picqer\Barcode\BarcodeGenerator::TYPE_IMB, 'barcodes' => ['123456789']], - ['type' => \Picqer\Barcode\BarcodeGenerator::TYPE_CODABAR, 'barcodes' => ['123456789']], - ['type' => \Picqer\Barcode\BarcodeGenerator::TYPE_CODE_11, 'barcodes' => ['123456789']], - ['type' => \Picqer\Barcode\BarcodeGenerator::TYPE_PHARMA_CODE, 'barcodes' => ['123456789']], - ['type' => \Picqer\Barcode\BarcodeGenerator::TYPE_PHARMA_CODE_TWO_TRACKS, 'barcodes' => ['123456789']], - ]; -} \ No newline at end of file +} From 43f16abf561aaa9ae52d934d12898ef49a835b96 Mon Sep 17 00:00:00 2001 From: Casper Bakker Date: Wed, 1 Jan 2020 11:37:57 +0100 Subject: [PATCH 018/155] Added needed php extensions to composer.json --- .github/workflows/phpunit.yml | 2 +- .gitignore | 3 ++- composer.json | 4 +++- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/.github/workflows/phpunit.yml b/.github/workflows/phpunit.yml index 4c8243d..4280a34 100644 --- a/.github/workflows/phpunit.yml +++ b/.github/workflows/phpunit.yml @@ -18,7 +18,7 @@ jobs: uses: shivammathur/setup-php@v1 with: php-version: ${{ matrix.php-versions }} - extensions: mbstring, gd + extensions: mbstring, gd, bcmath - name: Validate composer.json run: composer validate diff --git a/.gitignore b/.gitignore index 06b5400..defd14f 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,5 @@ .DS_Store vendor composer.lock -composer.phar \ No newline at end of file +composer.phar +.phpunit.result.cache \ No newline at end of file diff --git a/composer.json b/composer.json index 4730c16..e871dd2 100644 --- a/composer.json +++ b/composer.json @@ -17,7 +17,9 @@ } ], "require": { - "php": ">=7.2.0" + "php": ">=7.2.0", + "ext-mbstring": "*", + "ext-bcmath": "*" }, "require-dev": { "phpunit/phpunit": "^8.5" From 1dfea366a10929dba7d412eda17e1339630cebb4 Mon Sep 17 00:00:00 2001 From: Casper Bakker Date: Wed, 1 Jan 2020 11:43:05 +0100 Subject: [PATCH 019/155] Change test name --- .github/workflows/phpunit.yml | 2 +- Readme.md | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/.github/workflows/phpunit.yml b/.github/workflows/phpunit.yml index 4280a34..14ee637 100644 --- a/.github/workflows/phpunit.yml +++ b/.github/workflows/phpunit.yml @@ -1,4 +1,4 @@ -name: PHP Composer +name: phpunit on: [push] diff --git a/Readme.md b/Readme.md index 0dccb4d..e9017cc 100644 --- a/Readme.md +++ b/Readme.md @@ -1,4 +1,5 @@ -# PHP Barcode Generator [![Build Status](https://travis-ci.org/picqer/php-barcode-generator.svg?branch=master)](https://travis-ci.org/picqer/php-barcode-generator) [![Total Downloads](https://poser.pugx.org/picqer/php-barcode-generator/downloads)](https://packagist.org/packages/picqer/php-barcode-generator) +# PHP Barcode Generator +[![Build Status](https://travis-ci.org/picqer/php-barcode-generator.svg?branch=master)](https://travis-ci.org/picqer/php-barcode-generator) [![Github Actions](https://github.com/picqer/php-barcode-generator/workflows/phpunit/badge.svg)](https://travis-ci.org/picqer/php-barcode-generator) [![Total Downloads](https://poser.pugx.org/picqer/php-barcode-generator/downloads)](https://packagist.org/packages/picqer/php-barcode-generator) This is an easy to use, non-bloated, framework independent, barcode generator in PHP. It creates SVG, PNG, JPG and HTML images, from the most used 1D barcode standards. From ab2ccf1d284e3fdad2b4cfa2afb379e1710c1cd9 Mon Sep 17 00:00:00 2001 From: Casper Bakker Date: Wed, 1 Jan 2020 12:17:10 +0100 Subject: [PATCH 020/155] Moved some barcode types to own class --- Readme.md | 1 + src/BarcodeGenerator.php | 1046 +--------------------- src/Helpers/BinarySequenceConverter.php | 38 + src/Helpers/OldBarcodeArrayConverter.php | 28 + src/Types/TypeCodabar.php | 64 ++ src/Types/TypeCode11.php | 99 ++ src/Types/TypeIntelligentMailBarcode.php | 563 ++++++++++++ src/Types/TypeKix.php | 16 + src/Types/TypePharmacode.php | 34 + src/Types/TypePharmacodeTwoCode.php | 66 ++ src/Types/TypeRms4cc.php | 162 ++++ 11 files changed, 1095 insertions(+), 1022 deletions(-) create mode 100644 src/Helpers/BinarySequenceConverter.php create mode 100644 src/Helpers/OldBarcodeArrayConverter.php create mode 100644 src/Types/TypeCodabar.php create mode 100644 src/Types/TypeCode11.php create mode 100644 src/Types/TypeIntelligentMailBarcode.php create mode 100644 src/Types/TypeKix.php create mode 100644 src/Types/TypePharmacode.php create mode 100644 src/Types/TypePharmacodeTwoCode.php create mode 100644 src/Types/TypeRms4cc.php diff --git a/Readme.md b/Readme.md index e9017cc..efb1b0e 100644 --- a/Readme.md +++ b/Readme.md @@ -1,5 +1,6 @@ # PHP Barcode Generator [![Build Status](https://travis-ci.org/picqer/php-barcode-generator.svg?branch=master)](https://travis-ci.org/picqer/php-barcode-generator) [![Github Actions](https://github.com/picqer/php-barcode-generator/workflows/phpunit/badge.svg)](https://travis-ci.org/picqer/php-barcode-generator) [![Total Downloads](https://poser.pugx.org/picqer/php-barcode-generator/downloads)](https://packagist.org/packages/picqer/php-barcode-generator) + This is an easy to use, non-bloated, framework independent, barcode generator in PHP. It creates SVG, PNG, JPG and HTML images, from the most used 1D barcode standards. diff --git a/src/BarcodeGenerator.php b/src/BarcodeGenerator.php index fd19c60..7272cb1 100644 --- a/src/BarcodeGenerator.php +++ b/src/BarcodeGenerator.php @@ -35,9 +35,18 @@ use Picqer\Barcode\Exceptions\InvalidFormatException; use Picqer\Barcode\Exceptions\InvalidLengthException; use Picqer\Barcode\Exceptions\UnknownTypeException; +use Picqer\Barcode\Helpers\BinarySequenceConverter; +use Picqer\Barcode\Helpers\OldBarcodeArrayConverter; +use Picqer\Barcode\Types\TypeCodabar; +use Picqer\Barcode\Types\TypeCode11; use Picqer\Barcode\Types\TypeCode93; +use Picqer\Barcode\Types\TypeIntelligentMailBarcode; use Picqer\Barcode\Types\TypeInterleaved25; use Picqer\Barcode\Types\TypeInterleaved25Checksum; +use Picqer\Barcode\Types\TypeKix; +use Picqer\Barcode\Types\TypePharmacode; +use Picqer\Barcode\Types\TypePharmacodeTwoCode; +use Picqer\Barcode\Types\TypeRms4cc; abstract class BarcodeGenerator { @@ -101,7 +110,6 @@ protected function getBarcodeData($code, $type) case self::TYPE_CODE_93: $barcodeDataBuilder = new TypeCode93(); - $arrcode = $barcodeDataBuilder->getBarcodeData($code); break; case self::TYPE_STANDARD_2_5: @@ -114,12 +122,10 @@ protected function getBarcodeData($code, $type) case self::TYPE_INTERLEAVED_2_5: $barcodeDataBuilder = new TypeInterleaved25(); - $arrcode = $barcodeDataBuilder->getBarcodeData($code); break; case self::TYPE_INTERLEAVED_2_5_CHECKSUM: $barcodeDataBuilder = new TypeInterleaved25Checksum(); - $arrcode = $barcodeDataBuilder->getBarcodeData($code); break; case self::TYPE_CODE_128: @@ -179,39 +185,43 @@ protected function getBarcodeData($code, $type) break; case self::TYPE_RMS4CC: - $arrcode = $this->barcode_rms4cc($code, false); + $barcodeDataBuilder = new TypeRms4cc(); break; case self::TYPE_KIX: - $arrcode = $this->barcode_rms4cc($code, true); + $barcodeDataBuilder = new TypeKix(); break; case self::TYPE_IMB: - $arrcode = $this->barcode_imb($code); + $barcodeDataBuilder = new TypeIntelligentMailBarcode(); break; case self::TYPE_CODABAR: - $arrcode = $this->barcode_codabar($code); + $barcodeDataBuilder = new TypeCodabar(); break; case self::TYPE_CODE_11: - $arrcode = $this->barcode_code11($code); + $barcodeDataBuilder = new TypeCode11(); break; case self::TYPE_PHARMA_CODE: - $arrcode = $this->barcode_pharmacode($code); + $barcodeDataBuilder = new TypePharmacode(); break; case self::TYPE_PHARMA_CODE_TWO_TRACKS: - $arrcode = $this->barcode_pharmacode2t($code); + $barcodeDataBuilder = new TypePharmacodeTwoCode(); break; default: throw new UnknownTypeException(); } + if (! isset($arrcode) && isset($barcodeDataBuilder)) { + $arrcode = $barcodeDataBuilder->getBarcodeData($code); + } + if ( ! isset($arrcode['maxWidth'])) { - $arrcode = $this->convertBarcodeArrayToNewStyle($arrcode); + return OldBarcodeArrayConverter::convert($arrcode); } return $arrcode; @@ -618,7 +628,7 @@ protected function barcode_msi($code, $checksum = false) $seq .= '1001'; // right guard $bararray = array('code' => $code, 'maxw' => 0, 'maxh' => 1, 'bcode' => array()); - return $this->binseq_to_array($seq, $bararray); + return BinarySequenceConverter::convert($seq, $bararray); } /** @@ -663,40 +673,9 @@ protected function barcode_s25($code, $checksum = false) $seq .= '1101011'; $bararray = array('code' => $code, 'maxw' => 0, 'maxh' => 1, 'bcode' => array()); - return $this->binseq_to_array($seq, $bararray); + return BinarySequenceConverter::convert($seq, $bararray); } - /** - * Convert binary barcode sequence to TCPDF barcode array. - * - * @param $seq (string) barcode as binary sequence. - * @param $bararray (array) barcode array. - * òparam array $bararray TCPDF barcode array to fill up - * @return array barcode representation. - * @protected - */ - protected function binseq_to_array($seq, $bararray) - { - $len = strlen($seq); - $w = 0; - $k = 0; - for ($i = 0; $i < $len; ++$i) { - $w += 1; - if (($i == ($len - 1)) OR (($i < ($len - 1)) AND ($seq[$i] != $seq[($i + 1)]))) { - if ($seq[$i] == '1') { - $t = true; // bar - } else { - $t = false; // space - } - $bararray['bcode'][$k] = array('t' => $t, 'w' => $w, 'h' => 1, 'p' => 0); - $bararray['maxw'] += $w; - ++$k; - $w = 0; - } - } - - return $bararray; - } /** * C128 barcodes. @@ -1364,7 +1343,7 @@ protected function barcode_eanext($code, $len = 5) } $bararray = array('code' => $code, 'maxw' => 0, 'maxh' => 1, 'bcode' => array()); - return $this->binseq_to_array($seq, $bararray); + return BinarySequenceConverter::convert($seq, $bararray); } /** @@ -1443,819 +1422,6 @@ protected function barcode_postnet($code, $planet = false) return $bararray; } - /** - * RMS4CC - CBC - KIX - * RMS4CC (Royal Mail 4-state Customer Code) - CBC (Customer Bar Code) - KIX (Klant index - Customer index) - * RM4SCC is the name of the barcode symbology used by the Royal Mail for its Cleanmail service. - * - * @param $code (string) code to print - * @param $kix (boolean) if true prints the KIX variation (doesn't use the start and end symbols, and the checksum) - * - in this case the house number must be sufficed with an X and placed at the end of the code. - * @return array barcode representation. - * @protected - */ - protected function barcode_rms4cc($code, $kix = false) - { - $notkix = ! $kix; - // bar mode - // 1 = pos 1, length 2 - // 2 = pos 1, length 3 - // 3 = pos 2, length 1 - // 4 = pos 2, length 2 - $barmode = array( - '0' => array(3, 3, 2, 2), - '1' => array(3, 4, 1, 2), - '2' => array(3, 4, 2, 1), - '3' => array(4, 3, 1, 2), - '4' => array(4, 3, 2, 1), - '5' => array(4, 4, 1, 1), - '6' => array(3, 1, 4, 2), - '7' => array(3, 2, 3, 2), - '8' => array(3, 2, 4, 1), - '9' => array(4, 1, 3, 2), - 'A' => array(4, 1, 4, 1), - 'B' => array(4, 2, 3, 1), - 'C' => array(3, 1, 2, 4), - 'D' => array(3, 2, 1, 4), - 'E' => array(3, 2, 2, 3), - 'F' => array(4, 1, 1, 4), - 'G' => array(4, 1, 2, 3), - 'H' => array(4, 2, 1, 3), - 'I' => array(1, 3, 4, 2), - 'J' => array(1, 4, 3, 2), - 'K' => array(1, 4, 4, 1), - 'L' => array(2, 3, 3, 2), - 'M' => array(2, 3, 4, 1), - 'N' => array(2, 4, 3, 1), - 'O' => array(1, 3, 2, 4), - 'P' => array(1, 4, 1, 4), - 'Q' => array(1, 4, 2, 3), - 'R' => array(2, 3, 1, 4), - 'S' => array(2, 3, 2, 3), - 'T' => array(2, 4, 1, 3), - 'U' => array(1, 1, 4, 4), - 'V' => array(1, 2, 3, 4), - 'W' => array(1, 2, 4, 3), - 'X' => array(2, 1, 3, 4), - 'Y' => array(2, 1, 4, 3), - 'Z' => array(2, 2, 3, 3) - ); - $code = strtoupper($code); - $len = strlen($code); - $bararray = array('code' => $code, 'maxw' => 0, 'maxh' => 3, 'bcode' => array()); - if ($notkix) { - // table for checksum calculation (row,col) - $checktable = array( - '0' => array(1, 1), - '1' => array(1, 2), - '2' => array(1, 3), - '3' => array(1, 4), - '4' => array(1, 5), - '5' => array(1, 0), - '6' => array(2, 1), - '7' => array(2, 2), - '8' => array(2, 3), - '9' => array(2, 4), - 'A' => array(2, 5), - 'B' => array(2, 0), - 'C' => array(3, 1), - 'D' => array(3, 2), - 'E' => array(3, 3), - 'F' => array(3, 4), - 'G' => array(3, 5), - 'H' => array(3, 0), - 'I' => array(4, 1), - 'J' => array(4, 2), - 'K' => array(4, 3), - 'L' => array(4, 4), - 'M' => array(4, 5), - 'N' => array(4, 0), - 'O' => array(5, 1), - 'P' => array(5, 2), - 'Q' => array(5, 3), - 'R' => array(5, 4), - 'S' => array(5, 5), - 'T' => array(5, 0), - 'U' => array(0, 1), - 'V' => array(0, 2), - 'W' => array(0, 3), - 'X' => array(0, 4), - 'Y' => array(0, 5), - 'Z' => array(0, 0) - ); - $row = 0; - $col = 0; - for ($i = 0; $i < $len; ++$i) { - $row += $checktable[$code[$i]][0]; - $col += $checktable[$code[$i]][1]; - } - $row %= 6; - $col %= 6; - $chk = array_keys($checktable, array($row, $col)); - $code .= $chk[0]; - ++$len; - } - $k = 0; - if ($notkix) { - // start bar - $bararray['bcode'][$k++] = array('t' => 1, 'w' => 1, 'h' => 2, 'p' => 0); - $bararray['bcode'][$k++] = array('t' => 0, 'w' => 1, 'h' => 2, 'p' => 0); - $bararray['maxw'] += 2; - } - for ($i = 0; $i < $len; ++$i) { - for ($j = 0; $j < 4; ++$j) { - switch ($barmode[$code[$i]][$j]) { - case 1: { - $p = 0; - $h = 2; - break; - } - case 2: { - $p = 0; - $h = 3; - break; - } - case 3: { - $p = 1; - $h = 1; - break; - } - case 4: { - $p = 1; - $h = 2; - break; - } - } - $bararray['bcode'][$k++] = array('t' => 1, 'w' => 1, 'h' => $h, 'p' => $p); - $bararray['bcode'][$k++] = array('t' => 0, 'w' => 1, 'h' => 2, 'p' => 0); - $bararray['maxw'] += 2; - } - } - if ($notkix) { - // stop bar - $bararray['bcode'][$k++] = array('t' => 1, 'w' => 1, 'h' => 3, 'p' => 0); - $bararray['maxw'] += 1; - } - - return $bararray; - } - - /** - * CODABAR barcodes. - * Older code often used in library systems, sometimes in blood banks - * - * @param $code (string) code to represent. - * @return array barcode representation. - * @protected - */ - protected function barcode_codabar($code) - { - $chr = array( - '0' => '11111221', - '1' => '11112211', - '2' => '11121121', - '3' => '22111111', - '4' => '11211211', - '5' => '21111211', - '6' => '12111121', - '7' => '12112111', - '8' => '12211111', - '9' => '21121111', - '-' => '11122111', - '$' => '11221111', - ':' => '21112121', - '/' => '21211121', - '.' => '21212111', - '+' => '11222221', - 'A' => '11221211', - 'B' => '12121121', - 'C' => '11121221', - 'D' => '11122211' - ); - $bararray = array('code' => $code, 'maxw' => 0, 'maxh' => 1, 'bcode' => array()); - $k = 0; - $w = 0; - $seq = ''; - $code = 'A' . strtoupper($code) . 'A'; - $len = strlen($code); - for ($i = 0; $i < $len; ++$i) { - if ( ! isset($chr[$code[$i]])) { - throw new InvalidCharacterException('Char ' . $code[$i] . ' is unsupported'); - } - $seq = $chr[$code[$i]]; - for ($j = 0; $j < 8; ++$j) { - if (($j % 2) == 0) { - $t = true; // bar - } else { - $t = false; // space - } - $w = $seq[$j]; - $bararray['bcode'][$k] = array('t' => $t, 'w' => $w, 'h' => 1, 'p' => 0); - $bararray['maxw'] += $w; - ++$k; - } - } - - return $bararray; - } - - /** - * CODE11 barcodes. - * Used primarily for labeling telecommunications equipment - * - * @param $code (string) code to represent. - * @return array barcode representation. - * @protected - */ - protected function barcode_code11($code) - { - $chr = array( - '0' => '111121', - '1' => '211121', - '2' => '121121', - '3' => '221111', - '4' => '112121', - '5' => '212111', - '6' => '122111', - '7' => '111221', - '8' => '211211', - '9' => '211111', - '-' => '112111', - 'S' => '112211' - ); - $bararray = array('code' => $code, 'maxw' => 0, 'maxh' => 1, 'bcode' => array()); - $k = 0; - $w = 0; - $seq = ''; - $len = strlen($code); - // calculate check digit C - $p = 1; - $check = 0; - for ($i = ($len - 1); $i >= 0; --$i) { - $digit = $code[$i]; - if ($digit == '-') { - $dval = 10; - } else { - $dval = intval($digit); - } - $check += ($dval * $p); - ++$p; - if ($p > 10) { - $p = 1; - } - } - $check %= 11; - if ($check == 10) { - $check = '-'; - } - $code .= $check; - if ($len > 10) { - // calculate check digit K - $p = 1; - $check = 0; - for ($i = $len; $i >= 0; --$i) { - $digit = $code[$i]; - if ($digit == '-') { - $dval = 10; - } else { - $dval = intval($digit); - } - $check += ($dval * $p); - ++$p; - if ($p > 9) { - $p = 1; - } - } - $check %= 11; - $code .= $check; - ++$len; - } - $code = 'S' . $code . 'S'; - $len += 3; - for ($i = 0; $i < $len; ++$i) { - if ( ! isset($chr[$code[$i]])) { - throw new InvalidCharacterException('Char ' . $code[$i] . ' is unsupported'); - } - $seq = $chr[$code[$i]]; - for ($j = 0; $j < 6; ++$j) { - if (($j % 2) == 0) { - $t = true; // bar - } else { - $t = false; // space - } - $w = $seq[$j]; - $bararray['bcode'][$k] = array('t' => $t, 'w' => $w, 'h' => 1, 'p' => 0); - $bararray['maxw'] += $w; - ++$k; - } - } - - return $bararray; - } - - /** - * Pharmacode - * Contains digits (0 to 9) - * - * @param $code (string) code to represent. - * @return array barcode representation. - * @protected - */ - protected function barcode_pharmacode($code) - { - $seq = ''; - $code = intval($code); - while ($code > 0) { - if (($code % 2) == 0) { - $seq .= '11100'; - $code -= 2; - } else { - $seq .= '100'; - $code -= 1; - } - $code /= 2; - } - $seq = substr($seq, 0, -2); - $seq = strrev($seq); - $bararray = array('code' => $code, 'maxw' => 0, 'maxh' => 1, 'bcode' => array()); - - return $this->binseq_to_array($seq, $bararray); - } - - /** - * Pharmacode two-track - * Contains digits (0 to 9) - * - * @param $code (string) code to represent. - * @return array barcode representation. - * @protected - */ - protected function barcode_pharmacode2t($code) - { - $seq = ''; - $code = intval($code); - do { - switch ($code % 3) { - case 0: { - $seq .= '3'; - $code = ($code - 3) / 3; - break; - } - case 1: { - $seq .= '1'; - $code = ($code - 1) / 3; - break; - } - case 2: { - $seq .= '2'; - $code = ($code - 2) / 3; - break; - } - } - } while ($code != 0); - $seq = strrev($seq); - $k = 0; - $bararray = array('code' => $code, 'maxw' => 0, 'maxh' => 2, 'bcode' => array()); - $len = strlen($seq); - for ($i = 0; $i < $len; ++$i) { - switch ($seq[$i]) { - case '1': { - $p = 1; - $h = 1; - break; - } - case '2': { - $p = 0; - $h = 1; - break; - } - case '3': { - $p = 0; - $h = 2; - break; - } - } - $bararray['bcode'][$k++] = array('t' => 1, 'w' => 1, 'h' => $h, 'p' => $p); - $bararray['bcode'][$k++] = array('t' => 0, 'w' => 1, 'h' => 2, 'p' => 0); - $bararray['maxw'] += 2; - } - unset($bararray['bcode'][($k - 1)]); - --$bararray['maxw']; - - return $bararray; - } - - /** - * IMB - Intelligent Mail Barcode - Onecode - USPS-B-3200 - * (requires PHP bcmath extension) - * Intelligent Mail barcode is a 65-bar code for use on mail in the United States. - * The fields are described as follows:
  • The Barcode Identifier shall be assigned by USPS to encode the - * presort identification that is currently printed in human readable form on the optional endorsement line (OEL) - * as well as for future USPS use. This shall be two digits, with the second digit in the range of 0–4. The - * allowable encoding ranges shall be 00–04, 10–14, 20–24, 30–34, 40–44, 50–54, 60–64, 70–74, 80–84, and - * 90–94.
  • The Service Type Identifier shall be assigned by USPS for any combination of services requested - * on the mailpiece. The allowable encoding range shall be 000http://it2.php.net/manual/en/function.dechex.php–999. - * Each 3-digit value shall correspond to a particular mail class with a particular combination of service(s). Each - * service program, such as OneCode Confirm and OneCode ACS, shall provide the list of Service Type Identifier - * values.
  • The Mailer or Customer Identifier shall be assigned by USPS as a unique, 6 or 9 digit number - * that identifies a business entity. The allowable encoding range for the 6 digit Mailer ID shall be 000000- - * 899999, while the allowable encoding range for the 9 digit Mailer ID shall be 900000000-999999999.
  • The - * Serial or Sequence Number shall be assigned by the mailer for uniquely identifying and tracking mailpieces. The - * allowable encoding range shall be 000000000–999999999 when used with a 6 digit Mailer ID and 000000-999999 when - * used with a 9 digit Mailer ID. e. The Delivery Point ZIP Code shall be assigned by the mailer for routing the - * mailpiece. This shall replace POSTNET for routing the mailpiece to its final delivery point. The length may be - * 0, 5, 9, or 11 digits. The allowable encoding ranges shall be no ZIP Code, 00000–99999, 000000000–999999999, - * and 00000000000–99999999999.
- * - * @param $code (string) code to print, separate the ZIP (routing code) from the rest using a minus char '-' - * (BarcodeID_ServiceTypeID_MailerID_SerialNumber-RoutingCode) - * @return array barcode representation. - * @protected - */ - protected function barcode_imb($code) - { - $asc_chr = array( - 4, - 0, - 2, - 6, - 3, - 5, - 1, - 9, - 8, - 7, - 1, - 2, - 0, - 6, - 4, - 8, - 2, - 9, - 5, - 3, - 0, - 1, - 3, - 7, - 4, - 6, - 8, - 9, - 2, - 0, - 5, - 1, - 9, - 4, - 3, - 8, - 6, - 7, - 1, - 2, - 4, - 3, - 9, - 5, - 7, - 8, - 3, - 0, - 2, - 1, - 4, - 0, - 9, - 1, - 7, - 0, - 2, - 4, - 6, - 3, - 7, - 1, - 9, - 5, - 8 - ); - $dsc_chr = array( - 7, - 1, - 9, - 5, - 8, - 0, - 2, - 4, - 6, - 3, - 5, - 8, - 9, - 7, - 3, - 0, - 6, - 1, - 7, - 4, - 6, - 8, - 9, - 2, - 5, - 1, - 7, - 5, - 4, - 3, - 8, - 7, - 6, - 0, - 2, - 5, - 4, - 9, - 3, - 0, - 1, - 6, - 8, - 2, - 0, - 4, - 5, - 9, - 6, - 7, - 5, - 2, - 6, - 3, - 8, - 5, - 1, - 9, - 8, - 7, - 4, - 0, - 2, - 6, - 3 - ); - $asc_pos = array( - 3, - 0, - 8, - 11, - 1, - 12, - 8, - 11, - 10, - 6, - 4, - 12, - 2, - 7, - 9, - 6, - 7, - 9, - 2, - 8, - 4, - 0, - 12, - 7, - 10, - 9, - 0, - 7, - 10, - 5, - 7, - 9, - 6, - 8, - 2, - 12, - 1, - 4, - 2, - 0, - 1, - 5, - 4, - 6, - 12, - 1, - 0, - 9, - 4, - 7, - 5, - 10, - 2, - 6, - 9, - 11, - 2, - 12, - 6, - 7, - 5, - 11, - 0, - 3, - 2 - ); - $dsc_pos = array( - 2, - 10, - 12, - 5, - 9, - 1, - 5, - 4, - 3, - 9, - 11, - 5, - 10, - 1, - 6, - 3, - 4, - 1, - 10, - 0, - 2, - 11, - 8, - 6, - 1, - 12, - 3, - 8, - 6, - 4, - 4, - 11, - 0, - 6, - 1, - 9, - 11, - 5, - 3, - 7, - 3, - 10, - 7, - 11, - 8, - 2, - 10, - 3, - 5, - 8, - 0, - 3, - 12, - 11, - 8, - 4, - 5, - 1, - 3, - 0, - 7, - 12, - 9, - 8, - 10 - ); - $code_arr = explode('-', $code); - $tracking_number = $code_arr[0]; - if (isset($code_arr[1])) { - $routing_code = $code_arr[1]; - } else { - $routing_code = ''; - } - // Conversion of Routing Code - switch (strlen($routing_code)) { - case 0: { - $binary_code = 0; - break; - } - case 5: { - $binary_code = bcadd($routing_code, '1'); - break; - } - case 9: { - $binary_code = bcadd($routing_code, '100001'); - break; - } - case 11: { - $binary_code = bcadd($routing_code, '1000100001'); - break; - } - default: { - throw new BarcodeException('Routing code unknown'); - break; - } - } - $binary_code = bcmul($binary_code, 10); - $binary_code = bcadd($binary_code, $tracking_number[0]); - $binary_code = bcmul($binary_code, 5); - $binary_code = bcadd($binary_code, $tracking_number[1]); - $binary_code .= substr($tracking_number, 2, 18); - // convert to hexadecimal - $binary_code = $this->dec_to_hex($binary_code); - // pad to get 13 bytes - $binary_code = str_pad($binary_code, 26, '0', STR_PAD_LEFT); - // convert string to array of bytes - $binary_code_arr = chunk_split($binary_code, 2, "\r"); - $binary_code_arr = substr($binary_code_arr, 0, -1); - $binary_code_arr = explode("\r", $binary_code_arr); - // calculate frame check sequence - $fcs = $this->imb_crc11fcs($binary_code_arr); - // exclude first 2 bits from first byte - $first_byte = sprintf('%2s', dechex((hexdec($binary_code_arr[0]) << 2) >> 2)); - $binary_code_102bit = $first_byte . substr($binary_code, 2); - // convert binary data to codewords - $codewords = array(); - $data = $this->hex_to_dec($binary_code_102bit); - $codewords[0] = bcmod($data, 636) * 2; - $data = bcdiv($data, 636); - for ($i = 1; $i < 9; ++$i) { - $codewords[$i] = bcmod($data, 1365); - $data = bcdiv($data, 1365); - } - $codewords[9] = $data; - if (($fcs >> 10) == 1) { - $codewords[9] += 659; - } - // generate lookup tables - $table2of13 = $this->imb_tables(2, 78); - $table5of13 = $this->imb_tables(5, 1287); - // convert codewords to characters - $characters = array(); - $bitmask = 512; - foreach ($codewords as $k => $val) { - if ($val <= 1286) { - $chrcode = $table5of13[$val]; - } else { - $chrcode = $table2of13[($val - 1287)]; - } - if (($fcs & $bitmask) > 0) { - // bitwise invert - $chrcode = ((~$chrcode) & 8191); - } - $characters[] = $chrcode; - $bitmask /= 2; - } - $characters = array_reverse($characters); - // build bars - $k = 0; - $bararray = array('code' => $code, 'maxw' => 0, 'maxh' => 3, 'bcode' => array()); - for ($i = 0; $i < 65; ++$i) { - $asc = (($characters[$asc_chr[$i]] & pow(2, $asc_pos[$i])) > 0); - $dsc = (($characters[$dsc_chr[$i]] & pow(2, $dsc_pos[$i])) > 0); - if ($asc AND $dsc) { - // full bar (F) - $p = 0; - $h = 3; - } elseif ($asc) { - // ascender (A) - $p = 0; - $h = 2; - } elseif ($dsc) { - // descender (D) - $p = 1; - $h = 2; - } else { - // tracker (T) - $p = 1; - $h = 1; - } - $bararray['bcode'][$k++] = array('t' => 1, 'w' => 1, 'h' => $h, 'p' => $p); - $bararray['bcode'][$k++] = array('t' => 0, 'w' => 1, 'h' => 2, 'p' => 0); - $bararray['maxw'] += 2; - } - unset($bararray['bcode'][($k - 1)]); - --$bararray['maxw']; - - return $bararray; - } - /** * IMB - Intelligent Mail Barcode - Onecode - USPS-B-3200 * @@ -2308,168 +1474,4 @@ protected function barcode_imb_pre($code) return $bararray; } - - /** - * Convert large integer number to hexadecimal representation. - * (requires PHP bcmath extension) - * - * @param $number (string) number to convert specified as a string - * @return string hexadecimal representation - */ - protected function dec_to_hex($number) - { - if ($number == 0) { - return '00'; - } - - $hex = []; - - while ($number > 0) { - array_push($hex, strtoupper(dechex(bcmod($number, '16')))); - $number = bcdiv($number, '16', 0); - } - $hex = array_reverse($hex); - - return implode($hex); - } - - /** - * Convert large hexadecimal number to decimal representation (string). - * (requires PHP bcmath extension) - * - * @param $hex (string) hexadecimal number to convert specified as a string - * @return string hexadecimal representation - */ - protected function hex_to_dec($hex) - { - $dec = 0; - $bitval = 1; - $len = strlen($hex); - for ($pos = ($len - 1); $pos >= 0; --$pos) { - $dec = bcadd($dec, bcmul(hexdec($hex[$pos]), $bitval)); - $bitval = bcmul($bitval, 16); - } - - return $dec; - } - - /** - * Intelligent Mail Barcode calculation of Frame Check Sequence - * - * @param $code_arr (string) array of hexadecimal values (13 bytes holding 102 bits right justified). - * @return int 11 bit Frame Check Sequence as integer (decimal base) - * @protected - */ - protected function imb_crc11fcs($code_arr) - { - $genpoly = 0x0F35; // generator polynomial - $fcs = 0x07FF; // Frame Check Sequence - // do most significant byte skipping the 2 most significant bits - $data = hexdec($code_arr[0]) << 5; - for ($bit = 2; $bit < 8; ++$bit) { - if (($fcs ^ $data) & 0x400) { - $fcs = ($fcs << 1) ^ $genpoly; - } else { - $fcs = ($fcs << 1); - } - $fcs &= 0x7FF; - $data <<= 1; - } - // do rest of bytes - for ($byte = 1; $byte < 13; ++$byte) { - $data = hexdec($code_arr[$byte]) << 3; - for ($bit = 0; $bit < 8; ++$bit) { - if (($fcs ^ $data) & 0x400) { - $fcs = ($fcs << 1) ^ $genpoly; - } else { - $fcs = ($fcs << 1); - } - $fcs &= 0x7FF; - $data <<= 1; - } - } - - return $fcs; - } - - /** - * Reverse unsigned short value - * - * @param $num (int) value to reversr - * @return int reversed value - * @protected - */ - protected function imb_reverse_us($num) - { - $rev = 0; - for ($i = 0; $i < 16; ++$i) { - $rev <<= 1; - $rev |= ($num & 1); - $num >>= 1; - } - - return $rev; - } - - /** - * generate Nof13 tables used for Intelligent Mail Barcode - * - * @param $n (int) is the type of table: 2 for 2of13 table, 5 for 5of13table - * @param $size (int) size of table (78 for n=2 and 1287 for n=5) - * @return array requested table - * @protected - */ - protected function imb_tables($n, $size) - { - $table = array(); - $lli = 0; // LUT lower index - $lui = $size - 1; // LUT upper index - for ($count = 0; $count < 8192; ++$count) { - $bit_count = 0; - for ($bit_index = 0; $bit_index < 13; ++$bit_index) { - $bit_count += intval(($count & (1 << $bit_index)) != 0); - } - // if we don't have the right number of bits on, go on to the next value - if ($bit_count == $n) { - $reverse = ($this->imb_reverse_us($count) >> 3); - // if the reverse is less than count, we have already visited this pair before - if ($reverse >= $count) { - // If count is symmetric, place it at the first free slot from the end of the list. - // Otherwise, place it at the first free slot from the beginning of the list AND place $reverse ath the next free slot from the beginning of the list - if ($reverse == $count) { - $table[$lui] = $count; - --$lui; - } else { - $table[$lli] = $count; - ++$lli; - $table[$lli] = $reverse; - ++$lli; - } - } - } - } - - return $table; - } - - protected function convertBarcodeArrayToNewStyle($oldBarcodeArray) - { - $newBarcodeArray = []; - $newBarcodeArray['code'] = $oldBarcodeArray['code']; - $newBarcodeArray['maxWidth'] = $oldBarcodeArray['maxw']; - $newBarcodeArray['maxHeight'] = $oldBarcodeArray['maxh']; - $newBarcodeArray['bars'] = []; - foreach ($oldBarcodeArray['bcode'] as $oldbar) { - $newBar = []; - $newBar['width'] = $oldbar['w']; - $newBar['height'] = $oldbar['h']; - $newBar['positionVertical'] = $oldbar['p']; - $newBar['drawBar'] = $oldbar['t']; - $newBar['drawSpacing'] = ! $oldbar['t']; - - $newBarcodeArray['bars'][] = $newBar; - } - - return $newBarcodeArray; - } } diff --git a/src/Helpers/BinarySequenceConverter.php b/src/Helpers/BinarySequenceConverter.php new file mode 100644 index 0000000..7ef8f7c --- /dev/null +++ b/src/Helpers/BinarySequenceConverter.php @@ -0,0 +1,38 @@ + $t, 'w' => $w, 'h' => 1, 'p' => 0); + $bararray['maxw'] += $w; + ++$k; + $w = 0; + } + } + + return $bararray; + } +} \ No newline at end of file diff --git a/src/Helpers/OldBarcodeArrayConverter.php b/src/Helpers/OldBarcodeArrayConverter.php new file mode 100644 index 0000000..480ba76 --- /dev/null +++ b/src/Helpers/OldBarcodeArrayConverter.php @@ -0,0 +1,28 @@ + '11111221', + '1' => '11112211', + '2' => '11121121', + '3' => '22111111', + '4' => '11211211', + '5' => '21111211', + '6' => '12111121', + '7' => '12112111', + '8' => '12211111', + '9' => '21121111', + '-' => '11122111', + '$' => '11221111', + ':' => '21112121', + '/' => '21211121', + '.' => '21212111', + '+' => '11222221', + 'A' => '11221211', + 'B' => '12121121', + 'C' => '11121221', + 'D' => '11122211' + ); + $bararray = array('code' => $code, 'maxw' => 0, 'maxh' => 1, 'bcode' => array()); + $k = 0; + $w = 0; + $seq = ''; + $code = 'A' . strtoupper($code) . 'A'; + $len = strlen($code); + for ($i = 0; $i < $len; ++$i) { + if ( ! isset($chr[$code[$i]])) { + throw new InvalidCharacterException('Char ' . $code[$i] . ' is unsupported'); + } + $seq = $chr[$code[$i]]; + for ($j = 0; $j < 8; ++$j) { + if (($j % 2) == 0) { + $t = true; // bar + } else { + $t = false; // space + } + $w = $seq[$j]; + $bararray['bcode'][$k] = array('t' => $t, 'w' => $w, 'h' => 1, 'p' => 0); + $bararray['maxw'] += $w; + ++$k; + } + } + + return $bararray; + } +} diff --git a/src/Types/TypeCode11.php b/src/Types/TypeCode11.php new file mode 100644 index 0000000..3e9794f --- /dev/null +++ b/src/Types/TypeCode11.php @@ -0,0 +1,99 @@ + '111121', + '1' => '211121', + '2' => '121121', + '3' => '221111', + '4' => '112121', + '5' => '212111', + '6' => '122111', + '7' => '111221', + '8' => '211211', + '9' => '211111', + '-' => '112111', + 'S' => '112211' + ); + $bararray = array('code' => $code, 'maxw' => 0, 'maxh' => 1, 'bcode' => array()); + $k = 0; + $w = 0; + $seq = ''; + $len = strlen($code); + // calculate check digit C + $p = 1; + $check = 0; + for ($i = ($len - 1); $i >= 0; --$i) { + $digit = $code[$i]; + if ($digit == '-') { + $dval = 10; + } else { + $dval = intval($digit); + } + $check += ($dval * $p); + ++$p; + if ($p > 10) { + $p = 1; + } + } + $check %= 11; + if ($check == 10) { + $check = '-'; + } + $code .= $check; + if ($len > 10) { + // calculate check digit K + $p = 1; + $check = 0; + for ($i = $len; $i >= 0; --$i) { + $digit = $code[$i]; + if ($digit == '-') { + $dval = 10; + } else { + $dval = intval($digit); + } + $check += ($dval * $p); + ++$p; + if ($p > 9) { + $p = 1; + } + } + $check %= 11; + $code .= $check; + ++$len; + } + $code = 'S' . $code . 'S'; + $len += 3; + for ($i = 0; $i < $len; ++$i) { + if ( ! isset($chr[$code[$i]])) { + throw new InvalidCharacterException('Char ' . $code[$i] . ' is unsupported'); + } + $seq = $chr[$code[$i]]; + for ($j = 0; $j < 6; ++$j) { + if (($j % 2) == 0) { + $t = true; // bar + } else { + $t = false; // space + } + $w = $seq[$j]; + $bararray['bcode'][$k] = array('t' => $t, 'w' => $w, 'h' => 1, 'p' => 0); + $bararray['maxw'] += $w; + ++$k; + } + } + + return $bararray; + } +} diff --git a/src/Types/TypeIntelligentMailBarcode.php b/src/Types/TypeIntelligentMailBarcode.php new file mode 100644 index 0000000..f5cf5a2 --- /dev/null +++ b/src/Types/TypeIntelligentMailBarcode.php @@ -0,0 +1,563 @@ +
  • The Barcode Identifier shall be assigned by USPS to encode the + * presort identification that is currently printed in human readable form on the optional endorsement line (OEL) + * as well as for future USPS use. This shall be two digits, with the second digit in the range of 0–4. The + * allowable encoding ranges shall be 00–04, 10–14, 20–24, 30–34, 40–44, 50–54, 60–64, 70–74, 80–84, and + * 90–94.
  • The Service Type Identifier shall be assigned by USPS for any combination of services requested + * on the mailpiece. The allowable encoding range shall be 000http://it2.php.net/manual/en/function.dechex.php–999. + * Each 3-digit value shall correspond to a particular mail class with a particular combination of service(s). Each + * service program, such as OneCode Confirm and OneCode ACS, shall provide the list of Service Type Identifier + * values.
  • The Mailer or Customer Identifier shall be assigned by USPS as a unique, 6 or 9 digit number + * that identifies a business entity. The allowable encoding range for the 6 digit Mailer ID shall be 000000- + * 899999, while the allowable encoding range for the 9 digit Mailer ID shall be 900000000-999999999.
  • The + * Serial or Sequence Number shall be assigned by the mailer for uniquely identifying and tracking mailpieces. The + * allowable encoding range shall be 000000000–999999999 when used with a 6 digit Mailer ID and 000000-999999 when + * used with a 9 digit Mailer ID. e. The Delivery Point ZIP Code shall be assigned by the mailer for routing the + * mailpiece. This shall replace POSTNET for routing the mailpiece to its final delivery point. The length may be + * 0, 5, 9, or 11 digits. The allowable encoding ranges shall be no ZIP Code, 00000–99999, 000000000–999999999, + * and 00000000000–99999999999.
  • + * + * code to print, separate the ZIP (routing code) from the rest using a minus char '-' + * (BarcodeID_ServiceTypeID_MailerID_SerialNumber-RoutingCode) + */ + +class TypeIntelligentMailBarcode extends Type +{ + public function getBarcodeData(string $code): array + { + $asc_chr = array( + 4, + 0, + 2, + 6, + 3, + 5, + 1, + 9, + 8, + 7, + 1, + 2, + 0, + 6, + 4, + 8, + 2, + 9, + 5, + 3, + 0, + 1, + 3, + 7, + 4, + 6, + 8, + 9, + 2, + 0, + 5, + 1, + 9, + 4, + 3, + 8, + 6, + 7, + 1, + 2, + 4, + 3, + 9, + 5, + 7, + 8, + 3, + 0, + 2, + 1, + 4, + 0, + 9, + 1, + 7, + 0, + 2, + 4, + 6, + 3, + 7, + 1, + 9, + 5, + 8 + ); + $dsc_chr = array( + 7, + 1, + 9, + 5, + 8, + 0, + 2, + 4, + 6, + 3, + 5, + 8, + 9, + 7, + 3, + 0, + 6, + 1, + 7, + 4, + 6, + 8, + 9, + 2, + 5, + 1, + 7, + 5, + 4, + 3, + 8, + 7, + 6, + 0, + 2, + 5, + 4, + 9, + 3, + 0, + 1, + 6, + 8, + 2, + 0, + 4, + 5, + 9, + 6, + 7, + 5, + 2, + 6, + 3, + 8, + 5, + 1, + 9, + 8, + 7, + 4, + 0, + 2, + 6, + 3 + ); + $asc_pos = array( + 3, + 0, + 8, + 11, + 1, + 12, + 8, + 11, + 10, + 6, + 4, + 12, + 2, + 7, + 9, + 6, + 7, + 9, + 2, + 8, + 4, + 0, + 12, + 7, + 10, + 9, + 0, + 7, + 10, + 5, + 7, + 9, + 6, + 8, + 2, + 12, + 1, + 4, + 2, + 0, + 1, + 5, + 4, + 6, + 12, + 1, + 0, + 9, + 4, + 7, + 5, + 10, + 2, + 6, + 9, + 11, + 2, + 12, + 6, + 7, + 5, + 11, + 0, + 3, + 2 + ); + $dsc_pos = array( + 2, + 10, + 12, + 5, + 9, + 1, + 5, + 4, + 3, + 9, + 11, + 5, + 10, + 1, + 6, + 3, + 4, + 1, + 10, + 0, + 2, + 11, + 8, + 6, + 1, + 12, + 3, + 8, + 6, + 4, + 4, + 11, + 0, + 6, + 1, + 9, + 11, + 5, + 3, + 7, + 3, + 10, + 7, + 11, + 8, + 2, + 10, + 3, + 5, + 8, + 0, + 3, + 12, + 11, + 8, + 4, + 5, + 1, + 3, + 0, + 7, + 12, + 9, + 8, + 10 + ); + $code_arr = explode('-', $code); + $tracking_number = $code_arr[0]; + if (isset($code_arr[1])) { + $routing_code = $code_arr[1]; + } else { + $routing_code = ''; + } + // Conversion of Routing Code + switch (strlen($routing_code)) { + case 0: { + $binary_code = 0; + break; + } + case 5: { + $binary_code = bcadd($routing_code, '1'); + break; + } + case 9: { + $binary_code = bcadd($routing_code, '100001'); + break; + } + case 11: { + $binary_code = bcadd($routing_code, '1000100001'); + break; + } + default: { + throw new BarcodeException('Routing code unknown'); + break; + } + } + $binary_code = bcmul($binary_code, 10); + $binary_code = bcadd($binary_code, $tracking_number[0]); + $binary_code = bcmul($binary_code, 5); + $binary_code = bcadd($binary_code, $tracking_number[1]); + $binary_code .= substr($tracking_number, 2, 18); + // convert to hexadecimal + $binary_code = $this->dec_to_hex($binary_code); + // pad to get 13 bytes + $binary_code = str_pad($binary_code, 26, '0', STR_PAD_LEFT); + // convert string to array of bytes + $binary_code_arr = chunk_split($binary_code, 2, "\r"); + $binary_code_arr = substr($binary_code_arr, 0, -1); + $binary_code_arr = explode("\r", $binary_code_arr); + // calculate frame check sequence + $fcs = $this->imb_crc11fcs($binary_code_arr); + // exclude first 2 bits from first byte + $first_byte = sprintf('%2s', dechex((hexdec($binary_code_arr[0]) << 2) >> 2)); + $binary_code_102bit = $first_byte . substr($binary_code, 2); + // convert binary data to codewords + $codewords = array(); + $data = $this->hex_to_dec($binary_code_102bit); + $codewords[0] = bcmod($data, 636) * 2; + $data = bcdiv($data, 636); + for ($i = 1; $i < 9; ++$i) { + $codewords[$i] = bcmod($data, 1365); + $data = bcdiv($data, 1365); + } + $codewords[9] = $data; + if (($fcs >> 10) == 1) { + $codewords[9] += 659; + } + // generate lookup tables + $table2of13 = $this->imb_tables(2, 78); + $table5of13 = $this->imb_tables(5, 1287); + // convert codewords to characters + $characters = array(); + $bitmask = 512; + foreach ($codewords as $k => $val) { + if ($val <= 1286) { + $chrcode = $table5of13[$val]; + } else { + $chrcode = $table2of13[($val - 1287)]; + } + if (($fcs & $bitmask) > 0) { + // bitwise invert + $chrcode = ((~$chrcode) & 8191); + } + $characters[] = $chrcode; + $bitmask /= 2; + } + $characters = array_reverse($characters); + // build bars + $k = 0; + $bararray = array('code' => $code, 'maxw' => 0, 'maxh' => 3, 'bcode' => array()); + for ($i = 0; $i < 65; ++$i) { + $asc = (($characters[$asc_chr[$i]] & pow(2, $asc_pos[$i])) > 0); + $dsc = (($characters[$dsc_chr[$i]] & pow(2, $dsc_pos[$i])) > 0); + if ($asc AND $dsc) { + // full bar (F) + $p = 0; + $h = 3; + } elseif ($asc) { + // ascender (A) + $p = 0; + $h = 2; + } elseif ($dsc) { + // descender (D) + $p = 1; + $h = 2; + } else { + // tracker (T) + $p = 1; + $h = 1; + } + $bararray['bcode'][$k++] = array('t' => 1, 'w' => 1, 'h' => $h, 'p' => $p); + $bararray['bcode'][$k++] = array('t' => 0, 'w' => 1, 'h' => 2, 'p' => 0); + $bararray['maxw'] += 2; + } + unset($bararray['bcode'][($k - 1)]); + --$bararray['maxw']; + + return $bararray; + } + + /** + * Convert large integer number to hexadecimal representation. + * (requires PHP bcmath extension) + * + * @param $number (string) number to convert specified as a string + * @return string hexadecimal representation + */ + protected function dec_to_hex($number) + { + if ($number == 0) { + return '00'; + } + + $hex = []; + + while ($number > 0) { + array_push($hex, strtoupper(dechex(bcmod($number, '16')))); + $number = bcdiv($number, '16', 0); + } + $hex = array_reverse($hex); + + return implode($hex); + } + + + /** + * Intelligent Mail Barcode calculation of Frame Check Sequence + * + * @param $code_arr (string) array of hexadecimal values (13 bytes holding 102 bits right justified). + * @return int 11 bit Frame Check Sequence as integer (decimal base) + * @protected + */ + protected function imb_crc11fcs($code_arr) + { + $genpoly = 0x0F35; // generator polynomial + $fcs = 0x07FF; // Frame Check Sequence + // do most significant byte skipping the 2 most significant bits + $data = hexdec($code_arr[0]) << 5; + for ($bit = 2; $bit < 8; ++$bit) { + if (($fcs ^ $data) & 0x400) { + $fcs = ($fcs << 1) ^ $genpoly; + } else { + $fcs = ($fcs << 1); + } + $fcs &= 0x7FF; + $data <<= 1; + } + // do rest of bytes + for ($byte = 1; $byte < 13; ++$byte) { + $data = hexdec($code_arr[$byte]) << 3; + for ($bit = 0; $bit < 8; ++$bit) { + if (($fcs ^ $data) & 0x400) { + $fcs = ($fcs << 1) ^ $genpoly; + } else { + $fcs = ($fcs << 1); + } + $fcs &= 0x7FF; + $data <<= 1; + } + } + + return $fcs; + } + + /** + * Convert large hexadecimal number to decimal representation (string). + * (requires PHP bcmath extension) + * + * @param $hex (string) hexadecimal number to convert specified as a string + * @return string hexadecimal representation + */ + protected function hex_to_dec($hex) + { + $dec = 0; + $bitval = 1; + $len = strlen($hex); + for ($pos = ($len - 1); $pos >= 0; --$pos) { + $dec = bcadd($dec, bcmul(hexdec($hex[$pos]), $bitval)); + $bitval = bcmul($bitval, 16); + } + + return $dec; + } + + + /** + * generate Nof13 tables used for Intelligent Mail Barcode + * + * @param $n (int) is the type of table: 2 for 2of13 table, 5 for 5of13table + * @param $size (int) size of table (78 for n=2 and 1287 for n=5) + * @return array requested table + * @protected + */ + protected function imb_tables($n, $size) + { + $table = array(); + $lli = 0; // LUT lower index + $lui = $size - 1; // LUT upper index + for ($count = 0; $count < 8192; ++$count) { + $bit_count = 0; + for ($bit_index = 0; $bit_index < 13; ++$bit_index) { + $bit_count += intval(($count & (1 << $bit_index)) != 0); + } + // if we don't have the right number of bits on, go on to the next value + if ($bit_count == $n) { + $reverse = ($this->imb_reverse_us($count) >> 3); + // if the reverse is less than count, we have already visited this pair before + if ($reverse >= $count) { + // If count is symmetric, place it at the first free slot from the end of the list. + // Otherwise, place it at the first free slot from the beginning of the list AND place $reverse ath the next free slot from the beginning of the list + if ($reverse == $count) { + $table[$lui] = $count; + --$lui; + } else { + $table[$lli] = $count; + ++$lli; + $table[$lli] = $reverse; + ++$lli; + } + } + } + } + + return $table; + } + + /** + * Reverse unsigned short value + * + * @param $num (int) value to reversr + * @return int reversed value + * @protected + */ + protected function imb_reverse_us($num) + { + $rev = 0; + for ($i = 0; $i < 16; ++$i) { + $rev <<= 1; + $rev |= ($num & 1); + $num >>= 1; + } + + return $rev; + } +} diff --git a/src/Types/TypeKix.php b/src/Types/TypeKix.php new file mode 100644 index 0000000..08595a5 --- /dev/null +++ b/src/Types/TypeKix.php @@ -0,0 +1,16 @@ + 0) { + if (($code % 2) == 0) { + $seq .= '11100'; + $code -= 2; + } else { + $seq .= '100'; + $code -= 1; + } + $code /= 2; + } + $seq = substr($seq, 0, -2); + $seq = strrev($seq); + $bararray = array('code' => $code, 'maxw' => 0, 'maxh' => 1, 'bcode' => array()); + + return BinarySequenceConverter::convert($seq, $bararray); + } +} diff --git a/src/Types/TypePharmacodeTwoCode.php b/src/Types/TypePharmacodeTwoCode.php new file mode 100644 index 0000000..465a9c2 --- /dev/null +++ b/src/Types/TypePharmacodeTwoCode.php @@ -0,0 +1,66 @@ + $code, 'maxw' => 0, 'maxh' => 2, 'bcode' => array()); + $len = strlen($seq); + for ($i = 0; $i < $len; ++$i) { + switch ($seq[$i]) { + case '1': { + $p = 1; + $h = 1; + break; + } + case '2': { + $p = 0; + $h = 1; + break; + } + case '3': { + $p = 0; + $h = 2; + break; + } + } + $bararray['bcode'][$k++] = array('t' => 1, 'w' => 1, 'h' => $h, 'p' => $p); + $bararray['bcode'][$k++] = array('t' => 0, 'w' => 1, 'h' => 2, 'p' => 0); + $bararray['maxw'] += 2; + } + unset($bararray['bcode'][($k - 1)]); + --$bararray['maxw']; + + return $bararray; + } +} diff --git a/src/Types/TypeRms4cc.php b/src/Types/TypeRms4cc.php new file mode 100644 index 0000000..c68c93f --- /dev/null +++ b/src/Types/TypeRms4cc.php @@ -0,0 +1,162 @@ +kix; + // bar mode + // 1 = pos 1, length 2 + // 2 = pos 1, length 3 + // 3 = pos 2, length 1 + // 4 = pos 2, length 2 + $barmode = array( + '0' => array(3, 3, 2, 2), + '1' => array(3, 4, 1, 2), + '2' => array(3, 4, 2, 1), + '3' => array(4, 3, 1, 2), + '4' => array(4, 3, 2, 1), + '5' => array(4, 4, 1, 1), + '6' => array(3, 1, 4, 2), + '7' => array(3, 2, 3, 2), + '8' => array(3, 2, 4, 1), + '9' => array(4, 1, 3, 2), + 'A' => array(4, 1, 4, 1), + 'B' => array(4, 2, 3, 1), + 'C' => array(3, 1, 2, 4), + 'D' => array(3, 2, 1, 4), + 'E' => array(3, 2, 2, 3), + 'F' => array(4, 1, 1, 4), + 'G' => array(4, 1, 2, 3), + 'H' => array(4, 2, 1, 3), + 'I' => array(1, 3, 4, 2), + 'J' => array(1, 4, 3, 2), + 'K' => array(1, 4, 4, 1), + 'L' => array(2, 3, 3, 2), + 'M' => array(2, 3, 4, 1), + 'N' => array(2, 4, 3, 1), + 'O' => array(1, 3, 2, 4), + 'P' => array(1, 4, 1, 4), + 'Q' => array(1, 4, 2, 3), + 'R' => array(2, 3, 1, 4), + 'S' => array(2, 3, 2, 3), + 'T' => array(2, 4, 1, 3), + 'U' => array(1, 1, 4, 4), + 'V' => array(1, 2, 3, 4), + 'W' => array(1, 2, 4, 3), + 'X' => array(2, 1, 3, 4), + 'Y' => array(2, 1, 4, 3), + 'Z' => array(2, 2, 3, 3) + ); + $code = strtoupper($code); + $len = strlen($code); + $bararray = array('code' => $code, 'maxw' => 0, 'maxh' => 3, 'bcode' => array()); + if ($notkix) { + // table for checksum calculation (row,col) + $checktable = array( + '0' => array(1, 1), + '1' => array(1, 2), + '2' => array(1, 3), + '3' => array(1, 4), + '4' => array(1, 5), + '5' => array(1, 0), + '6' => array(2, 1), + '7' => array(2, 2), + '8' => array(2, 3), + '9' => array(2, 4), + 'A' => array(2, 5), + 'B' => array(2, 0), + 'C' => array(3, 1), + 'D' => array(3, 2), + 'E' => array(3, 3), + 'F' => array(3, 4), + 'G' => array(3, 5), + 'H' => array(3, 0), + 'I' => array(4, 1), + 'J' => array(4, 2), + 'K' => array(4, 3), + 'L' => array(4, 4), + 'M' => array(4, 5), + 'N' => array(4, 0), + 'O' => array(5, 1), + 'P' => array(5, 2), + 'Q' => array(5, 3), + 'R' => array(5, 4), + 'S' => array(5, 5), + 'T' => array(5, 0), + 'U' => array(0, 1), + 'V' => array(0, 2), + 'W' => array(0, 3), + 'X' => array(0, 4), + 'Y' => array(0, 5), + 'Z' => array(0, 0) + ); + $row = 0; + $col = 0; + for ($i = 0; $i < $len; ++$i) { + $row += $checktable[$code[$i]][0]; + $col += $checktable[$code[$i]][1]; + } + $row %= 6; + $col %= 6; + $chk = array_keys($checktable, array($row, $col)); + $code .= $chk[0]; + ++$len; + } + $k = 0; + if ($notkix) { + // start bar + $bararray['bcode'][$k++] = array('t' => 1, 'w' => 1, 'h' => 2, 'p' => 0); + $bararray['bcode'][$k++] = array('t' => 0, 'w' => 1, 'h' => 2, 'p' => 0); + $bararray['maxw'] += 2; + } + for ($i = 0; $i < $len; ++$i) { + for ($j = 0; $j < 4; ++$j) { + switch ($barmode[$code[$i]][$j]) { + case 1: { + $p = 0; + $h = 2; + break; + } + case 2: { + $p = 0; + $h = 3; + break; + } + case 3: { + $p = 1; + $h = 1; + break; + } + case 4: { + $p = 1; + $h = 2; + break; + } + } + $bararray['bcode'][$k++] = array('t' => 1, 'w' => 1, 'h' => $h, 'p' => $p); + $bararray['bcode'][$k++] = array('t' => 0, 'w' => 1, 'h' => 2, 'p' => 0); + $bararray['maxw'] += 2; + } + } + if ($notkix) { + // stop bar + $bararray['bcode'][$k++] = array('t' => 1, 'w' => 1, 'h' => 3, 'p' => 0); + $bararray['maxw'] += 1; + } + + return $bararray; + } +} From d84069b79a12eb9d873001d00686db396efa8ecf Mon Sep 17 00:00:00 2001 From: Casper Bakker Date: Wed, 1 Jan 2020 14:32:50 +0100 Subject: [PATCH 021/155] Moved some barcode types to own class --- src/BarcodeGenerator.php | 364 ++-------------------------------- src/Types/TypeEan13.php | 20 ++ src/Types/TypeEan8.php | 20 ++ src/Types/TypeEanUpcBase.php | 211 ++++++++++++++++++++ src/Types/TypeMsi.php | 17 ++ src/Types/TypeMsiChecksum.php | 71 +++++++ src/Types/TypePlanet.php | 28 +++ src/Types/TypePostnet.php | 66 ++++++ src/Types/TypeUpcA.php | 20 ++ src/Types/TypeUpcE.php | 20 ++ tests/VerifiedBarcodeTest.php | 6 +- 11 files changed, 494 insertions(+), 349 deletions(-) create mode 100644 src/Types/TypeEan13.php create mode 100644 src/Types/TypeEan8.php create mode 100644 src/Types/TypeEanUpcBase.php create mode 100644 src/Types/TypeMsi.php create mode 100644 src/Types/TypeMsiChecksum.php create mode 100644 src/Types/TypePlanet.php create mode 100644 src/Types/TypePostnet.php create mode 100644 src/Types/TypeUpcA.php create mode 100644 src/Types/TypeUpcE.php diff --git a/src/BarcodeGenerator.php b/src/BarcodeGenerator.php index 7272cb1..1236ddc 100644 --- a/src/BarcodeGenerator.php +++ b/src/BarcodeGenerator.php @@ -29,7 +29,6 @@ namespace Picqer\Barcode; -use Picqer\Barcode\Exceptions\BarcodeException; use Picqer\Barcode\Exceptions\InvalidCharacterException; use Picqer\Barcode\Exceptions\InvalidCheckDigitException; use Picqer\Barcode\Exceptions\InvalidFormatException; @@ -40,13 +39,21 @@ use Picqer\Barcode\Types\TypeCodabar; use Picqer\Barcode\Types\TypeCode11; use Picqer\Barcode\Types\TypeCode93; +use Picqer\Barcode\Types\TypeEan13; +use Picqer\Barcode\Types\TypeEan8; use Picqer\Barcode\Types\TypeIntelligentMailBarcode; use Picqer\Barcode\Types\TypeInterleaved25; use Picqer\Barcode\Types\TypeInterleaved25Checksum; use Picqer\Barcode\Types\TypeKix; +use Picqer\Barcode\Types\TypeMsi; +use Picqer\Barcode\Types\TypeMsiChecksum; use Picqer\Barcode\Types\TypePharmacode; use Picqer\Barcode\Types\TypePharmacodeTwoCode; +use Picqer\Barcode\Types\TypePlanet; +use Picqer\Barcode\Types\TypePostnet; use Picqer\Barcode\Types\TypeRms4cc; +use Picqer\Barcode\Types\TypeUpcA; +use Picqer\Barcode\Types\TypeUpcE; abstract class BarcodeGenerator { @@ -153,35 +160,35 @@ protected function getBarcodeData($code, $type) break; case self::TYPE_EAN_8: - $arrcode = $this->barcode_eanupc($code, 8); + $barcodeDataBuilder = new TypeEan8(); break; case self::TYPE_EAN_13: - $arrcode = $this->barcode_eanupc($code, 13); + $barcodeDataBuilder = new TypeEan13(); break; case self::TYPE_UPC_A: - $arrcode = $this->barcode_eanupc($code, 12); + $barcodeDataBuilder = new TypeUpcA(); break; case self::TYPE_UPC_E: - $arrcode = $this->barcode_eanupc($code, 6); + $barcodeDataBuilder = new TypeUpcE(); break; case self::TYPE_MSI: - $arrcode = $this->barcode_msi($code, false); + $barcodeDataBuilder = new TypeMsi(); break; case self::TYPE_MSI_CHECKSUM: - $arrcode = $this->barcode_msi($code, true); + $barcodeDataBuilder = new TypeMsiChecksum(); break; case self::TYPE_POSTNET: - $arrcode = $this->barcode_postnet($code, false); + $barcodeDataBuilder = new TypePostnet(); break; case self::TYPE_PLANET: - $arrcode = $this->barcode_postnet($code, true); + $barcodeDataBuilder = new TypePlanet(); break; case self::TYPE_RMS4CC: @@ -570,67 +577,6 @@ protected function checksum_s25($code) return $r; } - /** - * MSI. - * Variation of Plessey code, with similar applications - * Contains digits (0 to 9) and encodes the data only in the width of bars. - * - * @param $code (string) code to represent. - * @param $checksum (boolean) if true add a checksum to the code (modulo 11) - * @return array barcode representation. - * @protected - */ - protected function barcode_msi($code, $checksum = false) - { - $chr['0'] = '100100100100'; - $chr['1'] = '100100100110'; - $chr['2'] = '100100110100'; - $chr['3'] = '100100110110'; - $chr['4'] = '100110100100'; - $chr['5'] = '100110100110'; - $chr['6'] = '100110110100'; - $chr['7'] = '100110110110'; - $chr['8'] = '110100100100'; - $chr['9'] = '110100100110'; - $chr['A'] = '110100110100'; - $chr['B'] = '110100110110'; - $chr['C'] = '110110100100'; - $chr['D'] = '110110100110'; - $chr['E'] = '110110110100'; - $chr['F'] = '110110110110'; - if ($checksum) { - // add checksum - $clen = strlen($code); - $p = 2; - $check = 0; - for ($i = ($clen - 1); $i >= 0; --$i) { - $check += (hexdec($code[$i]) * $p); - ++$p; - if ($p > 7) { - $p = 2; - } - } - $check %= 11; - if ($check > 0) { - $check = 11 - $check; - } - $code .= $check; - } - $seq = '110'; // left guard - $clen = strlen($code); - for ($i = 0; $i < $clen; ++$i) { - $digit = $code[$i]; - if ( ! isset($chr[$digit])) { - throw new InvalidCharacterException('Char ' . $digit . ' is unsupported'); - } - $seq .= $chr[$digit]; - } - $seq .= '1001'; // right guard - $bararray = array('code' => $code, 'maxw' => 0, 'maxh' => 1, 'bcode' => array()); - - return BinarySequenceConverter::convert($seq, $bararray); - } - /** * Standard 2 of 5 barcodes. * Used in airline ticket marking, photofinishing @@ -1063,208 +1009,6 @@ protected function get128ABsequence($code) return $sequence; } - /** - * EAN13 and UPC-A barcodes. - * EAN13: European Article Numbering international retail product code - * UPC-A: Universal product code seen on almost all retail products in the USA and Canada - * UPC-E: Short version of UPC symbol - * - * @param $code (string) code to represent. - * @param $len (string) barcode type: 6 = UPC-E, 8 = EAN8, 13 = EAN13, 12 = UPC-A - * @return array barcode representation. - * @protected - */ - protected function barcode_eanupc($code, $len = 13) - { - $upce = false; - if ($len == 6) { - $len = 12; // UPC-A - $upce = true; // UPC-E mode - } - $data_len = $len - 1; - //Padding - $code = str_pad($code, $data_len, '0', STR_PAD_LEFT); - $code_len = strlen($code); - // calculate check digit - $sum_a = 0; - for ($i = 1; $i < $data_len; $i += 2) { - $sum_a += $code[$i]; - } - if ($len > 12) { - $sum_a *= 3; - } - $sum_b = 0; - for ($i = 0; $i < $data_len; $i += 2) { - $sum_b += intval(($code[$i])); - } - if ($len < 13) { - $sum_b *= 3; - } - $r = ($sum_a + $sum_b) % 10; - if ($r > 0) { - $r = (10 - $r); - } - if ($code_len == $data_len) { - // add check digit - $code .= $r; - } elseif ($r !== intval($code[$data_len])) { - throw new InvalidCheckDigitException(); - } - if ($len == 12) { - // UPC-A - $code = '0' . $code; - ++$len; - } - if ($upce) { - // convert UPC-A to UPC-E - $tmp = substr($code, 4, 3); - if (($tmp == '000') OR ($tmp == '100') OR ($tmp == '200')) { - // manufacturer code ends in 000, 100, or 200 - $upce_code = substr($code, 2, 2) . substr($code, 9, 3) . substr($code, 4, 1); - } else { - $tmp = substr($code, 5, 2); - if ($tmp == '00') { - // manufacturer code ends in 00 - $upce_code = substr($code, 2, 3) . substr($code, 10, 2) . '3'; - } else { - $tmp = substr($code, 6, 1); - if ($tmp == '0') { - // manufacturer code ends in 0 - $upce_code = substr($code, 2, 4) . substr($code, 11, 1) . '4'; - } else { - // manufacturer code does not end in zero - $upce_code = substr($code, 2, 5) . substr($code, 11, 1); - } - } - } - } - //Convert digits to bars - $codes = array( - 'A' => array( // left odd parity - '0' => '0001101', - '1' => '0011001', - '2' => '0010011', - '3' => '0111101', - '4' => '0100011', - '5' => '0110001', - '6' => '0101111', - '7' => '0111011', - '8' => '0110111', - '9' => '0001011' - ), - 'B' => array( // left even parity - '0' => '0100111', - '1' => '0110011', - '2' => '0011011', - '3' => '0100001', - '4' => '0011101', - '5' => '0111001', - '6' => '0000101', - '7' => '0010001', - '8' => '0001001', - '9' => '0010111' - ), - 'C' => array( // right - '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') - ); - $upce_parities = array(); - $upce_parities[0] = array( - '0' => array('B', 'B', 'B', 'A', 'A', 'A'), - '1' => array('B', 'B', 'A', 'B', 'A', 'A'), - '2' => array('B', 'B', 'A', 'A', 'B', 'A'), - '3' => array('B', 'B', 'A', 'A', 'A', 'B'), - '4' => array('B', 'A', 'B', 'B', 'A', 'A'), - '5' => array('B', 'A', 'A', 'B', 'B', 'A'), - '6' => array('B', 'A', 'A', 'A', 'B', 'B'), - '7' => array('B', 'A', 'B', 'A', 'B', 'A'), - '8' => array('B', 'A', 'B', 'A', 'A', 'B'), - '9' => array('B', 'A', 'A', 'B', 'A', 'B') - ); - $upce_parities[1] = array( - '0' => array('A', 'A', 'A', 'B', 'B', 'B'), - '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') - ); - $k = 0; - $seq = '101'; // left guard bar - if ($upce) { - $bararray = array('code' => $upce_code, 'maxw' => 0, 'maxh' => 1, 'bcode' => array()); - $p = $upce_parities[$code[1]][$r]; - for ($i = 0; $i < 6; ++$i) { - $seq .= $codes[$p[$i]][$upce_code[$i]]; - } - $seq .= '010101'; // right guard bar - } else { - $bararray = array('code' => $code, 'maxw' => 0, 'maxh' => 1, 'bcode' => array()); - $half_len = intval(ceil($len / 2)); - if ($len == 8) { - for ($i = 0; $i < $half_len; ++$i) { - $seq .= $codes['A'][$code[$i]]; - } - } else { - $p = $parities[$code[0]]; - for ($i = 1; $i < $half_len; ++$i) { - $seq .= $codes[$p[$i - 1]][$code[$i]]; - } - } - $seq .= '01010'; // center guard bar - for ($i = $half_len; $i < $len; ++$i) { - if ( ! isset($codes['C'][$code[$i]])) { - throw new InvalidCharacterException('Char ' . $code[$i] . ' not allowed'); - } - $seq .= $codes['C'][$code[$i]]; - } - $seq .= '101'; // right guard bar - } - $clen = strlen($seq); - $w = 0; - for ($i = 0; $i < $clen; ++$i) { - $w += 1; - if (($i == ($clen - 1)) OR (($i < ($clen - 1)) AND ($seq[$i] != $seq[($i + 1)]))) { - if ($seq[$i] == '1') { - $t = true; // bar - } else { - $t = false; // space - } - $bararray['bcode'][$k] = array('t' => $t, 'w' => $w, 'h' => 1, 'p' => 0); - $bararray['maxw'] += $w; - ++$k; - $w = 0; - } - } - - return $bararray; - } - /** * UPC-Based Extensions * 2-Digit Ext.: Used to indicate magazines and newspaper issue numbers @@ -1346,82 +1090,6 @@ protected function barcode_eanext($code, $len = 5) return BinarySequenceConverter::convert($seq, $bararray); } - /** - * POSTNET and PLANET barcodes. - * Used by U.S. Postal Service for automated mail sorting - * - * @param $code (string) zip code to represent. Must be a string containing a zip code of the form DDDDD or - * DDDDD-DDDD. - * @param $planet (boolean) if true print the PLANET barcode, otherwise print POSTNET - * @return array barcode representation. - * @protected - */ - protected function barcode_postnet($code, $planet = false) - { - // bar length - if ($planet) { - $barlen = Array( - 0 => Array(1, 1, 2, 2, 2), - 1 => Array(2, 2, 2, 1, 1), - 2 => Array(2, 2, 1, 2, 1), - 3 => Array(2, 2, 1, 1, 2), - 4 => Array(2, 1, 2, 2, 1), - 5 => Array(2, 1, 2, 1, 2), - 6 => Array(2, 1, 1, 2, 2), - 7 => Array(1, 2, 2, 2, 1), - 8 => Array(1, 2, 2, 1, 2), - 9 => Array(1, 2, 1, 2, 2) - ); - } else { - $barlen = Array( - 0 => Array(2, 2, 1, 1, 1), - 1 => Array(1, 1, 1, 2, 2), - 2 => Array(1, 1, 2, 1, 2), - 3 => Array(1, 1, 2, 2, 1), - 4 => Array(1, 2, 1, 1, 2), - 5 => Array(1, 2, 1, 2, 1), - 6 => Array(1, 2, 2, 1, 1), - 7 => Array(2, 1, 1, 1, 2), - 8 => Array(2, 1, 1, 2, 1), - 9 => Array(2, 1, 2, 1, 1) - ); - } - $bararray = array('code' => $code, 'maxw' => 0, 'maxh' => 2, 'bcode' => array()); - $k = 0; - $code = str_replace('-', '', $code); - $code = str_replace(' ', '', $code); - $len = strlen($code); - // calculate checksum - $sum = 0; - for ($i = 0; $i < $len; ++$i) { - $sum += intval($code[$i]); - } - $chkd = ($sum % 10); - if ($chkd > 0) { - $chkd = (10 - $chkd); - } - $code .= $chkd; - $len = strlen($code); - // start bar - $bararray['bcode'][$k++] = array('t' => 1, 'w' => 1, 'h' => 2, 'p' => 0); - $bararray['bcode'][$k++] = array('t' => 0, 'w' => 1, 'h' => 2, 'p' => 0); - $bararray['maxw'] += 2; - for ($i = 0; $i < $len; ++$i) { - for ($j = 0; $j < 5; ++$j) { - $h = $barlen[$code[$i]][$j]; - $p = floor(1 / $h); - $bararray['bcode'][$k++] = array('t' => 1, 'w' => 1, 'h' => $h, 'p' => $p); - $bararray['bcode'][$k++] = array('t' => 0, 'w' => 1, 'h' => 2, 'p' => 0); - $bararray['maxw'] += 2; - } - } - // end bar - $bararray['bcode'][$k++] = array('t' => 1, 'w' => 1, 'h' => 2, 'p' => 0); - $bararray['maxw'] += 1; - - return $bararray; - } - /** * IMB - Intelligent Mail Barcode - Onecode - USPS-B-3200 * diff --git a/src/Types/TypeEan13.php b/src/Types/TypeEan13.php new file mode 100644 index 0000000..194d3ee --- /dev/null +++ b/src/Types/TypeEan13.php @@ -0,0 +1,20 @@ +length; + + $data_len = $length - 1; + //Padding + $code = str_pad($code, $data_len, '0', STR_PAD_LEFT); + $code_len = strlen($code); + // calculate check digit + $sum_a = 0; + for ($i = 1; $i < $data_len; $i += 2) { + $sum_a += $code[$i]; + } + if ($length > 12) { + $sum_a *= 3; + } + $sum_b = 0; + for ($i = 0; $i < $data_len; $i += 2) { + $sum_b += intval(($code[$i])); + } + if ($length < 13) { + $sum_b *= 3; + } + $r = ($sum_a + $sum_b) % 10; + if ($r > 0) { + $r = (10 - $r); + } + if ($code_len == $data_len) { + // add check digit + $code .= $r; + } elseif ($r !== intval($code[$data_len])) { + throw new InvalidCheckDigitException(); + } + if ($this->upca || $this->upce) { + // UPC-A + $code = '0' . $code; + ++$length; + } + if ($this->upce) { + // convert UPC-A to UPC-E + $tmp = substr($code, 4, 3); + if (($tmp == '000') OR ($tmp == '100') OR ($tmp == '200')) { + // manufacturer code ends in 000, 100, or 200 + $upce_code = substr($code, 2, 2) . substr($code, 9, 3) . substr($code, 4, 1); + } else { + $tmp = substr($code, 5, 2); + if ($tmp == '00') { + // manufacturer code ends in 00 + $upce_code = substr($code, 2, 3) . substr($code, 10, 2) . '3'; + } else { + $tmp = substr($code, 6, 1); + if ($tmp == '0') { + // manufacturer code ends in 0 + $upce_code = substr($code, 2, 4) . substr($code, 11, 1) . '4'; + } else { + // manufacturer code does not end in zero + $upce_code = substr($code, 2, 5) . substr($code, 11, 1); + } + } + } + } + //Convert digits to bars + $codes = array( + 'A' => array( // left odd parity + '0' => '0001101', + '1' => '0011001', + '2' => '0010011', + '3' => '0111101', + '4' => '0100011', + '5' => '0110001', + '6' => '0101111', + '7' => '0111011', + '8' => '0110111', + '9' => '0001011' + ), + 'B' => array( // left even parity + '0' => '0100111', + '1' => '0110011', + '2' => '0011011', + '3' => '0100001', + '4' => '0011101', + '5' => '0111001', + '6' => '0000101', + '7' => '0010001', + '8' => '0001001', + '9' => '0010111' + ), + 'C' => array( // right + '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') + ); + $upce_parities = array(); + $upce_parities[0] = array( + '0' => array('B', 'B', 'B', 'A', 'A', 'A'), + '1' => array('B', 'B', 'A', 'B', 'A', 'A'), + '2' => array('B', 'B', 'A', 'A', 'B', 'A'), + '3' => array('B', 'B', 'A', 'A', 'A', 'B'), + '4' => array('B', 'A', 'B', 'B', 'A', 'A'), + '5' => array('B', 'A', 'A', 'B', 'B', 'A'), + '6' => array('B', 'A', 'A', 'A', 'B', 'B'), + '7' => array('B', 'A', 'B', 'A', 'B', 'A'), + '8' => array('B', 'A', 'B', 'A', 'A', 'B'), + '9' => array('B', 'A', 'A', 'B', 'A', 'B') + ); + $upce_parities[1] = array( + '0' => array('A', 'A', 'A', 'B', 'B', 'B'), + '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') + ); + $k = 0; + $seq = '101'; // left guard bar + if ($this->upce) { + $bararray = array('code' => $upce_code, 'maxw' => 0, 'maxh' => 1, 'bcode' => array()); + $p = $upce_parities[$code[1]][$r]; + for ($i = 0; $i < 6; ++$i) { + $seq .= $codes[$p[$i]][$upce_code[$i]]; + } + $seq .= '010101'; // right guard bar + } else { + $bararray = array('code' => $code, 'maxw' => 0, 'maxh' => 1, 'bcode' => array()); + $half_len = intval(ceil($length / 2)); + if ($length == 8) { + for ($i = 0; $i < $half_len; ++$i) { + $seq .= $codes['A'][$code[$i]]; + } + } else { + $p = $parities[$code[0]]; + for ($i = 1; $i < $half_len; ++$i) { + $seq .= $codes[$p[$i - 1]][$code[$i]]; + } + } + $seq .= '01010'; // center guard bar + for ($i = $half_len; $i < $length; ++$i) { + if ( ! isset($codes['C'][$code[$i]])) { + throw new InvalidCharacterException('Char ' . $code[$i] . ' not allowed'); + } + $seq .= $codes['C'][$code[$i]]; + } + $seq .= '101'; // right guard bar + } + $clen = strlen($seq); + $w = 0; + for ($i = 0; $i < $clen; ++$i) { + $w += 1; + if (($i == ($clen - 1)) OR (($i < ($clen - 1)) AND ($seq[$i] != $seq[($i + 1)]))) { + if ($seq[$i] == '1') { + $t = true; // bar + } else { + $t = false; // space + } + $bararray['bcode'][$k] = array('t' => $t, 'w' => $w, 'h' => 1, 'p' => 0); + $bararray['maxw'] += $w; + ++$k; + $w = 0; + } + } + + return $bararray; + } +} diff --git a/src/Types/TypeMsi.php b/src/Types/TypeMsi.php new file mode 100644 index 0000000..d6e91cb --- /dev/null +++ b/src/Types/TypeMsi.php @@ -0,0 +1,17 @@ +checksum) { + // add checksum + $clen = strlen($code); + $p = 2; + $check = 0; + for ($i = ($clen - 1); $i >= 0; --$i) { + $check += (hexdec($code[$i]) * $p); + ++$p; + if ($p > 7) { + $p = 2; + } + } + $check %= 11; + if ($check > 0) { + $check = 11 - $check; + } + $code .= $check; + } + $seq = '110'; // left guard + $clen = strlen($code); + for ($i = 0; $i < $clen; ++$i) { + $digit = $code[$i]; + if ( ! isset($chr[$digit])) { + throw new InvalidCharacterException('Char ' . $digit . ' is unsupported'); + } + $seq .= $chr[$digit]; + } + $seq .= '1001'; // right guard + $bararray = array('code' => $code, 'maxw' => 0, 'maxh' => 1, 'bcode' => array()); + + return BinarySequenceConverter::convert($seq, $bararray); + } +} diff --git a/src/Types/TypePlanet.php b/src/Types/TypePlanet.php new file mode 100644 index 0000000..1e3d5d3 --- /dev/null +++ b/src/Types/TypePlanet.php @@ -0,0 +1,28 @@ + Array(1, 1, 2, 2, 2), + 1 => Array(2, 2, 2, 1, 1), + 2 => Array(2, 2, 1, 2, 1), + 3 => Array(2, 2, 1, 1, 2), + 4 => Array(2, 1, 2, 2, 1), + 5 => Array(2, 1, 2, 1, 2), + 6 => Array(2, 1, 1, 2, 2), + 7 => Array(1, 2, 2, 2, 1), + 8 => Array(1, 2, 2, 1, 2), + 9 => Array(1, 2, 1, 2, 2) + ); +} diff --git a/src/Types/TypePostnet.php b/src/Types/TypePostnet.php new file mode 100644 index 0000000..b2d885a --- /dev/null +++ b/src/Types/TypePostnet.php @@ -0,0 +1,66 @@ + Array(2, 2, 1, 1, 1), + 1 => Array(1, 1, 1, 2, 2), + 2 => Array(1, 1, 2, 1, 2), + 3 => Array(1, 1, 2, 2, 1), + 4 => Array(1, 2, 1, 1, 2), + 5 => Array(1, 2, 1, 2, 1), + 6 => Array(1, 2, 2, 1, 1), + 7 => Array(2, 1, 1, 1, 2), + 8 => Array(2, 1, 1, 2, 1), + 9 => Array(2, 1, 2, 1, 1) + ); + + public function getBarcodeData(string $code): array + { + $bararray = array('code' => $code, 'maxw' => 0, 'maxh' => 2, 'bcode' => array()); + $k = 0; + $code = str_replace('-', '', $code); + $code = str_replace(' ', '', $code); + $len = strlen($code); + // calculate checksum + $sum = 0; + for ($i = 0; $i < $len; ++$i) { + $sum += intval($code[$i]); + } + $chkd = ($sum % 10); + if ($chkd > 0) { + $chkd = (10 - $chkd); + } + $code .= $chkd; + $len = strlen($code); + // start bar + $bararray['bcode'][$k++] = array('t' => 1, 'w' => 1, 'h' => 2, 'p' => 0); + $bararray['bcode'][$k++] = array('t' => 0, 'w' => 1, 'h' => 2, 'p' => 0); + $bararray['maxw'] += 2; + for ($i = 0; $i < $len; ++$i) { + for ($j = 0; $j < 5; ++$j) { + $h = $this->barlen[$code[$i]][$j]; + $p = floor(1 / $h); + $bararray['bcode'][$k++] = array('t' => 1, 'w' => 1, 'h' => $h, 'p' => $p); + $bararray['bcode'][$k++] = array('t' => 0, 'w' => 1, 'h' => 2, 'p' => 0); + $bararray['maxw'] += 2; + } + } + // end bar + $bararray['bcode'][$k++] = array('t' => 1, 'w' => 1, 'h' => 2, 'p' => 0); + $bararray['maxw'] += 1; + + return $bararray; + } +} diff --git a/src/Types/TypeUpcA.php b/src/Types/TypeUpcA.php new file mode 100644 index 0000000..11d45b9 --- /dev/null +++ b/src/Types/TypeUpcA.php @@ -0,0 +1,20 @@ +getBarcode($barcode, $barcodeTestSet['type']); - $this->assertStringEqualsFile(sprintf('tests/verified-files/%s-%s.svg', $barcodeTestSet['type'], $barcode), $result); + $this->assertStringEqualsFile( + sprintf('tests/verified-files/%s-%s.svg', $barcodeTestSet['type'], $barcode), + $result, + sprintf('%s x %s dynamic test failed', $barcodeTestSet['type'], $barcode) + ); } } } From 34d3dd677a674617c6a6156a986aa13e7b0e904f Mon Sep 17 00:00:00 2001 From: Casper Bakker Date: Wed, 1 Jan 2020 14:48:29 +0100 Subject: [PATCH 022/155] Moved some barcode types to own class + TypeInterface --- src/BarcodeGenerator.php | 504 +---------------------- src/Types/Type.php | 8 - src/Types/TypeCodabar.php | 2 +- src/Types/TypeCode11.php | 2 +- src/Types/TypeCode39.php | 325 +++++++++++++++ src/Types/TypeCode39Checksum.php | 14 + src/Types/TypeCode39Extended.php | 14 + src/Types/TypeCode39ExtendedChecksum.php | 14 + src/Types/TypeCode93.php | 2 +- src/Types/TypeEanUpcBase.php | 2 +- src/Types/TypeIntelligentMailBarcode.php | 2 +- src/Types/TypeInterface.php | 8 + src/Types/TypeInterleaved25Checksum.php | 2 +- src/Types/TypeMsiChecksum.php | 2 +- src/Types/TypePharmacode.php | 2 +- src/Types/TypePharmacodeTwoCode.php | 2 +- src/Types/TypePostnet.php | 2 +- src/Types/TypeRms4cc.php | 2 +- src/Types/TypeStandard2of5.php | 78 ++++ src/Types/TypeStandard2of5Checksum.php | 14 + src/Types/TypeUpcExtension2.php | 90 ++++ src/Types/TypeUpcExtension5.php | 14 + 22 files changed, 599 insertions(+), 506 deletions(-) delete mode 100644 src/Types/Type.php create mode 100644 src/Types/TypeCode39.php create mode 100644 src/Types/TypeCode39Checksum.php create mode 100644 src/Types/TypeCode39Extended.php create mode 100644 src/Types/TypeCode39ExtendedChecksum.php create mode 100644 src/Types/TypeInterface.php create mode 100644 src/Types/TypeStandard2of5.php create mode 100644 src/Types/TypeStandard2of5Checksum.php create mode 100644 src/Types/TypeUpcExtension2.php create mode 100644 src/Types/TypeUpcExtension5.php diff --git a/src/BarcodeGenerator.php b/src/BarcodeGenerator.php index 1236ddc..70fd4bb 100644 --- a/src/BarcodeGenerator.php +++ b/src/BarcodeGenerator.php @@ -38,6 +38,10 @@ use Picqer\Barcode\Helpers\OldBarcodeArrayConverter; use Picqer\Barcode\Types\TypeCodabar; use Picqer\Barcode\Types\TypeCode11; +use Picqer\Barcode\Types\TypeCode39; +use Picqer\Barcode\Types\TypeCode39Checksum; +use Picqer\Barcode\Types\TypeCode39Extended; +use Picqer\Barcode\Types\TypeCode39ExtendedChecksum; use Picqer\Barcode\Types\TypeCode93; use Picqer\Barcode\Types\TypeEan13; use Picqer\Barcode\Types\TypeEan8; @@ -52,8 +56,12 @@ use Picqer\Barcode\Types\TypePlanet; use Picqer\Barcode\Types\TypePostnet; use Picqer\Barcode\Types\TypeRms4cc; +use Picqer\Barcode\Types\TypeStandard2of5; +use Picqer\Barcode\Types\TypeStandard2of5Checksum; use Picqer\Barcode\Types\TypeUpcA; use Picqer\Barcode\Types\TypeUpcE; +use Picqer\Barcode\Types\TypeUpcExtension2; +use Picqer\Barcode\Types\TypeUpcExtension5; abstract class BarcodeGenerator { @@ -88,31 +96,23 @@ abstract class BarcodeGenerator const TYPE_PHARMA_CODE = 'PHARMA'; const TYPE_PHARMA_CODE_TWO_TRACKS = 'PHARMA2T'; - /** - * Get the barcode data - * - * @param string $code code to print - * @param string $type type of barcode - * @return array barcode array - * @public - */ - protected function getBarcodeData($code, $type) + protected function getBarcodeData(string $code, string $type): array { switch (strtoupper($type)) { case self::TYPE_CODE_39: - $arrcode = $this->barcode_code39($code, false, false); + $barcodeDataBuilder = new TypeCode39(); break; case self::TYPE_CODE_39_CHECKSUM: - $arrcode = $this->barcode_code39($code, false, true); + $barcodeDataBuilder = new TypeCode39Checksum(); break; case self::TYPE_CODE_39E: - $arrcode = $this->barcode_code39($code, true, false); + $barcodeDataBuilder = new TypeCode39Extended(); break; case self::TYPE_CODE_39E_CHECKSUM: - $arrcode = $this->barcode_code39($code, true, true); + $barcodeDataBuilder = new TypeCode39ExtendedChecksum(); break; case self::TYPE_CODE_93: @@ -120,11 +120,11 @@ protected function getBarcodeData($code, $type) break; case self::TYPE_STANDARD_2_5: - $arrcode = $this->barcode_s25($code, false); + $barcodeDataBuilder = new TypeStandard2of5(); break; case self::TYPE_STANDARD_2_5_CHECKSUM: - $arrcode = $this->barcode_s25($code, true); + $barcodeDataBuilder = new TypeStandard2of5Checksum(); break; case self::TYPE_INTERLEAVED_2_5: @@ -152,11 +152,11 @@ protected function getBarcodeData($code, $type) break; case self::TYPE_EAN_2: - $arrcode = $this->barcode_eanext($code, 2); + $barcodeDataBuilder = new TypeUpcExtension2(); break; case self::TYPE_EAN_5: - $arrcode = $this->barcode_eanext($code, 5); + $barcodeDataBuilder = new TypeUpcExtension5(); break; case self::TYPE_EAN_8: @@ -234,395 +234,6 @@ protected function getBarcodeData($code, $type) return $arrcode; } - /** - * CODE 39 - ANSI MH10.8M-1983 - USD-3 - 3 of 9. - * General-purpose code in very wide use world-wide - * - * @param $code (string) code to represent. - * @param $extended (boolean) if true uses the extended mode. - * @param $checksum (boolean) if true add a checksum to the code. - * @return array barcode representation. - * @protected - */ - protected function barcode_code39($code, $extended = false, $checksum = false) - { - $chr = []; - $chr['0'] = '111331311'; - $chr['1'] = '311311113'; - $chr['2'] = '113311113'; - $chr['3'] = '313311111'; - $chr['4'] = '111331113'; - $chr['5'] = '311331111'; - $chr['6'] = '113331111'; - $chr['7'] = '111311313'; - $chr['8'] = '311311311'; - $chr['9'] = '113311311'; - $chr['A'] = '311113113'; - $chr['B'] = '113113113'; - $chr['C'] = '313113111'; - $chr['D'] = '111133113'; - $chr['E'] = '311133111'; - $chr['F'] = '113133111'; - $chr['G'] = '111113313'; - $chr['H'] = '311113311'; - $chr['I'] = '113113311'; - $chr['J'] = '111133311'; - $chr['K'] = '311111133'; - $chr['L'] = '113111133'; - $chr['M'] = '313111131'; - $chr['N'] = '111131133'; - $chr['O'] = '311131131'; - $chr['P'] = '113131131'; - $chr['Q'] = '111111333'; - $chr['R'] = '311111331'; - $chr['S'] = '113111331'; - $chr['T'] = '111131331'; - $chr['U'] = '331111113'; - $chr['V'] = '133111113'; - $chr['W'] = '333111111'; - $chr['X'] = '131131113'; - $chr['Y'] = '331131111'; - $chr['Z'] = '133131111'; - $chr['-'] = '131111313'; - $chr['.'] = '331111311'; - $chr[' '] = '133111311'; - $chr['$'] = '131313111'; - $chr['/'] = '131311131'; - $chr['+'] = '131113131'; - $chr['%'] = '111313131'; - $chr['*'] = '131131311'; - - $code = strtoupper($code); - - if ($extended) { - // extended mode - $code = $this->encode_code39_ext($code); - } - - if ($checksum) { - // checksum - $code .= $this->checksum_code39($code); - } - - // add start and stop codes - $code = '*' . $code . '*'; - - $bararray = array('code' => $code, 'maxw' => 0, 'maxh' => 1, 'bcode' => array()); - $k = 0; - $clen = strlen($code); - for ($i = 0; $i < $clen; ++$i) { - $char = $code[$i]; - if ( ! isset($chr[$char])) { - throw new InvalidCharacterException('Char ' . $char . ' is unsupported'); - } - for ($j = 0; $j < 9; ++$j) { - if (($j % 2) == 0) { - $t = true; // bar - } else { - $t = false; // space - } - $w = $chr[$char][$j]; - $bararray['bcode'][$k] = array('t' => $t, 'w' => $w, 'h' => 1, 'p' => 0); - $bararray['maxw'] += $w; - ++$k; - } - // intercharacter gap - $bararray['bcode'][$k] = array('t' => false, 'w' => 1, 'h' => 1, 'p' => 0); - $bararray['maxw'] += 1; - ++$k; - } - - return $bararray; - } - - /** - * Encode a string to be used for CODE 39 Extended mode. - * - * @param string $code code to represent. - * @return bool|string encoded string. - * @protected - */ - protected function encode_code39_ext($code) - { - $encode = array( - chr(0) => '%U', - chr(1) => '$A', - chr(2) => '$B', - chr(3) => '$C', - chr(4) => '$D', - chr(5) => '$E', - chr(6) => '$F', - chr(7) => '$G', - chr(8) => '$H', - chr(9) => '$I', - chr(10) => '$J', - chr(11) => '£K', - chr(12) => '$L', - chr(13) => '$M', - chr(14) => '$N', - chr(15) => '$O', - chr(16) => '$P', - chr(17) => '$Q', - chr(18) => '$R', - chr(19) => '$S', - chr(20) => '$T', - chr(21) => '$U', - chr(22) => '$V', - chr(23) => '$W', - chr(24) => '$X', - chr(25) => '$Y', - chr(26) => '$Z', - chr(27) => '%A', - chr(28) => '%B', - chr(29) => '%C', - chr(30) => '%D', - chr(31) => '%E', - chr(32) => ' ', - chr(33) => '/A', - chr(34) => '/B', - chr(35) => '/C', - chr(36) => '/D', - chr(37) => '/E', - chr(38) => '/F', - chr(39) => '/G', - chr(40) => '/H', - chr(41) => '/I', - chr(42) => '/J', - chr(43) => '/K', - chr(44) => '/L', - chr(45) => '-', - chr(46) => '.', - chr(47) => '/O', - chr(48) => '0', - chr(49) => '1', - chr(50) => '2', - chr(51) => '3', - chr(52) => '4', - chr(53) => '5', - chr(54) => '6', - chr(55) => '7', - chr(56) => '8', - chr(57) => '9', - chr(58) => '/Z', - chr(59) => '%F', - chr(60) => '%G', - chr(61) => '%H', - chr(62) => '%I', - chr(63) => '%J', - chr(64) => '%V', - chr(65) => 'A', - chr(66) => 'B', - chr(67) => 'C', - chr(68) => 'D', - chr(69) => 'E', - chr(70) => 'F', - chr(71) => 'G', - chr(72) => 'H', - chr(73) => 'I', - chr(74) => 'J', - chr(75) => 'K', - chr(76) => 'L', - chr(77) => 'M', - chr(78) => 'N', - chr(79) => 'O', - chr(80) => 'P', - chr(81) => 'Q', - chr(82) => 'R', - chr(83) => 'S', - chr(84) => 'T', - chr(85) => 'U', - chr(86) => 'V', - chr(87) => 'W', - chr(88) => 'X', - chr(89) => 'Y', - chr(90) => 'Z', - chr(91) => '%K', - chr(92) => '%L', - chr(93) => '%M', - chr(94) => '%N', - chr(95) => '%O', - chr(96) => '%W', - chr(97) => '+A', - chr(98) => '+B', - chr(99) => '+C', - chr(100) => '+D', - chr(101) => '+E', - chr(102) => '+F', - chr(103) => '+G', - chr(104) => '+H', - chr(105) => '+I', - chr(106) => '+J', - chr(107) => '+K', - chr(108) => '+L', - chr(109) => '+M', - chr(110) => '+N', - chr(111) => '+O', - chr(112) => '+P', - chr(113) => '+Q', - chr(114) => '+R', - chr(115) => '+S', - chr(116) => '+T', - chr(117) => '+U', - chr(118) => '+V', - chr(119) => '+W', - chr(120) => '+X', - chr(121) => '+Y', - chr(122) => '+Z', - chr(123) => '%P', - chr(124) => '%Q', - chr(125) => '%R', - chr(126) => '%S', - chr(127) => '%T' - ); - $code_ext = ''; - $clen = strlen($code); - for ($i = 0; $i < $clen; ++$i) { - if (ord($code[$i]) > 127) { - throw new InvalidCharacterException('Only supports till char 127'); - } - $code_ext .= $encode[$code[$i]]; - } - - return $code_ext; - } - - /** - * Calculate CODE 39 checksum (modulo 43). - * - * @param string $code code to represent. - * @return string char checksum. - * @protected - */ - protected function checksum_code39($code) - { - $chars = array( - '0', - '1', - '2', - '3', - '4', - '5', - '6', - '7', - '8', - '9', - 'A', - 'B', - 'C', - 'D', - 'E', - 'F', - 'G', - 'H', - 'I', - 'J', - 'K', - 'L', - 'M', - 'N', - 'O', - 'P', - 'Q', - 'R', - 'S', - 'T', - 'U', - 'V', - 'W', - 'X', - 'Y', - 'Z', - '-', - '.', - ' ', - '$', - '/', - '+', - '%' - ); - $sum = 0; - $codelength = strlen($code); - for ($i = 0; $i < $codelength; ++$i) { - $k = array_keys($chars, $code[$i]); - $sum += $k[0]; - } - $j = ($sum % 43); - - return $chars[$j]; - } - - /** - * Checksum for standard 2 of 5 barcodes. - * - * @param $code (string) code to process. - * @return int checksum. - * @protected - */ - protected function checksum_s25($code) - { - $len = strlen($code); - $sum = 0; - for ($i = 0; $i < $len; $i += 2) { - $sum += $code[$i]; - } - $sum *= 3; - for ($i = 1; $i < $len; $i += 2) { - $sum += ($code[$i]); - } - $r = $sum % 10; - if ($r > 0) { - $r = (10 - $r); - } - - return $r; - } - - /** - * Standard 2 of 5 barcodes. - * Used in airline ticket marking, photofinishing - * Contains digits (0 to 9) and encodes the data only in the width of bars. - * - * @param $code (string) code to represent. - * @param $checksum (boolean) if true add a checksum to the code - * @return array barcode representation. - * @protected - */ - protected function barcode_s25($code, $checksum = false) - { - $chr['0'] = '10101110111010'; - $chr['1'] = '11101010101110'; - $chr['2'] = '10111010101110'; - $chr['3'] = '11101110101010'; - $chr['4'] = '10101110101110'; - $chr['5'] = '11101011101010'; - $chr['6'] = '10111011101010'; - $chr['7'] = '10101011101110'; - $chr['8'] = '10101110111010'; - $chr['9'] = '10111010111010'; - if ($checksum) { - // add checksum - $code .= $this->checksum_s25($code); - } - if ((strlen($code) % 2) != 0) { - // add leading zero if code-length is odd - $code = '0' . $code; - } - $seq = '11011010'; - $clen = strlen($code); - for ($i = 0; $i < $clen; ++$i) { - $digit = $code[$i]; - if ( ! isset($chr[$digit])) { - throw new InvalidCharacterException('Char ' . $digit . ' is unsupported'); - } - $seq .= $chr[$digit]; - } - $seq .= '1101011'; - $bararray = array('code' => $code, 'maxw' => 0, 'maxh' => 1, 'bcode' => array()); - - return BinarySequenceConverter::convert($seq, $bararray); - } - - /** * C128 barcodes. * Very capable code, excellent density, high reliability; in very wide use world-wide @@ -1009,87 +620,6 @@ protected function get128ABsequence($code) return $sequence; } - /** - * UPC-Based Extensions - * 2-Digit Ext.: Used to indicate magazines and newspaper issue numbers - * 5-Digit Ext.: Used to mark suggested retail price of books - * - * @param $code (string) code to represent. - * @param $len (string) barcode type: 2 = 2-Digit, 5 = 5-Digit - * @return array barcode representation. - * @protected - */ - protected function barcode_eanext($code, $len = 5) - { - //Padding - $code = str_pad($code, $len, '0', STR_PAD_LEFT); - // calculate check digit - if ($len == 2) { - $r = $code % 4; - } elseif ($len == 5) { - $r = (3 * ($code[0] + $code[2] + $code[4])) + (9 * ($code[1] + $code[3])); - $r %= 10; - } else { - throw new InvalidCheckDigitException(); - } - //Convert digits to bars - $codes = array( - 'A' => array( // left odd parity - '0' => '0001101', - '1' => '0011001', - '2' => '0010011', - '3' => '0111101', - '4' => '0100011', - '5' => '0110001', - '6' => '0101111', - '7' => '0111011', - '8' => '0110111', - '9' => '0001011' - ), - 'B' => array( // left even parity - '0' => '0100111', - '1' => '0110011', - '2' => '0011011', - '3' => '0100001', - '4' => '0011101', - '5' => '0111001', - '6' => '0000101', - '7' => '0010001', - '8' => '0001001', - '9' => '0010111' - ) - ); - $parities = array(); - $parities[2] = array( - '0' => array('A', 'A'), - '1' => array('A', 'B'), - '2' => array('B', 'A'), - '3' => array('B', 'B') - ); - $parities[5] = array( - '0' => array('B', 'B', 'A', 'A', 'A'), - '1' => array('B', 'A', 'B', 'A', 'A'), - '2' => array('B', 'A', 'A', 'B', 'A'), - '3' => array('B', 'A', 'A', 'A', 'B'), - '4' => array('A', 'B', 'B', 'A', 'A'), - '5' => array('A', 'A', 'B', 'B', 'A'), - '6' => array('A', 'A', 'A', 'B', 'B'), - '7' => array('A', 'B', 'A', 'B', 'A'), - '8' => array('A', 'B', 'A', 'A', 'B'), - '9' => array('A', 'A', 'B', 'A', 'B') - ); - $p = $parities[$len][$r]; - $seq = '1011'; // left guard bar - $seq .= $codes[$p[0]][$code[0]]; - for ($i = 1; $i < $len; ++$i) { - $seq .= '01'; // separator - $seq .= $codes[$p[$i]][$code[$i]]; - } - $bararray = array('code' => $code, 'maxw' => 0, 'maxh' => 1, 'bcode' => array()); - - return BinarySequenceConverter::convert($seq, $bararray); - } - /** * IMB - Intelligent Mail Barcode - Onecode - USPS-B-3200 * diff --git a/src/Types/Type.php b/src/Types/Type.php deleted file mode 100644 index e093a20..0000000 --- a/src/Types/Type.php +++ /dev/null @@ -1,8 +0,0 @@ -extended) { + // extended mode + $code = $this->encode_code39_ext($code); + } + + if ($this->checksum) { + // checksum + $code .= $this->checksum_code39($code); + } + + // add start and stop codes + $code = '*' . $code . '*'; + + $bararray = array('code' => $code, 'maxw' => 0, 'maxh' => 1, 'bcode' => array()); + $k = 0; + $clen = strlen($code); + for ($i = 0; $i < $clen; ++$i) { + $char = $code[$i]; + if ( ! isset($chr[$char])) { + throw new InvalidCharacterException('Char ' . $char . ' is unsupported'); + } + for ($j = 0; $j < 9; ++$j) { + if (($j % 2) == 0) { + $t = true; // bar + } else { + $t = false; // space + } + $w = $chr[$char][$j]; + $bararray['bcode'][$k] = array('t' => $t, 'w' => $w, 'h' => 1, 'p' => 0); + $bararray['maxw'] += $w; + ++$k; + } + // intercharacter gap + $bararray['bcode'][$k] = array('t' => false, 'w' => 1, 'h' => 1, 'p' => 0); + $bararray['maxw'] += 1; + ++$k; + } + + return $bararray; + } + + + /** + * Encode a string to be used for CODE 39 Extended mode. + * + * @param string $code code to represent. + * @return bool|string encoded string. + * @protected + */ + protected function encode_code39_ext($code) + { + $encode = array( + chr(0) => '%U', + chr(1) => '$A', + chr(2) => '$B', + chr(3) => '$C', + chr(4) => '$D', + chr(5) => '$E', + chr(6) => '$F', + chr(7) => '$G', + chr(8) => '$H', + chr(9) => '$I', + chr(10) => '$J', + chr(11) => '£K', + chr(12) => '$L', + chr(13) => '$M', + chr(14) => '$N', + chr(15) => '$O', + chr(16) => '$P', + chr(17) => '$Q', + chr(18) => '$R', + chr(19) => '$S', + chr(20) => '$T', + chr(21) => '$U', + chr(22) => '$V', + chr(23) => '$W', + chr(24) => '$X', + chr(25) => '$Y', + chr(26) => '$Z', + chr(27) => '%A', + chr(28) => '%B', + chr(29) => '%C', + chr(30) => '%D', + chr(31) => '%E', + chr(32) => ' ', + chr(33) => '/A', + chr(34) => '/B', + chr(35) => '/C', + chr(36) => '/D', + chr(37) => '/E', + chr(38) => '/F', + chr(39) => '/G', + chr(40) => '/H', + chr(41) => '/I', + chr(42) => '/J', + chr(43) => '/K', + chr(44) => '/L', + chr(45) => '-', + chr(46) => '.', + chr(47) => '/O', + chr(48) => '0', + chr(49) => '1', + chr(50) => '2', + chr(51) => '3', + chr(52) => '4', + chr(53) => '5', + chr(54) => '6', + chr(55) => '7', + chr(56) => '8', + chr(57) => '9', + chr(58) => '/Z', + chr(59) => '%F', + chr(60) => '%G', + chr(61) => '%H', + chr(62) => '%I', + chr(63) => '%J', + chr(64) => '%V', + chr(65) => 'A', + chr(66) => 'B', + chr(67) => 'C', + chr(68) => 'D', + chr(69) => 'E', + chr(70) => 'F', + chr(71) => 'G', + chr(72) => 'H', + chr(73) => 'I', + chr(74) => 'J', + chr(75) => 'K', + chr(76) => 'L', + chr(77) => 'M', + chr(78) => 'N', + chr(79) => 'O', + chr(80) => 'P', + chr(81) => 'Q', + chr(82) => 'R', + chr(83) => 'S', + chr(84) => 'T', + chr(85) => 'U', + chr(86) => 'V', + chr(87) => 'W', + chr(88) => 'X', + chr(89) => 'Y', + chr(90) => 'Z', + chr(91) => '%K', + chr(92) => '%L', + chr(93) => '%M', + chr(94) => '%N', + chr(95) => '%O', + chr(96) => '%W', + chr(97) => '+A', + chr(98) => '+B', + chr(99) => '+C', + chr(100) => '+D', + chr(101) => '+E', + chr(102) => '+F', + chr(103) => '+G', + chr(104) => '+H', + chr(105) => '+I', + chr(106) => '+J', + chr(107) => '+K', + chr(108) => '+L', + chr(109) => '+M', + chr(110) => '+N', + chr(111) => '+O', + chr(112) => '+P', + chr(113) => '+Q', + chr(114) => '+R', + chr(115) => '+S', + chr(116) => '+T', + chr(117) => '+U', + chr(118) => '+V', + chr(119) => '+W', + chr(120) => '+X', + chr(121) => '+Y', + chr(122) => '+Z', + chr(123) => '%P', + chr(124) => '%Q', + chr(125) => '%R', + chr(126) => '%S', + chr(127) => '%T' + ); + $code_ext = ''; + $clen = strlen($code); + for ($i = 0; $i < $clen; ++$i) { + if (ord($code[$i]) > 127) { + throw new InvalidCharacterException('Only supports till char 127'); + } + $code_ext .= $encode[$code[$i]]; + } + + return $code_ext; + } + + + /** + * Calculate CODE 39 checksum (modulo 43). + * + * @param string $code code to represent. + * @return string char checksum. + * @protected + */ + protected function checksum_code39($code) + { + $chars = array( + '0', + '1', + '2', + '3', + '4', + '5', + '6', + '7', + '8', + '9', + 'A', + 'B', + 'C', + 'D', + 'E', + 'F', + 'G', + 'H', + 'I', + 'J', + 'K', + 'L', + 'M', + 'N', + 'O', + 'P', + 'Q', + 'R', + 'S', + 'T', + 'U', + 'V', + 'W', + 'X', + 'Y', + 'Z', + '-', + '.', + ' ', + '$', + '/', + '+', + '%' + ); + $sum = 0; + $codelength = strlen($code); + for ($i = 0; $i < $codelength; ++$i) { + $k = array_keys($chars, $code[$i]); + $sum += $k[0]; + } + $j = ($sum % 43); + + return $chars[$j]; + } +} diff --git a/src/Types/TypeCode39Checksum.php b/src/Types/TypeCode39Checksum.php new file mode 100644 index 0000000..b129f28 --- /dev/null +++ b/src/Types/TypeCode39Checksum.php @@ -0,0 +1,14 @@ + Array(2, 2, 1, 1, 1), diff --git a/src/Types/TypeRms4cc.php b/src/Types/TypeRms4cc.php index c68c93f..b1901cd 100644 --- a/src/Types/TypeRms4cc.php +++ b/src/Types/TypeRms4cc.php @@ -10,7 +10,7 @@ * - in this case the house number must be sufficed with an X and placed at the end of the code. */ -class TypeRms4cc extends Type +class TypeRms4cc implements TypeInterface { protected $kix = false; diff --git a/src/Types/TypeStandard2of5.php b/src/Types/TypeStandard2of5.php new file mode 100644 index 0000000..bbbc58d --- /dev/null +++ b/src/Types/TypeStandard2of5.php @@ -0,0 +1,78 @@ +checksum) { + // add checksum + $code .= $this->checksum_s25($code); + } + if ((strlen($code) % 2) != 0) { + // add leading zero if code-length is odd + $code = '0' . $code; + } + $seq = '11011010'; + $clen = strlen($code); + for ($i = 0; $i < $clen; ++$i) { + $digit = $code[$i]; + if ( ! isset($chr[$digit])) { + throw new InvalidCharacterException('Char ' . $digit . ' is unsupported'); + } + $seq .= $chr[$digit]; + } + $seq .= '1101011'; + $bararray = array('code' => $code, 'maxw' => 0, 'maxh' => 1, 'bcode' => array()); + + return BinarySequenceConverter::convert($seq, $bararray); + } + + /** + * Checksum for standard 2 of 5 barcodes. + * + * @param $code (string) code to process. + * @return int checksum. + * @protected + */ + protected function checksum_s25($code) + { + $len = strlen($code); + $sum = 0; + for ($i = 0; $i < $len; $i += 2) { + $sum += $code[$i]; + } + $sum *= 3; + for ($i = 1; $i < $len; $i += 2) { + $sum += ($code[$i]); + } + $r = $sum % 10; + if ($r > 0) { + $r = (10 - $r); + } + + return $r; + } +} diff --git a/src/Types/TypeStandard2of5Checksum.php b/src/Types/TypeStandard2of5Checksum.php new file mode 100644 index 0000000..25bc4a0 --- /dev/null +++ b/src/Types/TypeStandard2of5Checksum.php @@ -0,0 +1,14 @@ +length; + + //Padding + $code = str_pad($code, $len, '0', STR_PAD_LEFT); + // calculate check digit + if ($len == 2) { + $r = $code % 4; + } elseif ($len == 5) { + $r = (3 * ($code[0] + $code[2] + $code[4])) + (9 * ($code[1] + $code[3])); + $r %= 10; + } else { + throw new InvalidCheckDigitException(); + } + //Convert digits to bars + $codes = array( + 'A' => array( // left odd parity + '0' => '0001101', + '1' => '0011001', + '2' => '0010011', + '3' => '0111101', + '4' => '0100011', + '5' => '0110001', + '6' => '0101111', + '7' => '0111011', + '8' => '0110111', + '9' => '0001011' + ), + 'B' => array( // left even parity + '0' => '0100111', + '1' => '0110011', + '2' => '0011011', + '3' => '0100001', + '4' => '0011101', + '5' => '0111001', + '6' => '0000101', + '7' => '0010001', + '8' => '0001001', + '9' => '0010111' + ) + ); + $parities = array(); + $parities[2] = array( + '0' => array('A', 'A'), + '1' => array('A', 'B'), + '2' => array('B', 'A'), + '3' => array('B', 'B') + ); + $parities[5] = array( + '0' => array('B', 'B', 'A', 'A', 'A'), + '1' => array('B', 'A', 'B', 'A', 'A'), + '2' => array('B', 'A', 'A', 'B', 'A'), + '3' => array('B', 'A', 'A', 'A', 'B'), + '4' => array('A', 'B', 'B', 'A', 'A'), + '5' => array('A', 'A', 'B', 'B', 'A'), + '6' => array('A', 'A', 'A', 'B', 'B'), + '7' => array('A', 'B', 'A', 'B', 'A'), + '8' => array('A', 'B', 'A', 'A', 'B'), + '9' => array('A', 'A', 'B', 'A', 'B') + ); + $p = $parities[$len][$r]; + $seq = '1011'; // left guard bar + $seq .= $codes[$p[0]][$code[0]]; + for ($i = 1; $i < $len; ++$i) { + $seq .= '01'; // separator + $seq .= $codes[$p[$i]][$code[$i]]; + } + $bararray = array('code' => $code, 'maxw' => 0, 'maxh' => 1, 'bcode' => array()); + + return BinarySequenceConverter::convert($seq, $bararray); + } +} diff --git a/src/Types/TypeUpcExtension5.php b/src/Types/TypeUpcExtension5.php new file mode 100644 index 0000000..b1d3424 --- /dev/null +++ b/src/Types/TypeUpcExtension5.php @@ -0,0 +1,14 @@ + Date: Wed, 1 Jan 2020 14:55:04 +0100 Subject: [PATCH 023/155] Moved all barcode types to own class --- src/BarcodeGenerator.php | 465 +------------------------------------ src/Types/TypeCode128.php | 397 +++++++++++++++++++++++++++++++ src/Types/TypeCode128A.php | 19 ++ src/Types/TypeCode128B.php | 19 ++ src/Types/TypeCode128C.php | 19 ++ 5 files changed, 465 insertions(+), 454 deletions(-) create mode 100644 src/Types/TypeCode128.php create mode 100644 src/Types/TypeCode128A.php create mode 100644 src/Types/TypeCode128B.php create mode 100644 src/Types/TypeCode128C.php diff --git a/src/BarcodeGenerator.php b/src/BarcodeGenerator.php index 70fd4bb..06d092b 100644 --- a/src/BarcodeGenerator.php +++ b/src/BarcodeGenerator.php @@ -29,15 +29,13 @@ namespace Picqer\Barcode; -use Picqer\Barcode\Exceptions\InvalidCharacterException; -use Picqer\Barcode\Exceptions\InvalidCheckDigitException; -use Picqer\Barcode\Exceptions\InvalidFormatException; -use Picqer\Barcode\Exceptions\InvalidLengthException; use Picqer\Barcode\Exceptions\UnknownTypeException; -use Picqer\Barcode\Helpers\BinarySequenceConverter; use Picqer\Barcode\Helpers\OldBarcodeArrayConverter; use Picqer\Barcode\Types\TypeCodabar; use Picqer\Barcode\Types\TypeCode11; +use Picqer\Barcode\Types\TypeCode128; +use Picqer\Barcode\Types\TypeCode128A; +use Picqer\Barcode\Types\TypeCode128B; use Picqer\Barcode\Types\TypeCode39; use Picqer\Barcode\Types\TypeCode39Checksum; use Picqer\Barcode\Types\TypeCode39Extended; @@ -136,19 +134,19 @@ protected function getBarcodeData(string $code, string $type): array break; case self::TYPE_CODE_128: - $arrcode = $this->barcode_c128($code, ''); + $barcodeDataBuilder = new TypeCode128(); break; case self::TYPE_CODE_128_A: - $arrcode = $this->barcode_c128($code, 'A'); + $barcodeDataBuilder = new TypeCode128A(); break; case self::TYPE_CODE_128_B: - $arrcode = $this->barcode_c128($code, 'B'); + $barcodeDataBuilder = new TypeCode128B(); break; case self::TYPE_CODE_128_C: - $arrcode = $this->barcode_c128($code, 'C'); + $barcodeDataBuilder = new TypeCode128C(); break; case self::TYPE_EAN_2: @@ -223,453 +221,12 @@ protected function getBarcodeData(string $code, string $type): array throw new UnknownTypeException(); } - if (! isset($arrcode) && isset($barcodeDataBuilder)) { - $arrcode = $barcodeDataBuilder->getBarcodeData($code); - } - - if ( ! isset($arrcode['maxWidth'])) { - return OldBarcodeArrayConverter::convert($arrcode); - } + $barcodeData = $barcodeDataBuilder->getBarcodeData($code); - return $arrcode; - } - - /** - * C128 barcodes. - * Very capable code, excellent density, high reliability; in very wide use world-wide - * - * @param $code (string) code to represent. - * @param $type (string) barcode type: A, B, C or empty for automatic switch (AUTO mode) - * @return array barcode representation. - * @protected - */ - protected function barcode_c128($code, $type = '') - { - $chr = array( - '212222', /* 00 */ - '222122', /* 01 */ - '222221', /* 02 */ - '121223', /* 03 */ - '121322', /* 04 */ - '131222', /* 05 */ - '122213', /* 06 */ - '122312', /* 07 */ - '132212', /* 08 */ - '221213', /* 09 */ - '221312', /* 10 */ - '231212', /* 11 */ - '112232', /* 12 */ - '122132', /* 13 */ - '122231', /* 14 */ - '113222', /* 15 */ - '123122', /* 16 */ - '123221', /* 17 */ - '223211', /* 18 */ - '221132', /* 19 */ - '221231', /* 20 */ - '213212', /* 21 */ - '223112', /* 22 */ - '312131', /* 23 */ - '311222', /* 24 */ - '321122', /* 25 */ - '321221', /* 26 */ - '312212', /* 27 */ - '322112', /* 28 */ - '322211', /* 29 */ - '212123', /* 30 */ - '212321', /* 31 */ - '232121', /* 32 */ - '111323', /* 33 */ - '131123', /* 34 */ - '131321', /* 35 */ - '112313', /* 36 */ - '132113', /* 37 */ - '132311', /* 38 */ - '211313', /* 39 */ - '231113', /* 40 */ - '231311', /* 41 */ - '112133', /* 42 */ - '112331', /* 43 */ - '132131', /* 44 */ - '113123', /* 45 */ - '113321', /* 46 */ - '133121', /* 47 */ - '313121', /* 48 */ - '211331', /* 49 */ - '231131', /* 50 */ - '213113', /* 51 */ - '213311', /* 52 */ - '213131', /* 53 */ - '311123', /* 54 */ - '311321', /* 55 */ - '331121', /* 56 */ - '312113', /* 57 */ - '312311', /* 58 */ - '332111', /* 59 */ - '314111', /* 60 */ - '221411', /* 61 */ - '431111', /* 62 */ - '111224', /* 63 */ - '111422', /* 64 */ - '121124', /* 65 */ - '121421', /* 66 */ - '141122', /* 67 */ - '141221', /* 68 */ - '112214', /* 69 */ - '112412', /* 70 */ - '122114', /* 71 */ - '122411', /* 72 */ - '142112', /* 73 */ - '142211', /* 74 */ - '241211', /* 75 */ - '221114', /* 76 */ - '413111', /* 77 */ - '241112', /* 78 */ - '134111', /* 79 */ - '111242', /* 80 */ - '121142', /* 81 */ - '121241', /* 82 */ - '114212', /* 83 */ - '124112', /* 84 */ - '124211', /* 85 */ - '411212', /* 86 */ - '421112', /* 87 */ - '421211', /* 88 */ - '212141', /* 89 */ - '214121', /* 90 */ - '412121', /* 91 */ - '111143', /* 92 */ - '111341', /* 93 */ - '131141', /* 94 */ - '114113', /* 95 */ - '114311', /* 96 */ - '411113', /* 97 */ - '411311', /* 98 */ - '113141', /* 99 */ - '114131', /* 100 */ - '311141', /* 101 */ - '411131', /* 102 */ - '211412', /* 103 START A */ - '211214', /* 104 START B */ - '211232', /* 105 START C */ - '233111', /* STOP */ - '200000' /* END */ - ); - // ASCII characters for code A (ASCII 00 - 95) - $keys_a = ' !"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_'; - $keys_a .= chr(0) . chr(1) . chr(2) . chr(3) . chr(4) . chr(5) . chr(6) . chr(7) . chr(8) . chr(9); - $keys_a .= chr(10) . chr(11) . chr(12) . chr(13) . chr(14) . chr(15) . chr(16) . chr(17) . chr(18) . chr(19); - $keys_a .= chr(20) . chr(21) . chr(22) . chr(23) . chr(24) . chr(25) . chr(26) . chr(27) . chr(28) . chr(29); - $keys_a .= chr(30) . chr(31); - // ASCII characters for code B (ASCII 32 - 127) - $keys_b = ' !"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~' . chr(127); - // special codes - $fnc_a = array(241 => 102, 242 => 97, 243 => 96, 244 => 101); - $fnc_b = array(241 => 102, 242 => 97, 243 => 96, 244 => 100); - // array of symbols - $code_data = array(); - // length of the code - $len = strlen($code); - switch (strtoupper($type)) { - case 'A': { // MODE A - $startid = 103; - for ($i = 0; $i < $len; ++$i) { - $char = $code[$i]; - $char_id = ord($char); - if (($char_id >= 241) AND ($char_id <= 244)) { - $code_data[] = $fnc_a[$char_id]; - } elseif (($char_id >= 0) AND ($char_id <= 95)) { - $code_data[] = strpos($keys_a, $char); - } else { - throw new InvalidCharacterException('Char ' . $char . ' is unsupported'); - } - } - break; - } - case 'B': { // MODE B - $startid = 104; - for ($i = 0; $i < $len; ++$i) { - $char = $code[$i]; - $char_id = ord($char); - if (($char_id >= 241) AND ($char_id <= 244)) { - $code_data[] = $fnc_b[$char_id]; - } elseif (($char_id >= 32) AND ($char_id <= 127)) { - $code_data[] = strpos($keys_b, $char); - } else { - throw new InvalidCharacterException('Char ' . $char . ' is unsupported'); - } - } - break; - } - case 'C': { // MODE C - $startid = 105; - if (ord($code[0]) == 241) { - $code_data[] = 102; - $code = substr($code, 1); - --$len; - } - if (($len % 2) != 0) { - throw new InvalidLengthException('Length must be even'); - } - for ($i = 0; $i < $len; $i += 2) { - $chrnum = $code[$i] . $code[$i + 1]; - if (preg_match('/([0-9]{2})/', $chrnum) > 0) { - $code_data[] = intval($chrnum); - } else { - throw new InvalidCharacterException(); - } - } - break; - } - default: { // MODE AUTO - // split code into sequences - $sequence = array(); - // get numeric sequences (if any) - $numseq = array(); - preg_match_all('/([0-9]{4,})/', $code, $numseq, PREG_OFFSET_CAPTURE); - if (isset($numseq[1]) AND ! empty($numseq[1])) { - $end_offset = 0; - foreach ($numseq[1] as $val) { - $offset = $val[1]; - - // numeric sequence - $slen = strlen($val[0]); - if (($slen % 2) != 0) { - // the length must be even - ++$offset; - $val[0] = substr($val[0],1); - } - - if ($offset > $end_offset) { - // non numeric sequence - $sequence = array_merge($sequence, - $this->get128ABsequence(substr($code, $end_offset, ($offset - $end_offset)))); - } - // numeric sequence fallback - $slen = strlen($val[0]); - if (($slen % 2) != 0) { - // the length must be even - --$slen; - } - $sequence[] = array('C', substr($code, $offset, $slen), $slen); - $end_offset = $offset + $slen; - } - if ($end_offset < $len) { - $sequence = array_merge($sequence, $this->get128ABsequence(substr($code, $end_offset))); - } - } else { - // text code (non C mode) - $sequence = array_merge($sequence, $this->get128ABsequence($code)); - } - // process the sequence - foreach ($sequence as $key => $seq) { - switch ($seq[0]) { - case 'A': { - if ($key == 0) { - $startid = 103; - } elseif ($sequence[($key - 1)][0] != 'A') { - if (($seq[2] == 1) AND ($key > 0) AND ($sequence[($key - 1)][0] == 'B') AND ( ! isset($sequence[($key - 1)][3]))) { - // single character shift - $code_data[] = 98; - // mark shift - $sequence[$key][3] = true; - } elseif ( ! isset($sequence[($key - 1)][3])) { - $code_data[] = 101; - } - } - for ($i = 0; $i < $seq[2]; ++$i) { - $char = $seq[1][$i]; - $char_id = ord($char); - if (($char_id >= 241) AND ($char_id <= 244)) { - $code_data[] = $fnc_a[$char_id]; - } else { - $code_data[] = strpos($keys_a, $char); - } - } - break; - } - case 'B': { - if ($key == 0) { - $tmpchr = ord($seq[1][0]); - if (($seq[2] == 1) AND ($tmpchr >= 241) AND ($tmpchr <= 244) AND isset($sequence[($key + 1)]) AND ($sequence[($key + 1)][0] != 'B')) { - switch ($sequence[($key + 1)][0]) { - case 'A': { - $startid = 103; - $sequence[$key][0] = 'A'; - $code_data[] = $fnc_a[$tmpchr]; - break; - } - case 'C': { - $startid = 105; - $sequence[$key][0] = 'C'; - $code_data[] = $fnc_a[$tmpchr]; - break; - } - } - break; - } else { - $startid = 104; - } - } elseif ($sequence[($key - 1)][0] != 'B') { - if (($seq[2] == 1) AND ($key > 0) AND ($sequence[($key - 1)][0] == 'A') AND ( ! isset($sequence[($key - 1)][3]))) { - // single character shift - $code_data[] = 98; - // mark shift - $sequence[$key][3] = true; - } elseif ( ! isset($sequence[($key - 1)][3])) { - $code_data[] = 100; - } - } - for ($i = 0; $i < $seq[2]; ++$i) { - $char = $seq[1][$i]; - $char_id = ord($char); - if (($char_id >= 241) AND ($char_id <= 244)) { - $code_data[] = $fnc_b[$char_id]; - } else { - $code_data[] = strpos($keys_b, $char); - } - } - break; - } - case 'C': { - if ($key == 0) { - $startid = 105; - } elseif ($sequence[($key - 1)][0] != 'C') { - $code_data[] = 99; - } - for ($i = 0; $i < $seq[2]; $i += 2) { - $chrnum = $seq[1][$i] . $seq[1][$i + 1]; - $code_data[] = intval($chrnum); - } - break; - } - } - } - } - } - // calculate check character - $sum = $startid; - foreach ($code_data as $key => $val) { - $sum += ($val * ($key + 1)); - } - // add check character - $code_data[] = ($sum % 103); - // add stop sequence - $code_data[] = 106; - $code_data[] = 107; - // add start code at the beginning - array_unshift($code_data, $startid); - // build barcode array - $bararray = array('code' => $code, 'maxw' => 0, 'maxh' => 1, 'bcode' => array()); - foreach ($code_data as $val) { - $seq = $chr[$val]; - for ($j = 0; $j < 6; ++$j) { - if (($j % 2) == 0) { - $t = true; // bar - } else { - $t = false; // space - } - $w = $seq[$j]; - $bararray['bcode'][] = array('t' => $t, 'w' => $w, 'h' => 1, 'p' => 0); - $bararray['maxw'] += $w; - } - } - - return $bararray; - } - - /** - * Split text code in A/B sequence for 128 code - * - * @param $code (string) code to split. - * @return array sequence - * @protected - */ - protected function get128ABsequence($code) - { - $len = strlen($code); - $sequence = array(); - // get A sequences (if any) - $numseq = array(); - preg_match_all('/([\x00-\x1f])/', $code, $numseq, PREG_OFFSET_CAPTURE); - if (isset($numseq[1]) AND ! empty($numseq[1])) { - $end_offset = 0; - foreach ($numseq[1] as $val) { - $offset = $val[1]; - if ($offset > $end_offset) { - // B sequence - $sequence[] = array( - 'B', - substr($code, $end_offset, ($offset - $end_offset)), - ($offset - $end_offset) - ); - } - // A sequence - $slen = strlen($val[0]); - $sequence[] = array('A', substr($code, $offset, $slen), $slen); - $end_offset = $offset + $slen; - } - if ($end_offset < $len) { - $sequence[] = array('B', substr($code, $end_offset), ($len - $end_offset)); - } - } else { - // only B sequence - $sequence[] = array('B', $code, $len); - } - - return $sequence; - } - - /** - * IMB - Intelligent Mail Barcode - Onecode - USPS-B-3200 - * - * @param $code (string) pre-formatted IMB barcode (65 chars "FADT") - * @return array barcode representation. - * @protected - */ - protected function barcode_imb_pre($code) - { - if ( ! preg_match('/^[fadtFADT]{65}$/', $code) == 1) { - throw new InvalidFormatException(); - } - $characters = str_split(strtolower($code), 1); - // build bars - $k = 0; - $bararray = array('code' => $code, 'maxw' => 0, 'maxh' => 3, 'bcode' => array()); - for ($i = 0; $i < 65; ++$i) { - switch ($characters[$i]) { - case 'f': { - // full bar - $p = 0; - $h = 3; - break; - } - case 'a': { - // ascender - $p = 0; - $h = 2; - break; - } - case 'd': { - // descender - $p = 1; - $h = 2; - break; - } - case 't': { - // tracker (short) - $p = 1; - $h = 1; - break; - } - } - $bararray['bcode'][$k++] = array('t' => 1, 'w' => 1, 'h' => $h, 'p' => $p); - $bararray['bcode'][$k++] = array('t' => 0, 'w' => 1, 'h' => 2, 'p' => 0); - $bararray['maxw'] += 2; + if ( ! isset($barcodeData['maxWidth'])) { + return OldBarcodeArrayConverter::convert($barcodeData); } - unset($bararray['bcode'][($k - 1)]); - --$bararray['maxw']; - return $bararray; + return $barcodeData; } } diff --git a/src/Types/TypeCode128.php b/src/Types/TypeCode128.php new file mode 100644 index 0000000..6aadf00 --- /dev/null +++ b/src/Types/TypeCode128.php @@ -0,0 +1,397 @@ +?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_'; + $keys_a .= chr(0) . chr(1) . chr(2) . chr(3) . chr(4) . chr(5) . chr(6) . chr(7) . chr(8) . chr(9); + $keys_a .= chr(10) . chr(11) . chr(12) . chr(13) . chr(14) . chr(15) . chr(16) . chr(17) . chr(18) . chr(19); + $keys_a .= chr(20) . chr(21) . chr(22) . chr(23) . chr(24) . chr(25) . chr(26) . chr(27) . chr(28) . chr(29); + $keys_a .= chr(30) . chr(31); + // ASCII characters for code B (ASCII 32 - 127) + $keys_b = ' !"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~' . chr(127); + // special codes + $fnc_a = array(241 => 102, 242 => 97, 243 => 96, 244 => 101); + $fnc_b = array(241 => 102, 242 => 97, 243 => 96, 244 => 100); + // array of symbols + $code_data = array(); + // length of the code + $len = strlen($code); + switch (strtoupper($this->type)) { + case 'A': { // MODE A + $startid = 103; + for ($i = 0; $i < $len; ++$i) { + $char = $code[$i]; + $char_id = ord($char); + if (($char_id >= 241) AND ($char_id <= 244)) { + $code_data[] = $fnc_a[$char_id]; + } elseif (($char_id >= 0) AND ($char_id <= 95)) { + $code_data[] = strpos($keys_a, $char); + } else { + throw new InvalidCharacterException('Char ' . $char . ' is unsupported'); + } + } + break; + } + case 'B': { // MODE B + $startid = 104; + for ($i = 0; $i < $len; ++$i) { + $char = $code[$i]; + $char_id = ord($char); + if (($char_id >= 241) AND ($char_id <= 244)) { + $code_data[] = $fnc_b[$char_id]; + } elseif (($char_id >= 32) AND ($char_id <= 127)) { + $code_data[] = strpos($keys_b, $char); + } else { + throw new InvalidCharacterException('Char ' . $char . ' is unsupported'); + } + } + break; + } + case 'C': { // MODE C + $startid = 105; + if (ord($code[0]) == 241) { + $code_data[] = 102; + $code = substr($code, 1); + --$len; + } + if (($len % 2) != 0) { + throw new InvalidLengthException('Length must be even'); + } + for ($i = 0; $i < $len; $i += 2) { + $chrnum = $code[$i] . $code[$i + 1]; + if (preg_match('/([0-9]{2})/', $chrnum) > 0) { + $code_data[] = intval($chrnum); + } else { + throw new InvalidCharacterException(); + } + } + break; + } + default: { // MODE AUTO + // split code into sequences + $sequence = array(); + // get numeric sequences (if any) + $numseq = array(); + preg_match_all('/([0-9]{4,})/', $code, $numseq, PREG_OFFSET_CAPTURE); + if (isset($numseq[1]) AND ! empty($numseq[1])) { + $end_offset = 0; + foreach ($numseq[1] as $val) { + $offset = $val[1]; + + // numeric sequence + $slen = strlen($val[0]); + if (($slen % 2) != 0) { + // the length must be even + ++$offset; + $val[0] = substr($val[0],1); + } + + if ($offset > $end_offset) { + // non numeric sequence + $sequence = array_merge($sequence, + $this->get128ABsequence(substr($code, $end_offset, ($offset - $end_offset)))); + } + // numeric sequence fallback + $slen = strlen($val[0]); + if (($slen % 2) != 0) { + // the length must be even + --$slen; + } + $sequence[] = array('C', substr($code, $offset, $slen), $slen); + $end_offset = $offset + $slen; + } + if ($end_offset < $len) { + $sequence = array_merge($sequence, $this->get128ABsequence(substr($code, $end_offset))); + } + } else { + // text code (non C mode) + $sequence = array_merge($sequence, $this->get128ABsequence($code)); + } + // process the sequence + foreach ($sequence as $key => $seq) { + switch ($seq[0]) { + case 'A': { + if ($key == 0) { + $startid = 103; + } elseif ($sequence[($key - 1)][0] != 'A') { + if (($seq[2] == 1) AND ($key > 0) AND ($sequence[($key - 1)][0] == 'B') AND ( ! isset($sequence[($key - 1)][3]))) { + // single character shift + $code_data[] = 98; + // mark shift + $sequence[$key][3] = true; + } elseif ( ! isset($sequence[($key - 1)][3])) { + $code_data[] = 101; + } + } + for ($i = 0; $i < $seq[2]; ++$i) { + $char = $seq[1][$i]; + $char_id = ord($char); + if (($char_id >= 241) AND ($char_id <= 244)) { + $code_data[] = $fnc_a[$char_id]; + } else { + $code_data[] = strpos($keys_a, $char); + } + } + break; + } + case 'B': { + if ($key == 0) { + $tmpchr = ord($seq[1][0]); + if (($seq[2] == 1) AND ($tmpchr >= 241) AND ($tmpchr <= 244) AND isset($sequence[($key + 1)]) AND ($sequence[($key + 1)][0] != 'B')) { + switch ($sequence[($key + 1)][0]) { + case 'A': { + $startid = 103; + $sequence[$key][0] = 'A'; + $code_data[] = $fnc_a[$tmpchr]; + break; + } + case 'C': { + $startid = 105; + $sequence[$key][0] = 'C'; + $code_data[] = $fnc_a[$tmpchr]; + break; + } + } + break; + } else { + $startid = 104; + } + } elseif ($sequence[($key - 1)][0] != 'B') { + if (($seq[2] == 1) AND ($key > 0) AND ($sequence[($key - 1)][0] == 'A') AND ( ! isset($sequence[($key - 1)][3]))) { + // single character shift + $code_data[] = 98; + // mark shift + $sequence[$key][3] = true; + } elseif ( ! isset($sequence[($key - 1)][3])) { + $code_data[] = 100; + } + } + for ($i = 0; $i < $seq[2]; ++$i) { + $char = $seq[1][$i]; + $char_id = ord($char); + if (($char_id >= 241) AND ($char_id <= 244)) { + $code_data[] = $fnc_b[$char_id]; + } else { + $code_data[] = strpos($keys_b, $char); + } + } + break; + } + case 'C': { + if ($key == 0) { + $startid = 105; + } elseif ($sequence[($key - 1)][0] != 'C') { + $code_data[] = 99; + } + for ($i = 0; $i < $seq[2]; $i += 2) { + $chrnum = $seq[1][$i] . $seq[1][$i + 1]; + $code_data[] = intval($chrnum); + } + break; + } + } + } + } + } + // calculate check character + $sum = $startid; + foreach ($code_data as $key => $val) { + $sum += ($val * ($key + 1)); + } + // add check character + $code_data[] = ($sum % 103); + // add stop sequence + $code_data[] = 106; + $code_data[] = 107; + // add start code at the beginning + array_unshift($code_data, $startid); + // build barcode array + $bararray = array('code' => $code, 'maxw' => 0, 'maxh' => 1, 'bcode' => array()); + foreach ($code_data as $val) { + $seq = $chr[$val]; + for ($j = 0; $j < 6; ++$j) { + if (($j % 2) == 0) { + $t = true; // bar + } else { + $t = false; // space + } + $w = $seq[$j]; + $bararray['bcode'][] = array('t' => $t, 'w' => $w, 'h' => 1, 'p' => 0); + $bararray['maxw'] += $w; + } + } + + return $bararray; + } + + + /** + * Split text code in A/B sequence for 128 code + * + * @param $code (string) code to split. + * @return array sequence + * @protected + */ + protected function get128ABsequence($code) + { + $len = strlen($code); + $sequence = array(); + // get A sequences (if any) + $numseq = array(); + preg_match_all('/([\x00-\x1f])/', $code, $numseq, PREG_OFFSET_CAPTURE); + if (isset($numseq[1]) AND ! empty($numseq[1])) { + $end_offset = 0; + foreach ($numseq[1] as $val) { + $offset = $val[1]; + if ($offset > $end_offset) { + // B sequence + $sequence[] = array( + 'B', + substr($code, $end_offset, ($offset - $end_offset)), + ($offset - $end_offset) + ); + } + // A sequence + $slen = strlen($val[0]); + $sequence[] = array('A', substr($code, $offset, $slen), $slen); + $end_offset = $offset + $slen; + } + if ($end_offset < $len) { + $sequence[] = array('B', substr($code, $end_offset), ($len - $end_offset)); + } + } else { + // only B sequence + $sequence[] = array('B', $code, $len); + } + + return $sequence; + } +} diff --git a/src/Types/TypeCode128A.php b/src/Types/TypeCode128A.php new file mode 100644 index 0000000..6eca5ba --- /dev/null +++ b/src/Types/TypeCode128A.php @@ -0,0 +1,19 @@ + Date: Wed, 1 Jan 2020 15:01:47 +0100 Subject: [PATCH 024/155] Simplify factory --- src/BarcodeGenerator.php | 129 +++++++++++++++---------------------- src/Types/TypeCode128A.php | 3 - src/Types/TypeCode128B.php | 3 - src/Types/TypeCode128C.php | 3 - 4 files changed, 52 insertions(+), 86 deletions(-) diff --git a/src/BarcodeGenerator.php b/src/BarcodeGenerator.php index 06d092b..c7aa072 100644 --- a/src/BarcodeGenerator.php +++ b/src/BarcodeGenerator.php @@ -36,6 +36,7 @@ use Picqer\Barcode\Types\TypeCode128; use Picqer\Barcode\Types\TypeCode128A; use Picqer\Barcode\Types\TypeCode128B; +use Picqer\Barcode\Types\TypeCode128C; use Picqer\Barcode\Types\TypeCode39; use Picqer\Barcode\Types\TypeCode39Checksum; use Picqer\Barcode\Types\TypeCode39Extended; @@ -63,15 +64,15 @@ abstract class BarcodeGenerator { - const TYPE_CODE_39 = 'C39'; // CODE 39 - ANSI MH10.8M-1983 - USD-3 - 3 of 9. - const TYPE_CODE_39_CHECKSUM = 'C39+'; // CODE 39 with checksum + const TYPE_CODE_39 = 'C39'; + const TYPE_CODE_39_CHECKSUM = 'C39+'; const TYPE_CODE_39E = 'C39E'; // CODE 39 EXTENDED const TYPE_CODE_39E_CHECKSUM = 'C39E+'; // CODE 39 EXTENDED + CHECKSUM - const TYPE_CODE_93 = 'C93'; // CODE 93 - USS-93 - const TYPE_STANDARD_2_5 = 'S25'; // Standard 2 of 5 - const TYPE_STANDARD_2_5_CHECKSUM = 'S25+'; // Standard 2 of 5 + CHECKSUM - const TYPE_INTERLEAVED_2_5 = 'I25'; // Interleaved 2 of 5 - const TYPE_INTERLEAVED_2_5_CHECKSUM = 'I25+'; // Interleaved 2 of 5 + CHECKSUM + const TYPE_CODE_93 = 'C93'; + const TYPE_STANDARD_2_5 = 'S25'; + const TYPE_STANDARD_2_5_CHECKSUM = 'S25+'; + const TYPE_INTERLEAVED_2_5 = 'I25'; + const TYPE_INTERLEAVED_2_5_CHECKSUM = 'I25+'; const TYPE_CODE_128 = 'C128'; const TYPE_CODE_128_A = 'C128A'; const TYPE_CODE_128_B = 'C128B'; @@ -95,138 +96,112 @@ abstract class BarcodeGenerator const TYPE_PHARMA_CODE_TWO_TRACKS = 'PHARMA2T'; protected function getBarcodeData(string $code, string $type): array + { + $barcodeDataBuilder = $this->createDataBuilderForType($type); + + $barcodeData = $barcodeDataBuilder->getBarcodeData($code); + + if (! isset($barcodeData['maxWidth'])) { + return OldBarcodeArrayConverter::convert($barcodeData); + } + + return $barcodeData; + } + + protected function createDataBuilderForType(string $type) { switch (strtoupper($type)) { case self::TYPE_CODE_39: - $barcodeDataBuilder = new TypeCode39(); - break; + return new TypeCode39(); case self::TYPE_CODE_39_CHECKSUM: - $barcodeDataBuilder = new TypeCode39Checksum(); - break; + return new TypeCode39Checksum(); case self::TYPE_CODE_39E: - $barcodeDataBuilder = new TypeCode39Extended(); - break; + return new TypeCode39Extended(); case self::TYPE_CODE_39E_CHECKSUM: - $barcodeDataBuilder = new TypeCode39ExtendedChecksum(); - break; + return new TypeCode39ExtendedChecksum(); case self::TYPE_CODE_93: - $barcodeDataBuilder = new TypeCode93(); - break; + return new TypeCode93(); case self::TYPE_STANDARD_2_5: - $barcodeDataBuilder = new TypeStandard2of5(); - break; + return new TypeStandard2of5(); case self::TYPE_STANDARD_2_5_CHECKSUM: - $barcodeDataBuilder = new TypeStandard2of5Checksum(); - break; + return new TypeStandard2of5Checksum(); case self::TYPE_INTERLEAVED_2_5: - $barcodeDataBuilder = new TypeInterleaved25(); - break; + return new TypeInterleaved25(); case self::TYPE_INTERLEAVED_2_5_CHECKSUM: - $barcodeDataBuilder = new TypeInterleaved25Checksum(); - break; + return new TypeInterleaved25Checksum(); case self::TYPE_CODE_128: - $barcodeDataBuilder = new TypeCode128(); - break; + return new TypeCode128(); case self::TYPE_CODE_128_A: - $barcodeDataBuilder = new TypeCode128A(); - break; + return new TypeCode128A(); case self::TYPE_CODE_128_B: - $barcodeDataBuilder = new TypeCode128B(); - break; + return new TypeCode128B(); case self::TYPE_CODE_128_C: - $barcodeDataBuilder = new TypeCode128C(); - break; + return new TypeCode128C(); case self::TYPE_EAN_2: - $barcodeDataBuilder = new TypeUpcExtension2(); - break; + return new TypeUpcExtension2(); case self::TYPE_EAN_5: - $barcodeDataBuilder = new TypeUpcExtension5(); - break; + return new TypeUpcExtension5(); case self::TYPE_EAN_8: - $barcodeDataBuilder = new TypeEan8(); - break; + return new TypeEan8(); case self::TYPE_EAN_13: - $barcodeDataBuilder = new TypeEan13(); - break; + return new TypeEan13(); case self::TYPE_UPC_A: - $barcodeDataBuilder = new TypeUpcA(); - break; + return new TypeUpcA(); case self::TYPE_UPC_E: - $barcodeDataBuilder = new TypeUpcE(); - break; + return new TypeUpcE(); case self::TYPE_MSI: - $barcodeDataBuilder = new TypeMsi(); - break; + return new TypeMsi(); case self::TYPE_MSI_CHECKSUM: - $barcodeDataBuilder = new TypeMsiChecksum(); - break; + return new TypeMsiChecksum(); case self::TYPE_POSTNET: - $barcodeDataBuilder = new TypePostnet(); - break; + return new TypePostnet(); case self::TYPE_PLANET: - $barcodeDataBuilder = new TypePlanet(); - break; + return new TypePlanet(); case self::TYPE_RMS4CC: - $barcodeDataBuilder = new TypeRms4cc(); - break; + return new TypeRms4cc(); case self::TYPE_KIX: - $barcodeDataBuilder = new TypeKix(); - break; + return new TypeKix(); case self::TYPE_IMB: - $barcodeDataBuilder = new TypeIntelligentMailBarcode(); - break; + return new TypeIntelligentMailBarcode(); case self::TYPE_CODABAR: - $barcodeDataBuilder = new TypeCodabar(); - break; + return new TypeCodabar(); case self::TYPE_CODE_11: - $barcodeDataBuilder = new TypeCode11(); - break; + return new TypeCode11(); case self::TYPE_PHARMA_CODE: - $barcodeDataBuilder = new TypePharmacode(); - break; + return new TypePharmacode(); case self::TYPE_PHARMA_CODE_TWO_TRACKS: - $barcodeDataBuilder = new TypePharmacodeTwoCode(); - break; - - default: - throw new UnknownTypeException(); - } - - $barcodeData = $barcodeDataBuilder->getBarcodeData($code); - - if ( ! isset($barcodeData['maxWidth'])) { - return OldBarcodeArrayConverter::convert($barcodeData); + return new TypePharmacodeTwoCode(); } - return $barcodeData; + throw new UnknownTypeException(); } } diff --git a/src/Types/TypeCode128A.php b/src/Types/TypeCode128A.php index 6eca5ba..ff2fe0c 100644 --- a/src/Types/TypeCode128A.php +++ b/src/Types/TypeCode128A.php @@ -2,9 +2,6 @@ namespace Picqer\Barcode\Types; -use Picqer\Barcode\Exceptions\InvalidCharacterException; -use Picqer\Barcode\Exceptions\InvalidLengthException; - /* * C128 barcodes. * Very capable code, excellent density, high reliability; in very wide use world-wide diff --git a/src/Types/TypeCode128B.php b/src/Types/TypeCode128B.php index 84dc807..b462863 100644 --- a/src/Types/TypeCode128B.php +++ b/src/Types/TypeCode128B.php @@ -2,9 +2,6 @@ namespace Picqer\Barcode\Types; -use Picqer\Barcode\Exceptions\InvalidCharacterException; -use Picqer\Barcode\Exceptions\InvalidLengthException; - /* * C128 barcodes. * Very capable code, excellent density, high reliability; in very wide use world-wide diff --git a/src/Types/TypeCode128C.php b/src/Types/TypeCode128C.php index f323b94..f4d4460 100644 --- a/src/Types/TypeCode128C.php +++ b/src/Types/TypeCode128C.php @@ -2,9 +2,6 @@ namespace Picqer\Barcode\Types; -use Picqer\Barcode\Exceptions\InvalidCharacterException; -use Picqer\Barcode\Exceptions\InvalidLengthException; - /* * C128 barcodes. * Very capable code, excellent density, high reliability; in very wide use world-wide From 368404aeb54c115913df38b4000bdeaddc06f210 Mon Sep 17 00:00:00 2001 From: Casper Bakker Date: Wed, 1 Jan 2020 15:53:37 +0100 Subject: [PATCH 025/155] EAN generator cleanup --- src/Types/TypeEanUpcBase.php | 173 +++++++++++++++++++---------------- 1 file changed, 95 insertions(+), 78 deletions(-) diff --git a/src/Types/TypeEanUpcBase.php b/src/Types/TypeEanUpcBase.php index 0ef8fb8..f67ef0c 100644 --- a/src/Types/TypeEanUpcBase.php +++ b/src/Types/TypeEanUpcBase.php @@ -25,40 +25,26 @@ public function getBarcodeData(string $code): array { $length = $this->length; - $data_len = $length - 1; - //Padding - $code = str_pad($code, $data_len, '0', STR_PAD_LEFT); - $code_len = strlen($code); - // calculate check digit - $sum_a = 0; - for ($i = 1; $i < $data_len; $i += 2) { - $sum_a += $code[$i]; - } - if ($length > 12) { - $sum_a *= 3; - } - $sum_b = 0; - for ($i = 0; $i < $data_len; $i += 2) { - $sum_b += intval(($code[$i])); - } - if ($length < 13) { - $sum_b *= 3; - } - $r = ($sum_a + $sum_b) % 10; - if ($r > 0) { - $r = (10 - $r); - } - if ($code_len == $data_len) { - // add check digit - $code .= $r; - } elseif ($r !== intval($code[$data_len])) { + $dataLength = $length - 1; + + // Add zero padding in front + $code = str_pad($code, $dataLength, '0', STR_PAD_LEFT); + + $checksumDigit = $this->calculateChecksumDigit($code); + + if (strlen($code) == $dataLength) { + $code .= $checksumDigit; + } elseif ($checksumDigit !== intval($code[$dataLength])) { + // If length of given barcode is same as final length, barcode is including checksum + // Make sure that checksum is the same as we calculated throw new InvalidCheckDigitException(); } + if ($this->upca || $this->upce) { - // UPC-A $code = '0' . $code; ++$length; } + if ($this->upce) { // convert UPC-A to UPC-E $tmp = substr($code, 4, 3); @@ -82,9 +68,10 @@ public function getBarcodeData(string $code): array } } } - //Convert digits to bars - $codes = array( - 'A' => array( // left odd parity + + // Convert digits to bars + $codes = [ + 'A' => [ // left odd parity '0' => '0001101', '1' => '0011001', '2' => '0010011', @@ -95,8 +82,8 @@ public function getBarcodeData(string $code): array '7' => '0111011', '8' => '0110111', '9' => '0001011' - ), - 'B' => array( // left even parity + ], + 'B' => [ // left even parity '0' => '0100111', '1' => '0110011', '2' => '0011011', @@ -107,8 +94,8 @@ public function getBarcodeData(string $code): array '7' => '0010001', '8' => '0001001', '9' => '0010111' - ), - 'C' => array( // right + ], + 'C' => [ // right '0' => '1110010', '1' => '1100110', '2' => '1101100', @@ -119,56 +106,60 @@ public function getBarcodeData(string $code): array '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') - ); - $upce_parities = array(); - $upce_parities[0] = array( - '0' => array('B', 'B', 'B', 'A', 'A', 'A'), - '1' => array('B', 'B', 'A', 'B', 'A', 'A'), - '2' => array('B', 'B', 'A', 'A', 'B', 'A'), - '3' => array('B', 'B', 'A', 'A', 'A', 'B'), - '4' => array('B', 'A', 'B', 'B', 'A', 'A'), - '5' => array('B', 'A', 'A', 'B', 'B', 'A'), - '6' => array('B', 'A', 'A', 'A', 'B', 'B'), - '7' => array('B', 'A', 'B', 'A', 'B', 'A'), - '8' => array('B', 'A', 'B', 'A', 'A', 'B'), - '9' => array('B', 'A', 'A', 'B', 'A', 'B') - ); - $upce_parities[1] = array( - '0' => array('A', 'A', 'A', 'B', 'B', 'B'), - '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') - ); + ] + ]; + + $parities = [ + '0' => ['A', 'A', 'A', 'A', 'A', 'A'], + '1' => ['A', 'A', 'B', 'A', 'B', 'B'], + '2' => ['A', 'A', 'B', 'B', 'A', 'B'], + '3' => ['A', 'A', 'B', 'B', 'B', 'A'], + '4' => ['A', 'B', 'A', 'A', 'B', 'B'], + '5' => ['A', 'B', 'B', 'A', 'A', 'B'], + '6' => ['A', 'B', 'B', 'B', 'A', 'A'], + '7' => ['A', 'B', 'A', 'B', 'A', 'B'], + '8' => ['A', 'B', 'A', 'B', 'B', 'A'], + '9' => ['A', 'B', 'B', 'A', 'B', 'A'], + ]; + + $upce_parities = [ + [ + '0' => ['B', 'B', 'B', 'A', 'A', 'A'], + '1' => ['B', 'B', 'A', 'B', 'A', 'A'], + '2' => ['B', 'B', 'A', 'A', 'B', 'A'], + '3' => ['B', 'B', 'A', 'A', 'A', 'B'], + '4' => ['B', 'A', 'B', 'B', 'A', 'A'], + '5' => ['B', 'A', 'A', 'B', 'B', 'A'], + '6' => ['B', 'A', 'A', 'A', 'B', 'B'], + '7' => ['B', 'A', 'B', 'A', 'B', 'A'], + '8' => ['B', 'A', 'B', 'A', 'A', 'B'], + '9' => ['B', 'A', 'A', 'B', 'A', 'B'], + ], + [ + '0' => ['A', 'A', 'A', 'B', 'B', 'B'], + '1' => ['A', 'A', 'B', 'A', 'B', 'B'], + '2' => ['A', 'A', 'B', 'B', 'A', 'B'], + '3' => ['A', 'A', 'B', 'B', 'B', 'A'], + '4' => ['A', 'B', 'A', 'A', 'B', 'B'], + '5' => ['A', 'B', 'B', 'A', 'A', 'B'], + '6' => ['A', 'B', 'B', 'B', 'A', 'A'], + '7' => ['A', 'B', 'A', 'B', 'A', 'B'], + '8' => ['A', 'B', 'A', 'B', 'B', 'A'], + '9' => ['A', 'B', 'B', 'A', 'B', 'A'], + ], + ]; + $k = 0; $seq = '101'; // left guard bar if ($this->upce) { - $bararray = array('code' => $upce_code, 'maxw' => 0, 'maxh' => 1, 'bcode' => array()); - $p = $upce_parities[$code[1]][$r]; + $bararray = ['code' => $upce_code, 'maxw' => 0, 'maxh' => 1, 'bcode' => []]; + $p = $upce_parities[$code[1]][$checksumDigit]; for ($i = 0; $i < 6; ++$i) { $seq .= $codes[$p[$i]][$upce_code[$i]]; } $seq .= '010101'; // right guard bar } else { - $bararray = array('code' => $code, 'maxw' => 0, 'maxh' => 1, 'bcode' => array()); + $bararray = ['code' => $code, 'maxw' => 0, 'maxh' => 1, 'bcode' => []]; $half_len = intval(ceil($length / 2)); if ($length == 8) { for ($i = 0; $i < $half_len; ++$i) { @@ -189,6 +180,7 @@ public function getBarcodeData(string $code): array } $seq .= '101'; // right guard bar } + $clen = strlen($seq); $w = 0; for ($i = 0; $i < $clen; ++$i) { @@ -199,7 +191,7 @@ public function getBarcodeData(string $code): array } else { $t = false; // space } - $bararray['bcode'][$k] = array('t' => $t, 'w' => $w, 'h' => 1, 'p' => 0); + $bararray['bcode'][$k] = ['t' => $t, 'w' => $w, 'h' => 1, 'p' => 0]; $bararray['maxw'] += $w; ++$k; $w = 0; @@ -208,4 +200,29 @@ public function getBarcodeData(string $code): array return $bararray; } + + protected function calculateChecksumDigit(string $code) + { + // calculate check digit + $sum_a = 0; + for ($i = 1; $i < $this->length-1; $i += 2) { + $sum_a += $code[$i]; + } + if ($this->length > 12) { + $sum_a *= 3; + } + $sum_b = 0; + for ($i = 0; $i < $this->length-1; $i += 2) { + $sum_b += intval(($code[$i])); + } + if ($this->length < 13) { + $sum_b *= 3; + } + $checksumDigit = ($sum_a + $sum_b) % 10; + if ($checksumDigit > 0) { + $checksumDigit = (10 - $checksumDigit); + } + + return $checksumDigit; + } } From ec9e9fbf69e19f4318ae98b50b99ce9ad3ce4312 Mon Sep 17 00:00:00 2001 From: Casper Bakker Date: Wed, 1 Jan 2020 16:04:06 +0100 Subject: [PATCH 026/155] Code cleanup --- src/BarcodeGeneratorJPG.php | 9 +- src/BarcodeGeneratorPNG.php | 9 +- src/Types/TypeCodabar.php | 2 +- src/Types/TypeCode11.php | 2 +- src/Types/TypeCode128.php | 50 ++--- src/Types/TypeCode39.php | 202 ++++++++++---------- src/Types/TypeCode93.php | 227 ++++++++++++----------- src/Types/TypeEanUpcBase.php | 6 +- src/Types/TypeIntelligentMailBarcode.php | 30 +-- src/Types/TypeInterleaved25Checksum.php | 2 +- src/Types/TypeMsiChecksum.php | 2 +- src/Types/TypePharmacodeTwoCode.php | 27 +-- src/Types/TypeRms4cc.php | 19 +- src/Types/TypeStandard2of5.php | 2 +- 14 files changed, 314 insertions(+), 275 deletions(-) diff --git a/src/BarcodeGeneratorJPG.php b/src/BarcodeGeneratorJPG.php index 9772226..a6a9ae7 100644 --- a/src/BarcodeGeneratorJPG.php +++ b/src/BarcodeGeneratorJPG.php @@ -2,6 +2,9 @@ namespace Picqer\Barcode; +use Imagick; +use imagickdraw; +use imagickpixel; use Picqer\Barcode\Exceptions\BarcodeException; class BarcodeGeneratorJPG extends BarcodeGenerator @@ -36,10 +39,10 @@ public function getBarcode($code, $type, $widthFactor = 2, $totalHeight = 30, $c $colorForeground = imagecolorallocate($jpg, $color[0], $color[1], $color[2]); } elseif (extension_loaded('imagick')) { $imagick = true; - $colorForeground = new \imagickpixel('rgb(' . $color[0] . ',' . $color[1] . ',' . $color[2] . ')'); - $jpg = new \Imagick(); + $colorForeground = new imagickpixel('rgb(' . $color[0] . ',' . $color[1] . ',' . $color[2] . ')'); + $jpg = new Imagick(); $jpg->newImage($width, $height, 'white', 'jpg'); - $imageMagickObject = new \imagickdraw(); + $imageMagickObject = new imagickdraw(); $imageMagickObject->setFillColor($colorForeground); } else { throw new BarcodeException('Neither gd-lib or imagick are installed!'); diff --git a/src/BarcodeGeneratorPNG.php b/src/BarcodeGeneratorPNG.php index f788392..337c542 100644 --- a/src/BarcodeGeneratorPNG.php +++ b/src/BarcodeGeneratorPNG.php @@ -2,6 +2,9 @@ namespace Picqer\Barcode; +use Imagick; +use imagickdraw; +use imagickpixel; use Picqer\Barcode\Exceptions\BarcodeException; class BarcodeGeneratorPNG extends BarcodeGenerator @@ -35,10 +38,10 @@ public function getBarcode($code, $type, $widthFactor = 2, $totalHeight = 30, $c $colorForeground = imagecolorallocate($png, $color[0], $color[1], $color[2]); } elseif (extension_loaded('imagick')) { $imagick = true; - $colorForeground = new \imagickpixel('rgb(' . $color[0] . ',' . $color[1] . ',' . $color[2] . ')'); - $png = new \Imagick(); + $colorForeground = new imagickpixel('rgb(' . $color[0] . ',' . $color[1] . ',' . $color[2] . ')'); + $png = new Imagick(); $png->newImage($width, $height, 'none', 'png'); - $imageMagickObject = new \imagickdraw(); + $imageMagickObject = new imagickdraw(); $imageMagickObject->setFillColor($colorForeground); } else { throw new BarcodeException('Neither gd-lib or imagick are installed!'); diff --git a/src/Types/TypeCodabar.php b/src/Types/TypeCodabar.php index 213922b..12f9b68 100644 --- a/src/Types/TypeCodabar.php +++ b/src/Types/TypeCodabar.php @@ -42,7 +42,7 @@ public function getBarcodeData(string $code): array $code = 'A' . strtoupper($code) . 'A'; $len = strlen($code); for ($i = 0; $i < $len; ++$i) { - if ( ! isset($chr[$code[$i]])) { + if (! isset($chr[$code[$i]])) { throw new InvalidCharacterException('Char ' . $code[$i] . ' is unsupported'); } $seq = $chr[$code[$i]]; diff --git a/src/Types/TypeCode11.php b/src/Types/TypeCode11.php index 84a37ff..34a7fda 100644 --- a/src/Types/TypeCode11.php +++ b/src/Types/TypeCode11.php @@ -77,7 +77,7 @@ public function getBarcodeData(string $code): array $code = 'S' . $code . 'S'; $len += 3; for ($i = 0; $i < $len; ++$i) { - if ( ! isset($chr[$code[$i]])) { + if (! isset($chr[$code[$i]])) { throw new InvalidCharacterException('Char ' . $code[$i] . ' is unsupported'); } $seq = $chr[$code[$i]]; diff --git a/src/Types/TypeCode128.php b/src/Types/TypeCode128.php index 6aadf00..9c79185 100644 --- a/src/Types/TypeCode128.php +++ b/src/Types/TypeCode128.php @@ -129,23 +129,29 @@ public function getBarcodeData(string $code): array '233111', /* STOP */ '200000' /* END */ ); + // ASCII characters for code A (ASCII 00 - 95) $keys_a = ' !"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_'; $keys_a .= chr(0) . chr(1) . chr(2) . chr(3) . chr(4) . chr(5) . chr(6) . chr(7) . chr(8) . chr(9); $keys_a .= chr(10) . chr(11) . chr(12) . chr(13) . chr(14) . chr(15) . chr(16) . chr(17) . chr(18) . chr(19); $keys_a .= chr(20) . chr(21) . chr(22) . chr(23) . chr(24) . chr(25) . chr(26) . chr(27) . chr(28) . chr(29); $keys_a .= chr(30) . chr(31); + // ASCII characters for code B (ASCII 32 - 127) $keys_b = ' !"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~' . chr(127); + // special codes $fnc_a = array(241 => 102, 242 => 97, 243 => 96, 244 => 101); $fnc_b = array(241 => 102, 242 => 97, 243 => 96, 244 => 100); + // array of symbols $code_data = array(); + // length of the code $len = strlen($code); + switch (strtoupper($this->type)) { - case 'A': { // MODE A + case 'A': $startid = 103; for ($i = 0; $i < $len; ++$i) { $char = $code[$i]; @@ -159,8 +165,8 @@ public function getBarcodeData(string $code): array } } break; - } - case 'B': { // MODE B + + case 'B': $startid = 104; for ($i = 0; $i < $len; ++$i) { $char = $code[$i]; @@ -174,8 +180,8 @@ public function getBarcodeData(string $code): array } } break; - } - case 'C': { // MODE C + + case 'C': $startid = 105; if (ord($code[0]) == 241) { $code_data[] = 102; @@ -194,8 +200,8 @@ public function getBarcodeData(string $code): array } } break; - } - default: { // MODE AUTO + + default: // split code into sequences $sequence = array(); // get numeric sequences (if any) @@ -211,7 +217,7 @@ public function getBarcodeData(string $code): array if (($slen % 2) != 0) { // the length must be even ++$offset; - $val[0] = substr($val[0],1); + $val[0] = substr($val[0], 1); } if ($offset > $end_offset) { @@ -235,19 +241,20 @@ public function getBarcodeData(string $code): array // text code (non C mode) $sequence = array_merge($sequence, $this->get128ABsequence($code)); } + // process the sequence foreach ($sequence as $key => $seq) { switch ($seq[0]) { - case 'A': { + case 'A': if ($key == 0) { $startid = 103; } elseif ($sequence[($key - 1)][0] != 'A') { - if (($seq[2] == 1) AND ($key > 0) AND ($sequence[($key - 1)][0] == 'B') AND ( ! isset($sequence[($key - 1)][3]))) { + if (($seq[2] == 1) AND ($key > 0) AND ($sequence[($key - 1)][0] == 'B') AND (! isset($sequence[($key - 1)][3]))) { // single character shift $code_data[] = 98; // mark shift $sequence[$key][3] = true; - } elseif ( ! isset($sequence[($key - 1)][3])) { + } elseif (! isset($sequence[($key - 1)][3])) { $code_data[] = 101; } } @@ -261,19 +268,21 @@ public function getBarcodeData(string $code): array } } break; - } - case 'B': { + + case 'B': if ($key == 0) { $tmpchr = ord($seq[1][0]); if (($seq[2] == 1) AND ($tmpchr >= 241) AND ($tmpchr <= 244) AND isset($sequence[($key + 1)]) AND ($sequence[($key + 1)][0] != 'B')) { switch ($sequence[($key + 1)][0]) { - case 'A': { + case 'A': + { $startid = 103; $sequence[$key][0] = 'A'; $code_data[] = $fnc_a[$tmpchr]; break; } - case 'C': { + case 'C': + { $startid = 105; $sequence[$key][0] = 'C'; $code_data[] = $fnc_a[$tmpchr]; @@ -285,12 +294,12 @@ public function getBarcodeData(string $code): array $startid = 104; } } elseif ($sequence[($key - 1)][0] != 'B') { - if (($seq[2] == 1) AND ($key > 0) AND ($sequence[($key - 1)][0] == 'A') AND ( ! isset($sequence[($key - 1)][3]))) { + if (($seq[2] == 1) AND ($key > 0) AND ($sequence[($key - 1)][0] == 'A') AND (! isset($sequence[($key - 1)][3]))) { // single character shift $code_data[] = 98; // mark shift $sequence[$key][3] = true; - } elseif ( ! isset($sequence[($key - 1)][3])) { + } elseif (! isset($sequence[($key - 1)][3])) { $code_data[] = 100; } } @@ -304,8 +313,8 @@ public function getBarcodeData(string $code): array } } break; - } - case 'C': { + + case 'C': if ($key == 0) { $startid = 105; } elseif ($sequence[($key - 1)][0] != 'C') { @@ -316,11 +325,10 @@ public function getBarcodeData(string $code): array $code_data[] = intval($chrnum); } break; - } } } - } } + // calculate check character $sum = $startid; foreach ($code_data as $key => $val) { diff --git a/src/Types/TypeCode39.php b/src/Types/TypeCode39.php index 77ec8e6..692e0a3 100644 --- a/src/Types/TypeCode39.php +++ b/src/Types/TypeCode39.php @@ -82,7 +82,7 @@ public function getBarcodeData(string $code): array $clen = strlen($code); for ($i = 0; $i < $clen; ++$i) { $char = $code[$i]; - if ( ! isset($chr[$char])) { + if (! isset($chr[$char])) { throw new InvalidCharacterException('Char ' . $char . ' is unsupported'); } for ($j = 0; $j < 9; ++$j) { @@ -116,106 +116,106 @@ public function getBarcodeData(string $code): array protected function encode_code39_ext($code) { $encode = array( - chr(0) => '%U', - chr(1) => '$A', - chr(2) => '$B', - chr(3) => '$C', - chr(4) => '$D', - chr(5) => '$E', - chr(6) => '$F', - chr(7) => '$G', - chr(8) => '$H', - chr(9) => '$I', - chr(10) => '$J', - chr(11) => '£K', - chr(12) => '$L', - chr(13) => '$M', - chr(14) => '$N', - chr(15) => '$O', - chr(16) => '$P', - chr(17) => '$Q', - chr(18) => '$R', - chr(19) => '$S', - chr(20) => '$T', - chr(21) => '$U', - chr(22) => '$V', - chr(23) => '$W', - chr(24) => '$X', - chr(25) => '$Y', - chr(26) => '$Z', - chr(27) => '%A', - chr(28) => '%B', - chr(29) => '%C', - chr(30) => '%D', - chr(31) => '%E', - chr(32) => ' ', - chr(33) => '/A', - chr(34) => '/B', - chr(35) => '/C', - chr(36) => '/D', - chr(37) => '/E', - chr(38) => '/F', - chr(39) => '/G', - chr(40) => '/H', - chr(41) => '/I', - chr(42) => '/J', - chr(43) => '/K', - chr(44) => '/L', - chr(45) => '-', - chr(46) => '.', - chr(47) => '/O', - chr(48) => '0', - chr(49) => '1', - chr(50) => '2', - chr(51) => '3', - chr(52) => '4', - chr(53) => '5', - chr(54) => '6', - chr(55) => '7', - chr(56) => '8', - chr(57) => '9', - chr(58) => '/Z', - chr(59) => '%F', - chr(60) => '%G', - chr(61) => '%H', - chr(62) => '%I', - chr(63) => '%J', - chr(64) => '%V', - chr(65) => 'A', - chr(66) => 'B', - chr(67) => 'C', - chr(68) => 'D', - chr(69) => 'E', - chr(70) => 'F', - chr(71) => 'G', - chr(72) => 'H', - chr(73) => 'I', - chr(74) => 'J', - chr(75) => 'K', - chr(76) => 'L', - chr(77) => 'M', - chr(78) => 'N', - chr(79) => 'O', - chr(80) => 'P', - chr(81) => 'Q', - chr(82) => 'R', - chr(83) => 'S', - chr(84) => 'T', - chr(85) => 'U', - chr(86) => 'V', - chr(87) => 'W', - chr(88) => 'X', - chr(89) => 'Y', - chr(90) => 'Z', - chr(91) => '%K', - chr(92) => '%L', - chr(93) => '%M', - chr(94) => '%N', - chr(95) => '%O', - chr(96) => '%W', - chr(97) => '+A', - chr(98) => '+B', - chr(99) => '+C', + chr(0) => '%U', + chr(1) => '$A', + chr(2) => '$B', + chr(3) => '$C', + chr(4) => '$D', + chr(5) => '$E', + chr(6) => '$F', + chr(7) => '$G', + chr(8) => '$H', + chr(9) => '$I', + chr(10) => '$J', + chr(11) => '£K', + chr(12) => '$L', + chr(13) => '$M', + chr(14) => '$N', + chr(15) => '$O', + chr(16) => '$P', + chr(17) => '$Q', + chr(18) => '$R', + chr(19) => '$S', + chr(20) => '$T', + chr(21) => '$U', + chr(22) => '$V', + chr(23) => '$W', + chr(24) => '$X', + chr(25) => '$Y', + chr(26) => '$Z', + chr(27) => '%A', + chr(28) => '%B', + chr(29) => '%C', + chr(30) => '%D', + chr(31) => '%E', + chr(32) => ' ', + chr(33) => '/A', + chr(34) => '/B', + chr(35) => '/C', + chr(36) => '/D', + chr(37) => '/E', + chr(38) => '/F', + chr(39) => '/G', + chr(40) => '/H', + chr(41) => '/I', + chr(42) => '/J', + chr(43) => '/K', + chr(44) => '/L', + chr(45) => '-', + chr(46) => '.', + chr(47) => '/O', + chr(48) => '0', + chr(49) => '1', + chr(50) => '2', + chr(51) => '3', + chr(52) => '4', + chr(53) => '5', + chr(54) => '6', + chr(55) => '7', + chr(56) => '8', + chr(57) => '9', + chr(58) => '/Z', + chr(59) => '%F', + chr(60) => '%G', + chr(61) => '%H', + chr(62) => '%I', + chr(63) => '%J', + chr(64) => '%V', + chr(65) => 'A', + chr(66) => 'B', + chr(67) => 'C', + chr(68) => 'D', + chr(69) => 'E', + chr(70) => 'F', + chr(71) => 'G', + chr(72) => 'H', + chr(73) => 'I', + chr(74) => 'J', + chr(75) => 'K', + chr(76) => 'L', + chr(77) => 'M', + chr(78) => 'N', + chr(79) => 'O', + chr(80) => 'P', + chr(81) => 'Q', + chr(82) => 'R', + chr(83) => 'S', + chr(84) => 'T', + chr(85) => 'U', + chr(86) => 'V', + chr(87) => 'W', + chr(88) => 'X', + chr(89) => 'Y', + chr(90) => 'Z', + chr(91) => '%K', + chr(92) => '%L', + chr(93) => '%M', + chr(94) => '%N', + chr(95) => '%O', + chr(96) => '%W', + chr(97) => '+A', + chr(98) => '+B', + chr(99) => '+C', chr(100) => '+D', chr(101) => '+E', chr(102) => '+F', diff --git a/src/Types/TypeCode93.php b/src/Types/TypeCode93.php index 642a0a6..7d8770b 100644 --- a/src/Types/TypeCode93.php +++ b/src/Types/TypeCode93.php @@ -62,108 +62,110 @@ public function getBarcodeData(string $code): array $chr[130] = '122211'; // (+) $chr[131] = '312111'; // (%) $chr[42] = '111141'; // start-stop + $code = strtoupper($code); - $encode = array( - chr(0) => chr(131) . 'U', - chr(1) => chr(128) . 'A', - chr(2) => chr(128) . 'B', - chr(3) => chr(128) . 'C', - chr(4) => chr(128) . 'D', - chr(5) => chr(128) . 'E', - chr(6) => chr(128) . 'F', - chr(7) => chr(128) . 'G', - chr(8) => chr(128) . 'H', - chr(9) => chr(128) . 'I', - chr(10) => chr(128) . 'J', - chr(11) => '£K', - chr(12) => chr(128) . 'L', - chr(13) => chr(128) . 'M', - chr(14) => chr(128) . 'N', - chr(15) => chr(128) . 'O', - chr(16) => chr(128) . 'P', - chr(17) => chr(128) . 'Q', - chr(18) => chr(128) . 'R', - chr(19) => chr(128) . 'S', - chr(20) => chr(128) . 'T', - chr(21) => chr(128) . 'U', - chr(22) => chr(128) . 'V', - chr(23) => chr(128) . 'W', - chr(24) => chr(128) . 'X', - chr(25) => chr(128) . 'Y', - chr(26) => chr(128) . 'Z', - chr(27) => chr(131) . 'A', - chr(28) => chr(131) . 'B', - chr(29) => chr(131) . 'C', - chr(30) => chr(131) . 'D', - chr(31) => chr(131) . 'E', - chr(32) => ' ', - chr(33) => chr(129) . 'A', - chr(34) => chr(129) . 'B', - chr(35) => chr(129) . 'C', - chr(36) => chr(129) . 'D', - chr(37) => chr(129) . 'E', - chr(38) => chr(129) . 'F', - chr(39) => chr(129) . 'G', - chr(40) => chr(129) . 'H', - chr(41) => chr(129) . 'I', - chr(42) => chr(129) . 'J', - chr(43) => chr(129) . 'K', - chr(44) => chr(129) . 'L', - chr(45) => '-', - chr(46) => '.', - chr(47) => chr(129) . 'O', - chr(48) => '0', - chr(49) => '1', - chr(50) => '2', - chr(51) => '3', - chr(52) => '4', - chr(53) => '5', - chr(54) => '6', - chr(55) => '7', - chr(56) => '8', - chr(57) => '9', - chr(58) => chr(129) . 'Z', - chr(59) => chr(131) . 'F', - chr(60) => chr(131) . 'G', - chr(61) => chr(131) . 'H', - chr(62) => chr(131) . 'I', - chr(63) => chr(131) . 'J', - chr(64) => chr(131) . 'V', - chr(65) => 'A', - chr(66) => 'B', - chr(67) => 'C', - chr(68) => 'D', - chr(69) => 'E', - chr(70) => 'F', - chr(71) => 'G', - chr(72) => 'H', - chr(73) => 'I', - chr(74) => 'J', - chr(75) => 'K', - chr(76) => 'L', - chr(77) => 'M', - chr(78) => 'N', - chr(79) => 'O', - chr(80) => 'P', - chr(81) => 'Q', - chr(82) => 'R', - chr(83) => 'S', - chr(84) => 'T', - chr(85) => 'U', - chr(86) => 'V', - chr(87) => 'W', - chr(88) => 'X', - chr(89) => 'Y', - chr(90) => 'Z', - chr(91) => chr(131) . 'K', - chr(92) => chr(131) . 'L', - chr(93) => chr(131) . 'M', - chr(94) => chr(131) . 'N', - chr(95) => chr(131) . 'O', - chr(96) => chr(131) . 'W', - chr(97) => chr(130) . 'A', - chr(98) => chr(130) . 'B', - chr(99) => chr(130) . 'C', + + $encode = [ + chr(0) => chr(131) . 'U', + chr(1) => chr(128) . 'A', + chr(2) => chr(128) . 'B', + chr(3) => chr(128) . 'C', + chr(4) => chr(128) . 'D', + chr(5) => chr(128) . 'E', + chr(6) => chr(128) . 'F', + chr(7) => chr(128) . 'G', + chr(8) => chr(128) . 'H', + chr(9) => chr(128) . 'I', + chr(10) => chr(128) . 'J', + chr(11) => '£K', + chr(12) => chr(128) . 'L', + chr(13) => chr(128) . 'M', + chr(14) => chr(128) . 'N', + chr(15) => chr(128) . 'O', + chr(16) => chr(128) . 'P', + chr(17) => chr(128) . 'Q', + chr(18) => chr(128) . 'R', + chr(19) => chr(128) . 'S', + chr(20) => chr(128) . 'T', + chr(21) => chr(128) . 'U', + chr(22) => chr(128) . 'V', + chr(23) => chr(128) . 'W', + chr(24) => chr(128) . 'X', + chr(25) => chr(128) . 'Y', + chr(26) => chr(128) . 'Z', + chr(27) => chr(131) . 'A', + chr(28) => chr(131) . 'B', + chr(29) => chr(131) . 'C', + chr(30) => chr(131) . 'D', + chr(31) => chr(131) . 'E', + chr(32) => ' ', + chr(33) => chr(129) . 'A', + chr(34) => chr(129) . 'B', + chr(35) => chr(129) . 'C', + chr(36) => chr(129) . 'D', + chr(37) => chr(129) . 'E', + chr(38) => chr(129) . 'F', + chr(39) => chr(129) . 'G', + chr(40) => chr(129) . 'H', + chr(41) => chr(129) . 'I', + chr(42) => chr(129) . 'J', + chr(43) => chr(129) . 'K', + chr(44) => chr(129) . 'L', + chr(45) => '-', + chr(46) => '.', + chr(47) => chr(129) . 'O', + chr(48) => '0', + chr(49) => '1', + chr(50) => '2', + chr(51) => '3', + chr(52) => '4', + chr(53) => '5', + chr(54) => '6', + chr(55) => '7', + chr(56) => '8', + chr(57) => '9', + chr(58) => chr(129) . 'Z', + chr(59) => chr(131) . 'F', + chr(60) => chr(131) . 'G', + chr(61) => chr(131) . 'H', + chr(62) => chr(131) . 'I', + chr(63) => chr(131) . 'J', + chr(64) => chr(131) . 'V', + chr(65) => 'A', + chr(66) => 'B', + chr(67) => 'C', + chr(68) => 'D', + chr(69) => 'E', + chr(70) => 'F', + chr(71) => 'G', + chr(72) => 'H', + chr(73) => 'I', + chr(74) => 'J', + chr(75) => 'K', + chr(76) => 'L', + chr(77) => 'M', + chr(78) => 'N', + chr(79) => 'O', + chr(80) => 'P', + chr(81) => 'Q', + chr(82) => 'R', + chr(83) => 'S', + chr(84) => 'T', + chr(85) => 'U', + chr(86) => 'V', + chr(87) => 'W', + chr(88) => 'X', + chr(89) => 'Y', + chr(90) => 'Z', + chr(91) => chr(131) . 'K', + chr(92) => chr(131) . 'L', + chr(93) => chr(131) . 'M', + chr(94) => chr(131) . 'N', + chr(95) => chr(131) . 'O', + chr(96) => chr(131) . 'W', + chr(97) => chr(130) . 'A', + chr(98) => chr(130) . 'B', + chr(99) => chr(130) . 'C', chr(100) => chr(130) . 'D', chr(101) => chr(130) . 'E', chr(102) => chr(130) . 'F', @@ -191,8 +193,9 @@ public function getBarcodeData(string $code): array chr(124) => chr(131) . 'Q', chr(125) => chr(131) . 'R', chr(126) => chr(131) . 'S', - chr(127) => chr(131) . 'T' - ); + chr(127) => chr(131) . 'T', + ]; + $code_ext = ''; $clen = strlen($code); for ($i = 0; $i < $clen; ++$i) { @@ -201,16 +204,19 @@ public function getBarcodeData(string $code): array } $code_ext .= $encode[$code[$i]]; } + // checksum $code_ext .= $this->checksum_code93($code_ext); + // add start and stop codes $code = '*' . $code_ext . '*'; + $bararray = array('code' => $code, 'maxw' => 0, 'maxh' => 1, 'bcode' => array()); $k = 0; $clen = strlen($code); for ($i = 0; $i < $clen; ++$i) { $char = ord($code[$i]); - if ( ! isset($chr[$char])) { + if (! isset($chr[$char])) { throw new InvalidCharacterException('Char ' . $char . ' is unsupported'); } for ($j = 0; $j < 6; ++$j) { @@ -225,13 +231,13 @@ public function getBarcodeData(string $code): array ++$k; } } + $bararray['bcode'][$k] = array('t' => true, 'w' => 1, 'h' => 1, 'p' => 0); $bararray['maxw'] += 1; return $bararray; } - /** * Calculate CODE 93 checksum (modulo 47). * @@ -241,7 +247,7 @@ public function getBarcodeData(string $code): array */ protected function checksum_code93($code) { - $chars = array( + $chars = [ '0', '1', '2', @@ -288,11 +294,13 @@ protected function checksum_code93($code) '<', '=', '>', - '?' - ); + '?', + ]; + // translate special characters $code = strtr($code, chr(128) . chr(131) . chr(129) . chr(130), '<=>?'); $len = strlen($code); + // calculate check digit C $p = 1; $check = 0; @@ -307,6 +315,7 @@ protected function checksum_code93($code) $check %= 47; $c = $chars[$check]; $code .= $c; + // calculate check digit K $p = 1; $check = 0; @@ -320,7 +329,9 @@ protected function checksum_code93($code) } $check %= 47; $k = $chars[$check]; + $checksum = $c . $k; + // resto respecial characters $checksum = strtr($checksum, '<=>?', chr(128) . chr(131) . chr(129) . chr(130)); diff --git a/src/Types/TypeEanUpcBase.php b/src/Types/TypeEanUpcBase.php index f67ef0c..fff0cac 100644 --- a/src/Types/TypeEanUpcBase.php +++ b/src/Types/TypeEanUpcBase.php @@ -173,7 +173,7 @@ public function getBarcodeData(string $code): array } $seq .= '01010'; // center guard bar for ($i = $half_len; $i < $length; ++$i) { - if ( ! isset($codes['C'][$code[$i]])) { + if (! isset($codes['C'][$code[$i]])) { throw new InvalidCharacterException('Char ' . $code[$i] . ' not allowed'); } $seq .= $codes['C'][$code[$i]]; @@ -205,14 +205,14 @@ protected function calculateChecksumDigit(string $code) { // calculate check digit $sum_a = 0; - for ($i = 1; $i < $this->length-1; $i += 2) { + for ($i = 1; $i < $this->length - 1; $i += 2) { $sum_a += $code[$i]; } if ($this->length > 12) { $sum_a *= 3; } $sum_b = 0; - for ($i = 0; $i < $this->length-1; $i += 2) { + for ($i = 0; $i < $this->length - 1; $i += 2) { $sum_b += intval(($code[$i])); } if ($this->length < 13) { diff --git a/src/Types/TypeIntelligentMailBarcode.php b/src/Types/TypeIntelligentMailBarcode.php index 2b01d96..23e2fde 100644 --- a/src/Types/TypeIntelligentMailBarcode.php +++ b/src/Types/TypeIntelligentMailBarcode.php @@ -311,45 +311,50 @@ public function getBarcodeData(string $code): array } // Conversion of Routing Code switch (strlen($routing_code)) { - case 0: { + case 0: $binary_code = 0; break; - } - case 5: { + + case 5: $binary_code = bcadd($routing_code, '1'); break; - } - case 9: { + + case 9: $binary_code = bcadd($routing_code, '100001'); break; - } - case 11: { + + case 11: $binary_code = bcadd($routing_code, '1000100001'); break; - } - default: { + + default: throw new BarcodeException('Routing code unknown'); - break; - } } + $binary_code = bcmul($binary_code, 10); $binary_code = bcadd($binary_code, $tracking_number[0]); $binary_code = bcmul($binary_code, 5); $binary_code = bcadd($binary_code, $tracking_number[1]); $binary_code .= substr($tracking_number, 2, 18); + // convert to hexadecimal $binary_code = $this->dec_to_hex($binary_code); + // pad to get 13 bytes $binary_code = str_pad($binary_code, 26, '0', STR_PAD_LEFT); + // convert string to array of bytes $binary_code_arr = chunk_split($binary_code, 2, "\r"); $binary_code_arr = substr($binary_code_arr, 0, -1); $binary_code_arr = explode("\r", $binary_code_arr); + // calculate frame check sequence $fcs = $this->imb_crc11fcs($binary_code_arr); + // exclude first 2 bits from first byte $first_byte = sprintf('%2s', dechex((hexdec($binary_code_arr[0]) << 2) >> 2)); $binary_code_102bit = $first_byte . substr($binary_code, 2); + // convert binary data to codewords $codewords = array(); $data = $this->hex_to_dec($binary_code_102bit); @@ -363,9 +368,11 @@ public function getBarcodeData(string $code): array if (($fcs >> 10) == 1) { $codewords[9] += 659; } + // generate lookup tables $table2of13 = $this->imb_tables(2, 78); $table5of13 = $this->imb_tables(5, 1287); + // convert codewords to characters $characters = array(); $bitmask = 512; @@ -383,6 +390,7 @@ public function getBarcodeData(string $code): array $bitmask /= 2; } $characters = array_reverse($characters); + // build bars $k = 0; $bararray = array('code' => $code, 'maxw' => 0, 'maxh' => 3, 'bcode' => array()); diff --git a/src/Types/TypeInterleaved25Checksum.php b/src/Types/TypeInterleaved25Checksum.php index ad15a12..8cb6059 100644 --- a/src/Types/TypeInterleaved25Checksum.php +++ b/src/Types/TypeInterleaved25Checksum.php @@ -43,7 +43,7 @@ public function getBarcodeData(string $code): array for ($i = 0; $i < $clen; $i = ($i + 2)) { $char_bar = $code[$i]; $char_space = $code[$i + 1]; - if ( ! isset($chr[$char_bar]) || ! isset($chr[$char_space])) { + if (! isset($chr[$char_bar]) || ! isset($chr[$char_space])) { throw new InvalidCharacterException(); } // create a bar-space sequence diff --git a/src/Types/TypeMsiChecksum.php b/src/Types/TypeMsiChecksum.php index d4df45f..51a008f 100644 --- a/src/Types/TypeMsiChecksum.php +++ b/src/Types/TypeMsiChecksum.php @@ -58,7 +58,7 @@ public function getBarcodeData(string $code): array $clen = strlen($code); for ($i = 0; $i < $clen; ++$i) { $digit = $code[$i]; - if ( ! isset($chr[$digit])) { + if (! isset($chr[$digit])) { throw new InvalidCharacterException('Char ' . $digit . ' is unsupported'); } $seq .= $chr[$digit]; diff --git a/src/Types/TypePharmacodeTwoCode.php b/src/Types/TypePharmacodeTwoCode.php index b6c716c..bc20360 100644 --- a/src/Types/TypePharmacodeTwoCode.php +++ b/src/Types/TypePharmacodeTwoCode.php @@ -13,51 +13,54 @@ public function getBarcodeData(string $code): array { $seq = ''; $code = intval($code); + do { switch ($code % 3) { - case 0: { + case 0: $seq .= '3'; $code = ($code - 3) / 3; break; - } - case 1: { + + case 1: $seq .= '1'; $code = ($code - 1) / 3; break; - } - case 2: { + + case 2: $seq .= '2'; $code = ($code - 2) / 3; break; - } } } while ($code != 0); + $seq = strrev($seq); $k = 0; $bararray = array('code' => $code, 'maxw' => 0, 'maxh' => 2, 'bcode' => array()); $len = strlen($seq); + for ($i = 0; $i < $len; ++$i) { switch ($seq[$i]) { - case '1': { + case '1': $p = 1; $h = 1; break; - } - case '2': { + + case '2': $p = 0; $h = 1; break; - } - case '3': { + + case '3': $p = 0; $h = 2; break; - } } + $bararray['bcode'][$k++] = array('t' => 1, 'w' => 1, 'h' => $h, 'p' => $p); $bararray['bcode'][$k++] = array('t' => 0, 'w' => 1, 'h' => 2, 'p' => 0); $bararray['maxw'] += 2; } + unset($bararray['bcode'][($k - 1)]); --$bararray['maxw']; diff --git a/src/Types/TypeRms4cc.php b/src/Types/TypeRms4cc.php index b1901cd..aa0007d 100644 --- a/src/Types/TypeRms4cc.php +++ b/src/Types/TypeRms4cc.php @@ -116,41 +116,44 @@ public function getBarcodeData(string $code): array ++$len; } $k = 0; + if ($notkix) { // start bar $bararray['bcode'][$k++] = array('t' => 1, 'w' => 1, 'h' => 2, 'p' => 0); $bararray['bcode'][$k++] = array('t' => 0, 'w' => 1, 'h' => 2, 'p' => 0); $bararray['maxw'] += 2; } + for ($i = 0; $i < $len; ++$i) { for ($j = 0; $j < 4; ++$j) { switch ($barmode[$code[$i]][$j]) { - case 1: { + case 1: $p = 0; $h = 2; break; - } - case 2: { + + case 2: $p = 0; $h = 3; break; - } - case 3: { + + case 3: $p = 1; $h = 1; break; - } - case 4: { + + case 4: $p = 1; $h = 2; break; - } } + $bararray['bcode'][$k++] = array('t' => 1, 'w' => 1, 'h' => $h, 'p' => $p); $bararray['bcode'][$k++] = array('t' => 0, 'w' => 1, 'h' => 2, 'p' => 0); $bararray['maxw'] += 2; } } + if ($notkix) { // stop bar $bararray['bcode'][$k++] = array('t' => 1, 'w' => 1, 'h' => 3, 'p' => 0); diff --git a/src/Types/TypeStandard2of5.php b/src/Types/TypeStandard2of5.php index bbbc58d..c77c72e 100644 --- a/src/Types/TypeStandard2of5.php +++ b/src/Types/TypeStandard2of5.php @@ -39,7 +39,7 @@ public function getBarcodeData(string $code): array $clen = strlen($code); for ($i = 0; $i < $clen; ++$i) { $digit = $code[$i]; - if ( ! isset($chr[$digit])) { + if (! isset($chr[$digit])) { throw new InvalidCharacterException('Char ' . $digit . ' is unsupported'); } $seq .= $chr[$digit]; From 5b65b1937ac47a90871589d2ec046d4b606d2599 Mon Sep 17 00:00:00 2001 From: Casper Bakker Date: Thu, 2 Jan 2020 20:18:13 +0100 Subject: [PATCH 027/155] Improve variable naming --- src/BarcodeGeneratorHTML.php | 15 +++++++-------- src/BarcodeGeneratorJPG.php | 13 ++++++------- src/BarcodeGeneratorPNG.php | 15 +++++++-------- src/BarcodeGeneratorSVG.php | 27 +++++++++++++-------------- 4 files changed, 33 insertions(+), 37 deletions(-) diff --git a/src/BarcodeGeneratorHTML.php b/src/BarcodeGeneratorHTML.php index eeb9507..a21c80f 100644 --- a/src/BarcodeGeneratorHTML.php +++ b/src/BarcodeGeneratorHTML.php @@ -4,23 +4,22 @@ class BarcodeGeneratorHTML extends BarcodeGenerator { - /** * Return an HTML representation of barcode. * - * @param string $code code to print + * @param string $barcode code to print * @param string $type type of barcode * @param int $widthFactor Width of a single bar element in pixels. * @param int $totalHeight Height of a single bar element in pixels. - * @param int|string $color Foreground color for bar elements (background is transparent). + * @param int|string $foregroundColor Foreground color for bar elements as '#333' or 'orange' for example (background is transparent). * @return string HTML code. * @public */ - public function getBarcode($code, $type, $widthFactor = 2, $totalHeight = 30, $color = 'black') + public function getBarcode($barcode, $type, int $widthFactor = 2, int $totalHeight = 30, string $foregroundColor = 'black') { - $barcodeData = $this->getBarcodeData($code, $type); + $barcodeData = $this->getBarcodeData($barcode, $type); - $html = '
    ' . "\n"; + $html = '
    ' . PHP_EOL; $positionHorizontal = 0; foreach ($barcodeData['bars'] as $bar) { @@ -30,13 +29,13 @@ public function getBarcode($code, $type, $widthFactor = 2, $totalHeight = 30, $c if ($bar['drawBar']) { $positionVertical = round(($bar['positionVertical'] * $totalHeight / $barcodeData['maxHeight']), 3); // draw a vertical bar - $html .= '
     
    ' . "\n"; + $html .= '
     
    ' . PHP_EOL; } $positionHorizontal += $barWidth; } - $html .= '
    ' . "\n"; + $html .= '
    ' . PHP_EOL; return $html; } diff --git a/src/BarcodeGeneratorJPG.php b/src/BarcodeGeneratorJPG.php index a6a9ae7..cec29ad 100644 --- a/src/BarcodeGeneratorJPG.php +++ b/src/BarcodeGeneratorJPG.php @@ -9,22 +9,21 @@ class BarcodeGeneratorJPG extends BarcodeGenerator { - /** * Return a JPG image representation of barcode (requires GD or Imagick library). * - * @param string $code code to print + * @param string $barcode code to print * @param string $type type of barcode: * @param int $widthFactor Width of a single bar element in pixels. * @param int $totalHeight Height of a single bar element in pixels. - * @param array $color RGB (0-255) foreground color for bar elements (background is transparent). + * @param array $foregroundColor RGB (0-255) foreground color for bar elements (background is transparent). * @return string image data or false in case of error. * @public * @throws BarcodeException */ - public function getBarcode($code, $type, $widthFactor = 2, $totalHeight = 30, $color = array(0, 0, 0)) + public function getBarcode($barcode, $type, int $widthFactor = 2, int $totalHeight = 30, array $foregroundColor = [0, 0, 0]) { - $barcodeData = $this->getBarcodeData($code, $type); + $barcodeData = $this->getBarcodeData($barcode, $type); // calculate image size $width = ($barcodeData['maxWidth'] * $widthFactor); @@ -36,10 +35,10 @@ public function getBarcode($code, $type, $widthFactor = 2, $totalHeight = 30, $c $jpg = imagecreate($width, $height); $colorBackground = imagecolorallocate($jpg, 255, 255, 255); imagecolortransparent($jpg, $colorBackground); - $colorForeground = imagecolorallocate($jpg, $color[0], $color[1], $color[2]); + $colorForeground = imagecolorallocate($jpg, $foregroundColor[0], $foregroundColor[1], $foregroundColor[2]); } elseif (extension_loaded('imagick')) { $imagick = true; - $colorForeground = new imagickpixel('rgb(' . $color[0] . ',' . $color[1] . ',' . $color[2] . ')'); + $colorForeground = new imagickpixel('rgb(' . $foregroundColor[0] . ',' . $foregroundColor[1] . ',' . $foregroundColor[2] . ')'); $jpg = new Imagick(); $jpg->newImage($width, $height, 'white', 'jpg'); $imageMagickObject = new imagickdraw(); diff --git a/src/BarcodeGeneratorPNG.php b/src/BarcodeGeneratorPNG.php index 337c542..5659e41 100644 --- a/src/BarcodeGeneratorPNG.php +++ b/src/BarcodeGeneratorPNG.php @@ -9,21 +9,20 @@ class BarcodeGeneratorPNG extends BarcodeGenerator { - /** * Return a PNG image representation of barcode (requires GD or Imagick library). * - * @param string $code code to print + * @param string $barcode code to print * @param string $type type of barcode: * @param int $widthFactor Width of a single bar element in pixels. * @param int $totalHeight Height of a single bar element in pixels. - * @param array $color RGB (0-255) foreground color for bar elements (background is transparent). + * @param array $foregroundColor RGB (0-255) foreground color for bar elements (background is transparent). * @return string image data or false in case of error. * @public */ - public function getBarcode($code, $type, $widthFactor = 2, $totalHeight = 30, $color = array(0, 0, 0)) + public function getBarcode($barcode, $type, int $widthFactor = 2, int $totalHeight = 30, array$foregroundColor = [0, 0, 0]) { - $barcodeData = $this->getBarcodeData($code, $type); + $barcodeData = $this->getBarcodeData($barcode, $type); // calculate image size $width = ($barcodeData['maxWidth'] * $widthFactor); @@ -35,10 +34,10 @@ public function getBarcode($code, $type, $widthFactor = 2, $totalHeight = 30, $c $png = imagecreate($width, $height); $colorBackground = imagecolorallocate($png, 255, 255, 255); imagecolortransparent($png, $colorBackground); - $colorForeground = imagecolorallocate($png, $color[0], $color[1], $color[2]); + $colorForeground = imagecolorallocate($png, $foregroundColor[0], $foregroundColor[1], $foregroundColor[2]); } elseif (extension_loaded('imagick')) { $imagick = true; - $colorForeground = new imagickpixel('rgb(' . $color[0] . ',' . $color[1] . ',' . $color[2] . ')'); + $colorForeground = new imagickpixel('rgb(' . $foregroundColor[0] . ',' . $foregroundColor[1] . ',' . $foregroundColor[2] . ')'); $png = new Imagick(); $png->newImage($width, $height, 'none', 'png'); $imageMagickObject = new imagickdraw(); @@ -76,4 +75,4 @@ public function getBarcode($code, $type, $widthFactor = 2, $totalHeight = 30, $c return $image; } -} \ No newline at end of file +} diff --git a/src/BarcodeGeneratorSVG.php b/src/BarcodeGeneratorSVG.php index 01af454..a027666 100644 --- a/src/BarcodeGeneratorSVG.php +++ b/src/BarcodeGeneratorSVG.php @@ -4,32 +4,31 @@ class BarcodeGeneratorSVG extends BarcodeGenerator { - /** * Return a SVG string representation of barcode. * - * @param $code (string) code to print + * @param $barcode (string) code to print * @param $type (const) type of barcode * @param $widthFactor (int) Minimum width of a single bar in user units. * @param $totalHeight (int) Height of barcode in user units. - * @param $color (string) Foreground color (in SVG format) for bar elements (background is transparent). + * @param $foregroundColor (string) Foreground color (in SVG format) for bar elements (background is transparent). * @return string SVG code. * @public */ - public function getBarcode($code, $type, $widthFactor = 2, $totalHeight = 30, $color = 'black') + public function getBarcode($barcode, $type, int $widthFactor = 2, int $totalHeight = 30, string $foregroundColor = 'black') { - $barcodeData = $this->getBarcodeData($code, $type); + $barcodeData = $this->getBarcodeData($barcode, $type); // replace table for special characters - $repstr = array("\0" => '', '&' => '&', '<' => '<', '>' => '>'); + $repstr = ["\0" => '', '&' => '&', '<' => '<', '>' => '>']; $width = round(($barcodeData['maxWidth'] * $widthFactor), 3); - $svg = '' . "\n"; - $svg .= '' . "\n"; - $svg .= '' . "\n"; - $svg .= "\t" . '' . strtr($barcodeData['code'], $repstr) . '' . "\n"; - $svg .= "\t" . '' . "\n"; + $svg = '' . PHP_EOL; + $svg .= '' . PHP_EOL; + $svg .= '' . PHP_EOL; + $svg .= "\t" . '' . strtr($barcodeData['code'], $repstr) . '' . PHP_EOL; + $svg .= "\t" . '' . PHP_EOL; // print bars $positionHorizontal = 0; foreach ($barcodeData['bars'] as $bar) { @@ -38,12 +37,12 @@ public function getBarcode($code, $type, $widthFactor = 2, $totalHeight = 30, $c if ($bar['drawBar']) { $positionVertical = round(($bar['positionVertical'] * $totalHeight / $barcodeData['maxHeight']), 3); // draw a vertical bar - $svg .= "\t\t" . '' . "\n"; + $svg .= "\t\t" . '' . PHP_EOL; } $positionHorizontal += $barWidth; } - $svg .= "\t" . '' . "\n"; - $svg .= '' . "\n"; + $svg .= "\t" . PHP_EOL; + $svg .= '' . PHP_EOL; return $svg; } From ff3a19a74017dcbba19ca87e5bf95f602e8165b6 Mon Sep 17 00:00:00 2001 From: Casper Bakker Date: Thu, 2 Jan 2020 21:05:53 +0100 Subject: [PATCH 028/155] Test different jpg/png image sizes --- tests/BarcodeTest.php | 81 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) diff --git a/tests/BarcodeTest.php b/tests/BarcodeTest.php index 3198e29..885fce7 100644 --- a/tests/BarcodeTest.php +++ b/tests/BarcodeTest.php @@ -33,7 +33,88 @@ public function test_jpg_barcode_generator_can_generate_code_128_barcode() $generator = new Picqer\Barcode\BarcodeGeneratorJPG(); $result = $generator->getBarcode('081231723897', $generator::TYPE_CODE_128); + $imageInfo = getimagesizefromstring($result); + + $this->assertGreaterThan(1000, strlen($result)); + $this->assertEquals(202, $imageInfo[0]); // Image width + $this->assertEquals(30, $imageInfo[1]); // Image height + $this->assertEquals('image/jpeg', $imageInfo['mime']); + } + + public function test_jpg_barcode_generator_can_generate_code_39_barcode() + { + $generator = new Picqer\Barcode\BarcodeGeneratorJPG(); + $result = $generator->getBarcode('081231723897', $generator::TYPE_CODE_39, 1); + + $imageInfo = getimagesizefromstring($result); + + $this->assertGreaterThan(1000, strlen($result)); + $this->assertEquals(224, $imageInfo[0]); // Image width + $this->assertEquals(30, $imageInfo[1]); // Image height + $this->assertEquals('image/jpeg', $imageInfo['mime']); + } + + public function test_jpg_barcode_generator_can_use_different_height() + { + $generator = new Picqer\Barcode\BarcodeGeneratorJPG(); + $result = $generator->getBarcode('081231723897', $generator::TYPE_CODE_128, 2, 45); + + $imageInfo = getimagesizefromstring($result); + + $this->assertGreaterThan(1000, strlen($result)); + $this->assertEquals(202, $imageInfo[0]); // Image width + $this->assertEquals(45, $imageInfo[1]); // Image height + $this->assertEquals('image/jpeg', $imageInfo['mime']); + } + + public function test_jpg_barcode_generator_can_use_different_width_factor() + { + $generator = new Picqer\Barcode\BarcodeGeneratorJPG(); + $result = $generator->getBarcode('081231723897', $generator::TYPE_CODE_128, 5); + + $imageInfo = getimagesizefromstring($result); + $this->assertGreaterThan(1000, strlen($result)); + $this->assertEquals(505, $imageInfo[0]); // Image width + $this->assertEquals('image/jpeg', $imageInfo['mime']); + } + + public function test_png_barcode_generator_can_generate_code_39_barcode() + { + $generator = new Picqer\Barcode\BarcodeGeneratorPNG(); + $result = $generator->getBarcode('081231723897', $generator::TYPE_CODE_39, 1); + + $imageInfo = getimagesizefromstring($result); + + $this->assertGreaterThan(100, strlen($result)); + $this->assertEquals(224, $imageInfo[0]); // Image width + $this->assertEquals(30, $imageInfo[1]); // Image height + $this->assertEquals('image/png', $imageInfo['mime']); + } + + public function test_png_barcode_generator_can_use_different_height() + { + $generator = new Picqer\Barcode\BarcodeGeneratorPNG(); + $result = $generator->getBarcode('081231723897', $generator::TYPE_CODE_128, 2, 45); + + $imageInfo = getimagesizefromstring($result); + + $this->assertGreaterThan(100, strlen($result)); + $this->assertEquals(202, $imageInfo[0]); // Image width + $this->assertEquals(45, $imageInfo[1]); // Image height + $this->assertEquals('image/png', $imageInfo['mime']); + } + + public function test_png_barcode_generator_can_use_different_width_factor() + { + $generator = new Picqer\Barcode\BarcodeGeneratorPNG(); + $result = $generator->getBarcode('081231723897', $generator::TYPE_CODE_128, 5); + + $imageInfo = getimagesizefromstring($result); + + $this->assertGreaterThan(100, strlen($result)); + $this->assertEquals(505, $imageInfo[0]); // Image width + $this->assertEquals('image/png', $imageInfo['mime']); } public function test_ean13_generator_throws_exception_if_invalid_chars_are_used() From 253d74a52cd563ba5b91801594a6c1be569bcaa1 Mon Sep 17 00:00:00 2001 From: Casper Bakker Date: Thu, 2 Jan 2020 21:30:48 +0100 Subject: [PATCH 029/155] Merge PNG and JPG generators --- src/BarcodeGeneratorJPG.php | 77 ++++-------------------- src/BarcodeGeneratorPNG.php | 115 ++++++++++++++++++++++++------------ 2 files changed, 87 insertions(+), 105 deletions(-) diff --git a/src/BarcodeGeneratorJPG.php b/src/BarcodeGeneratorJPG.php index cec29ad..bdd88cc 100644 --- a/src/BarcodeGeneratorJPG.php +++ b/src/BarcodeGeneratorJPG.php @@ -3,77 +3,20 @@ namespace Picqer\Barcode; use Imagick; -use imagickdraw; -use imagickpixel; -use Picqer\Barcode\Exceptions\BarcodeException; -class BarcodeGeneratorJPG extends BarcodeGenerator +class BarcodeGeneratorJPG extends BarcodeGeneratorPNG { - /** - * Return a JPG image representation of barcode (requires GD or Imagick library). - * - * @param string $barcode code to print - * @param string $type type of barcode: - * @param int $widthFactor Width of a single bar element in pixels. - * @param int $totalHeight Height of a single bar element in pixels. - * @param array $foregroundColor RGB (0-255) foreground color for bar elements (background is transparent). - * @return string image data or false in case of error. - * @public - * @throws BarcodeException - */ - public function getBarcode($barcode, $type, int $widthFactor = 2, int $totalHeight = 30, array $foregroundColor = [0, 0, 0]) + protected function createImagickImageObject(int $width, int $height): Imagick { - $barcodeData = $this->getBarcodeData($barcode, $type); - - // calculate image size - $width = ($barcodeData['maxWidth'] * $widthFactor); - $height = $totalHeight; - - if (function_exists('imagecreate')) { - // GD library - $imagick = false; - $jpg = imagecreate($width, $height); - $colorBackground = imagecolorallocate($jpg, 255, 255, 255); - imagecolortransparent($jpg, $colorBackground); - $colorForeground = imagecolorallocate($jpg, $foregroundColor[0], $foregroundColor[1], $foregroundColor[2]); - } elseif (extension_loaded('imagick')) { - $imagick = true; - $colorForeground = new imagickpixel('rgb(' . $foregroundColor[0] . ',' . $foregroundColor[1] . ',' . $foregroundColor[2] . ')'); - $jpg = new Imagick(); - $jpg->newImage($width, $height, 'white', 'jpg'); - $imageMagickObject = new imagickdraw(); - $imageMagickObject->setFillColor($colorForeground); - } else { - throw new BarcodeException('Neither gd-lib or imagick are installed!'); - } - - // print bars - $positionHorizontal = 0; - foreach ($barcodeData['bars'] as $bar) { - $bw = round(($bar['width'] * $widthFactor), 3); - $bh = round(($bar['height'] * $totalHeight / $barcodeData['maxHeight']), 3); - if ($bar['drawBar']) { - $y = round(($bar['positionVertical'] * $totalHeight / $barcodeData['maxHeight']), 3); - // draw a vertical bar - if ($imagick && isset($imageMagickObject)) { - $imageMagickObject->rectangle($positionHorizontal, $y, ($positionHorizontal + $bw), ($y + $bh)); - } else { - imagefilledrectangle($jpg, $positionHorizontal, $y, ($positionHorizontal + $bw) - 1, ($y + $bh), - $colorForeground); - } - } - $positionHorizontal += $bw; - } - ob_start(); - if ($imagick && isset($imageMagickObject)) { - $jpg->drawImage($imageMagickObject); - echo $jpg; - } else { - imagejpeg($jpg); - imagedestroy($jpg); - } - $image = ob_get_clean(); + $image = new Imagick(); + $image->newImage($width, $height, 'none', 'JPG'); return $image; } + + protected function generateGdImage($image) + { + imagejpeg($image); + imagedestroy($image); + } } diff --git a/src/BarcodeGeneratorPNG.php b/src/BarcodeGeneratorPNG.php index 5659e41..48d5e1f 100644 --- a/src/BarcodeGeneratorPNG.php +++ b/src/BarcodeGeneratorPNG.php @@ -9,70 +9,109 @@ class BarcodeGeneratorPNG extends BarcodeGenerator { + protected $useImagick = true; + + public function __construct() + { + // Auto switch between GD and Imagick based on what is installed + if (function_exists('imagecreate')) { + $this->useImagick = false; + } elseif (extension_loaded('imagick')) { + $this->useImagick = true; + } else { + throw new BarcodeException('Neither gd-lib or imagick are installed!'); + } + } + + /** + * Force the use of Imagick image extension + */ + public function useImagick() + { + $this->useImagick = true; + } + + /** + * Force the use of the GD image library + */ + public function useGd() + { + $this->useImagick = false; + } + /** * Return a PNG image representation of barcode (requires GD or Imagick library). * * @param string $barcode code to print * @param string $type type of barcode: * @param int $widthFactor Width of a single bar element in pixels. - * @param int $totalHeight Height of a single bar element in pixels. + * @param int $height Height of a single bar element in pixels. * @param array $foregroundColor RGB (0-255) foreground color for bar elements (background is transparent). * @return string image data or false in case of error. - * @public */ - public function getBarcode($barcode, $type, int $widthFactor = 2, int $totalHeight = 30, array$foregroundColor = [0, 0, 0]) + public function getBarcode($barcode, $type, int $widthFactor = 2, int $height = 30, array $foregroundColor = [0, 0, 0]) { $barcodeData = $this->getBarcodeData($barcode, $type); + $width = round($barcodeData['maxWidth'] * $widthFactor); - // calculate image size - $width = ($barcodeData['maxWidth'] * $widthFactor); - $height = $totalHeight; - - if (function_exists('imagecreate')) { - // GD library - $imagick = false; - $png = imagecreate($width, $height); - $colorBackground = imagecolorallocate($png, 255, 255, 255); - imagecolortransparent($png, $colorBackground); - $colorForeground = imagecolorallocate($png, $foregroundColor[0], $foregroundColor[1], $foregroundColor[2]); - } elseif (extension_loaded('imagick')) { - $imagick = true; - $colorForeground = new imagickpixel('rgb(' . $foregroundColor[0] . ',' . $foregroundColor[1] . ',' . $foregroundColor[2] . ')'); - $png = new Imagick(); - $png->newImage($width, $height, 'none', 'png'); - $imageMagickObject = new imagickdraw(); - $imageMagickObject->setFillColor($colorForeground); + if ($this->useImagick) { + $imagickBarsShape = new imagickdraw(); + $imagickBarsShape->setFillColor(new imagickpixel('rgb(' . implode(',', $foregroundColor) .')')); } else { - throw new BarcodeException('Neither gd-lib or imagick are installed!'); + $image = $this->createGdImageObject($width, $height); + $gdForegroundColor = imagecolorallocate($image, $foregroundColor[0], $foregroundColor[1], $foregroundColor[2]); } // print bars $positionHorizontal = 0; foreach ($barcodeData['bars'] as $bar) { - $bw = round(($bar['width'] * $widthFactor), 3); - $bh = round(($bar['height'] * $totalHeight / $barcodeData['maxHeight']), 3); + $barWidth = round(($bar['width'] * $widthFactor), 3); + if ($bar['drawBar']) { - $y = round(($bar['positionVertical'] * $totalHeight / $barcodeData['maxHeight']), 3); + $y = round(($bar['positionVertical'] * $height / $barcodeData['maxHeight']), 3); + $barHeight = round(($bar['height'] * $height / $barcodeData['maxHeight']), 3); + // draw a vertical bar - if ($imagick && isset($imageMagickObject)) { - $imageMagickObject->rectangle($positionHorizontal, $y, ($positionHorizontal + $bw), ($y + $bh)); + if ($this->useImagick && isset($imagickBarsShape)) { + $imagickBarsShape->rectangle($positionHorizontal, $y, ($positionHorizontal + $barWidth), ($y + $barHeight)); } else { - imagefilledrectangle($png, $positionHorizontal, $y, ($positionHorizontal + $bw) - 1, ($y + $bh), - $colorForeground); + imagefilledrectangle($image, $positionHorizontal, $y, ($positionHorizontal + $barWidth) - 1, ($y + $barHeight), $gdForegroundColor); } } - $positionHorizontal += $bw; + $positionHorizontal += $barWidth; } - ob_start(); - if ($imagick && isset($imageMagickObject)) { - $png->drawImage($imageMagickObject); - echo $png; - } else { - imagepng($png); - imagedestroy($png); + + if ($this->useImagick && isset($imagickBarsShape)) { + $image = $this->createImagickImageObject($width, $height); + $image->drawImage($imagickBarsShape); + return $image->getImageBlob(); } - $image = ob_get_clean(); + + ob_start(); + $this->generateGdImage($image); + return ob_get_clean(); + } + + protected function createGdImageObject(int $width, int $height) + { + $image = imagecreate($width, $height); + $colorBackground = imagecolorallocate($image, 255, 255, 255); + imagecolortransparent($image, $colorBackground); + + return $image; + } + + protected function createImagickImageObject(int $width, int $height): Imagick + { + $image = new Imagick(); + $image->newImage($width, $height, 'none', 'PNG'); return $image; } + + protected function generateGdImage($image) + { + imagepng($image); + imagedestroy($image); + } } From b05b917703b46e75a9ad13719a9bd6a04762be80 Mon Sep 17 00:00:00 2001 From: Casper Bakker Date: Thu, 2 Jan 2020 21:44:28 +0100 Subject: [PATCH 030/155] Add examples file to readme --- Readme.md | 4 +- examples.md | 129 ++++++++++++++++++++++++++++++++++++++++++ generate-examples.php | 22 +++++++ 3 files changed, 154 insertions(+), 1 deletion(-) create mode 100644 examples.md create mode 100644 generate-examples.php diff --git a/Readme.md b/Readme.md index efb1b0e..d3399a0 100644 --- a/Readme.md +++ b/Readme.md @@ -5,7 +5,7 @@ This is an easy to use, non-bloated, framework independent, barcode generator in It creates SVG, PNG, JPG and HTML images, from the most used 1D barcode standards. -*The codebase is largely from the [TCPDF barcode generator](https://github.com/tecnickcom/TCPDF) by Nicola Asuni. This code is therefor licensed under LGPLv3. It is still a bit of a mess, bit I will clean it in the future. I do not expect the interface of this class will change during the clean ups.* +*The codebase is largely from the [TCPDF barcode generator](https://github.com/tecnickcom/TCPDF) by Nicola Asuni. This code is therefor licensed under LGPLv3.* ## Installation Install through [composer](https://getcomposer.org/doc/00-intro.md): @@ -71,6 +71,8 @@ $generatorHTML = new Picqer\Barcode\BarcodeGeneratorHTML(); - TYPE_PHARMA_CODE - TYPE_PHARMA_CODE_TWO_TRACKS +[See example images for all supported barcode types](examples.md) + ## Examples Embedded PNG image in HTML: diff --git a/examples.md b/examples.md new file mode 100644 index 0000000..7a889c3 --- /dev/null +++ b/examples.md @@ -0,0 +1,129 @@ +# Examples of supported barcodes + +These are examples of supported barcodes with this library. + + + +### C39 + +![Barcode 1234567890abcABC as C39](tests/verified-files/C39-1234567890abcABC.svg) + +### C39+ + +![Barcode 1234567890abcABC as C39+](tests/verified-files/C39+-1234567890abcABC.svg) + +### C39E+ + +![Barcode 1234567890abcABC as C39E+](tests/verified-files/C39E+-1234567890abcABC.svg) + +### C93 + +![Barcode 1234567890abcABC as C93](tests/verified-files/C93-1234567890abcABC.svg) + +### S25 + +![Barcode 1234567890 as S25](tests/verified-files/S25-1234567890.svg) + +### S25+ + +![Barcode 1234567890 as S25+](tests/verified-files/S25+-1234567890.svg) + +### I25 + +![Barcode 1234567890 as I25](tests/verified-files/I25-1234567890.svg) + +### I25+ + +![Barcode 1234567890 as I25+](tests/verified-files/I25+-1234567890.svg) + +### EAN13 + +![Barcode 081231723897 as EAN13](tests/verified-files/EAN13-081231723897.svg) + +![Barcode 0049000004632 as EAN13](tests/verified-files/EAN13-0049000004632.svg) + +![Barcode 004900000463 as EAN13](tests/verified-files/EAN13-004900000463.svg) + +### C128 + +![Barcode 081231723897 as C128](tests/verified-files/C128-081231723897.svg) + +![Barcode 1234567890abcABC-283*33 as C128](tests/verified-files/C128-1234567890abcABC-283*33.svg) + +### C128A + +![Barcode 1234567890 as C128A](tests/verified-files/C128A-1234567890.svg) + +### C128B + +![Barcode 081231723897 as C128B](tests/verified-files/C128B-081231723897.svg) + +![Barcode 1234567890abcABC-283*33 as C128B](tests/verified-files/C128B-1234567890abcABC-283*33.svg) + +### EAN2 + +![Barcode 22 as EAN2](tests/verified-files/EAN2-22.svg) + +### EAN5 + +![Barcode 1234567890abcABC-283*33 as EAN5](tests/verified-files/EAN5-1234567890abcABC-283*33.svg) + +### EAN8 + +![Barcode 1234568 as EAN8](tests/verified-files/EAN8-1234568.svg) + +### UPCA + +![Barcode 123456789 as UPCA](tests/verified-files/UPCA-123456789.svg) + +### UPCE + +![Barcode 123456789 as UPCE](tests/verified-files/UPCE-123456789.svg) + +### MSI + +![Barcode 123456789 as MSI](tests/verified-files/MSI-123456789.svg) + +### MSI+ + +![Barcode 123456789 as MSI+](tests/verified-files/MSI+-123456789.svg) + +### POSTNET + +![Barcode 123456789 as POSTNET](tests/verified-files/POSTNET-123456789.svg) + +### PLANET + +![Barcode 123456789 as PLANET](tests/verified-files/PLANET-123456789.svg) + +### RMS4CC + +![Barcode 123456789 as RMS4CC](tests/verified-files/RMS4CC-123456789.svg) + +### KIX + +![Barcode 123456789 as KIX](tests/verified-files/KIX-123456789.svg) + +### IMB + +![Barcode 123456789 as IMB](tests/verified-files/IMB-123456789.svg) + +### CODABAR + +![Barcode 123456789 as CODABAR](tests/verified-files/CODABAR-123456789.svg) + +### CODE11 + +![Barcode 123456789 as CODE11](tests/verified-files/CODE11-123456789.svg) + +### PHARMA + +![Barcode 123456789 as PHARMA](tests/verified-files/PHARMA-123456789.svg) + +### PHARMA2T + +![Barcode 123456789 as PHARMA2T](tests/verified-files/PHARMA2T-123456789.svg) + + + +*This file is generated by generate-examples.php* \ No newline at end of file diff --git a/generate-examples.php b/generate-examples.php new file mode 100644 index 0000000..30cfe96 --- /dev/null +++ b/generate-examples.php @@ -0,0 +1,22 @@ + Date: Thu, 2 Jan 2020 21:54:42 +0100 Subject: [PATCH 031/155] Split unit tests to individual files --- tests/BarcodeHtmlTest.php | 14 ++ tests/BarcodeJpgTest.php | 57 ++++++++ tests/BarcodePngTest.php | 52 +++++++ tests/BarcodeSvgTest.php | 14 ++ tests/GeneratorTest.php | 54 ++++++++ tests/{BarcodeTest.php => TypesTest.php} | 164 +---------------------- 6 files changed, 192 insertions(+), 163 deletions(-) create mode 100644 tests/BarcodeHtmlTest.php create mode 100644 tests/BarcodeJpgTest.php create mode 100644 tests/BarcodePngTest.php create mode 100644 tests/BarcodeSvgTest.php create mode 100644 tests/GeneratorTest.php rename tests/{BarcodeTest.php => TypesTest.php} (55%) diff --git a/tests/BarcodeHtmlTest.php b/tests/BarcodeHtmlTest.php new file mode 100644 index 0000000..bf5263d --- /dev/null +++ b/tests/BarcodeHtmlTest.php @@ -0,0 +1,14 @@ +getBarcode('081231723897', $generator::TYPE_CODE_128); + + $this->assertStringEqualsFile('tests/verified-files/081231723897-code128.html', $generated); + } +} diff --git a/tests/BarcodeJpgTest.php b/tests/BarcodeJpgTest.php new file mode 100644 index 0000000..3573a03 --- /dev/null +++ b/tests/BarcodeJpgTest.php @@ -0,0 +1,57 @@ +getBarcode('081231723897', $generator::TYPE_CODE_128); + + $imageInfo = getimagesizefromstring($result); + + $this->assertGreaterThan(1000, strlen($result)); + $this->assertEquals(202, $imageInfo[0]); // Image width + $this->assertEquals(30, $imageInfo[1]); // Image height + $this->assertEquals('image/jpeg', $imageInfo['mime']); + } + + public function test_jpg_barcode_generator_can_generate_code_39_barcode() + { + $generator = new Picqer\Barcode\BarcodeGeneratorJPG(); + $result = $generator->getBarcode('081231723897', $generator::TYPE_CODE_39, 1); + + $imageInfo = getimagesizefromstring($result); + + $this->assertGreaterThan(1000, strlen($result)); + $this->assertEquals(224, $imageInfo[0]); // Image width + $this->assertEquals(30, $imageInfo[1]); // Image height + $this->assertEquals('image/jpeg', $imageInfo['mime']); + } + + public function test_jpg_barcode_generator_can_use_different_height() + { + $generator = new Picqer\Barcode\BarcodeGeneratorJPG(); + $result = $generator->getBarcode('081231723897', $generator::TYPE_CODE_128, 2, 45); + + $imageInfo = getimagesizefromstring($result); + + $this->assertGreaterThan(1000, strlen($result)); + $this->assertEquals(202, $imageInfo[0]); // Image width + $this->assertEquals(45, $imageInfo[1]); // Image height + $this->assertEquals('image/jpeg', $imageInfo['mime']); + } + + public function test_jpg_barcode_generator_can_use_different_width_factor() + { + $generator = new Picqer\Barcode\BarcodeGeneratorJPG(); + $result = $generator->getBarcode('081231723897', $generator::TYPE_CODE_128, 5); + + $imageInfo = getimagesizefromstring($result); + + $this->assertGreaterThan(1000, strlen($result)); + $this->assertEquals(505, $imageInfo[0]); // Image width + $this->assertEquals('image/jpeg', $imageInfo['mime']); + } +} diff --git a/tests/BarcodePngTest.php b/tests/BarcodePngTest.php new file mode 100644 index 0000000..128340c --- /dev/null +++ b/tests/BarcodePngTest.php @@ -0,0 +1,52 @@ +getBarcode('081231723897', $generator::TYPE_CODE_128); + + $this->assertEquals('PNG', substr($generated, 1, 3)); + } + + public function test_png_barcode_generator_can_generate_code_39_barcode() + { + $generator = new Picqer\Barcode\BarcodeGeneratorPNG(); + $result = $generator->getBarcode('081231723897', $generator::TYPE_CODE_39, 1); + + $imageInfo = getimagesizefromstring($result); + + $this->assertGreaterThan(100, strlen($result)); + $this->assertEquals(224, $imageInfo[0]); // Image width + $this->assertEquals(30, $imageInfo[1]); // Image height + $this->assertEquals('image/png', $imageInfo['mime']); + } + + public function test_png_barcode_generator_can_use_different_height() + { + $generator = new Picqer\Barcode\BarcodeGeneratorPNG(); + $result = $generator->getBarcode('081231723897', $generator::TYPE_CODE_128, 2, 45); + + $imageInfo = getimagesizefromstring($result); + + $this->assertGreaterThan(100, strlen($result)); + $this->assertEquals(202, $imageInfo[0]); // Image width + $this->assertEquals(45, $imageInfo[1]); // Image height + $this->assertEquals('image/png', $imageInfo['mime']); + } + + public function test_png_barcode_generator_can_use_different_width_factor() + { + $generator = new Picqer\Barcode\BarcodeGeneratorPNG(); + $result = $generator->getBarcode('081231723897', $generator::TYPE_CODE_128, 5); + + $imageInfo = getimagesizefromstring($result); + + $this->assertGreaterThan(100, strlen($result)); + $this->assertEquals(505, $imageInfo[0]); // Image width + $this->assertEquals('image/png', $imageInfo['mime']); + } +} diff --git a/tests/BarcodeSvgTest.php b/tests/BarcodeSvgTest.php new file mode 100644 index 0000000..14c0a21 --- /dev/null +++ b/tests/BarcodeSvgTest.php @@ -0,0 +1,14 @@ +getBarcode('081231723897', $generator::TYPE_EAN_13); + + $this->assertStringEqualsFile('tests/verified-files/081231723897-ean13.svg', $generated); + } +} diff --git a/tests/GeneratorTest.php b/tests/GeneratorTest.php new file mode 100644 index 0000000..17f8d71 --- /dev/null +++ b/tests/GeneratorTest.php @@ -0,0 +1,54 @@ +expectException(Picqer\Barcode\Exceptions\InvalidCharacterException::class); + + $generator = new Picqer\Barcode\BarcodeGeneratorSVG(); + $generator->getBarcode('A123', $generator::TYPE_EAN_13); + } + + public function test_ean13_generator_accepting_13_chars() + { + $generator = new Picqer\Barcode\BarcodeGeneratorSVG(); + $generated = $generator->getBarcode('0049000004632', $generator::TYPE_EAN_13); + + $this->assertStringEqualsFile('tests/verified-files/0049000004632-ean13.svg', $generated); + } + + public function test_ean13_generator_accepting_12_chars_and_generates_13th_check_digit() + { + $generator = new Picqer\Barcode\BarcodeGeneratorSVG(); + $generated = $generator->getBarcode('004900000463', $generator::TYPE_EAN_13); + + $this->assertStringEqualsFile('tests/verified-files/0049000004632-ean13.svg', $generated); + } + + public function test_ean13_generator_accepting_11_chars_and_generates_13th_check_digit_and_adds_leading_zero() + { + $generator = new Picqer\Barcode\BarcodeGeneratorSVG(); + $generated = $generator->getBarcode('04900000463', $generator::TYPE_EAN_13); + + $this->assertStringEqualsFile('tests/verified-files/0049000004632-ean13.svg', $generated); + } + + public function test_ean13_generator_throws_exception_when_wrong_check_digit_is_given() + { + $this->expectException(Picqer\Barcode\Exceptions\InvalidCheckDigitException::class); + + $generator = new Picqer\Barcode\BarcodeGeneratorSVG(); + $generator->getBarcode('0049000004633', $generator::TYPE_EAN_13); + } + + public function test_generator_throws_unknown_type_exceptions() + { + $this->expectException(Picqer\Barcode\Exceptions\UnknownTypeException::class); + + $generator = new Picqer\Barcode\BarcodeGeneratorSVG(); + $generator->getBarcode('0049000004633', 'vladimir'); + } +} \ No newline at end of file diff --git a/tests/BarcodeTest.php b/tests/TypesTest.php similarity index 55% rename from tests/BarcodeTest.php rename to tests/TypesTest.php index 885fce7..ca995d8 100644 --- a/tests/BarcodeTest.php +++ b/tests/TypesTest.php @@ -2,169 +2,8 @@ use PHPUnit\Framework\TestCase; -class BarcodeTest extends TestCase +class TypesTest extends TestCase { - public function test_png_barcode_generator_can_generate_code_128_barcode() - { - $generator = new Picqer\Barcode\BarcodeGeneratorPNG(); - $generated = $generator->getBarcode('081231723897', $generator::TYPE_CODE_128); - - $this->assertEquals('PNG', substr($generated, 1, 3)); - } - - public function test_svg_barcode_generator_can_generate_ean_13_barcode() - { - $generator = new Picqer\Barcode\BarcodeGeneratorSVG(); - $generated = $generator->getBarcode('081231723897', $generator::TYPE_EAN_13); - - $this->assertStringEqualsFile('tests/verified-files/081231723897-ean13.svg', $generated); - } - - public function test_html_barcode_generator_can_generate_code_128_barcode() - { - $generator = new Picqer\Barcode\BarcodeGeneratorHTML(); - $generated = $generator->getBarcode('081231723897', $generator::TYPE_CODE_128); - - $this->assertStringEqualsFile('tests/verified-files/081231723897-code128.html', $generated); - } - - public function test_jpg_barcode_generator_can_generate_code_128_barcode() - { - $generator = new Picqer\Barcode\BarcodeGeneratorJPG(); - $result = $generator->getBarcode('081231723897', $generator::TYPE_CODE_128); - - $imageInfo = getimagesizefromstring($result); - - $this->assertGreaterThan(1000, strlen($result)); - $this->assertEquals(202, $imageInfo[0]); // Image width - $this->assertEquals(30, $imageInfo[1]); // Image height - $this->assertEquals('image/jpeg', $imageInfo['mime']); - } - - public function test_jpg_barcode_generator_can_generate_code_39_barcode() - { - $generator = new Picqer\Barcode\BarcodeGeneratorJPG(); - $result = $generator->getBarcode('081231723897', $generator::TYPE_CODE_39, 1); - - $imageInfo = getimagesizefromstring($result); - - $this->assertGreaterThan(1000, strlen($result)); - $this->assertEquals(224, $imageInfo[0]); // Image width - $this->assertEquals(30, $imageInfo[1]); // Image height - $this->assertEquals('image/jpeg', $imageInfo['mime']); - } - - public function test_jpg_barcode_generator_can_use_different_height() - { - $generator = new Picqer\Barcode\BarcodeGeneratorJPG(); - $result = $generator->getBarcode('081231723897', $generator::TYPE_CODE_128, 2, 45); - - $imageInfo = getimagesizefromstring($result); - - $this->assertGreaterThan(1000, strlen($result)); - $this->assertEquals(202, $imageInfo[0]); // Image width - $this->assertEquals(45, $imageInfo[1]); // Image height - $this->assertEquals('image/jpeg', $imageInfo['mime']); - } - - public function test_jpg_barcode_generator_can_use_different_width_factor() - { - $generator = new Picqer\Barcode\BarcodeGeneratorJPG(); - $result = $generator->getBarcode('081231723897', $generator::TYPE_CODE_128, 5); - - $imageInfo = getimagesizefromstring($result); - - $this->assertGreaterThan(1000, strlen($result)); - $this->assertEquals(505, $imageInfo[0]); // Image width - $this->assertEquals('image/jpeg', $imageInfo['mime']); - } - - public function test_png_barcode_generator_can_generate_code_39_barcode() - { - $generator = new Picqer\Barcode\BarcodeGeneratorPNG(); - $result = $generator->getBarcode('081231723897', $generator::TYPE_CODE_39, 1); - - $imageInfo = getimagesizefromstring($result); - - $this->assertGreaterThan(100, strlen($result)); - $this->assertEquals(224, $imageInfo[0]); // Image width - $this->assertEquals(30, $imageInfo[1]); // Image height - $this->assertEquals('image/png', $imageInfo['mime']); - } - - public function test_png_barcode_generator_can_use_different_height() - { - $generator = new Picqer\Barcode\BarcodeGeneratorPNG(); - $result = $generator->getBarcode('081231723897', $generator::TYPE_CODE_128, 2, 45); - - $imageInfo = getimagesizefromstring($result); - - $this->assertGreaterThan(100, strlen($result)); - $this->assertEquals(202, $imageInfo[0]); // Image width - $this->assertEquals(45, $imageInfo[1]); // Image height - $this->assertEquals('image/png', $imageInfo['mime']); - } - - public function test_png_barcode_generator_can_use_different_width_factor() - { - $generator = new Picqer\Barcode\BarcodeGeneratorPNG(); - $result = $generator->getBarcode('081231723897', $generator::TYPE_CODE_128, 5); - - $imageInfo = getimagesizefromstring($result); - - $this->assertGreaterThan(100, strlen($result)); - $this->assertEquals(505, $imageInfo[0]); // Image width - $this->assertEquals('image/png', $imageInfo['mime']); - } - - public function test_ean13_generator_throws_exception_if_invalid_chars_are_used() - { - $this->expectException(Picqer\Barcode\Exceptions\InvalidCharacterException::class); - - $generator = new Picqer\Barcode\BarcodeGeneratorSVG(); - $generator->getBarcode('A123', $generator::TYPE_EAN_13); - } - - public function test_ean13_generator_accepting_13_chars() - { - $generator = new Picqer\Barcode\BarcodeGeneratorSVG(); - $generated = $generator->getBarcode('0049000004632', $generator::TYPE_EAN_13); - - $this->assertStringEqualsFile('tests/verified-files/0049000004632-ean13.svg', $generated); - } - - public function test_ean13_generator_accepting_12_chars_and_generates_13th_check_digit() - { - $generator = new Picqer\Barcode\BarcodeGeneratorSVG(); - $generated = $generator->getBarcode('004900000463', $generator::TYPE_EAN_13); - - $this->assertStringEqualsFile('tests/verified-files/0049000004632-ean13.svg', $generated); - } - - public function test_ean13_generator_accepting_11_chars_and_generates_13th_check_digit_and_adds_leading_zero() - { - $generator = new Picqer\Barcode\BarcodeGeneratorSVG(); - $generated = $generator->getBarcode('04900000463', $generator::TYPE_EAN_13); - - $this->assertStringEqualsFile('tests/verified-files/0049000004632-ean13.svg', $generated); - } - - public function test_ean13_generator_throws_exception_when_wrong_check_digit_is_given() - { - $this->expectException(Picqer\Barcode\Exceptions\InvalidCheckDigitException::class); - - $generator = new Picqer\Barcode\BarcodeGeneratorSVG(); - $generator->getBarcode('0049000004633', $generator::TYPE_EAN_13); - } - - public function test_generator_throws_unknown_type_exceptions() - { - $this->expectException(Picqer\Barcode\Exceptions\UnknownTypeException::class); - - $generator = new Picqer\Barcode\BarcodeGeneratorSVG(); - $generator->getBarcode('0049000004633', 'vladimir'); - } - public function test_generator_can_generate_code_39_barcode() { $generator = new Picqer\Barcode\BarcodeGeneratorSVG(); @@ -388,5 +227,4 @@ public function test_generator_can_generate_pharma_code_2_tracks_barcode() $this->assertGreaterThan(100, strlen($result)); } - } \ No newline at end of file From 0cf123c35399fa773883e53f612c8e2556206768 Mon Sep 17 00:00:00 2001 From: Casper Bakker Date: Thu, 2 Jan 2020 21:59:36 +0100 Subject: [PATCH 032/155] Does github actions support imagick? --- .github/workflows/phpunit.yml | 2 +- .travis.yml | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/.github/workflows/phpunit.yml b/.github/workflows/phpunit.yml index 14ee637..0a3ddea 100644 --- a/.github/workflows/phpunit.yml +++ b/.github/workflows/phpunit.yml @@ -18,7 +18,7 @@ jobs: uses: shivammathur/setup-php@v1 with: php-version: ${{ matrix.php-versions }} - extensions: mbstring, gd, bcmath + extensions: mbstring, gd, bcmath, imagick - name: Validate composer.json run: composer validate diff --git a/.travis.yml b/.travis.yml index 2e9486a..841a1d1 100644 --- a/.travis.yml +++ b/.travis.yml @@ -7,6 +7,8 @@ php: sudo: false +before_script: yes | pecl install imagick + install: composer install --no-interaction --prefer-source script: vendor/bin/phpunit From 032f74d57c2482931ea15e0b72b4b4649f9c3f6d Mon Sep 17 00:00:00 2001 From: Casper Bakker Date: Thu, 2 Jan 2020 22:15:41 +0100 Subject: [PATCH 033/155] Test everything with Imagick as well --- tests/BarcodeJpgTest.php | 78 ++++++++++++++++++++++++++++++++++++++++ tests/BarcodePngTest.php | 73 +++++++++++++++++++++++++++++++++++++ 2 files changed, 151 insertions(+) diff --git a/tests/BarcodeJpgTest.php b/tests/BarcodeJpgTest.php index 3573a03..52634b3 100644 --- a/tests/BarcodeJpgTest.php +++ b/tests/BarcodeJpgTest.php @@ -7,6 +7,7 @@ class BarcodeJpgTest extends TestCase public function test_jpg_barcode_generator_can_generate_code_128_barcode() { $generator = new Picqer\Barcode\BarcodeGeneratorJPG(); + $generator->useGd(); $result = $generator->getBarcode('081231723897', $generator::TYPE_CODE_128); $imageInfo = getimagesizefromstring($result); @@ -20,6 +21,7 @@ public function test_jpg_barcode_generator_can_generate_code_128_barcode() public function test_jpg_barcode_generator_can_generate_code_39_barcode() { $generator = new Picqer\Barcode\BarcodeGeneratorJPG(); + $generator->useGd(); $result = $generator->getBarcode('081231723897', $generator::TYPE_CODE_39, 1); $imageInfo = getimagesizefromstring($result); @@ -33,6 +35,7 @@ public function test_jpg_barcode_generator_can_generate_code_39_barcode() public function test_jpg_barcode_generator_can_use_different_height() { $generator = new Picqer\Barcode\BarcodeGeneratorJPG(); + $generator->useGd(); $result = $generator->getBarcode('081231723897', $generator::TYPE_CODE_128, 2, 45); $imageInfo = getimagesizefromstring($result); @@ -46,6 +49,81 @@ public function test_jpg_barcode_generator_can_use_different_height() public function test_jpg_barcode_generator_can_use_different_width_factor() { $generator = new Picqer\Barcode\BarcodeGeneratorJPG(); + $generator->useGd(); + $result = $generator->getBarcode('081231723897', $generator::TYPE_CODE_128, 5); + + $imageInfo = getimagesizefromstring($result); + + $this->assertGreaterThan(1000, strlen($result)); + $this->assertEquals(505, $imageInfo[0]); // Image width + $this->assertEquals('image/jpeg', $imageInfo['mime']); + } + + + // Copied as Imagick + + public function test_jpg_barcode_generator_can_generate_code_128_barcode_imagick() + { + if (! extension_loaded('imagick')) { + $this->markTestSkipped(); + } + + $generator = new Picqer\Barcode\BarcodeGeneratorJPG(); + $generator->useImagick(); + $result = $generator->getBarcode('081231723897', $generator::TYPE_CODE_128); + + $imageInfo = getimagesizefromstring($result); + + $this->assertGreaterThan(1000, strlen($result)); + $this->assertEquals(202, $imageInfo[0]); // Image width + $this->assertEquals(30, $imageInfo[1]); // Image height + $this->assertEquals('image/jpeg', $imageInfo['mime']); + } + + public function test_jpg_barcode_generator_can_generate_code_39_barcode_imagick() + { + if (! extension_loaded('imagick')) { + $this->markTestSkipped(); + } + + $generator = new Picqer\Barcode\BarcodeGeneratorJPG(); + $generator->useImagick(); + $result = $generator->getBarcode('081231723897', $generator::TYPE_CODE_39, 1); + + $imageInfo = getimagesizefromstring($result); + + $this->assertGreaterThan(1000, strlen($result)); + $this->assertEquals(224, $imageInfo[0]); // Image width + $this->assertEquals(30, $imageInfo[1]); // Image height + $this->assertEquals('image/jpeg', $imageInfo['mime']); + } + + public function test_jpg_barcode_generator_can_use_different_height_imagick() + { + if (! extension_loaded('imagick')) { + $this->markTestSkipped(); + } + + $generator = new Picqer\Barcode\BarcodeGeneratorJPG(); + $generator->useImagick(); + $result = $generator->getBarcode('081231723897', $generator::TYPE_CODE_128, 2, 45); + + $imageInfo = getimagesizefromstring($result); + + $this->assertGreaterThan(1000, strlen($result)); + $this->assertEquals(202, $imageInfo[0]); // Image width + $this->assertEquals(45, $imageInfo[1]); // Image height + $this->assertEquals('image/jpeg', $imageInfo['mime']); + } + + public function test_jpg_barcode_generator_can_use_different_width_factor_imagick() + { + if (! extension_loaded('imagick')) { + $this->markTestSkipped(); + } + + $generator = new Picqer\Barcode\BarcodeGeneratorJPG(); + $generator->useImagick(); $result = $generator->getBarcode('081231723897', $generator::TYPE_CODE_128, 5); $imageInfo = getimagesizefromstring($result); diff --git a/tests/BarcodePngTest.php b/tests/BarcodePngTest.php index 128340c..700baa2 100644 --- a/tests/BarcodePngTest.php +++ b/tests/BarcodePngTest.php @@ -7,6 +7,7 @@ class BarcodePngTest extends TestCase public function test_png_barcode_generator_can_generate_code_128_barcode() { $generator = new Picqer\Barcode\BarcodeGeneratorPNG(); + $generator->useGd(); $generated = $generator->getBarcode('081231723897', $generator::TYPE_CODE_128); $this->assertEquals('PNG', substr($generated, 1, 3)); @@ -15,6 +16,7 @@ public function test_png_barcode_generator_can_generate_code_128_barcode() public function test_png_barcode_generator_can_generate_code_39_barcode() { $generator = new Picqer\Barcode\BarcodeGeneratorPNG(); + $generator->useGd(); $result = $generator->getBarcode('081231723897', $generator::TYPE_CODE_39, 1); $imageInfo = getimagesizefromstring($result); @@ -28,6 +30,7 @@ public function test_png_barcode_generator_can_generate_code_39_barcode() public function test_png_barcode_generator_can_use_different_height() { $generator = new Picqer\Barcode\BarcodeGeneratorPNG(); + $generator->useGd(); $result = $generator->getBarcode('081231723897', $generator::TYPE_CODE_128, 2, 45); $imageInfo = getimagesizefromstring($result); @@ -41,6 +44,76 @@ public function test_png_barcode_generator_can_use_different_height() public function test_png_barcode_generator_can_use_different_width_factor() { $generator = new Picqer\Barcode\BarcodeGeneratorPNG(); + $generator->useGd(); + $result = $generator->getBarcode('081231723897', $generator::TYPE_CODE_128, 5); + + $imageInfo = getimagesizefromstring($result); + + $this->assertGreaterThan(100, strlen($result)); + $this->assertEquals(505, $imageInfo[0]); // Image width + $this->assertEquals('image/png', $imageInfo['mime']); + } + + + // Copied as Imagick + + public function test_png_barcode_generator_can_generate_code_128_barcode_imagick() + { + if (! extension_loaded('imagick')) { + $this->markTestSkipped(); + } + + $generator = new Picqer\Barcode\BarcodeGeneratorPNG(); + $generator->useImagick(); + $generated = $generator->getBarcode('081231723897', $generator::TYPE_CODE_128); + + $this->assertEquals('PNG', substr($generated, 1, 3)); + } + + public function test_png_barcode_generator_can_generate_code_39_barcode_imagick() + { + if (! extension_loaded('imagick')) { + $this->markTestSkipped(); + } + + $generator = new Picqer\Barcode\BarcodeGeneratorPNG(); + $generator->useImagick(); + $result = $generator->getBarcode('081231723897', $generator::TYPE_CODE_39, 1); + + $imageInfo = getimagesizefromstring($result); + + $this->assertGreaterThan(100, strlen($result)); + $this->assertEquals(224, $imageInfo[0]); // Image width + $this->assertEquals(30, $imageInfo[1]); // Image height + $this->assertEquals('image/png', $imageInfo['mime']); + } + + public function test_png_barcode_generator_can_use_different_height_imagick() + { + if (! extension_loaded('imagick')) { + $this->markTestSkipped(); + } + + $generator = new Picqer\Barcode\BarcodeGeneratorPNG(); + $generator->useImagick(); + $result = $generator->getBarcode('081231723897', $generator::TYPE_CODE_128, 2, 45); + + $imageInfo = getimagesizefromstring($result); + + $this->assertGreaterThan(100, strlen($result)); + $this->assertEquals(202, $imageInfo[0]); // Image width + $this->assertEquals(45, $imageInfo[1]); // Image height + $this->assertEquals('image/png', $imageInfo['mime']); + } + + public function test_png_barcode_generator_can_use_different_width_factor_imagick() + { + if (! extension_loaded('imagick')) { + $this->markTestSkipped(); + } + + $generator = new Picqer\Barcode\BarcodeGeneratorPNG(); + $generator->useImagick(); $result = $generator->getBarcode('081231723897', $generator::TYPE_CODE_128, 5); $imageInfo = getimagesizefromstring($result); From 35fad092407923f539eab90825f6567fd84733b6 Mon Sep 17 00:00:00 2001 From: Casper Bakker Date: Thu, 2 Jan 2020 22:17:51 +0100 Subject: [PATCH 034/155] Imagick is apperently way more efficient then gd --- tests/BarcodeJpgTest.php | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/tests/BarcodeJpgTest.php b/tests/BarcodeJpgTest.php index 52634b3..d597f4a 100644 --- a/tests/BarcodeJpgTest.php +++ b/tests/BarcodeJpgTest.php @@ -12,7 +12,7 @@ public function test_jpg_barcode_generator_can_generate_code_128_barcode() $imageInfo = getimagesizefromstring($result); - $this->assertGreaterThan(1000, strlen($result)); + $this->assertGreaterThan(100, strlen($result)); $this->assertEquals(202, $imageInfo[0]); // Image width $this->assertEquals(30, $imageInfo[1]); // Image height $this->assertEquals('image/jpeg', $imageInfo['mime']); @@ -26,7 +26,7 @@ public function test_jpg_barcode_generator_can_generate_code_39_barcode() $imageInfo = getimagesizefromstring($result); - $this->assertGreaterThan(1000, strlen($result)); + $this->assertGreaterThan(100, strlen($result)); $this->assertEquals(224, $imageInfo[0]); // Image width $this->assertEquals(30, $imageInfo[1]); // Image height $this->assertEquals('image/jpeg', $imageInfo['mime']); @@ -40,7 +40,7 @@ public function test_jpg_barcode_generator_can_use_different_height() $imageInfo = getimagesizefromstring($result); - $this->assertGreaterThan(1000, strlen($result)); + $this->assertGreaterThan(100, strlen($result)); $this->assertEquals(202, $imageInfo[0]); // Image width $this->assertEquals(45, $imageInfo[1]); // Image height $this->assertEquals('image/jpeg', $imageInfo['mime']); @@ -54,7 +54,7 @@ public function test_jpg_barcode_generator_can_use_different_width_factor() $imageInfo = getimagesizefromstring($result); - $this->assertGreaterThan(1000, strlen($result)); + $this->assertGreaterThan(100, strlen($result)); $this->assertEquals(505, $imageInfo[0]); // Image width $this->assertEquals('image/jpeg', $imageInfo['mime']); } @@ -74,7 +74,7 @@ public function test_jpg_barcode_generator_can_generate_code_128_barcode_imagick $imageInfo = getimagesizefromstring($result); - $this->assertGreaterThan(1000, strlen($result)); + $this->assertGreaterThan(100, strlen($result)); $this->assertEquals(202, $imageInfo[0]); // Image width $this->assertEquals(30, $imageInfo[1]); // Image height $this->assertEquals('image/jpeg', $imageInfo['mime']); @@ -92,7 +92,7 @@ public function test_jpg_barcode_generator_can_generate_code_39_barcode_imagick( $imageInfo = getimagesizefromstring($result); - $this->assertGreaterThan(1000, strlen($result)); + $this->assertGreaterThan(100, strlen($result)); $this->assertEquals(224, $imageInfo[0]); // Image width $this->assertEquals(30, $imageInfo[1]); // Image height $this->assertEquals('image/jpeg', $imageInfo['mime']); @@ -110,7 +110,7 @@ public function test_jpg_barcode_generator_can_use_different_height_imagick() $imageInfo = getimagesizefromstring($result); - $this->assertGreaterThan(1000, strlen($result)); + $this->assertGreaterThan(100, strlen($result)); $this->assertEquals(202, $imageInfo[0]); // Image width $this->assertEquals(45, $imageInfo[1]); // Image height $this->assertEquals('image/jpeg', $imageInfo['mime']); @@ -128,7 +128,7 @@ public function test_jpg_barcode_generator_can_use_different_width_factor_imagic $imageInfo = getimagesizefromstring($result); - $this->assertGreaterThan(1000, strlen($result)); + $this->assertGreaterThan(100, strlen($result)); $this->assertEquals(505, $imageInfo[0]); // Image width $this->assertEquals('image/jpeg', $imageInfo['mime']); } From c275de088f72920c9ca0415a22c70381821aa03c Mon Sep 17 00:00:00 2001 From: Casper Bakker Date: Fri, 3 Jan 2020 09:53:38 +0100 Subject: [PATCH 035/155] Do not require optional dependencies --- .gitignore | 3 --- composer.json | 8 ++++++-- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/.gitignore b/.gitignore index defd14f..d069e29 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,3 @@ -.idea -.DS_Store vendor composer.lock composer.phar -.phpunit.result.cache \ No newline at end of file diff --git a/composer.json b/composer.json index e871dd2..2fdd469 100644 --- a/composer.json +++ b/composer.json @@ -18,12 +18,16 @@ ], "require": { "php": ">=7.2.0", - "ext-mbstring": "*", - "ext-bcmath": "*" + "ext-mbstring": "*" }, "require-dev": { "phpunit/phpunit": "^8.5" }, + "suggest": { + "ext-bcmath": "Barcode IMB (Intelligent Mail Barcode) needs bcmath extension", + "ext-gd": "For JPG and PNG generators, GD or Imagick is required", + "ext-imagick": "For JPG and PNG generators, GD or Imagick is required" + }, "autoload": { "psr-4": { "Picqer\\Barcode\\": "src" From fdd368a88946e3d01c6fd9397d56e082d6b8d64a Mon Sep 17 00:00:00 2001 From: Casper Bakker Date: Fri, 3 Jan 2020 09:54:08 +0100 Subject: [PATCH 036/155] Unify generator interface names --- src/BarcodeGeneratorHTML.php | 13 ++++++------- src/BarcodeGeneratorSVG.php | 10 +++++----- 2 files changed, 11 insertions(+), 12 deletions(-) diff --git a/src/BarcodeGeneratorHTML.php b/src/BarcodeGeneratorHTML.php index a21c80f..969c909 100644 --- a/src/BarcodeGeneratorHTML.php +++ b/src/BarcodeGeneratorHTML.php @@ -10,24 +10,23 @@ class BarcodeGeneratorHTML extends BarcodeGenerator * @param string $barcode code to print * @param string $type type of barcode * @param int $widthFactor Width of a single bar element in pixels. - * @param int $totalHeight Height of a single bar element in pixels. - * @param int|string $foregroundColor Foreground color for bar elements as '#333' or 'orange' for example (background is transparent). + * @param int $height Height of a single bar element in pixels. + * @param string $foregroundColor Foreground color for bar elements as '#333' or 'orange' for example (background is transparent). * @return string HTML code. - * @public */ - public function getBarcode($barcode, $type, int $widthFactor = 2, int $totalHeight = 30, string $foregroundColor = 'black') + public function getBarcode($barcode, $type, int $widthFactor = 2, int $height = 30, string $foregroundColor = 'black') { $barcodeData = $this->getBarcodeData($barcode, $type); - $html = '
    ' . PHP_EOL; + $html = '
    ' . PHP_EOL; $positionHorizontal = 0; foreach ($barcodeData['bars'] as $bar) { $barWidth = round(($bar['width'] * $widthFactor), 3); - $barHeight = round(($bar['height'] * $totalHeight / $barcodeData['maxHeight']), 3); + $barHeight = round(($bar['height'] * $height / $barcodeData['maxHeight']), 3); if ($bar['drawBar']) { - $positionVertical = round(($bar['positionVertical'] * $totalHeight / $barcodeData['maxHeight']), 3); + $positionVertical = round(($bar['positionVertical'] * $height / $barcodeData['maxHeight']), 3); // draw a vertical bar $html .= '
     
    ' . PHP_EOL; } diff --git a/src/BarcodeGeneratorSVG.php b/src/BarcodeGeneratorSVG.php index a027666..8903a36 100644 --- a/src/BarcodeGeneratorSVG.php +++ b/src/BarcodeGeneratorSVG.php @@ -10,12 +10,12 @@ class BarcodeGeneratorSVG extends BarcodeGenerator * @param $barcode (string) code to print * @param $type (const) type of barcode * @param $widthFactor (int) Minimum width of a single bar in user units. - * @param $totalHeight (int) Height of barcode in user units. + * @param $height (int) Height of barcode in user units. * @param $foregroundColor (string) Foreground color (in SVG format) for bar elements (background is transparent). * @return string SVG code. * @public */ - public function getBarcode($barcode, $type, int $widthFactor = 2, int $totalHeight = 30, string $foregroundColor = 'black') + public function getBarcode($barcode, $type, int $widthFactor = 2, int $height = 30, string $foregroundColor = 'black') { $barcodeData = $this->getBarcodeData($barcode, $type); @@ -26,16 +26,16 @@ public function getBarcode($barcode, $type, int $widthFactor = 2, int $totalHeig $svg = '' . PHP_EOL; $svg .= '' . PHP_EOL; - $svg .= '' . PHP_EOL; + $svg .= '' . PHP_EOL; $svg .= "\t" . '' . strtr($barcodeData['code'], $repstr) . '' . PHP_EOL; $svg .= "\t" . '' . PHP_EOL; // print bars $positionHorizontal = 0; foreach ($barcodeData['bars'] as $bar) { $barWidth = round(($bar['width'] * $widthFactor), 3); - $barHeight = round(($bar['height'] * $totalHeight / $barcodeData['maxHeight']), 3); + $barHeight = round(($bar['height'] * $height / $barcodeData['maxHeight']), 3); if ($bar['drawBar']) { - $positionVertical = round(($bar['positionVertical'] * $totalHeight / $barcodeData['maxHeight']), 3); + $positionVertical = round(($bar['positionVertical'] * $height / $barcodeData['maxHeight']), 3); // draw a vertical bar $svg .= "\t\t" . '' . PHP_EOL; } From 3303ba189f4e02e11eec74d985eab76b187c0ff4 Mon Sep 17 00:00:00 2001 From: Casper Bakker Date: Fri, 3 Jan 2020 13:54:07 +0100 Subject: [PATCH 037/155] Do not draw unneeded 0 width lines #74 --- composer.json | 3 ++- src/BarcodeGeneratorHTML.php | 2 +- src/BarcodeGeneratorPNG.php | 2 +- src/BarcodeGeneratorSVG.php | 5 ++++- src/Helpers/BinarySequenceConverter.php | 16 +++++---------- src/Helpers/OldBarcodeArrayConverter.php | 20 +++++++++---------- .../verified-files/081231723897-code128.html | 2 -- tests/verified-files/C128-081231723897.svg | 2 -- .../C128-1234567890abcABC-283*33.svg | 2 -- tests/verified-files/C128A-1234567890.svg | 2 -- tests/verified-files/C128B-081231723897.svg | 2 -- .../C128B-1234567890abcABC-283*33.svg | 2 -- 12 files changed, 23 insertions(+), 37 deletions(-) diff --git a/composer.json b/composer.json index 2fdd469..18fb848 100644 --- a/composer.json +++ b/composer.json @@ -13,7 +13,8 @@ }, { "name": "Casper Bakker", - "email": "info@picqer.com" + "email": "info@picqer.com", + "homepage": "https://picqer.com/" } ], "require": { diff --git a/src/BarcodeGeneratorHTML.php b/src/BarcodeGeneratorHTML.php index 969c909..499bf99 100644 --- a/src/BarcodeGeneratorHTML.php +++ b/src/BarcodeGeneratorHTML.php @@ -25,7 +25,7 @@ public function getBarcode($barcode, $type, int $widthFactor = 2, int $height = $barWidth = round(($bar['width'] * $widthFactor), 3); $barHeight = round(($bar['height'] * $height / $barcodeData['maxHeight']), 3); - if ($bar['drawBar']) { + if ($bar['drawBar'] && $barWidth > 0) { $positionVertical = round(($bar['positionVertical'] * $height / $barcodeData['maxHeight']), 3); // draw a vertical bar $html .= '
     
    ' . PHP_EOL; diff --git a/src/BarcodeGeneratorPNG.php b/src/BarcodeGeneratorPNG.php index 48d5e1f..0becde1 100644 --- a/src/BarcodeGeneratorPNG.php +++ b/src/BarcodeGeneratorPNG.php @@ -67,7 +67,7 @@ public function getBarcode($barcode, $type, int $widthFactor = 2, int $height = foreach ($barcodeData['bars'] as $bar) { $barWidth = round(($bar['width'] * $widthFactor), 3); - if ($bar['drawBar']) { + if ($bar['drawBar'] && $barWidth > 0) { $y = round(($bar['positionVertical'] * $height / $barcodeData['maxHeight']), 3); $barHeight = round(($bar['height'] * $height / $barcodeData['maxHeight']), 3); diff --git a/src/BarcodeGeneratorSVG.php b/src/BarcodeGeneratorSVG.php index 8903a36..e419e04 100644 --- a/src/BarcodeGeneratorSVG.php +++ b/src/BarcodeGeneratorSVG.php @@ -29,16 +29,19 @@ public function getBarcode($barcode, $type, int $widthFactor = 2, int $height = $svg .= '' . PHP_EOL; $svg .= "\t" . '' . strtr($barcodeData['code'], $repstr) . '' . PHP_EOL; $svg .= "\t" . '' . PHP_EOL; + // print bars $positionHorizontal = 0; foreach ($barcodeData['bars'] as $bar) { $barWidth = round(($bar['width'] * $widthFactor), 3); $barHeight = round(($bar['height'] * $height / $barcodeData['maxHeight']), 3); - if ($bar['drawBar']) { + + if ($bar['drawBar'] && $barWidth > 0) { $positionVertical = round(($bar['positionVertical'] * $height / $barcodeData['maxHeight']), 3); // draw a vertical bar $svg .= "\t\t" . '' . PHP_EOL; } + $positionHorizontal += $barWidth; } $svg .= "\t" . PHP_EOL; diff --git a/src/Helpers/BinarySequenceConverter.php b/src/Helpers/BinarySequenceConverter.php index 7ef8f7c..30d98ab 100644 --- a/src/Helpers/BinarySequenceConverter.php +++ b/src/Helpers/BinarySequenceConverter.php @@ -2,18 +2,12 @@ namespace Picqer\Barcode\Helpers; +/** + * Convert binary barcode sequence to barcode array. + */ class BinarySequenceConverter { - /** - * Convert binary barcode sequence to TCPDF barcode array. - * - * @param $seq (string) barcode as binary sequence. - * @param $bararray (array) barcode array. - * òparam array $bararray TCPDF barcode array to fill up - * @return array barcode representation. - * @protected - */ - public static function convert($seq, $bararray) + public static function convert(string $seq, array $bararray): array { $len = strlen($seq); $w = 0; @@ -35,4 +29,4 @@ public static function convert($seq, $bararray) return $bararray; } -} \ No newline at end of file +} diff --git a/src/Helpers/OldBarcodeArrayConverter.php b/src/Helpers/OldBarcodeArrayConverter.php index 480ba76..5c8a2ac 100644 --- a/src/Helpers/OldBarcodeArrayConverter.php +++ b/src/Helpers/OldBarcodeArrayConverter.php @@ -4,25 +4,25 @@ class OldBarcodeArrayConverter { - public static function convert($oldBarcodeArray): array + public static function convert(array $oldBarcodeArray): array { $newBarcodeArray = []; - $newBarcodeArray['code'] = $oldBarcodeArray['code']; - $newBarcodeArray['maxWidth'] = $oldBarcodeArray['maxw']; - $newBarcodeArray['maxHeight'] = $oldBarcodeArray['maxh']; + $newBarcodeArray['code'] = (string)$oldBarcodeArray['code']; + $newBarcodeArray['maxWidth'] = (int)$oldBarcodeArray['maxw']; + $newBarcodeArray['maxHeight'] = (int)$oldBarcodeArray['maxh']; $newBarcodeArray['bars'] = []; foreach ($oldBarcodeArray['bcode'] as $oldbar) { $newBar = []; - $newBar['width'] = $oldbar['w']; - $newBar['height'] = $oldbar['h']; - $newBar['positionVertical'] = $oldbar['p']; - $newBar['drawBar'] = $oldbar['t']; - $newBar['drawSpacing'] = ! $oldbar['t']; + $newBar['width'] = (int)$oldbar['w']; + $newBar['height'] = (int)$oldbar['h']; + $newBar['positionVertical'] = (int)$oldbar['p']; + $newBar['drawBar'] = (bool)$oldbar['t']; + $newBar['drawSpacing'] = ! (bool)$oldbar['t']; $newBarcodeArray['bars'][] = $newBar; } return $newBarcodeArray; } -} \ No newline at end of file +} diff --git a/tests/verified-files/081231723897-code128.html b/tests/verified-files/081231723897-code128.html index 4a332e5..503081a 100644 --- a/tests/verified-files/081231723897-code128.html +++ b/tests/verified-files/081231723897-code128.html @@ -27,6 +27,4 @@
     
     
     
    -
     
    -
     
    diff --git a/tests/verified-files/C128-081231723897.svg b/tests/verified-files/C128-081231723897.svg index 7d5486f..64b544f 100644 --- a/tests/verified-files/C128-081231723897.svg +++ b/tests/verified-files/C128-081231723897.svg @@ -31,7 +31,5 @@ - - diff --git a/tests/verified-files/C128-1234567890abcABC-283*33.svg b/tests/verified-files/C128-1234567890abcABC-283*33.svg index 8184e9b..4a3708a 100644 --- a/tests/verified-files/C128-1234567890abcABC-283*33.svg +++ b/tests/verified-files/C128-1234567890abcABC-283*33.svg @@ -70,7 +70,5 @@ - - diff --git a/tests/verified-files/C128A-1234567890.svg b/tests/verified-files/C128A-1234567890.svg index 0ca2f72..22d2cf4 100644 --- a/tests/verified-files/C128A-1234567890.svg +++ b/tests/verified-files/C128A-1234567890.svg @@ -43,7 +43,5 @@ - - diff --git a/tests/verified-files/C128B-081231723897.svg b/tests/verified-files/C128B-081231723897.svg index 3af5645..f3ee4e0 100644 --- a/tests/verified-files/C128B-081231723897.svg +++ b/tests/verified-files/C128B-081231723897.svg @@ -49,7 +49,5 @@ - - diff --git a/tests/verified-files/C128B-1234567890abcABC-283*33.svg b/tests/verified-files/C128B-1234567890abcABC-283*33.svg index 2a1f71e..ec25033 100644 --- a/tests/verified-files/C128B-1234567890abcABC-283*33.svg +++ b/tests/verified-files/C128B-1234567890abcABC-283*33.svg @@ -82,7 +82,5 @@ - - From aecd72e691617b6ee83ee8b71f33b7003102738d Mon Sep 17 00:00:00 2001 From: Casper Bakker Date: Fri, 3 Jan 2020 13:59:53 +0100 Subject: [PATCH 038/155] Test if barcode string is empty #42 --- src/Types/TypeCode128.php | 4 ++++ src/Types/TypeCode39.php | 5 +++++ src/Types/TypeEanUpcBase.php | 5 +++++ tests/GeneratorTest.php | 16 ++++++++++++++++ 4 files changed, 30 insertions(+) diff --git a/src/Types/TypeCode128.php b/src/Types/TypeCode128.php index 9c79185..4b87af5 100644 --- a/src/Types/TypeCode128.php +++ b/src/Types/TypeCode128.php @@ -19,6 +19,10 @@ class TypeCode128 implements TypeInterface public function getBarcodeData(string $code): array { + if (strlen(trim($code)) === 0) { + throw new InvalidLengthException('You should provide a barcode string.'); + } + $chr = array( '212222', /* 00 */ '222122', /* 01 */ diff --git a/src/Types/TypeCode39.php b/src/Types/TypeCode39.php index 692e0a3..7fa1df4 100644 --- a/src/Types/TypeCode39.php +++ b/src/Types/TypeCode39.php @@ -3,6 +3,7 @@ namespace Picqer\Barcode\Types; use Picqer\Barcode\Exceptions\InvalidCharacterException; +use Picqer\Barcode\Exceptions\InvalidLengthException; /* * CODE 39 - ANSI MH10.8M-1983 - USD-3 - 3 of 9. @@ -16,6 +17,10 @@ class TypeCode39 implements TypeInterface public function getBarcodeData(string $code): array { + if (strlen(trim($code)) === 0) { + throw new InvalidLengthException('You should provide a barcode string.'); + } + $chr = []; $chr['0'] = '111331311'; $chr['1'] = '311311113'; diff --git a/src/Types/TypeEanUpcBase.php b/src/Types/TypeEanUpcBase.php index fff0cac..80713da 100644 --- a/src/Types/TypeEanUpcBase.php +++ b/src/Types/TypeEanUpcBase.php @@ -4,6 +4,7 @@ use Picqer\Barcode\Exceptions\InvalidCharacterException; use Picqer\Barcode\Exceptions\InvalidCheckDigitException; +use Picqer\Barcode\Exceptions\InvalidLengthException; /* * EAN13 and UPC-A barcodes. @@ -23,6 +24,10 @@ abstract class TypeEanUpcBase implements TypeInterface public function getBarcodeData(string $code): array { + if (strlen(trim($code)) === 0) { + throw new InvalidLengthException('You should provide a barcode string.'); + } + $length = $this->length; $dataLength = $length - 1; diff --git a/tests/GeneratorTest.php b/tests/GeneratorTest.php index 17f8d71..293efc0 100644 --- a/tests/GeneratorTest.php +++ b/tests/GeneratorTest.php @@ -4,6 +4,22 @@ class GeneratorTest extends TestCase { + public function test_throws_exception_if_empty_barcode_is_used_in_ean13() + { + $this->expectException(Picqer\Barcode\Exceptions\InvalidLengthException::class); + + $generator = new Picqer\Barcode\BarcodeGeneratorSVG(); + $generator->getBarcode('', $generator::TYPE_EAN_13); + } + + public function test_throws_exception_if_empty_barcode_is_used_in_code128() + { + $this->expectException(Picqer\Barcode\Exceptions\InvalidLengthException::class); + + $generator = new Picqer\Barcode\BarcodeGeneratorSVG(); + $generator->getBarcode('', $generator::TYPE_CODE_128); + } + public function test_ean13_generator_throws_exception_if_invalid_chars_are_used() { $this->expectException(Picqer\Barcode\Exceptions\InvalidCharacterException::class); From 7abe5e7878fcdb1f8f37f341f8578533710756e8 Mon Sep 17 00:00:00 2001 From: Casper Bakker Date: Sat, 4 Jan 2020 16:40:11 +0100 Subject: [PATCH 039/155] Introduce Barcode class --- src/Barcode.php | 43 ++++++++++++++++++++++++ src/BarcodeBar.php | 42 +++++++++++++++++++++++ src/BarcodeGenerator.php | 11 ++---- src/BarcodeGeneratorHTML.php | 15 +++++---- src/BarcodeGeneratorPNG.php | 13 +++---- src/BarcodeGeneratorSVG.php | 15 +++++---- src/Helpers/BinarySequenceConverter.php | 23 +++++++------ src/Helpers/OldBarcodeArrayConverter.php | 28 --------------- src/Types/TypeCodabar.php | 18 +++++----- src/Types/TypeCode11.php | 22 +++++++----- src/Types/TypeCode128.php | 13 ++++--- src/Types/TypeCode39.php | 18 +++++----- src/Types/TypeCode93.php | 22 ++++++------ src/Types/TypeEanUpcBase.php | 18 +++++----- src/Types/TypeIntelligentMailBarcode.php | 18 +++++----- src/Types/TypeInterface.php | 4 ++- src/Types/TypeInterleaved25Checksum.php | 21 ++++++------ src/Types/TypeMsiChecksum.php | 6 ++-- src/Types/TypePharmacode.php | 10 +++--- src/Types/TypePharmacodeTwoCode.php | 27 ++++++++------- src/Types/TypePostnet.php | 31 +++++++++-------- src/Types/TypeRms4cc.php | 31 ++++++++--------- src/Types/TypeStandard2of5.php | 10 +++--- src/Types/TypeUpcExtension2.php | 10 ++++-- 24 files changed, 271 insertions(+), 198 deletions(-) create mode 100644 src/Barcode.php create mode 100644 src/BarcodeBar.php delete mode 100644 src/Helpers/OldBarcodeArrayConverter.php diff --git a/src/Barcode.php b/src/Barcode.php new file mode 100644 index 0000000..4ab9f20 --- /dev/null +++ b/src/Barcode.php @@ -0,0 +1,43 @@ +barcode = $barcode; + } + + public function addBar(BarcodeBar $bar) + { + $this->bars[] = $bar; + $this->width += $bar->getWidth(); + $this->height = max($this->height, $bar->getHeight()); + } + + public function getBarcode(): string + { + return $this->barcode; + } + + public function getWidth(): int + { + return $this->width; + } + + public function getHeight(): int + { + return $this->height; + } + + public function getBars(): array + { + return $this->bars; + } +} \ No newline at end of file diff --git a/src/BarcodeBar.php b/src/BarcodeBar.php new file mode 100644 index 0000000..332a661 --- /dev/null +++ b/src/BarcodeBar.php @@ -0,0 +1,42 @@ +width = $width; + $this->height = $height; + $this->positionVertical = $positionVertical; + $this->type = $drawBar ? self::TYPE_BAR : self::TYPE_SPACING; + } + + public function getWidth(): int + { + return $this->width; + } + + public function getHeight(): int + { + return $this->height; + } + + public function getPositionVertical(): int + { + return $this->positionVertical; + } + + public function isBar(): bool + { + return $this->type === self::TYPE_BAR; + } +} \ No newline at end of file diff --git a/src/BarcodeGenerator.php b/src/BarcodeGenerator.php index c7aa072..8a57e1e 100644 --- a/src/BarcodeGenerator.php +++ b/src/BarcodeGenerator.php @@ -30,7 +30,6 @@ namespace Picqer\Barcode; use Picqer\Barcode\Exceptions\UnknownTypeException; -use Picqer\Barcode\Helpers\OldBarcodeArrayConverter; use Picqer\Barcode\Types\TypeCodabar; use Picqer\Barcode\Types\TypeCode11; use Picqer\Barcode\Types\TypeCode128; @@ -95,17 +94,11 @@ abstract class BarcodeGenerator const TYPE_PHARMA_CODE = 'PHARMA'; const TYPE_PHARMA_CODE_TWO_TRACKS = 'PHARMA2T'; - protected function getBarcodeData(string $code, string $type): array + protected function getBarcodeData(string $code, string $type): Barcode { $barcodeDataBuilder = $this->createDataBuilderForType($type); - $barcodeData = $barcodeDataBuilder->getBarcodeData($code); - - if (! isset($barcodeData['maxWidth'])) { - return OldBarcodeArrayConverter::convert($barcodeData); - } - - return $barcodeData; + return $barcodeDataBuilder->getBarcodeData($code); } protected function createDataBuilderForType(string $type) diff --git a/src/BarcodeGeneratorHTML.php b/src/BarcodeGeneratorHTML.php index 499bf99..86d87da 100644 --- a/src/BarcodeGeneratorHTML.php +++ b/src/BarcodeGeneratorHTML.php @@ -18,15 +18,16 @@ public function getBarcode($barcode, $type, int $widthFactor = 2, int $height = { $barcodeData = $this->getBarcodeData($barcode, $type); - $html = '
    ' . PHP_EOL; + $html = '
    ' . PHP_EOL; $positionHorizontal = 0; - foreach ($barcodeData['bars'] as $bar) { - $barWidth = round(($bar['width'] * $widthFactor), 3); - $barHeight = round(($bar['height'] * $height / $barcodeData['maxHeight']), 3); + /** @var BarcodeBar $bar */ + foreach ($barcodeData->getBars() as $bar) { + $barWidth = round(($bar->getWidth() * $widthFactor), 3); + $barHeight = round(($bar->getHeight() * $height / $barcodeData->getHeight()), 3); - if ($bar['drawBar'] && $barWidth > 0) { - $positionVertical = round(($bar['positionVertical'] * $height / $barcodeData['maxHeight']), 3); + if ($bar->isBar() && $barWidth > 0) { + $positionVertical = round(($bar->getPositionVertical() * $height / $barcodeData->getHeight()), 3); // draw a vertical bar $html .= '
     
    ' . PHP_EOL; } @@ -38,4 +39,4 @@ public function getBarcode($barcode, $type, int $widthFactor = 2, int $height = return $html; } -} \ No newline at end of file +} diff --git a/src/BarcodeGeneratorPNG.php b/src/BarcodeGeneratorPNG.php index 0becde1..14b3396 100644 --- a/src/BarcodeGeneratorPNG.php +++ b/src/BarcodeGeneratorPNG.php @@ -52,7 +52,7 @@ public function useGd() public function getBarcode($barcode, $type, int $widthFactor = 2, int $height = 30, array $foregroundColor = [0, 0, 0]) { $barcodeData = $this->getBarcodeData($barcode, $type); - $width = round($barcodeData['maxWidth'] * $widthFactor); + $width = round($barcodeData->getWidth() * $widthFactor); if ($this->useImagick) { $imagickBarsShape = new imagickdraw(); @@ -64,12 +64,13 @@ public function getBarcode($barcode, $type, int $widthFactor = 2, int $height = // print bars $positionHorizontal = 0; - foreach ($barcodeData['bars'] as $bar) { - $barWidth = round(($bar['width'] * $widthFactor), 3); + /** @var BarcodeBar $bar */ + foreach ($barcodeData->getBars() as $bar) { + $barWidth = round(($bar->getWidth() * $widthFactor), 3); - if ($bar['drawBar'] && $barWidth > 0) { - $y = round(($bar['positionVertical'] * $height / $barcodeData['maxHeight']), 3); - $barHeight = round(($bar['height'] * $height / $barcodeData['maxHeight']), 3); + if ($bar->isBar() && $barWidth > 0) { + $y = round(($bar->getPositionVertical() * $height / $barcodeData->getHeight()), 3); + $barHeight = round(($bar->getHeight() * $height / $barcodeData->getHeight()), 3); // draw a vertical bar if ($this->useImagick && isset($imagickBarsShape)) { diff --git a/src/BarcodeGeneratorSVG.php b/src/BarcodeGeneratorSVG.php index e419e04..11652ea 100644 --- a/src/BarcodeGeneratorSVG.php +++ b/src/BarcodeGeneratorSVG.php @@ -22,22 +22,23 @@ public function getBarcode($barcode, $type, int $widthFactor = 2, int $height = // replace table for special characters $repstr = ["\0" => '', '&' => '&', '<' => '<', '>' => '>']; - $width = round(($barcodeData['maxWidth'] * $widthFactor), 3); + $width = round(($barcodeData->getWidth() * $widthFactor), 3); $svg = '' . PHP_EOL; $svg .= '' . PHP_EOL; $svg .= '' . PHP_EOL; - $svg .= "\t" . '' . strtr($barcodeData['code'], $repstr) . '' . PHP_EOL; + $svg .= "\t" . '' . strtr($barcodeData->getBarcode(), $repstr) . '' . PHP_EOL; $svg .= "\t" . '' . PHP_EOL; // print bars $positionHorizontal = 0; - foreach ($barcodeData['bars'] as $bar) { - $barWidth = round(($bar['width'] * $widthFactor), 3); - $barHeight = round(($bar['height'] * $height / $barcodeData['maxHeight']), 3); + /** @var BarcodeBar $bar */ + foreach ($barcodeData->getBars() as $bar) { + $barWidth = round(($bar->getWidth() * $widthFactor), 3); + $barHeight = round(($bar->getHeight() * $height / $barcodeData->getHeight()), 3); - if ($bar['drawBar'] && $barWidth > 0) { - $positionVertical = round(($bar['positionVertical'] * $height / $barcodeData['maxHeight']), 3); + if ($bar->isBar() && $barWidth > 0) { + $positionVertical = round(($bar->getPositionVertical() * $height / $barcodeData->getHeight()), 3); // draw a vertical bar $svg .= "\t\t" . '' . PHP_EOL; } diff --git a/src/Helpers/BinarySequenceConverter.php b/src/Helpers/BinarySequenceConverter.php index 30d98ab..a9d5d4f 100644 --- a/src/Helpers/BinarySequenceConverter.php +++ b/src/Helpers/BinarySequenceConverter.php @@ -2,31 +2,34 @@ namespace Picqer\Barcode\Helpers; +use Picqer\Barcode\Barcode; +use Picqer\Barcode\BarcodeBar; + /** - * Convert binary barcode sequence to barcode array. + * Convert binary barcode sequence string to Barcode representation. */ class BinarySequenceConverter { - public static function convert(string $seq, array $bararray): array + public static function convert(string $code, string $sequence): Barcode { - $len = strlen($seq); + $barcode = new Barcode($code); + + $len = strlen($sequence); $w = 0; - $k = 0; for ($i = 0; $i < $len; ++$i) { $w += 1; - if (($i == ($len - 1)) OR (($i < ($len - 1)) AND ($seq[$i] != $seq[($i + 1)]))) { - if ($seq[$i] == '1') { + if (($i == ($len - 1)) OR (($i < ($len - 1)) AND ($sequence[$i] != $sequence[($i + 1)]))) { + if ($sequence[$i] == '1') { $t = true; // bar } else { $t = false; // space } - $bararray['bcode'][$k] = array('t' => $t, 'w' => $w, 'h' => 1, 'p' => 0); - $bararray['maxw'] += $w; - ++$k; + + $barcode->addBar(new BarcodeBar($w, 1, $t)); $w = 0; } } - return $bararray; + return $barcode; } } diff --git a/src/Helpers/OldBarcodeArrayConverter.php b/src/Helpers/OldBarcodeArrayConverter.php deleted file mode 100644 index 5c8a2ac..0000000 --- a/src/Helpers/OldBarcodeArrayConverter.php +++ /dev/null @@ -1,28 +0,0 @@ - '11111221', @@ -35,16 +37,16 @@ public function getBarcodeData(string $code): array 'C' => '11121221', 'D' => '11122211' ); - $bararray = array('code' => $code, 'maxw' => 0, 'maxh' => 1, 'bcode' => array()); - $k = 0; - $w = 0; - $seq = ''; + + $barcode = new Barcode($code); + $code = 'A' . strtoupper($code) . 'A'; $len = strlen($code); for ($i = 0; $i < $len; ++$i) { if (! isset($chr[$code[$i]])) { throw new InvalidCharacterException('Char ' . $code[$i] . ' is unsupported'); } + $seq = $chr[$code[$i]]; for ($j = 0; $j < 8; ++$j) { if (($j % 2) == 0) { @@ -53,12 +55,10 @@ public function getBarcodeData(string $code): array $t = false; // space } $w = $seq[$j]; - $bararray['bcode'][$k] = array('t' => $t, 'w' => $w, 'h' => 1, 'p' => 0); - $bararray['maxw'] += $w; - ++$k; + $barcode->addBar(new BarcodeBar($w, 1, $t)); } } - return $bararray; + return $barcode; } } diff --git a/src/Types/TypeCode11.php b/src/Types/TypeCode11.php index 34a7fda..7193ee0 100644 --- a/src/Types/TypeCode11.php +++ b/src/Types/TypeCode11.php @@ -2,6 +2,8 @@ namespace Picqer\Barcode\Types; +use Picqer\Barcode\Barcode; +use Picqer\Barcode\BarcodeBar; use Picqer\Barcode\Exceptions\InvalidCharacterException; /* @@ -11,7 +13,7 @@ class TypeCode11 implements TypeInterface { - public function getBarcodeData(string $code): array + public function getBarcodeData(string $code): Barcode { $chr = array( '0' => '111121', @@ -27,10 +29,9 @@ public function getBarcodeData(string $code): array '-' => '112111', 'S' => '112211' ); - $bararray = array('code' => $code, 'maxw' => 0, 'maxh' => 1, 'bcode' => array()); - $k = 0; - $w = 0; - $seq = ''; + + $barcode = new Barcode($code); + $len = strlen($code); // calculate check digit C $p = 1; @@ -53,6 +54,7 @@ public function getBarcodeData(string $code): array $check = '-'; } $code .= $check; + if ($len > 10) { // calculate check digit K $p = 1; @@ -74,12 +76,15 @@ public function getBarcodeData(string $code): array $code .= $check; ++$len; } + $code = 'S' . $code . 'S'; $len += 3; + for ($i = 0; $i < $len; ++$i) { if (! isset($chr[$code[$i]])) { throw new InvalidCharacterException('Char ' . $code[$i] . ' is unsupported'); } + $seq = $chr[$code[$i]]; for ($j = 0; $j < 6; ++$j) { if (($j % 2) == 0) { @@ -88,12 +93,11 @@ public function getBarcodeData(string $code): array $t = false; // space } $w = $seq[$j]; - $bararray['bcode'][$k] = array('t' => $t, 'w' => $w, 'h' => 1, 'p' => 0); - $bararray['maxw'] += $w; - ++$k; + + $barcode->addBar(new BarcodeBar($w, 1, $t)); } } - return $bararray; + return $barcode; } } diff --git a/src/Types/TypeCode128.php b/src/Types/TypeCode128.php index 4b87af5..28e95f5 100644 --- a/src/Types/TypeCode128.php +++ b/src/Types/TypeCode128.php @@ -2,6 +2,8 @@ namespace Picqer\Barcode\Types; +use Picqer\Barcode\Barcode; +use Picqer\Barcode\BarcodeBar; use Picqer\Barcode\Exceptions\InvalidCharacterException; use Picqer\Barcode\Exceptions\InvalidLengthException; @@ -17,7 +19,7 @@ class TypeCode128 implements TypeInterface { protected $type = null; - public function getBarcodeData(string $code): array + public function getBarcodeData(string $code): Barcode { if (strlen(trim($code)) === 0) { throw new InvalidLengthException('You should provide a barcode string.'); @@ -345,8 +347,9 @@ public function getBarcodeData(string $code): array $code_data[] = 107; // add start code at the beginning array_unshift($code_data, $startid); + // build barcode array - $bararray = array('code' => $code, 'maxw' => 0, 'maxh' => 1, 'bcode' => array()); + $barcode = new Barcode($code); foreach ($code_data as $val) { $seq = $chr[$val]; for ($j = 0; $j < 6; ++$j) { @@ -356,12 +359,12 @@ public function getBarcodeData(string $code): array $t = false; // space } $w = $seq[$j]; - $bararray['bcode'][] = array('t' => $t, 'w' => $w, 'h' => 1, 'p' => 0); - $bararray['maxw'] += $w; + + $barcode->addBar(new BarcodeBar($w, 1, $t)); } } - return $bararray; + return $barcode; } diff --git a/src/Types/TypeCode39.php b/src/Types/TypeCode39.php index 7fa1df4..926921f 100644 --- a/src/Types/TypeCode39.php +++ b/src/Types/TypeCode39.php @@ -2,6 +2,8 @@ namespace Picqer\Barcode\Types; +use Picqer\Barcode\Barcode; +use Picqer\Barcode\BarcodeBar; use Picqer\Barcode\Exceptions\InvalidCharacterException; use Picqer\Barcode\Exceptions\InvalidLengthException; @@ -15,7 +17,7 @@ class TypeCode39 implements TypeInterface protected $extended = false; protected $checksum = false; - public function getBarcodeData(string $code): array + public function getBarcodeData(string $code): Barcode { if (strlen(trim($code)) === 0) { throw new InvalidLengthException('You should provide a barcode string.'); @@ -82,8 +84,8 @@ public function getBarcodeData(string $code): array // add start and stop codes $code = '*' . $code . '*'; - $bararray = array('code' => $code, 'maxw' => 0, 'maxh' => 1, 'bcode' => array()); - $k = 0; + $barcode = new Barcode($code); + $clen = strlen($code); for ($i = 0; $i < $clen; ++$i) { $char = $code[$i]; @@ -97,17 +99,13 @@ public function getBarcodeData(string $code): array $t = false; // space } $w = $chr[$char][$j]; - $bararray['bcode'][$k] = array('t' => $t, 'w' => $w, 'h' => 1, 'p' => 0); - $bararray['maxw'] += $w; - ++$k; + $barcode->addBar(new BarcodeBar($w, 1, $t)); } // intercharacter gap - $bararray['bcode'][$k] = array('t' => false, 'w' => 1, 'h' => 1, 'p' => 0); - $bararray['maxw'] += 1; - ++$k; + $barcode->addBar(new BarcodeBar(1, 1, false)); } - return $bararray; + return $barcode; } diff --git a/src/Types/TypeCode93.php b/src/Types/TypeCode93.php index 7d8770b..11fdc39 100644 --- a/src/Types/TypeCode93.php +++ b/src/Types/TypeCode93.php @@ -2,6 +2,8 @@ namespace Picqer\Barcode\Types; +use Picqer\Barcode\Barcode; +use Picqer\Barcode\BarcodeBar; use Picqer\Barcode\Exceptions\InvalidCharacterException; /* @@ -11,7 +13,7 @@ class TypeCode93 implements TypeInterface { - public function getBarcodeData(string $code): array + public function getBarcodeData(string $code): Barcode { $chr = []; $chr[48] = '131112'; // 0 @@ -211,14 +213,14 @@ public function getBarcodeData(string $code): array // add start and stop codes $code = '*' . $code_ext . '*'; - $bararray = array('code' => $code, 'maxw' => 0, 'maxh' => 1, 'bcode' => array()); - $k = 0; - $clen = strlen($code); - for ($i = 0; $i < $clen; ++$i) { + $barcode = new Barcode($code); + + for ($i = 0; $i < strlen($code); ++$i) { $char = ord($code[$i]); if (! isset($chr[$char])) { throw new InvalidCharacterException('Char ' . $char . ' is unsupported'); } + for ($j = 0; $j < 6; ++$j) { if (($j % 2) == 0) { $t = true; // bar @@ -226,16 +228,14 @@ public function getBarcodeData(string $code): array $t = false; // space } $w = $chr[$char][$j]; - $bararray['bcode'][$k] = array('t' => $t, 'w' => $w, 'h' => 1, 'p' => 0); - $bararray['maxw'] += $w; - ++$k; + + $barcode->addBar(new BarcodeBar($w, 1, $t)); } } - $bararray['bcode'][$k] = array('t' => true, 'w' => 1, 'h' => 1, 'p' => 0); - $bararray['maxw'] += 1; + $barcode->addBar(new BarcodeBar(1, 1, true)); - return $bararray; + return $barcode; } /** diff --git a/src/Types/TypeEanUpcBase.php b/src/Types/TypeEanUpcBase.php index 80713da..2ad218e 100644 --- a/src/Types/TypeEanUpcBase.php +++ b/src/Types/TypeEanUpcBase.php @@ -2,6 +2,8 @@ namespace Picqer\Barcode\Types; +use Picqer\Barcode\Barcode; +use Picqer\Barcode\BarcodeBar; use Picqer\Barcode\Exceptions\InvalidCharacterException; use Picqer\Barcode\Exceptions\InvalidCheckDigitException; use Picqer\Barcode\Exceptions\InvalidLengthException; @@ -22,7 +24,7 @@ abstract class TypeEanUpcBase implements TypeInterface protected $upca = false; protected $upce = false; - public function getBarcodeData(string $code): array + public function getBarcodeData(string $code): Barcode { if (strlen(trim($code)) === 0) { throw new InvalidLengthException('You should provide a barcode string.'); @@ -49,7 +51,7 @@ public function getBarcodeData(string $code): array $code = '0' . $code; ++$length; } - + if ($this->upce) { // convert UPC-A to UPC-E $tmp = substr($code, 4, 3); @@ -154,17 +156,16 @@ public function getBarcodeData(string $code): array ], ]; - $k = 0; $seq = '101'; // left guard bar if ($this->upce) { - $bararray = ['code' => $upce_code, 'maxw' => 0, 'maxh' => 1, 'bcode' => []]; + $barcode = new Barcode($upce_code); $p = $upce_parities[$code[1]][$checksumDigit]; for ($i = 0; $i < 6; ++$i) { $seq .= $codes[$p[$i]][$upce_code[$i]]; } $seq .= '010101'; // right guard bar } else { - $bararray = ['code' => $code, 'maxw' => 0, 'maxh' => 1, 'bcode' => []]; + $barcode = new Barcode($code); $half_len = intval(ceil($length / 2)); if ($length == 8) { for ($i = 0; $i < $half_len; ++$i) { @@ -196,14 +197,13 @@ public function getBarcodeData(string $code): array } else { $t = false; // space } - $bararray['bcode'][$k] = ['t' => $t, 'w' => $w, 'h' => 1, 'p' => 0]; - $bararray['maxw'] += $w; - ++$k; + + $barcode->addBar(new BarcodeBar($w, 1, $t)); $w = 0; } } - return $bararray; + return $barcode; } protected function calculateChecksumDigit(string $code) diff --git a/src/Types/TypeIntelligentMailBarcode.php b/src/Types/TypeIntelligentMailBarcode.php index 23e2fde..b21710b 100644 --- a/src/Types/TypeIntelligentMailBarcode.php +++ b/src/Types/TypeIntelligentMailBarcode.php @@ -2,6 +2,8 @@ namespace Picqer\Barcode\Types; +use Picqer\Barcode\Barcode; +use Picqer\Barcode\BarcodeBar; use Picqer\Barcode\Exceptions\BarcodeException; /* @@ -32,7 +34,7 @@ class TypeIntelligentMailBarcode implements TypeInterface { - public function getBarcodeData(string $code): array + public function getBarcodeData(string $code): Barcode { $asc_chr = array( 4, @@ -392,8 +394,7 @@ public function getBarcodeData(string $code): array $characters = array_reverse($characters); // build bars - $k = 0; - $bararray = array('code' => $code, 'maxw' => 0, 'maxh' => 3, 'bcode' => array()); + $barcode = new Barcode($code); for ($i = 0; $i < 65; ++$i) { $asc = (($characters[$asc_chr[$i]] & pow(2, $asc_pos[$i])) > 0); $dsc = (($characters[$dsc_chr[$i]] & pow(2, $dsc_pos[$i])) > 0); @@ -414,14 +415,13 @@ public function getBarcodeData(string $code): array $p = 1; $h = 1; } - $bararray['bcode'][$k++] = array('t' => 1, 'w' => 1, 'h' => $h, 'p' => $p); - $bararray['bcode'][$k++] = array('t' => 0, 'w' => 1, 'h' => 2, 'p' => 0); - $bararray['maxw'] += 2; + $barcode->addBar(new BarcodeBar(1, $h, true, $p)); + if ($i < 64) { + $barcode->addBar(new BarcodeBar(1, 2, false, 0)); + } } - unset($bararray['bcode'][($k - 1)]); - --$bararray['maxw']; - return $bararray; + return $barcode; } /** diff --git a/src/Types/TypeInterface.php b/src/Types/TypeInterface.php index 68faf31..f380c38 100644 --- a/src/Types/TypeInterface.php +++ b/src/Types/TypeInterface.php @@ -2,7 +2,9 @@ namespace Picqer\Barcode\Types; +use Picqer\Barcode\Barcode; + interface TypeInterface { - public function getBarcodeData(string $code): array; + public function getBarcodeData(string $code): Barcode; } diff --git a/src/Types/TypeInterleaved25Checksum.php b/src/Types/TypeInterleaved25Checksum.php index 8cb6059..127efc0 100644 --- a/src/Types/TypeInterleaved25Checksum.php +++ b/src/Types/TypeInterleaved25Checksum.php @@ -2,6 +2,8 @@ namespace Picqer\Barcode\Types; +use Picqer\Barcode\Barcode; +use Picqer\Barcode\BarcodeBar; use Picqer\Barcode\Exceptions\InvalidCharacterException; /* @@ -12,7 +14,7 @@ class TypeInterleaved25Checksum implements TypeInterface { - public function getBarcodeData(string $code): array + public function getBarcodeData(string $code): Barcode { $chr['0'] = '11221'; $chr['1'] = '21112'; @@ -37,36 +39,33 @@ public function getBarcodeData(string $code): array // add start and stop codes $code = 'AA' . strtolower($code) . 'ZA'; - $bararray = array('code' => $code, 'maxw' => 0, 'maxh' => 1, 'bcode' => array()); - $k = 0; - $clen = strlen($code); - for ($i = 0; $i < $clen; $i = ($i + 2)) { + $barcode = new Barcode($code); + for ($i = 0; $i < strlen($code); $i = ($i + 2)) { $char_bar = $code[$i]; $char_space = $code[$i + 1]; if (! isset($chr[$char_bar]) || ! isset($chr[$char_space])) { throw new InvalidCharacterException(); } + // create a bar-space sequence $seq = ''; $chrlen = strlen($chr[$char_bar]); for ($s = 0; $s < $chrlen; $s++) { $seq .= $chr[$char_bar][$s] . $chr[$char_space][$s]; } - $seqlen = strlen($seq); - for ($j = 0; $j < $seqlen; ++$j) { + + for ($j = 0; $j < strlen($seq); ++$j) { if (($j % 2) == 0) { $t = true; // bar } else { $t = false; // space } $w = $seq[$j]; - $bararray['bcode'][$k] = array('t' => $t, 'w' => $w, 'h' => 1, 'p' => 0); - $bararray['maxw'] += $w; - ++$k; + $barcode->addBar(new BarcodeBar($w, 1, $t)); } } - return $bararray; + return $barcode; } protected function getChecksum(string $code): string diff --git a/src/Types/TypeMsiChecksum.php b/src/Types/TypeMsiChecksum.php index 51a008f..cb3a0ff 100644 --- a/src/Types/TypeMsiChecksum.php +++ b/src/Types/TypeMsiChecksum.php @@ -2,6 +2,7 @@ namespace Picqer\Barcode\Types; +use Picqer\Barcode\Barcode; use Picqer\Barcode\Exceptions\InvalidCharacterException; use Picqer\Barcode\Helpers\BinarySequenceConverter; @@ -18,7 +19,7 @@ class TypeMsiChecksum implements TypeInterface { protected $checksum = true; - public function getBarcodeData(string $code): array + public function getBarcodeData(string $code): Barcode { $chr['0'] = '100100100100'; $chr['1'] = '100100100110'; @@ -64,8 +65,7 @@ public function getBarcodeData(string $code): array $seq .= $chr[$digit]; } $seq .= '1001'; // right guard - $bararray = array('code' => $code, 'maxw' => 0, 'maxh' => 1, 'bcode' => array()); - return BinarySequenceConverter::convert($seq, $bararray); + return BinarySequenceConverter::convert($code, $seq); } } diff --git a/src/Types/TypePharmacode.php b/src/Types/TypePharmacode.php index 9c6896c..5f1f128 100644 --- a/src/Types/TypePharmacode.php +++ b/src/Types/TypePharmacode.php @@ -2,6 +2,7 @@ namespace Picqer\Barcode\Types; +use Picqer\Barcode\Barcode; use Picqer\Barcode\Helpers\BinarySequenceConverter; /* @@ -11,10 +12,11 @@ class TypePharmacode implements TypeInterface { - public function getBarcodeData(string $code): array + public function getBarcodeData(string $code): Barcode { - $seq = ''; $code = intval($code); + + $seq = ''; while ($code > 0) { if (($code % 2) == 0) { $seq .= '11100'; @@ -25,10 +27,10 @@ public function getBarcodeData(string $code): array } $code /= 2; } + $seq = substr($seq, 0, -2); $seq = strrev($seq); - $bararray = array('code' => $code, 'maxw' => 0, 'maxh' => 1, 'bcode' => array()); - return BinarySequenceConverter::convert($seq, $bararray); + return BinarySequenceConverter::convert($code, $seq); } } diff --git a/src/Types/TypePharmacodeTwoCode.php b/src/Types/TypePharmacodeTwoCode.php index bc20360..dca3811 100644 --- a/src/Types/TypePharmacodeTwoCode.php +++ b/src/Types/TypePharmacodeTwoCode.php @@ -7,13 +7,17 @@ * Contains digits (0 to 9) */ +use Picqer\Barcode\Barcode; +use Picqer\Barcode\BarcodeBar; + class TypePharmacodeTwoCode implements TypeInterface { - public function getBarcodeData(string $code): array + public function getBarcodeData(string $code): Barcode { - $seq = ''; $code = intval($code); + $seq = ''; + do { switch ($code % 3) { case 0: @@ -34,11 +38,10 @@ public function getBarcodeData(string $code): array } while ($code != 0); $seq = strrev($seq); - $k = 0; - $bararray = array('code' => $code, 'maxw' => 0, 'maxh' => 2, 'bcode' => array()); - $len = strlen($seq); - for ($i = 0; $i < $len; ++$i) { + $barcode = new Barcode($code); + + for ($i = 0; $i < strlen($seq); ++$i) { switch ($seq[$i]) { case '1': $p = 1; @@ -56,14 +59,12 @@ public function getBarcodeData(string $code): array break; } - $bararray['bcode'][$k++] = array('t' => 1, 'w' => 1, 'h' => $h, 'p' => $p); - $bararray['bcode'][$k++] = array('t' => 0, 'w' => 1, 'h' => 2, 'p' => 0); - $bararray['maxw'] += 2; + $barcode->addBar(new BarcodeBar(1, $h, 1, $p)); + if ($i < (strlen($seq) - 1)) { + $barcode->addBar(new BarcodeBar(1, 2, 0, 0)); + } } - unset($bararray['bcode'][($k - 1)]); - --$bararray['maxw']; - - return $bararray; + return $barcode; } } diff --git a/src/Types/TypePostnet.php b/src/Types/TypePostnet.php index 0736d18..e52d855 100644 --- a/src/Types/TypePostnet.php +++ b/src/Types/TypePostnet.php @@ -11,6 +11,9 @@ * @param $planet (boolean) if true print the PLANET barcode, otherwise print POSTNET */ +use Picqer\Barcode\Barcode; +use Picqer\Barcode\BarcodeBar; + class TypePostnet implements TypeInterface { protected $barlen = Array( @@ -26,13 +29,13 @@ class TypePostnet implements TypeInterface 9 => Array(2, 1, 2, 1, 1) ); - public function getBarcodeData(string $code): array + public function getBarcodeData(string $code): Barcode { - $bararray = array('code' => $code, 'maxw' => 0, 'maxh' => 2, 'bcode' => array()); - $k = 0; - $code = str_replace('-', '', $code); - $code = str_replace(' ', '', $code); + $code = str_replace(['-', ' '], '', $code); $len = strlen($code); + + $barcode = new Barcode($code); + // calculate checksum $sum = 0; for ($i = 0; $i < $len; ++$i) { @@ -44,23 +47,23 @@ public function getBarcodeData(string $code): array } $code .= $chkd; $len = strlen($code); + // start bar - $bararray['bcode'][$k++] = array('t' => 1, 'w' => 1, 'h' => 2, 'p' => 0); - $bararray['bcode'][$k++] = array('t' => 0, 'w' => 1, 'h' => 2, 'p' => 0); - $bararray['maxw'] += 2; + $barcode->addBar(new BarcodeBar(1, 2, 1)); + $barcode->addBar(new BarcodeBar(1, 2, 0)); + for ($i = 0; $i < $len; ++$i) { for ($j = 0; $j < 5; ++$j) { $h = $this->barlen[$code[$i]][$j]; $p = floor(1 / $h); - $bararray['bcode'][$k++] = array('t' => 1, 'w' => 1, 'h' => $h, 'p' => $p); - $bararray['bcode'][$k++] = array('t' => 0, 'w' => 1, 'h' => 2, 'p' => 0); - $bararray['maxw'] += 2; + $barcode->addBar(new BarcodeBar(1, $h, 1, $p)); + $barcode->addBar(new BarcodeBar(1, 2, 0)); } } + // end bar - $bararray['bcode'][$k++] = array('t' => 1, 'w' => 1, 'h' => 2, 'p' => 0); - $bararray['maxw'] += 1; + $barcode->addBar(new BarcodeBar(1, 2, 1)); - return $bararray; + return $barcode; } } diff --git a/src/Types/TypeRms4cc.php b/src/Types/TypeRms4cc.php index aa0007d..f5ee1a3 100644 --- a/src/Types/TypeRms4cc.php +++ b/src/Types/TypeRms4cc.php @@ -10,13 +10,17 @@ * - in this case the house number must be sufficed with an X and placed at the end of the code. */ +use Picqer\Barcode\Barcode; +use Picqer\Barcode\BarcodeBar; + class TypeRms4cc implements TypeInterface { protected $kix = false; - public function getBarcodeData(string $code): array + public function getBarcodeData(string $code): Barcode { $notkix = ! $this->kix; + // bar mode // 1 = pos 1, length 2 // 2 = pos 1, length 3 @@ -60,9 +64,12 @@ public function getBarcodeData(string $code): array 'Y' => array(2, 1, 4, 3), 'Z' => array(2, 2, 3, 3) ); + $code = strtoupper($code); $len = strlen($code); - $bararray = array('code' => $code, 'maxw' => 0, 'maxh' => 3, 'bcode' => array()); + + $barcode = new Barcode($code); + if ($notkix) { // table for checksum calculation (row,col) $checktable = array( @@ -114,14 +121,10 @@ public function getBarcodeData(string $code): array $chk = array_keys($checktable, array($row, $col)); $code .= $chk[0]; ++$len; - } - $k = 0; - - if ($notkix) { + // start bar - $bararray['bcode'][$k++] = array('t' => 1, 'w' => 1, 'h' => 2, 'p' => 0); - $bararray['bcode'][$k++] = array('t' => 0, 'w' => 1, 'h' => 2, 'p' => 0); - $bararray['maxw'] += 2; + $barcode->addBar(new BarcodeBar(1, 2, 1)); + $barcode->addBar(new BarcodeBar(1, 2, 0)); } for ($i = 0; $i < $len; ++$i) { @@ -148,18 +151,16 @@ public function getBarcodeData(string $code): array break; } - $bararray['bcode'][$k++] = array('t' => 1, 'w' => 1, 'h' => $h, 'p' => $p); - $bararray['bcode'][$k++] = array('t' => 0, 'w' => 1, 'h' => 2, 'p' => 0); - $bararray['maxw'] += 2; + $barcode->addBar(new BarcodeBar(1, $h, 1, $p)); + $barcode->addBar(new BarcodeBar(1, 2, 0)); } } if ($notkix) { // stop bar - $bararray['bcode'][$k++] = array('t' => 1, 'w' => 1, 'h' => 3, 'p' => 0); - $bararray['maxw'] += 1; + $barcode->addBar(new BarcodeBar(1, 3, 1)); } - return $bararray; + return $barcode; } } diff --git a/src/Types/TypeStandard2of5.php b/src/Types/TypeStandard2of5.php index c77c72e..59a5958 100644 --- a/src/Types/TypeStandard2of5.php +++ b/src/Types/TypeStandard2of5.php @@ -2,6 +2,7 @@ namespace Picqer\Barcode\Types; +use Picqer\Barcode\Barcode; use Picqer\Barcode\Exceptions\InvalidCharacterException; use Picqer\Barcode\Helpers\BinarySequenceConverter; @@ -15,7 +16,7 @@ class TypeStandard2of5 implements TypeInterface { protected $checksum = false; - public function getBarcodeData(string $code): array + public function getBarcodeData(string $code): Barcode { $chr['0'] = '10101110111010'; $chr['1'] = '11101010101110'; @@ -36,8 +37,8 @@ public function getBarcodeData(string $code): array $code = '0' . $code; } $seq = '11011010'; - $clen = strlen($code); - for ($i = 0; $i < $clen; ++$i) { + + for ($i = 0; $i < strlen($code); ++$i) { $digit = $code[$i]; if (! isset($chr[$digit])) { throw new InvalidCharacterException('Char ' . $digit . ' is unsupported'); @@ -45,9 +46,8 @@ public function getBarcodeData(string $code): array $seq .= $chr[$digit]; } $seq .= '1101011'; - $bararray = array('code' => $code, 'maxw' => 0, 'maxh' => 1, 'bcode' => array()); - return BinarySequenceConverter::convert($seq, $bararray); + return BinarySequenceConverter::convert($code, $seq); } /** diff --git a/src/Types/TypeUpcExtension2.php b/src/Types/TypeUpcExtension2.php index 8a3111b..56e40f3 100644 --- a/src/Types/TypeUpcExtension2.php +++ b/src/Types/TypeUpcExtension2.php @@ -2,6 +2,7 @@ namespace Picqer\Barcode\Types; +use Picqer\Barcode\Barcode; use Picqer\Barcode\Exceptions\InvalidCheckDigitException; use Picqer\Barcode\Helpers\BinarySequenceConverter; @@ -15,12 +16,13 @@ class TypeUpcExtension2 implements TypeInterface { protected $length = 2; - public function getBarcodeData(string $code): array + public function getBarcodeData(string $code): Barcode { $len = $this->length; //Padding $code = str_pad($code, $len, '0', STR_PAD_LEFT); + // calculate check digit if ($len == 2) { $r = $code % 4; @@ -30,6 +32,7 @@ public function getBarcodeData(string $code): array } else { throw new InvalidCheckDigitException(); } + //Convert digits to bars $codes = array( 'A' => array( // left odd parity @@ -57,6 +60,7 @@ public function getBarcodeData(string $code): array '9' => '0010111' ) ); + $parities = array(); $parities[2] = array( '0' => array('A', 'A'), @@ -76,6 +80,7 @@ public function getBarcodeData(string $code): array '8' => array('A', 'B', 'A', 'A', 'B'), '9' => array('A', 'A', 'B', 'A', 'B') ); + $p = $parities[$len][$r]; $seq = '1011'; // left guard bar $seq .= $codes[$p[0]][$code[0]]; @@ -83,8 +88,7 @@ public function getBarcodeData(string $code): array $seq .= '01'; // separator $seq .= $codes[$p[$i]][$code[$i]]; } - $bararray = array('code' => $code, 'maxw' => 0, 'maxh' => 1, 'bcode' => array()); - return BinarySequenceConverter::convert($seq, $bararray); + return BinarySequenceConverter::convert($code, $seq); } } From 77157d2bcf6b96af6cc907b53f25a7de7413959d Mon Sep 17 00:00:00 2001 From: Casper Bakker Date: Sat, 4 Jan 2020 17:28:26 +0100 Subject: [PATCH 040/155] Small cleanups --- generate-examples.php | 2 +- generate-verified-files.php | 2 +- src/Types/TypeCodabar.php | 54 ++--- src/Types/TypeCode11.php | 124 +++++------ src/Types/TypeCode128.php | 250 +++++++++++------------ src/Types/TypeCode39.php | 121 +++++------ src/Types/TypeCode93.php | 155 +++++--------- src/Types/TypeIntelligentMailBarcode.php | 22 +- src/Types/TypePlanet.php | 24 +-- src/Types/TypePostnet.php | 24 +-- src/Types/TypeRms4cc.php | 161 ++++++++------- src/Types/TypeUpcExtension2.php | 51 ++--- 12 files changed, 477 insertions(+), 513 deletions(-) diff --git a/generate-examples.php b/generate-examples.php index 30cfe96..c234a96 100644 --- a/generate-examples.php +++ b/generate-examples.php @@ -1,7 +1,7 @@ '11111221', + '1' => '11112211', + '2' => '11121121', + '3' => '22111111', + '4' => '11211211', + '5' => '21111211', + '6' => '12111121', + '7' => '12112111', + '8' => '12211111', + '9' => '21121111', + '-' => '11122111', + '$' => '11221111', + ':' => '21112121', + '/' => '21211121', + '.' => '21212111', + '+' => '11222221', + 'A' => '11221211', + 'B' => '12121121', + 'C' => '11121221', + 'D' => '11122211' + ]; + public function getBarcodeData(string $code): Barcode { - $chr = array( - '0' => '11111221', - '1' => '11112211', - '2' => '11121121', - '3' => '22111111', - '4' => '11211211', - '5' => '21111211', - '6' => '12111121', - '7' => '12112111', - '8' => '12211111', - '9' => '21121111', - '-' => '11122111', - '$' => '11221111', - ':' => '21112121', - '/' => '21211121', - '.' => '21212111', - '+' => '11222221', - 'A' => '11221211', - 'B' => '12121121', - 'C' => '11121221', - 'D' => '11122211' - ); - $barcode = new Barcode($code); $code = 'A' . strtoupper($code) . 'A'; - $len = strlen($code); - for ($i = 0; $i < $len; ++$i) { - if (! isset($chr[$code[$i]])) { + + for ($i = 0; $i < strlen($code); ++$i) { + if (! isset($this->conversionTable[$code[$i]])) { throw new InvalidCharacterException('Char ' . $code[$i] . ' is unsupported'); } - $seq = $chr[$code[$i]]; + $seq = $this->conversionTable[$code[$i]]; for ($j = 0; $j < 8; ++$j) { if (($j % 2) == 0) { $t = true; // bar diff --git a/src/Types/TypeCode11.php b/src/Types/TypeCode11.php index 7193ee0..3793a24 100644 --- a/src/Types/TypeCode11.php +++ b/src/Types/TypeCode11.php @@ -13,30 +13,56 @@ class TypeCode11 implements TypeInterface { + protected $conversionTable = [ + '0' => '111121', + '1' => '211121', + '2' => '121121', + '3' => '221111', + '4' => '112121', + '5' => '212111', + '6' => '122111', + '7' => '111221', + '8' => '211211', + '9' => '211111', + '-' => '112111', + 'S' => '112211', + ]; + public function getBarcodeData(string $code): Barcode { - $chr = array( - '0' => '111121', - '1' => '211121', - '2' => '121121', - '3' => '221111', - '4' => '112121', - '5' => '212111', - '6' => '122111', - '7' => '111221', - '8' => '211211', - '9' => '211111', - '-' => '112111', - 'S' => '112211' - ); - $barcode = new Barcode($code); - $len = strlen($code); - // calculate check digit C + $code .= $this->getCheckDigitC($code); + $code .= $this->getCheckDigitK($code); + + $code = 'S' . $code . 'S'; + + for ($i = 0; $i < strlen($code); ++$i) { + if (! isset($this->conversionTable[$code[$i]])) { + throw new InvalidCharacterException('Char ' . $code[$i] . ' is unsupported'); + } + + $seq = $this->conversionTable[$code[$i]]; + for ($j = 0; $j < strlen($seq); ++$j) { + if (($j % 2) == 0) { + $t = true; // bar + } else { + $t = false; // space + } + $w = $seq[$j]; + + $barcode->addBar(new BarcodeBar($w, 1, $t)); + } + } + + return $barcode; + } + + private function getCheckDigitC(string $code): string + { $p = 1; $check = 0; - for ($i = ($len - 1); $i >= 0; --$i) { + for ($i = (strlen($code) - 1); $i >= 0; --$i) { $digit = $code[$i]; if ($digit == '-') { $dval = 10; @@ -53,51 +79,33 @@ public function getBarcodeData(string $code): Barcode if ($check == 10) { $check = '-'; } - $code .= $check; - - if ($len > 10) { - // calculate check digit K - $p = 1; - $check = 0; - for ($i = $len; $i >= 0; --$i) { - $digit = $code[$i]; - if ($digit == '-') { - $dval = 10; - } else { - $dval = intval($digit); - } - $check += ($dval * $p); - ++$p; - if ($p > 9) { - $p = 1; - } - } - $check %= 11; - $code .= $check; - ++$len; - } - $code = 'S' . $code . 'S'; - $len += 3; - - for ($i = 0; $i < $len; ++$i) { - if (! isset($chr[$code[$i]])) { - throw new InvalidCharacterException('Char ' . $code[$i] . ' is unsupported'); - } + return $check; + } - $seq = $chr[$code[$i]]; - for ($j = 0; $j < 6; ++$j) { - if (($j % 2) == 0) { - $t = true; // bar - } else { - $t = false; // space - } - $w = $seq[$j]; + private function getCheckDigitK(string $code): string + { + if (strlen($code) <= 10) { + return ''; + } - $barcode->addBar(new BarcodeBar($w, 1, $t)); + $p = 1; + $check = 0; + for ($i = strlen($code); $i >= 0; --$i) { + $digit = $code[$i]; + if ($digit == '-') { + $dval = 10; + } else { + $dval = intval($digit); + } + $check += ($dval * $p); + ++$p; + if ($p > 9) { + $p = 1; } } + $check %= 11; - return $barcode; + return $check; } } diff --git a/src/Types/TypeCode128.php b/src/Types/TypeCode128.php index 28e95f5..3bf04f2 100644 --- a/src/Types/TypeCode128.php +++ b/src/Types/TypeCode128.php @@ -19,123 +19,123 @@ class TypeCode128 implements TypeInterface { protected $type = null; + protected $conversionTable = [ + '212222', /* 00 */ + '222122', /* 01 */ + '222221', /* 02 */ + '121223', /* 03 */ + '121322', /* 04 */ + '131222', /* 05 */ + '122213', /* 06 */ + '122312', /* 07 */ + '132212', /* 08 */ + '221213', /* 09 */ + '221312', /* 10 */ + '231212', /* 11 */ + '112232', /* 12 */ + '122132', /* 13 */ + '122231', /* 14 */ + '113222', /* 15 */ + '123122', /* 16 */ + '123221', /* 17 */ + '223211', /* 18 */ + '221132', /* 19 */ + '221231', /* 20 */ + '213212', /* 21 */ + '223112', /* 22 */ + '312131', /* 23 */ + '311222', /* 24 */ + '321122', /* 25 */ + '321221', /* 26 */ + '312212', /* 27 */ + '322112', /* 28 */ + '322211', /* 29 */ + '212123', /* 30 */ + '212321', /* 31 */ + '232121', /* 32 */ + '111323', /* 33 */ + '131123', /* 34 */ + '131321', /* 35 */ + '112313', /* 36 */ + '132113', /* 37 */ + '132311', /* 38 */ + '211313', /* 39 */ + '231113', /* 40 */ + '231311', /* 41 */ + '112133', /* 42 */ + '112331', /* 43 */ + '132131', /* 44 */ + '113123', /* 45 */ + '113321', /* 46 */ + '133121', /* 47 */ + '313121', /* 48 */ + '211331', /* 49 */ + '231131', /* 50 */ + '213113', /* 51 */ + '213311', /* 52 */ + '213131', /* 53 */ + '311123', /* 54 */ + '311321', /* 55 */ + '331121', /* 56 */ + '312113', /* 57 */ + '312311', /* 58 */ + '332111', /* 59 */ + '314111', /* 60 */ + '221411', /* 61 */ + '431111', /* 62 */ + '111224', /* 63 */ + '111422', /* 64 */ + '121124', /* 65 */ + '121421', /* 66 */ + '141122', /* 67 */ + '141221', /* 68 */ + '112214', /* 69 */ + '112412', /* 70 */ + '122114', /* 71 */ + '122411', /* 72 */ + '142112', /* 73 */ + '142211', /* 74 */ + '241211', /* 75 */ + '221114', /* 76 */ + '413111', /* 77 */ + '241112', /* 78 */ + '134111', /* 79 */ + '111242', /* 80 */ + '121142', /* 81 */ + '121241', /* 82 */ + '114212', /* 83 */ + '124112', /* 84 */ + '124211', /* 85 */ + '411212', /* 86 */ + '421112', /* 87 */ + '421211', /* 88 */ + '212141', /* 89 */ + '214121', /* 90 */ + '412121', /* 91 */ + '111143', /* 92 */ + '111341', /* 93 */ + '131141', /* 94 */ + '114113', /* 95 */ + '114311', /* 96 */ + '411113', /* 97 */ + '411311', /* 98 */ + '113141', /* 99 */ + '114131', /* 100 */ + '311141', /* 101 */ + '411131', /* 102 */ + '211412', /* 103 START A */ + '211214', /* 104 START B */ + '211232', /* 105 START C */ + '233111', /* STOP */ + '200000' /* END */ + ]; + public function getBarcodeData(string $code): Barcode { if (strlen(trim($code)) === 0) { throw new InvalidLengthException('You should provide a barcode string.'); } - $chr = array( - '212222', /* 00 */ - '222122', /* 01 */ - '222221', /* 02 */ - '121223', /* 03 */ - '121322', /* 04 */ - '131222', /* 05 */ - '122213', /* 06 */ - '122312', /* 07 */ - '132212', /* 08 */ - '221213', /* 09 */ - '221312', /* 10 */ - '231212', /* 11 */ - '112232', /* 12 */ - '122132', /* 13 */ - '122231', /* 14 */ - '113222', /* 15 */ - '123122', /* 16 */ - '123221', /* 17 */ - '223211', /* 18 */ - '221132', /* 19 */ - '221231', /* 20 */ - '213212', /* 21 */ - '223112', /* 22 */ - '312131', /* 23 */ - '311222', /* 24 */ - '321122', /* 25 */ - '321221', /* 26 */ - '312212', /* 27 */ - '322112', /* 28 */ - '322211', /* 29 */ - '212123', /* 30 */ - '212321', /* 31 */ - '232121', /* 32 */ - '111323', /* 33 */ - '131123', /* 34 */ - '131321', /* 35 */ - '112313', /* 36 */ - '132113', /* 37 */ - '132311', /* 38 */ - '211313', /* 39 */ - '231113', /* 40 */ - '231311', /* 41 */ - '112133', /* 42 */ - '112331', /* 43 */ - '132131', /* 44 */ - '113123', /* 45 */ - '113321', /* 46 */ - '133121', /* 47 */ - '313121', /* 48 */ - '211331', /* 49 */ - '231131', /* 50 */ - '213113', /* 51 */ - '213311', /* 52 */ - '213131', /* 53 */ - '311123', /* 54 */ - '311321', /* 55 */ - '331121', /* 56 */ - '312113', /* 57 */ - '312311', /* 58 */ - '332111', /* 59 */ - '314111', /* 60 */ - '221411', /* 61 */ - '431111', /* 62 */ - '111224', /* 63 */ - '111422', /* 64 */ - '121124', /* 65 */ - '121421', /* 66 */ - '141122', /* 67 */ - '141221', /* 68 */ - '112214', /* 69 */ - '112412', /* 70 */ - '122114', /* 71 */ - '122411', /* 72 */ - '142112', /* 73 */ - '142211', /* 74 */ - '241211', /* 75 */ - '221114', /* 76 */ - '413111', /* 77 */ - '241112', /* 78 */ - '134111', /* 79 */ - '111242', /* 80 */ - '121142', /* 81 */ - '121241', /* 82 */ - '114212', /* 83 */ - '124112', /* 84 */ - '124211', /* 85 */ - '411212', /* 86 */ - '421112', /* 87 */ - '421211', /* 88 */ - '212141', /* 89 */ - '214121', /* 90 */ - '412121', /* 91 */ - '111143', /* 92 */ - '111341', /* 93 */ - '131141', /* 94 */ - '114113', /* 95 */ - '114311', /* 96 */ - '411113', /* 97 */ - '411311', /* 98 */ - '113141', /* 99 */ - '114131', /* 100 */ - '311141', /* 101 */ - '411131', /* 102 */ - '211412', /* 103 START A */ - '211214', /* 104 START B */ - '211232', /* 105 START C */ - '233111', /* STOP */ - '200000' /* END */ - ); - // ASCII characters for code A (ASCII 00 - 95) $keys_a = ' !"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_'; $keys_a .= chr(0) . chr(1) . chr(2) . chr(3) . chr(4) . chr(5) . chr(6) . chr(7) . chr(8) . chr(9); @@ -147,11 +147,11 @@ public function getBarcodeData(string $code): Barcode $keys_b = ' !"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~' . chr(127); // special codes - $fnc_a = array(241 => 102, 242 => 97, 243 => 96, 244 => 101); - $fnc_b = array(241 => 102, 242 => 97, 243 => 96, 244 => 100); + $fnc_a = [241 => 102, 242 => 97, 243 => 96, 244 => 101]; + $fnc_b = [241 => 102, 242 => 97, 243 => 96, 244 => 100]; // array of symbols - $code_data = array(); + $code_data = []; // length of the code $len = strlen($code); @@ -209,9 +209,9 @@ public function getBarcodeData(string $code): Barcode default: // split code into sequences - $sequence = array(); + $sequence = []; // get numeric sequences (if any) - $numseq = array(); + $numseq = []; preg_match_all('/([0-9]{4,})/', $code, $numseq, PREG_OFFSET_CAPTURE); if (isset($numseq[1]) AND ! empty($numseq[1])) { $end_offset = 0; @@ -237,7 +237,7 @@ public function getBarcodeData(string $code): Barcode // the length must be even --$slen; } - $sequence[] = array('C', substr($code, $offset, $slen), $slen); + $sequence[] = ['C', substr($code, $offset, $slen), $slen]; $end_offset = $offset + $slen; } if ($end_offset < $len) { @@ -351,7 +351,7 @@ public function getBarcodeData(string $code): Barcode // build barcode array $barcode = new Barcode($code); foreach ($code_data as $val) { - $seq = $chr[$val]; + $seq = $this->conversionTable[$val]; for ($j = 0; $j < 6; ++$j) { if (($j % 2) == 0) { $t = true; // bar @@ -378,9 +378,9 @@ public function getBarcodeData(string $code): Barcode protected function get128ABsequence($code) { $len = strlen($code); - $sequence = array(); + $sequence = []; // get A sequences (if any) - $numseq = array(); + $numseq = []; preg_match_all('/([\x00-\x1f])/', $code, $numseq, PREG_OFFSET_CAPTURE); if (isset($numseq[1]) AND ! empty($numseq[1])) { $end_offset = 0; @@ -388,23 +388,23 @@ protected function get128ABsequence($code) $offset = $val[1]; if ($offset > $end_offset) { // B sequence - $sequence[] = array( + $sequence[] = [ 'B', substr($code, $end_offset, ($offset - $end_offset)), ($offset - $end_offset) - ); + ]; } // A sequence $slen = strlen($val[0]); - $sequence[] = array('A', substr($code, $offset, $slen), $slen); + $sequence[] = ['A', substr($code, $offset, $slen), $slen]; $end_offset = $offset + $slen; } if ($end_offset < $len) { - $sequence[] = array('B', substr($code, $end_offset), ($len - $end_offset)); + $sequence[] = ['B', substr($code, $end_offset), ($len - $end_offset)]; } } else { // only B sequence - $sequence[] = array('B', $code, $len); + $sequence[] = ['B', $code, $len]; } return $sequence; diff --git a/src/Types/TypeCode39.php b/src/Types/TypeCode39.php index 926921f..4dc675f 100644 --- a/src/Types/TypeCode39.php +++ b/src/Types/TypeCode39.php @@ -17,58 +17,59 @@ class TypeCode39 implements TypeInterface protected $extended = false; protected $checksum = false; + protected $conversionTable = [ + '0' => '111331311', + '1' => '311311113', + '2' => '113311113', + '3' => '313311111', + '4' => '111331113', + '5' => '311331111', + '6' => '113331111', + '7' => '111311313', + '8' => '311311311', + '9' => '113311311', + 'A' => '311113113', + 'B' => '113113113', + 'C' => '313113111', + 'D' => '111133113', + 'E' => '311133111', + 'F' => '113133111', + 'G' => '111113313', + 'H' => '311113311', + 'I' => '113113311', + 'J' => '111133311', + 'K' => '311111133', + 'L' => '113111133', + 'M' => '313111131', + 'N' => '111131133', + 'O' => '311131131', + 'P' => '113131131', + 'Q' => '111111333', + 'R' => '311111331', + 'S' => '113111331', + 'T' => '111131331', + 'U' => '331111113', + 'V' => '133111113', + 'W' => '333111111', + 'X' => '131131113', + 'Y' => '331131111', + 'Z' => '133131111', + '-' => '131111313', + '.' => '331111311', + ' ' => '133111311', + '$' => '131313111', + '/' => '131311131', + '+' => '131113131', + '%' => '111313131', + '*' => '131131311', + ]; + public function getBarcodeData(string $code): Barcode { if (strlen(trim($code)) === 0) { throw new InvalidLengthException('You should provide a barcode string.'); } - $chr = []; - $chr['0'] = '111331311'; - $chr['1'] = '311311113'; - $chr['2'] = '113311113'; - $chr['3'] = '313311111'; - $chr['4'] = '111331113'; - $chr['5'] = '311331111'; - $chr['6'] = '113331111'; - $chr['7'] = '111311313'; - $chr['8'] = '311311311'; - $chr['9'] = '113311311'; - $chr['A'] = '311113113'; - $chr['B'] = '113113113'; - $chr['C'] = '313113111'; - $chr['D'] = '111133113'; - $chr['E'] = '311133111'; - $chr['F'] = '113133111'; - $chr['G'] = '111113313'; - $chr['H'] = '311113311'; - $chr['I'] = '113113311'; - $chr['J'] = '111133311'; - $chr['K'] = '311111133'; - $chr['L'] = '113111133'; - $chr['M'] = '313111131'; - $chr['N'] = '111131133'; - $chr['O'] = '311131131'; - $chr['P'] = '113131131'; - $chr['Q'] = '111111333'; - $chr['R'] = '311111331'; - $chr['S'] = '113111331'; - $chr['T'] = '111131331'; - $chr['U'] = '331111113'; - $chr['V'] = '133111113'; - $chr['W'] = '333111111'; - $chr['X'] = '131131113'; - $chr['Y'] = '331131111'; - $chr['Z'] = '133131111'; - $chr['-'] = '131111313'; - $chr['.'] = '331111311'; - $chr[' '] = '133111311'; - $chr['$'] = '131313111'; - $chr['/'] = '131311131'; - $chr['+'] = '131113131'; - $chr['%'] = '111313131'; - $chr['*'] = '131131311'; - $code = strtoupper($code); if ($this->extended) { @@ -86,22 +87,23 @@ public function getBarcodeData(string $code): Barcode $barcode = new Barcode($code); - $clen = strlen($code); - for ($i = 0; $i < $clen; ++$i) { + for ($i = 0; $i < strlen($code); ++$i) { $char = $code[$i]; - if (! isset($chr[$char])) { + if (! isset($this->conversionTable[$char])) { throw new InvalidCharacterException('Char ' . $char . ' is unsupported'); } + for ($j = 0; $j < 9; ++$j) { if (($j % 2) == 0) { $t = true; // bar } else { $t = false; // space } - $w = $chr[$char][$j]; + $w = $this->conversionTable[$char][$j]; $barcode->addBar(new BarcodeBar($w, 1, $t)); } - // intercharacter gap + + // inter character gap $barcode->addBar(new BarcodeBar(1, 1, false)); } @@ -118,7 +120,7 @@ public function getBarcodeData(string $code): Barcode */ protected function encode_code39_ext($code) { - $encode = array( + $encode = [ chr(0) => '%U', chr(1) => '$A', chr(2) => '$B', @@ -247,13 +249,14 @@ protected function encode_code39_ext($code) chr(125) => '%R', chr(126) => '%S', chr(127) => '%T' - ); + ]; + $code_ext = ''; - $clen = strlen($code); - for ($i = 0; $i < $clen; ++$i) { + for ($i = 0; $i < strlen($code); ++$i) { if (ord($code[$i]) > 127) { throw new InvalidCharacterException('Only supports till char 127'); } + $code_ext .= $encode[$code[$i]]; } @@ -270,7 +273,7 @@ protected function encode_code39_ext($code) */ protected function checksum_code39($code) { - $chars = array( + $chars = [ '0', '1', '2', @@ -314,10 +317,10 @@ protected function checksum_code39($code) '/', '+', '%' - ); + ]; + $sum = 0; - $codelength = strlen($code); - for ($i = 0; $i < $codelength; ++$i) { + for ($i = 0; $i < strlen($code); ++$i) { $k = array_keys($chars, $code[$i]); $sum += $k[0]; } diff --git a/src/Types/TypeCode93.php b/src/Types/TypeCode93.php index 11fdc39..481a168 100644 --- a/src/Types/TypeCode93.php +++ b/src/Types/TypeCode93.php @@ -13,58 +13,59 @@ class TypeCode93 implements TypeInterface { + protected $conversionTable = [ + 48 => '131112', // 0 + 49 => '111213', // 1 + 50 => '111312', // 2 + 51 => '111411', // 3 + 52 => '121113', // 4 + 53 => '121212', // 5 + 54 => '121311', // 6 + 55 => '111114', // 7 + 56 => '131211', // 8 + 57 => '141111', // 9 + 65 => '211113', // A + 66 => '211212', // B + 67 => '211311', // C + 68 => '221112', // D + 69 => '221211', // E + 70 => '231111', // F + 71 => '112113', // G + 72 => '112212', // H + 73 => '112311', // I + 74 => '122112', // J + 75 => '132111', // K + 76 => '111123', // L + 77 => '111222', // M + 78 => '111321', // N + 79 => '121122', // O + 80 => '131121', // P + 81 => '212112', // Q + 82 => '212211', // R + 83 => '211122', // S + 84 => '211221', // T + 85 => '221121', // U + 86 => '222111', // V + 87 => '112122', // W + 88 => '112221', // X + 89 => '122121', // Y + 90 => '123111', // Z + 45 => '121131', // - + 46 => '311112', // . + 32 => '311211', // + 36 => '321111', // $ + 47 => '112131', // / + 43 => '113121', // + + 37 => '211131', // % + 128 => '121221', // ($) + 129 => '311121', // (/) + 130 => '122211', // (+) + 131 => '312111', // (%) + 42 => '111141', // start-stop + ]; + public function getBarcodeData(string $code): Barcode { - $chr = []; - $chr[48] = '131112'; // 0 - $chr[49] = '111213'; // 1 - $chr[50] = '111312'; // 2 - $chr[51] = '111411'; // 3 - $chr[52] = '121113'; // 4 - $chr[53] = '121212'; // 5 - $chr[54] = '121311'; // 6 - $chr[55] = '111114'; // 7 - $chr[56] = '131211'; // 8 - $chr[57] = '141111'; // 9 - $chr[65] = '211113'; // A - $chr[66] = '211212'; // B - $chr[67] = '211311'; // C - $chr[68] = '221112'; // D - $chr[69] = '221211'; // E - $chr[70] = '231111'; // F - $chr[71] = '112113'; // G - $chr[72] = '112212'; // H - $chr[73] = '112311'; // I - $chr[74] = '122112'; // J - $chr[75] = '132111'; // K - $chr[76] = '111123'; // L - $chr[77] = '111222'; // M - $chr[78] = '111321'; // N - $chr[79] = '121122'; // O - $chr[80] = '131121'; // P - $chr[81] = '212112'; // Q - $chr[82] = '212211'; // R - $chr[83] = '211122'; // S - $chr[84] = '211221'; // T - $chr[85] = '221121'; // U - $chr[86] = '222111'; // V - $chr[87] = '112122'; // W - $chr[88] = '112221'; // X - $chr[89] = '122121'; // Y - $chr[90] = '123111'; // Z - $chr[45] = '121131'; // - - $chr[46] = '311112'; // . - $chr[32] = '311211'; // - $chr[36] = '321111'; // $ - $chr[47] = '112131'; // / - $chr[43] = '113121'; // + - $chr[37] = '211131'; // % - $chr[128] = '121221'; // ($) - $chr[129] = '311121'; // (/) - $chr[130] = '122211'; // (+) - $chr[131] = '312111'; // (%) - $chr[42] = '111141'; // start-stop - $code = strtoupper($code); $encode = [ @@ -217,7 +218,7 @@ public function getBarcodeData(string $code): Barcode for ($i = 0; $i < strlen($code); ++$i) { $char = ord($code[$i]); - if (! isset($chr[$char])) { + if (! isset($this->conversionTable[$char])) { throw new InvalidCharacterException('Char ' . $char . ' is unsupported'); } @@ -227,7 +228,7 @@ public function getBarcodeData(string $code): Barcode } else { $t = false; // space } - $w = $chr[$char][$j]; + $w = $this->conversionTable[$char][$j]; $barcode->addBar(new BarcodeBar($w, 1, $t)); } @@ -247,55 +248,7 @@ public function getBarcodeData(string $code): Barcode */ protected function checksum_code93($code) { - $chars = [ - '0', - '1', - '2', - '3', - '4', - '5', - '6', - '7', - '8', - '9', - 'A', - 'B', - 'C', - 'D', - 'E', - 'F', - 'G', - 'H', - 'I', - 'J', - 'K', - 'L', - 'M', - 'N', - 'O', - 'P', - 'Q', - 'R', - 'S', - 'T', - 'U', - 'V', - 'W', - 'X', - 'Y', - 'Z', - '-', - '.', - ' ', - '$', - '/', - '+', - '%', - '<', - '=', - '>', - '?', - ]; + $chars = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '-', '.', ' ', '$', '/', '+', '%', '<', '=', '>', '?']; // translate special characters $code = strtr($code, chr(128) . chr(131) . chr(129) . chr(130), '<=>?'); diff --git a/src/Types/TypeIntelligentMailBarcode.php b/src/Types/TypeIntelligentMailBarcode.php index b21710b..8c4f8c4 100644 --- a/src/Types/TypeIntelligentMailBarcode.php +++ b/src/Types/TypeIntelligentMailBarcode.php @@ -36,7 +36,7 @@ class TypeIntelligentMailBarcode implements TypeInterface { public function getBarcodeData(string $code): Barcode { - $asc_chr = array( + $asc_chr = [ 4, 0, 2, @@ -102,8 +102,8 @@ public function getBarcodeData(string $code): Barcode 9, 5, 8 - ); - $dsc_chr = array( + ]; + $dsc_chr = [ 7, 1, 9, @@ -169,8 +169,8 @@ public function getBarcodeData(string $code): Barcode 2, 6, 3 - ); - $asc_pos = array( + ]; + $asc_pos = [ 3, 0, 8, @@ -236,8 +236,8 @@ public function getBarcodeData(string $code): Barcode 0, 3, 2 - ); - $dsc_pos = array( + ]; + $dsc_pos = [ 2, 10, 12, @@ -303,7 +303,7 @@ public function getBarcodeData(string $code): Barcode 9, 8, 10 - ); + ]; $code_arr = explode('-', $code); $tracking_number = $code_arr[0]; if (isset($code_arr[1])) { @@ -358,7 +358,7 @@ public function getBarcodeData(string $code): Barcode $binary_code_102bit = $first_byte . substr($binary_code, 2); // convert binary data to codewords - $codewords = array(); + $codewords = []; $data = $this->hex_to_dec($binary_code_102bit); $codewords[0] = bcmod($data, 636) * 2; $data = bcdiv($data, 636); @@ -376,7 +376,7 @@ public function getBarcodeData(string $code): Barcode $table5of13 = $this->imb_tables(5, 1287); // convert codewords to characters - $characters = array(); + $characters = []; $bitmask = 512; foreach ($codewords as $k => $val) { if ($val <= 1286) { @@ -519,7 +519,7 @@ protected function hex_to_dec($hex) */ protected function imb_tables($n, $size) { - $table = array(); + $table = []; $lli = 0; // LUT lower index $lui = $size - 1; // LUT upper index for ($count = 0; $count < 8192; ++$count) { diff --git a/src/Types/TypePlanet.php b/src/Types/TypePlanet.php index 1e3d5d3..0752386 100644 --- a/src/Types/TypePlanet.php +++ b/src/Types/TypePlanet.php @@ -13,16 +13,16 @@ class TypePlanet extends TypePostnet { - protected $barlen = Array( - 0 => Array(1, 1, 2, 2, 2), - 1 => Array(2, 2, 2, 1, 1), - 2 => Array(2, 2, 1, 2, 1), - 3 => Array(2, 2, 1, 1, 2), - 4 => Array(2, 1, 2, 2, 1), - 5 => Array(2, 1, 2, 1, 2), - 6 => Array(2, 1, 1, 2, 2), - 7 => Array(1, 2, 2, 2, 1), - 8 => Array(1, 2, 2, 1, 2), - 9 => Array(1, 2, 1, 2, 2) - ); + protected $barlen = [ + 0 => [1, 1, 2, 2, 2], + 1 => [2, 2, 2, 1, 1], + 2 => [2, 2, 1, 2, 1], + 3 => [2, 2, 1, 1, 2], + 4 => [2, 1, 2, 2, 1], + 5 => [2, 1, 2, 1, 2], + 6 => [2, 1, 1, 2, 2], + 7 => [1, 2, 2, 2, 1], + 8 => [1, 2, 2, 1, 2], + 9 => [1, 2, 1, 2, 2] + ]; } diff --git a/src/Types/TypePostnet.php b/src/Types/TypePostnet.php index e52d855..1f99ae2 100644 --- a/src/Types/TypePostnet.php +++ b/src/Types/TypePostnet.php @@ -16,18 +16,18 @@ class TypePostnet implements TypeInterface { - protected $barlen = Array( - 0 => Array(2, 2, 1, 1, 1), - 1 => Array(1, 1, 1, 2, 2), - 2 => Array(1, 1, 2, 1, 2), - 3 => Array(1, 1, 2, 2, 1), - 4 => Array(1, 2, 1, 1, 2), - 5 => Array(1, 2, 1, 2, 1), - 6 => Array(1, 2, 2, 1, 1), - 7 => Array(2, 1, 1, 1, 2), - 8 => Array(2, 1, 1, 2, 1), - 9 => Array(2, 1, 2, 1, 1) - ); + protected $barlen = [ + 0 => [2, 2, 1, 1, 1], + 1 => [1, 1, 1, 2, 2], + 2 => [1, 1, 2, 1, 2], + 3 => [1, 1, 2, 2, 1], + 4 => [1, 2, 1, 1, 2], + 5 => [1, 2, 1, 2, 1], + 6 => [1, 2, 2, 1, 1], + 7 => [2, 1, 1, 1, 2], + 8 => [2, 1, 1, 2, 1], + 9 => [2, 1, 2, 1, 1] + ]; public function getBarcodeData(string $code): Barcode { diff --git a/src/Types/TypeRms4cc.php b/src/Types/TypeRms4cc.php index f5ee1a3..c728bcb 100644 --- a/src/Types/TypeRms4cc.php +++ b/src/Types/TypeRms4cc.php @@ -19,97 +19,96 @@ class TypeRms4cc implements TypeInterface public function getBarcodeData(string $code): Barcode { - $notkix = ! $this->kix; - // bar mode // 1 = pos 1, length 2 // 2 = pos 1, length 3 // 3 = pos 2, length 1 // 4 = pos 2, length 2 - $barmode = array( - '0' => array(3, 3, 2, 2), - '1' => array(3, 4, 1, 2), - '2' => array(3, 4, 2, 1), - '3' => array(4, 3, 1, 2), - '4' => array(4, 3, 2, 1), - '5' => array(4, 4, 1, 1), - '6' => array(3, 1, 4, 2), - '7' => array(3, 2, 3, 2), - '8' => array(3, 2, 4, 1), - '9' => array(4, 1, 3, 2), - 'A' => array(4, 1, 4, 1), - 'B' => array(4, 2, 3, 1), - 'C' => array(3, 1, 2, 4), - 'D' => array(3, 2, 1, 4), - 'E' => array(3, 2, 2, 3), - 'F' => array(4, 1, 1, 4), - 'G' => array(4, 1, 2, 3), - 'H' => array(4, 2, 1, 3), - 'I' => array(1, 3, 4, 2), - 'J' => array(1, 4, 3, 2), - 'K' => array(1, 4, 4, 1), - 'L' => array(2, 3, 3, 2), - 'M' => array(2, 3, 4, 1), - 'N' => array(2, 4, 3, 1), - 'O' => array(1, 3, 2, 4), - 'P' => array(1, 4, 1, 4), - 'Q' => array(1, 4, 2, 3), - 'R' => array(2, 3, 1, 4), - 'S' => array(2, 3, 2, 3), - 'T' => array(2, 4, 1, 3), - 'U' => array(1, 1, 4, 4), - 'V' => array(1, 2, 3, 4), - 'W' => array(1, 2, 4, 3), - 'X' => array(2, 1, 3, 4), - 'Y' => array(2, 1, 4, 3), - 'Z' => array(2, 2, 3, 3) - ); + $barmode = [ + '0' => [3, 3, 2, 2], + '1' => [3, 4, 1, 2], + '2' => [3, 4, 2, 1], + '3' => [4, 3, 1, 2], + '4' => [4, 3, 2, 1], + '5' => [4, 4, 1, 1], + '6' => [3, 1, 4, 2], + '7' => [3, 2, 3, 2], + '8' => [3, 2, 4, 1], + '9' => [4, 1, 3, 2], + 'A' => [4, 1, 4, 1], + 'B' => [4, 2, 3, 1], + 'C' => [3, 1, 2, 4], + 'D' => [3, 2, 1, 4], + 'E' => [3, 2, 2, 3], + 'F' => [4, 1, 1, 4], + 'G' => [4, 1, 2, 3], + 'H' => [4, 2, 1, 3], + 'I' => [1, 3, 4, 2], + 'J' => [1, 4, 3, 2], + 'K' => [1, 4, 4, 1], + 'L' => [2, 3, 3, 2], + 'M' => [2, 3, 4, 1], + 'N' => [2, 4, 3, 1], + 'O' => [1, 3, 2, 4], + 'P' => [1, 4, 1, 4], + 'Q' => [1, 4, 2, 3], + 'R' => [2, 3, 1, 4], + 'S' => [2, 3, 2, 3], + 'T' => [2, 4, 1, 3], + 'U' => [1, 1, 4, 4], + 'V' => [1, 2, 3, 4], + 'W' => [1, 2, 4, 3], + 'X' => [2, 1, 3, 4], + 'Y' => [2, 1, 4, 3], + 'Z' => [2, 2, 3, 3] + ]; $code = strtoupper($code); $len = strlen($code); $barcode = new Barcode($code); - if ($notkix) { + if (! $this->kix) { // table for checksum calculation (row,col) - $checktable = array( - '0' => array(1, 1), - '1' => array(1, 2), - '2' => array(1, 3), - '3' => array(1, 4), - '4' => array(1, 5), - '5' => array(1, 0), - '6' => array(2, 1), - '7' => array(2, 2), - '8' => array(2, 3), - '9' => array(2, 4), - 'A' => array(2, 5), - 'B' => array(2, 0), - 'C' => array(3, 1), - 'D' => array(3, 2), - 'E' => array(3, 3), - 'F' => array(3, 4), - 'G' => array(3, 5), - 'H' => array(3, 0), - 'I' => array(4, 1), - 'J' => array(4, 2), - 'K' => array(4, 3), - 'L' => array(4, 4), - 'M' => array(4, 5), - 'N' => array(4, 0), - 'O' => array(5, 1), - 'P' => array(5, 2), - 'Q' => array(5, 3), - 'R' => array(5, 4), - 'S' => array(5, 5), - 'T' => array(5, 0), - 'U' => array(0, 1), - 'V' => array(0, 2), - 'W' => array(0, 3), - 'X' => array(0, 4), - 'Y' => array(0, 5), - 'Z' => array(0, 0) - ); + $checktable = [ + '0' => [1, 1], + '1' => [1, 2], + '2' => [1, 3], + '3' => [1, 4], + '4' => [1, 5], + '5' => [1, 0], + '6' => [2, 1], + '7' => [2, 2], + '8' => [2, 3], + '9' => [2, 4], + 'A' => [2, 5], + 'B' => [2, 0], + 'C' => [3, 1], + 'D' => [3, 2], + 'E' => [3, 3], + 'F' => [3, 4], + 'G' => [3, 5], + 'H' => [3, 0], + 'I' => [4, 1], + 'J' => [4, 2], + 'K' => [4, 3], + 'L' => [4, 4], + 'M' => [4, 5], + 'N' => [4, 0], + 'O' => [5, 1], + 'P' => [5, 2], + 'Q' => [5, 3], + 'R' => [5, 4], + 'S' => [5, 5], + 'T' => [5, 0], + 'U' => [0, 1], + 'V' => [0, 2], + 'W' => [0, 3], + 'X' => [0, 4], + 'Y' => [0, 5], + 'Z' => [0, 0] + ]; + $row = 0; $col = 0; for ($i = 0; $i < $len; ++$i) { @@ -118,7 +117,7 @@ public function getBarcodeData(string $code): Barcode } $row %= 6; $col %= 6; - $chk = array_keys($checktable, array($row, $col)); + $chk = array_keys($checktable, [$row, $col]); $code .= $chk[0]; ++$len; @@ -156,7 +155,7 @@ public function getBarcodeData(string $code): Barcode } } - if ($notkix) { + if (! $this->kix) { // stop bar $barcode->addBar(new BarcodeBar(1, 3, 1)); } diff --git a/src/Types/TypeUpcExtension2.php b/src/Types/TypeUpcExtension2.php index 56e40f3..98ee3c6 100644 --- a/src/Types/TypeUpcExtension2.php +++ b/src/Types/TypeUpcExtension2.php @@ -34,8 +34,8 @@ public function getBarcodeData(string $code): Barcode } //Convert digits to bars - $codes = array( - 'A' => array( // left odd parity + $codes = [ + 'A' => [ // left odd parity '0' => '0001101', '1' => '0011001', '2' => '0010011', @@ -46,8 +46,8 @@ public function getBarcodeData(string $code): Barcode '7' => '0111011', '8' => '0110111', '9' => '0001011' - ), - 'B' => array( // left even parity + ], + 'B' => [ // left even parity '0' => '0100111', '1' => '0110011', '2' => '0011011', @@ -58,28 +58,29 @@ public function getBarcodeData(string $code): Barcode '7' => '0010001', '8' => '0001001', '9' => '0010111' - ) - ); + ] + ]; - $parities = array(); - $parities[2] = array( - '0' => array('A', 'A'), - '1' => array('A', 'B'), - '2' => array('B', 'A'), - '3' => array('B', 'B') - ); - $parities[5] = array( - '0' => array('B', 'B', 'A', 'A', 'A'), - '1' => array('B', 'A', 'B', 'A', 'A'), - '2' => array('B', 'A', 'A', 'B', 'A'), - '3' => array('B', 'A', 'A', 'A', 'B'), - '4' => array('A', 'B', 'B', 'A', 'A'), - '5' => array('A', 'A', 'B', 'B', 'A'), - '6' => array('A', 'A', 'A', 'B', 'B'), - '7' => array('A', 'B', 'A', 'B', 'A'), - '8' => array('A', 'B', 'A', 'A', 'B'), - '9' => array('A', 'A', 'B', 'A', 'B') - ); + $parities = [ + 2 =>[ + '0' => ['A', 'A'], + '1' => ['A', 'B'], + '2' => ['B', 'A'], + '3' => ['B', 'B'] + ], + 5 => [ + '0' => ['B', 'B', 'A', 'A', 'A'], + '1' => ['B', 'A', 'B', 'A', 'A'], + '2' => ['B', 'A', 'A', 'B', 'A'], + '3' => ['B', 'A', 'A', 'A', 'B'], + '4' => ['A', 'B', 'B', 'A', 'A'], + '5' => ['A', 'A', 'B', 'B', 'A'], + '6' => ['A', 'A', 'A', 'B', 'B'], + '7' => ['A', 'B', 'A', 'B', 'A'], + '8' => ['A', 'B', 'A', 'A', 'B'], + '9' => ['A', 'A', 'B', 'A', 'B'] + ] + ]; $p = $parities[$len][$r]; $seq = '1011'; // left guard bar From d96798a2cbf2c8c355130420433fe57ddd76b263 Mon Sep 17 00:00:00 2001 From: Casper Bakker Date: Sat, 11 Jan 2020 19:52:42 +0100 Subject: [PATCH 041/155] Fix possible casting issues in codabar #92 --- src/Types/TypeCodabar.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Types/TypeCodabar.php b/src/Types/TypeCodabar.php index ffc90eb..8ef5b85 100644 --- a/src/Types/TypeCodabar.php +++ b/src/Types/TypeCodabar.php @@ -43,11 +43,11 @@ public function getBarcodeData(string $code): Barcode $code = 'A' . strtoupper($code) . 'A'; for ($i = 0; $i < strlen($code); ++$i) { - if (! isset($this->conversionTable[$code[$i]])) { + if (! isset($this->conversionTable[(string)$code[$i]])) { throw new InvalidCharacterException('Char ' . $code[$i] . ' is unsupported'); } - $seq = $this->conversionTable[$code[$i]]; + $seq = $this->conversionTable[(string)$code[$i]]; for ($j = 0; $j < 8; ++$j) { if (($j % 2) == 0) { $t = true; // bar From 155613fc434a8b90ce3d639c90e3cf27c5e88810 Mon Sep 17 00:00:00 2001 From: Casper Bakker Date: Sat, 11 Jan 2020 20:38:01 +0100 Subject: [PATCH 042/155] Improve some naming --- src/Helpers/BinarySequenceConverter.php | 14 +++++++------- src/Types/TypeCodabar.php | 8 ++++---- src/Types/TypeCode11.php | 8 ++++---- src/Types/TypeCode39.php | 8 ++++---- src/Types/TypeCode93.php | 8 ++++---- 5 files changed, 23 insertions(+), 23 deletions(-) diff --git a/src/Helpers/BinarySequenceConverter.php b/src/Helpers/BinarySequenceConverter.php index a9d5d4f..76309a8 100644 --- a/src/Helpers/BinarySequenceConverter.php +++ b/src/Helpers/BinarySequenceConverter.php @@ -15,18 +15,18 @@ public static function convert(string $code, string $sequence): Barcode $barcode = new Barcode($code); $len = strlen($sequence); - $w = 0; + $barWidth = 0; for ($i = 0; $i < $len; ++$i) { - $w += 1; - if (($i == ($len - 1)) OR (($i < ($len - 1)) AND ($sequence[$i] != $sequence[($i + 1)]))) { + $barWidth += 1; + if (($i == ($len - 1)) || (($i < ($len - 1)) && ($sequence[$i] != $sequence[($i + 1)]))) { if ($sequence[$i] == '1') { - $t = true; // bar + $drawBar = true; } else { - $t = false; // space + $drawBar = false; } - $barcode->addBar(new BarcodeBar($w, 1, $t)); - $w = 0; + $barcode->addBar(new BarcodeBar($barWidth, 1, $drawBar)); + $barWidth = 0; } } diff --git a/src/Types/TypeCodabar.php b/src/Types/TypeCodabar.php index 8ef5b85..0bf40e1 100644 --- a/src/Types/TypeCodabar.php +++ b/src/Types/TypeCodabar.php @@ -50,12 +50,12 @@ public function getBarcodeData(string $code): Barcode $seq = $this->conversionTable[(string)$code[$i]]; for ($j = 0; $j < 8; ++$j) { if (($j % 2) == 0) { - $t = true; // bar + $drawBar = true; } else { - $t = false; // space + $drawBar = false; } - $w = $seq[$j]; - $barcode->addBar(new BarcodeBar($w, 1, $t)); + $barWidth = $seq[$j]; + $barcode->addBar(new BarcodeBar($barWidth, 1, $drawBar)); } } diff --git a/src/Types/TypeCode11.php b/src/Types/TypeCode11.php index 3793a24..ffba8d5 100644 --- a/src/Types/TypeCode11.php +++ b/src/Types/TypeCode11.php @@ -45,13 +45,13 @@ public function getBarcodeData(string $code): Barcode $seq = $this->conversionTable[$code[$i]]; for ($j = 0; $j < strlen($seq); ++$j) { if (($j % 2) == 0) { - $t = true; // bar + $drawBar = true; } else { - $t = false; // space + $drawBar = false; } - $w = $seq[$j]; + $barWidth = $seq[$j]; - $barcode->addBar(new BarcodeBar($w, 1, $t)); + $barcode->addBar(new BarcodeBar($barWidth, 1, $drawBar)); } } diff --git a/src/Types/TypeCode39.php b/src/Types/TypeCode39.php index 4dc675f..4cc32b8 100644 --- a/src/Types/TypeCode39.php +++ b/src/Types/TypeCode39.php @@ -95,12 +95,12 @@ public function getBarcodeData(string $code): Barcode for ($j = 0; $j < 9; ++$j) { if (($j % 2) == 0) { - $t = true; // bar + $drawBar = true; } else { - $t = false; // space + $drawBar = false; } - $w = $this->conversionTable[$char][$j]; - $barcode->addBar(new BarcodeBar($w, 1, $t)); + $barWidth = $this->conversionTable[$char][$j]; + $barcode->addBar(new BarcodeBar($barWidth, 1, $drawBar)); } // inter character gap diff --git a/src/Types/TypeCode93.php b/src/Types/TypeCode93.php index 481a168..e91b2c1 100644 --- a/src/Types/TypeCode93.php +++ b/src/Types/TypeCode93.php @@ -224,13 +224,13 @@ public function getBarcodeData(string $code): Barcode for ($j = 0; $j < 6; ++$j) { if (($j % 2) == 0) { - $t = true; // bar + $drawBar = true; } else { - $t = false; // space + $drawBar = false; } - $w = $this->conversionTable[$char][$j]; + $barWidth = $this->conversionTable[$char][$j]; - $barcode->addBar(new BarcodeBar($w, 1, $t)); + $barcode->addBar(new BarcodeBar($barWidth, 1, $drawBar)); } } From b01dcfefae3b27e9de0d56aaaf59b2175f166dce Mon Sep 17 00:00:00 2001 From: Casper Bakker Date: Sat, 11 Jan 2020 20:38:29 +0100 Subject: [PATCH 043/155] Switch default image builder from GD to Imagick --- src/BarcodeGeneratorPNG.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/BarcodeGeneratorPNG.php b/src/BarcodeGeneratorPNG.php index 14b3396..16647d6 100644 --- a/src/BarcodeGeneratorPNG.php +++ b/src/BarcodeGeneratorPNG.php @@ -14,10 +14,10 @@ class BarcodeGeneratorPNG extends BarcodeGenerator public function __construct() { // Auto switch between GD and Imagick based on what is installed - if (function_exists('imagecreate')) { - $this->useImagick = false; - } elseif (extension_loaded('imagick')) { + if (extension_loaded('imagick')) { $this->useImagick = true; + } elseif (function_exists('imagecreate')) { + $this->useImagick = false; } else { throw new BarcodeException('Neither gd-lib or imagick are installed!'); } From 1eb2da0eac01d9d8bd101790b1388caff5b849b6 Mon Sep 17 00:00:00 2001 From: Casper Bakker Date: Sat, 11 Jan 2020 20:39:30 +0100 Subject: [PATCH 044/155] Improve readme for v2 --- Readme.md | 58 +++++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 48 insertions(+), 10 deletions(-) diff --git a/Readme.md b/Readme.md index d3399a0..c1cd43e 100644 --- a/Readme.md +++ b/Readme.md @@ -5,7 +5,10 @@ This is an easy to use, non-bloated, framework independent, barcode generator in It creates SVG, PNG, JPG and HTML images, from the most used 1D barcode standards. -*The codebase is largely from the [TCPDF barcode generator](https://github.com/tecnickcom/TCPDF) by Nicola Asuni. This code is therefor licensed under LGPLv3.* +*The codebase is based on the [TCPDF barcode generator](https://github.com/tecnickcom/TCPDF) by Nicola Asuni. This code is therefor licensed under LGPLv3.* + +## No support for... +We do not support any 2D barcodes, like QR codes. We also only generate the 'bars' part of a barcode. If you want text of the code below the barcode, you could add it later to the output of this package. ## Installation Install through [composer](https://getcomposer.org/doc/00-intro.md): @@ -20,16 +23,31 @@ If you want to generate PNG or JPG images, you need the GD library or Imagick in Initiate the barcode generator for the output you want, then call the ->getBarcode() routine as many times as you want. ```php +getBarcode('081231723897', $generator::TYPE_CODE_128); ``` -The ->getBarcode() routine accepts the following: -- $code Data for the barcode -- $type Type of barcode, use the constants defined in the class -- $widthFactor Width is based on the length of the data, with this factor you can make the barcode bars wider than default -- $totalHeight The total height of the barcode -- $color Hex code of the foreground color +The `getBarcode()` method accepts the following parameters: +- `$barcode` String needed to encode in the barcode +- `$type` Type of barcode, use the constants defined in the class +- `$widthFactor` Width is based on the length of the data, with this factor you can make the barcode bars wider than default +- `$height` The total height of the barcode in pixels +- `$foregroundColor` Hex code as string, or array of RGB, of the colors of the bars (the foreground color) + +```php +getBarcode('081231723897', $generator::TYPE_CODE_128, 3, 50, $redColor)); +``` ## Image types ```php @@ -39,7 +57,11 @@ $generatorJPG = new Picqer\Barcode\BarcodeGeneratorJPG(); $generatorHTML = new Picqer\Barcode\BarcodeGeneratorHTML(); ``` -## Accepted types +## Accepted barcode types +These barcode types are supported. All types support different character sets or have mandatory lengths. Please see wikipedia for supported chars and lengths per type. + +Most used types are TYPE_CODE_128 and TYPE_CODE_39. Because of the best scanner support, variable length and most chars supported. + - TYPE_CODE_39 - TYPE_CODE_39_CHECKSUM - TYPE_CODE_39E @@ -73,10 +95,26 @@ $generatorHTML = new Picqer\Barcode\BarcodeGeneratorHTML(); [See example images for all supported barcode types](examples.md) +## A note about PNG and JPG images +If you want to use PNG or JPG images, you need to install [Imagick](https://www.php.net/manual/en/intro.imagick.php) or the [GD library](https://www.php.net/manual/en/intro.image.php). This package will use Imagick if that is installed, or fall back to GD. If you have both installed but you want a specific method, you can use `$generator->useGd()` or `$generator->useImagick()` to force your preference. + +We use Imagick as standard because the size of the output is factor 10 smaller. + ## Examples -Embedded PNG image in HTML: +### Embedded PNG image in HTML ```php -$generator = new \Picqer\Barcode\BarcodeGeneratorPNG(); +$generator = new Picqer\Barcode\BarcodeGeneratorPNG(); echo ''; ``` + +### Save JPG barcode to disk +```php +$generator = new Picqer\Barcode\BarcodeGeneratorJPG(); +file_put_contents('barcode.jpg', $generator->getBarcode('081231723897', $generator::TYPE_CODABAR)); +``` + +### Oneliner SVG output to disk +```php +file_put_contents('barcode.svg', (new Picqer\Barcode\BarcodeGeneratorSVG())->getBarcode('6825ME601', Picqer\Barcode\BarcodeGeneratorSVG::TYPE_KIX)); +``` \ No newline at end of file From 981b1aeee75615962f798e6457dc0647e3074206 Mon Sep 17 00:00:00 2001 From: Casper Bakker Date: Sat, 11 Jan 2020 21:02:48 +0100 Subject: [PATCH 045/155] Added changelog --- CHANGELOG.md | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ Readme.md | 4 ++-- 2 files changed, 54 insertions(+), 2 deletions(-) create mode 100644 CHANGELOG.md diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..4d32149 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,52 @@ +# Changelog +All notable changes to this project will be documented in this file. + +The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), +and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). + +## [Unreleased] + +## [2.0.0] - 2020-01-11 +### Added +- Introduced Barcode and BarcodeBar classes to standardise generator output. +- Introduced methods to force use of GD or Imagick, see readme. +- Loads of new tests added, including tests on Github Actions. + +### Changed +- Splitted all barcode types to different files. +- Refactored a lot of code for better readability, stricter checking, and to be more efficient. +- Merged JPG and PNG generators, because of duplicate code. + +### Fixed +- Fixed a bug in Codabar generation 2d1128f5222d9368fc6151d2b51801ea29ba1052 +- Do not draw multiple bars on the same position #74 +- Do not try to draw barcodes for empty strings #42 +- Fixed possible casting issue in Codabar #92 + +## [0.4.0] - 2019-12-31 +### Added +- Added support for PHP 7.4, thanks to @pilif #80 + +## [0.3.0] - 2019-01-12 +### Added +- SVG: Add viewBox attribute to allow svg scaling #68 by @cuchac +- Adjust CODE_128 to handle odd number of digits #55 by @richayles + +### Fixed +- Bugfix update imagick function #51 by @Keinbockwurst + +## [0.2.2] - 2017-09-28 +### Added +- Raising exceptions if we cannot generate JPG or PNG because of missing libraries. Thanks @OskarStark + +## [0.2.1] - 2016-10-24 +### Fixed +- Bugfixes for wrong constant values. + +## [0.2.0] - 2016-05-14 +### Added +- This release adds exceptions to this package. Now it is easier to detect if the generated barcode is correct or not. + +## [0.1.0] - 2015-08-13 +### Added +- Everything. First release of this package. diff --git a/Readme.md b/Readme.md index c1cd43e..48cc3aa 100644 --- a/Readme.md +++ b/Readme.md @@ -38,6 +38,8 @@ The `getBarcode()` method accepts the following parameters: - `$height` The total height of the barcode in pixels - `$foregroundColor` Hex code as string, or array of RGB, of the colors of the bars (the foreground color) +Example of usage of all parameters: + ```php useGd()` or `$generator->useImagick()` to force your preference. -We use Imagick as standard because the size of the output is factor 10 smaller. - ## Examples ### Embedded PNG image in HTML From 5be2b84c738856f281e7b481fc194d68df12b740 Mon Sep 17 00:00:00 2001 From: Casper Bakker Date: Tue, 28 Jan 2020 13:48:45 +0100 Subject: [PATCH 046/155] Fix invalid chars in test files filename #94 --- examples.md | 6 +++--- generate-examples.php | 6 +++++- generate-verified-files.php | 6 +++++- tests/TypesTest.php | 4 ++-- tests/VerifiedBarcodeTest.php | 6 +++++- ...90abcABC-283*33.svg => C128-1234567890abcABC-283-33.svg} | 0 ...0abcABC-283*33.svg => C128B-1234567890abcABC-283-33.svg} | 0 ...90abcABC-283*33.svg => EAN5-1234567890abcABC-283-33.svg} | 0 8 files changed, 20 insertions(+), 8 deletions(-) rename tests/verified-files/{C128-1234567890abcABC-283*33.svg => C128-1234567890abcABC-283-33.svg} (100%) rename tests/verified-files/{C128B-1234567890abcABC-283*33.svg => C128B-1234567890abcABC-283-33.svg} (100%) rename tests/verified-files/{EAN5-1234567890abcABC-283*33.svg => EAN5-1234567890abcABC-283-33.svg} (100%) diff --git a/examples.md b/examples.md index 7a889c3..4e0bec2 100644 --- a/examples.md +++ b/examples.md @@ -48,7 +48,7 @@ These are examples of supported barcodes with this library. ![Barcode 081231723897 as C128](tests/verified-files/C128-081231723897.svg) -![Barcode 1234567890abcABC-283*33 as C128](tests/verified-files/C128-1234567890abcABC-283*33.svg) +![Barcode 1234567890abcABC-283*33 as C128](tests/verified-files/C128-1234567890abcABC-283-33.svg) ### C128A @@ -58,7 +58,7 @@ These are examples of supported barcodes with this library. ![Barcode 081231723897 as C128B](tests/verified-files/C128B-081231723897.svg) -![Barcode 1234567890abcABC-283*33 as C128B](tests/verified-files/C128B-1234567890abcABC-283*33.svg) +![Barcode 1234567890abcABC-283*33 as C128B](tests/verified-files/C128B-1234567890abcABC-283-33.svg) ### EAN2 @@ -66,7 +66,7 @@ These are examples of supported barcodes with this library. ### EAN5 -![Barcode 1234567890abcABC-283*33 as EAN5](tests/verified-files/EAN5-1234567890abcABC-283*33.svg) +![Barcode 1234567890abcABC-283*33 as EAN5](tests/verified-files/EAN5-1234567890abcABC-283-33.svg) ### EAN8 diff --git a/generate-examples.php b/generate-examples.php index c234a96..5929908 100644 --- a/generate-examples.php +++ b/generate-examples.php @@ -1,5 +1,9 @@ getBarcode('081231723897', $generatorSVG::TYPE_EAN_13)); @@ -19,6 +23,6 @@ $generatorSVG = new Picqer\Barcode\BarcodeGeneratorSVG(); foreach ($verifiedFiles as $verifiedFile) { foreach ($verifiedFile['barcodes'] as $barcode) { - file_put_contents('tests/verified-files/' . $verifiedFile['type'] . '-' . $barcode . '.svg', $generatorSVG->getBarcode($barcode, $verifiedFile['type'])); + file_put_contents('tests/verified-files/' . getSaveFilename($verifiedFile['type'] . '-' . $barcode) . '.svg', $generatorSVG->getBarcode($barcode, $verifiedFile['type'])); } } diff --git a/tests/TypesTest.php b/tests/TypesTest.php index ca995d8..16b3083 100644 --- a/tests/TypesTest.php +++ b/tests/TypesTest.php @@ -73,7 +73,7 @@ public function test_generator_can_generate_code_128_barcode() $generator = new Picqer\Barcode\BarcodeGeneratorSVG(); $result = $generator->getBarcode('1234567890abcABC-283*33', $generator::TYPE_CODE_128); - $this->assertStringEqualsFile('tests/verified-files/C128-1234567890abcABC-283*33.svg', $result); + $this->assertStringEqualsFile('tests/verified-files/C128-1234567890abcABC-283-33.svg', $result); } public function test_generator_can_generate_code_128_a_barcode() @@ -89,7 +89,7 @@ public function test_generator_can_generate_code_128_b_barcode() $generator = new Picqer\Barcode\BarcodeGeneratorSVG(); $result = $generator->getBarcode('1234567890abcABC-283*33', $generator::TYPE_CODE_128_B); - $this->assertStringEqualsFile('tests/verified-files/C128B-1234567890abcABC-283*33.svg', $result); + $this->assertStringEqualsFile('tests/verified-files/C128B-1234567890abcABC-283-33.svg', $result); } public function test_generator_can_generate_ean_2_barcode() diff --git a/tests/VerifiedBarcodeTest.php b/tests/VerifiedBarcodeTest.php index becdc22..95e4ae3 100644 --- a/tests/VerifiedBarcodeTest.php +++ b/tests/VerifiedBarcodeTest.php @@ -53,11 +53,15 @@ public function testAllSupportedBarcodeTypes() $result = $generator->getBarcode($barcode, $barcodeTestSet['type']); $this->assertStringEqualsFile( - sprintf('tests/verified-files/%s-%s.svg', $barcodeTestSet['type'], $barcode), + sprintf('tests/verified-files/%s.svg', $this->getSaveFilename($barcodeTestSet['type'] . '-' . $barcode)), $result, sprintf('%s x %s dynamic test failed', $barcodeTestSet['type'], $barcode) ); } } } + + protected function getSaveFilename($value) { + return preg_replace('/[^a-zA-Z0-9_ \-+]/s', '-', $value); + } } diff --git a/tests/verified-files/C128-1234567890abcABC-283*33.svg b/tests/verified-files/C128-1234567890abcABC-283-33.svg similarity index 100% rename from tests/verified-files/C128-1234567890abcABC-283*33.svg rename to tests/verified-files/C128-1234567890abcABC-283-33.svg diff --git a/tests/verified-files/C128B-1234567890abcABC-283*33.svg b/tests/verified-files/C128B-1234567890abcABC-283-33.svg similarity index 100% rename from tests/verified-files/C128B-1234567890abcABC-283*33.svg rename to tests/verified-files/C128B-1234567890abcABC-283-33.svg diff --git a/tests/verified-files/EAN5-1234567890abcABC-283*33.svg b/tests/verified-files/EAN5-1234567890abcABC-283-33.svg similarity index 100% rename from tests/verified-files/EAN5-1234567890abcABC-283*33.svg rename to tests/verified-files/EAN5-1234567890abcABC-283-33.svg From 16c51a795454198500cdfb4f82de288945af3960 Mon Sep 17 00:00:00 2001 From: Casper Bakker Date: Tue, 28 Jan 2020 14:00:23 +0100 Subject: [PATCH 047/155] Added 2.0.1 to changelog --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4d32149..63777c2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +## [2.0.1] - 2020-01-28 +### Fixed +- Removed special chars from filenames we use for test validation #94 + ## [2.0.0] - 2020-01-11 ### Added - Introduced Barcode and BarcodeBar classes to standardise generator output. From 232359d076ca08a1966d000317195371d282547d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Greg=C3=B3rio=20Bonfante=20Borba?= Date: Wed, 10 Jun 2020 20:47:08 -0300 Subject: [PATCH 048/155] Fix: imagick result differs from gd --- src/BarcodeGeneratorPNG.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/BarcodeGeneratorPNG.php b/src/BarcodeGeneratorPNG.php index 16647d6..87e570c 100644 --- a/src/BarcodeGeneratorPNG.php +++ b/src/BarcodeGeneratorPNG.php @@ -74,9 +74,9 @@ public function getBarcode($barcode, $type, int $widthFactor = 2, int $height = // draw a vertical bar if ($this->useImagick && isset($imagickBarsShape)) { - $imagickBarsShape->rectangle($positionHorizontal, $y, ($positionHorizontal + $barWidth), ($y + $barHeight)); + $imagickBarsShape->rectangle($positionHorizontal, $y, ($positionHorizontal + $barWidth) - 1, ($y + $barHeight) - 1); } else { - imagefilledrectangle($image, $positionHorizontal, $y, ($positionHorizontal + $barWidth) - 1, ($y + $barHeight), $gdForegroundColor); + imagefilledrectangle($image, $positionHorizontal, $y, ($positionHorizontal + $barWidth) - 1, ($y + $barHeight) - 1, $gdForegroundColor); } } $positionHorizontal += $barWidth; From 54733d816cfa7061b0d9bf70ca73d197baedf31b Mon Sep 17 00:00:00 2001 From: Casper Bakker Date: Wed, 16 Sep 2020 21:25:38 +0200 Subject: [PATCH 049/155] Change in barHeight is not needed --- src/BarcodeGeneratorPNG.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/BarcodeGeneratorPNG.php b/src/BarcodeGeneratorPNG.php index 87e570c..e73a054 100644 --- a/src/BarcodeGeneratorPNG.php +++ b/src/BarcodeGeneratorPNG.php @@ -74,9 +74,9 @@ public function getBarcode($barcode, $type, int $widthFactor = 2, int $height = // draw a vertical bar if ($this->useImagick && isset($imagickBarsShape)) { - $imagickBarsShape->rectangle($positionHorizontal, $y, ($positionHorizontal + $barWidth) - 1, ($y + $barHeight) - 1); + $imagickBarsShape->rectangle($positionHorizontal, $y, ($positionHorizontal + $barWidth - 1), ($y + $barHeight)); } else { - imagefilledrectangle($image, $positionHorizontal, $y, ($positionHorizontal + $barWidth) - 1, ($y + $barHeight) - 1, $gdForegroundColor); + imagefilledrectangle($image, $positionHorizontal, $y, ($positionHorizontal + $barWidth - 1), ($y + $barHeight), $gdForegroundColor); } } $positionHorizontal += $barWidth; From 1f0b7f6599d04d414494353df78c985f1e461dda Mon Sep 17 00:00:00 2001 From: Casper Bakker Date: Thu, 24 Dec 2020 16:07:57 +0100 Subject: [PATCH 050/155] Change master branch to main --- .travis.yml | 14 -------------- Readme.md | 2 +- 2 files changed, 1 insertion(+), 15 deletions(-) delete mode 100644 .travis.yml diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index 841a1d1..0000000 --- a/.travis.yml +++ /dev/null @@ -1,14 +0,0 @@ -language: php - -php: - - 7.2 - - 7.3 - - 7.4 - -sudo: false - -before_script: yes | pecl install imagick - -install: composer install --no-interaction --prefer-source - -script: vendor/bin/phpunit diff --git a/Readme.md b/Readme.md index 48cc3aa..3c3c104 100644 --- a/Readme.md +++ b/Readme.md @@ -1,5 +1,5 @@ # PHP Barcode Generator -[![Build Status](https://travis-ci.org/picqer/php-barcode-generator.svg?branch=master)](https://travis-ci.org/picqer/php-barcode-generator) [![Github Actions](https://github.com/picqer/php-barcode-generator/workflows/phpunit/badge.svg)](https://travis-ci.org/picqer/php-barcode-generator) [![Total Downloads](https://poser.pugx.org/picqer/php-barcode-generator/downloads)](https://packagist.org/packages/picqer/php-barcode-generator) +[![Build Status](https://travis-ci.org/picqer/php-barcode-generator.svg?branch=main)](https://travis-ci.org/picqer/php-barcode-generator) [![Github Actions](https://github.com/picqer/php-barcode-generator/workflows/phpunit/badge.svg)](https://travis-ci.org/picqer/php-barcode-generator) [![Total Downloads](https://poser.pugx.org/picqer/php-barcode-generator/downloads)](https://packagist.org/packages/picqer/php-barcode-generator) This is an easy to use, non-bloated, framework independent, barcode generator in PHP. From 4e8fa46fd131e2f24b533280f9da298962b224d8 Mon Sep 17 00:00:00 2001 From: Casper Bakker Date: Thu, 24 Dec 2020 16:08:03 +0100 Subject: [PATCH 051/155] Test php 8 --- .github/workflows/phpunit.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/phpunit.yml b/.github/workflows/phpunit.yml index 0a3ddea..43be512 100644 --- a/.github/workflows/phpunit.yml +++ b/.github/workflows/phpunit.yml @@ -9,7 +9,7 @@ jobs: strategy: matrix: - php-versions: ['7.2', '7.3', '7.4'] + php-versions: ['7.2', '7.3', '7.4', '8.0'] steps: - uses: actions/checkout@v2 From 1c8ebbb3f64ebda0e2b44aca29b2cf8105c1afd6 Mon Sep 17 00:00:00 2001 From: Casper Bakker Date: Thu, 24 Dec 2020 16:09:40 +0100 Subject: [PATCH 052/155] Tests with setup-php@v2 --- .github/workflows/phpunit.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/phpunit.yml b/.github/workflows/phpunit.yml index 43be512..2323b2f 100644 --- a/.github/workflows/phpunit.yml +++ b/.github/workflows/phpunit.yml @@ -15,7 +15,7 @@ jobs: - uses: actions/checkout@v2 - name: Setup PHP - uses: shivammathur/setup-php@v1 + uses: shivammathur/setup-php@v2 with: php-version: ${{ matrix.php-versions }} extensions: mbstring, gd, bcmath, imagick From 83b06f1f3396d87a6f69a7c15b9945597ede8d3b Mon Sep 17 00:00:00 2001 From: Casper Bakker Date: Thu, 24 Dec 2020 16:15:15 +0100 Subject: [PATCH 053/155] Support specific PHP versions in composer --- composer.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index 18fb848..a4660b2 100644 --- a/composer.json +++ b/composer.json @@ -18,11 +18,11 @@ } ], "require": { - "php": ">=7.2.0", + "php": "^7.2|^8.0", "ext-mbstring": "*" }, "require-dev": { - "phpunit/phpunit": "^8.5" + "phpunit/phpunit": "^9.5" }, "suggest": { "ext-bcmath": "Barcode IMB (Intelligent Mail Barcode) needs bcmath extension", From b38d20f71c31e997844240904bda89f6be2b7c86 Mon Sep 17 00:00:00 2001 From: Casper Bakker Date: Thu, 24 Dec 2020 16:19:20 +0100 Subject: [PATCH 054/155] Dropped support for php 7.2 for new phpunit version --- .github/workflows/phpunit.yml | 2 +- composer.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/phpunit.yml b/.github/workflows/phpunit.yml index 2323b2f..76263ae 100644 --- a/.github/workflows/phpunit.yml +++ b/.github/workflows/phpunit.yml @@ -9,7 +9,7 @@ jobs: strategy: matrix: - php-versions: ['7.2', '7.3', '7.4', '8.0'] + php-versions: ['7.3', '7.4', '8.0'] steps: - uses: actions/checkout@v2 diff --git a/composer.json b/composer.json index a4660b2..180639e 100644 --- a/composer.json +++ b/composer.json @@ -18,7 +18,7 @@ } ], "require": { - "php": "^7.2|^8.0", + "php": "^7.3|^8.0", "ext-mbstring": "*" }, "require-dev": { From 5cae7eb13debfda138adab9a9a02464da3f576e1 Mon Sep 17 00:00:00 2001 From: Casper Bakker Date: Thu, 24 Dec 2020 16:20:42 +0100 Subject: [PATCH 055/155] Remove no-suggest --- .github/workflows/phpunit.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/phpunit.yml b/.github/workflows/phpunit.yml index 76263ae..58d5ed1 100644 --- a/.github/workflows/phpunit.yml +++ b/.github/workflows/phpunit.yml @@ -24,7 +24,7 @@ jobs: run: composer validate - name: Install dependencies - run: composer install --prefer-dist --no-progress --no-suggest + run: composer install --prefer-dist --no-progress - name: Run test suite run: composer run-script test From 2221d834efab0dbfa9d37f9dfb4673d7f27f3757 Mon Sep 17 00:00:00 2001 From: Casper Bakker Date: Thu, 24 Dec 2020 16:31:19 +0100 Subject: [PATCH 056/155] Fix generating Pharmacode 2 with invalid input #118 --- src/Types/TypePharmacodeTwoCode.php | 5 +++++ tests/PharmacodeTest.php | 15 +++++++++++++++ 2 files changed, 20 insertions(+) create mode 100644 tests/PharmacodeTest.php diff --git a/src/Types/TypePharmacodeTwoCode.php b/src/Types/TypePharmacodeTwoCode.php index dca3811..7c1959a 100644 --- a/src/Types/TypePharmacodeTwoCode.php +++ b/src/Types/TypePharmacodeTwoCode.php @@ -9,6 +9,7 @@ use Picqer\Barcode\Barcode; use Picqer\Barcode\BarcodeBar; +use Picqer\Barcode\Exceptions\InvalidLengthException; class TypePharmacodeTwoCode implements TypeInterface { @@ -16,6 +17,10 @@ public function getBarcodeData(string $code): Barcode { $code = intval($code); + if ($code < 1) { + throw new InvalidLengthException('Pharmacode 2 needs a number of 1 or larger'); + } + $seq = ''; do { diff --git a/tests/PharmacodeTest.php b/tests/PharmacodeTest.php new file mode 100644 index 0000000..c068ffb --- /dev/null +++ b/tests/PharmacodeTest.php @@ -0,0 +1,15 @@ +expectException(Picqer\Barcode\Exceptions\InvalidLengthException::class); + + $pharmacode->getBarcodeData('0'); + } +} From 516df0d3c2ca2934bdecccb90293af427338a9b6 Mon Sep 17 00:00:00 2001 From: Casper Bakker Date: Fri, 25 Dec 2020 12:35:36 +0100 Subject: [PATCH 057/155] Create FUNDING.yml --- .github/FUNDING.yml | 1 + 1 file changed, 1 insertion(+) create mode 100644 .github/FUNDING.yml diff --git a/.github/FUNDING.yml b/.github/FUNDING.yml new file mode 100644 index 0000000..a8ff52a --- /dev/null +++ b/.github/FUNDING.yml @@ -0,0 +1 @@ +github: casperbakker From b23628f5b9f67842f3fac469171bc0a20da9c3de Mon Sep 17 00:00:00 2001 From: Callum Smith Date: Tue, 16 Mar 2021 10:55:11 +0000 Subject: [PATCH 058/155] Added a dynamic HTML barcode that scales to the size of it's container --- .gitignore | 1 + Readme.md | 1 + generate-verified-files.php | 3 ++ src/BarcodeGeneratorDynamicHTML.php | 40 +++++++++++++++++++ tests/BarcodeDynamicHtmlTest.php | 14 +++++++ .../081231723897-dynamic-code128.html | 30 ++++++++++++++ 6 files changed, 89 insertions(+) create mode 100644 src/BarcodeGeneratorDynamicHTML.php create mode 100644 tests/BarcodeDynamicHtmlTest.php create mode 100644 tests/verified-files/081231723897-dynamic-code128.html diff --git a/.gitignore b/.gitignore index d069e29..ac4bc3c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ vendor composer.lock composer.phar +.phpunit.result.cache \ No newline at end of file diff --git a/Readme.md b/Readme.md index 3c3c104..54a9315 100644 --- a/Readme.md +++ b/Readme.md @@ -57,6 +57,7 @@ $generatorSVG = new Picqer\Barcode\BarcodeGeneratorSVG(); $generatorPNG = new Picqer\Barcode\BarcodeGeneratorPNG(); $generatorJPG = new Picqer\Barcode\BarcodeGeneratorJPG(); $generatorHTML = new Picqer\Barcode\BarcodeGeneratorHTML(); +$generatorHTML = new Picqer\Barcode\BarcodeGeneratorDynamicHTML(); ``` ## Accepted barcode types diff --git a/generate-verified-files.php b/generate-verified-files.php index d6d4145..a280be8 100644 --- a/generate-verified-files.php +++ b/generate-verified-files.php @@ -12,6 +12,9 @@ function getSaveFilename($value) { $generatorHTML = new Picqer\Barcode\BarcodeGeneratorHTML(); file_put_contents('tests/verified-files/081231723897-code128.html', $generatorHTML->getBarcode('081231723897', $generatorHTML::TYPE_CODE_128)); +$generatorDynamicHTML = new Picqer\Barcode\BarcodeGeneratorDynamicHTML(); +file_put_contents('tests/verified-files/081231723897-dynamic-code128.html', $generatorDynamicHTML->getBarcode('081231723897', $generatorDynamicHTML::TYPE_CODE_128)); + $generatorSVG = new Picqer\Barcode\BarcodeGeneratorSVG(); file_put_contents('tests/verified-files/0049000004632-ean13.svg', $generatorSVG->getBarcode('0049000004632', $generatorSVG::TYPE_EAN_13)); diff --git a/src/BarcodeGeneratorDynamicHTML.php b/src/BarcodeGeneratorDynamicHTML.php new file mode 100644 index 0000000..5025ffe --- /dev/null +++ b/src/BarcodeGeneratorDynamicHTML.php @@ -0,0 +1,40 @@ +getBarcodeData($barcode, $type); + + $widthFactor = 100 / $barcodeData->getWidth(); + + $html = '
    ' . PHP_EOL; + + $positionHorizontal = 0; + /** @var BarcodeBar $bar */ + foreach ($barcodeData->getBars() as $bar) { + $barWidth = $bar->getWidth() / $widthFactor; + + if ($bar->isBar() && $barWidth > 0) { + // draw a vertical bar + $html .= '
     
    ' . PHP_EOL; + } + + $positionHorizontal += $barWidth; + } + + $html .= '
    ' . PHP_EOL; + + return $html; + } +} diff --git a/tests/BarcodeDynamicHtmlTest.php b/tests/BarcodeDynamicHtmlTest.php new file mode 100644 index 0000000..547f417 --- /dev/null +++ b/tests/BarcodeDynamicHtmlTest.php @@ -0,0 +1,14 @@ +getBarcode('081231723897', $generator::TYPE_CODE_128); + + $this->assertStringEqualsFile('tests/verified-files/081231723897-dynamic-code128.html', $generated); + } +} diff --git a/tests/verified-files/081231723897-dynamic-code128.html b/tests/verified-files/081231723897-dynamic-code128.html new file mode 100644 index 0000000..145066b --- /dev/null +++ b/tests/verified-files/081231723897-dynamic-code128.html @@ -0,0 +1,30 @@ +
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
    From da573956d2e354cbbfc3646300bd7eb6ad6bb58e Mon Sep 17 00:00:00 2001 From: Callum Smith Date: Tue, 16 Mar 2021 12:21:22 +0000 Subject: [PATCH 059/155] Correct math calcuation of bar width --- src/BarcodeGeneratorDynamicHTML.php | 2 +- .../081231723897-dynamic-code128.html | 56 +++++++++---------- 2 files changed, 29 insertions(+), 29 deletions(-) diff --git a/src/BarcodeGeneratorDynamicHTML.php b/src/BarcodeGeneratorDynamicHTML.php index 5025ffe..d2ca681 100644 --- a/src/BarcodeGeneratorDynamicHTML.php +++ b/src/BarcodeGeneratorDynamicHTML.php @@ -23,7 +23,7 @@ public function getBarcode($barcode, $type, string $foregroundColor = 'black') $positionHorizontal = 0; /** @var BarcodeBar $bar */ foreach ($barcodeData->getBars() as $bar) { - $barWidth = $bar->getWidth() / $widthFactor; + $barWidth = $bar->getWidth() * $widthFactor; if ($bar->isBar() && $barWidth > 0) { // draw a vertical bar diff --git a/tests/verified-files/081231723897-dynamic-code128.html b/tests/verified-files/081231723897-dynamic-code128.html index 145066b..b097b59 100644 --- a/tests/verified-files/081231723897-dynamic-code128.html +++ b/tests/verified-files/081231723897-dynamic-code128.html @@ -1,30 +1,30 @@
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    From c1e82d7be077ceddbddd177af068dd4c96fa7695 Mon Sep 17 00:00:00 2001 From: Casper Bakker Date: Sat, 27 Mar 2021 09:56:50 +0100 Subject: [PATCH 060/155] Add height support to Dynamic HTML renderer --- generate-verified-files.php | 4 ++ src/BarcodeGeneratorDynamicHTML.php | 11 +-- src/BarcodeGeneratorHTML.php | 1 + tests/BarcodeDynamicHtmlTest.php | 10 ++- tests/BarcodeHtmlTest.php | 8 +++ .../081231723897-dynamic-code128.html | 56 ++++++++-------- .../12345678903-dynamic-imb.html | 67 +++++++++++++++++++ tests/verified-files/12345678903-imb.html | 67 +++++++++++++++++++ 8 files changed, 191 insertions(+), 33 deletions(-) create mode 100644 tests/verified-files/12345678903-dynamic-imb.html create mode 100644 tests/verified-files/12345678903-imb.html diff --git a/generate-verified-files.php b/generate-verified-files.php index a280be8..4ec5bc2 100644 --- a/generate-verified-files.php +++ b/generate-verified-files.php @@ -12,9 +12,13 @@ function getSaveFilename($value) { $generatorHTML = new Picqer\Barcode\BarcodeGeneratorHTML(); file_put_contents('tests/verified-files/081231723897-code128.html', $generatorHTML->getBarcode('081231723897', $generatorHTML::TYPE_CODE_128)); +file_put_contents('tests/verified-files/12345678903-imb.html', $generatorHTML->getBarcode('12345678903', $generatorHTML::TYPE_IMB)); + $generatorDynamicHTML = new Picqer\Barcode\BarcodeGeneratorDynamicHTML(); file_put_contents('tests/verified-files/081231723897-dynamic-code128.html', $generatorDynamicHTML->getBarcode('081231723897', $generatorDynamicHTML::TYPE_CODE_128)); +file_put_contents('tests/verified-files/12345678903-dynamic-imb.html', $generatorDynamicHTML->getBarcode('12345678903', $generatorDynamicHTML::TYPE_IMB)); + $generatorSVG = new Picqer\Barcode\BarcodeGeneratorSVG(); file_put_contents('tests/verified-files/0049000004632-ean13.svg', $generatorSVG->getBarcode('0049000004632', $generatorSVG::TYPE_EAN_13)); diff --git a/src/BarcodeGeneratorDynamicHTML.php b/src/BarcodeGeneratorDynamicHTML.php index d2ca681..687e459 100644 --- a/src/BarcodeGeneratorDynamicHTML.php +++ b/src/BarcodeGeneratorDynamicHTML.php @@ -4,6 +4,8 @@ class BarcodeGeneratorDynamicHTML extends BarcodeGenerator { + private const WIDTH_PRECISION = 6; + /** * Return an HTML representation of barcode. * @@ -16,18 +18,19 @@ public function getBarcode($barcode, $type, string $foregroundColor = 'black') { $barcodeData = $this->getBarcodeData($barcode, $type); - $widthFactor = 100 / $barcodeData->getWidth(); - $html = '
    ' . PHP_EOL; $positionHorizontal = 0; /** @var BarcodeBar $bar */ foreach ($barcodeData->getBars() as $bar) { - $barWidth = $bar->getWidth() * $widthFactor; + $barWidth = $bar->getWidth() / $barcodeData->getWidth() * 100; + $barHeight = round(($bar->getHeight() / $barcodeData->getHeight() * 100), 3); if ($bar->isBar() && $barWidth > 0) { + $positionVertical = round(($bar->getPositionVertical() / $barcodeData->getHeight() * 100), 3); + // draw a vertical bar - $html .= '
     
    ' . PHP_EOL; + $html .= '
     
    ' . PHP_EOL; } $positionHorizontal += $barWidth; diff --git a/src/BarcodeGeneratorHTML.php b/src/BarcodeGeneratorHTML.php index 86d87da..dc2fef0 100644 --- a/src/BarcodeGeneratorHTML.php +++ b/src/BarcodeGeneratorHTML.php @@ -28,6 +28,7 @@ public function getBarcode($barcode, $type, int $widthFactor = 2, int $height = if ($bar->isBar() && $barWidth > 0) { $positionVertical = round(($bar->getPositionVertical() * $height / $barcodeData->getHeight()), 3); + // draw a vertical bar $html .= '
     
    ' . PHP_EOL; } diff --git a/tests/BarcodeDynamicHtmlTest.php b/tests/BarcodeDynamicHtmlTest.php index 547f417..3762b0e 100644 --- a/tests/BarcodeDynamicHtmlTest.php +++ b/tests/BarcodeDynamicHtmlTest.php @@ -4,11 +4,19 @@ class BarcodeDynamicHtmlTest extends TestCase { - public function test_html_barcode_generator_can_generate_code_128_barcode() + public function test_dynamic_html_barcode_generator_can_generate_code_128_barcode() { $generator = new Picqer\Barcode\BarcodeGeneratorDynamicHTML(); $generated = $generator->getBarcode('081231723897', $generator::TYPE_CODE_128); $this->assertStringEqualsFile('tests/verified-files/081231723897-dynamic-code128.html', $generated); } + + public function test_dynamic_html_barcode_generator_can_generate_imb_barcode_to_test_heights() + { + $generator = new Picqer\Barcode\BarcodeGeneratorDynamicHTML(); + $generated = $generator->getBarcode('12345678903', $generator::TYPE_IMB); + + $this->assertStringEqualsFile('tests/verified-files/12345678903-dynamic-imb.html', $generated); + } } diff --git a/tests/BarcodeHtmlTest.php b/tests/BarcodeHtmlTest.php index bf5263d..ecdf617 100644 --- a/tests/BarcodeHtmlTest.php +++ b/tests/BarcodeHtmlTest.php @@ -11,4 +11,12 @@ public function test_html_barcode_generator_can_generate_code_128_barcode() $this->assertStringEqualsFile('tests/verified-files/081231723897-code128.html', $generated); } + + public function test_html_barcode_generator_can_generate_imb_barcode_to_test_heights() + { + $generator = new Picqer\Barcode\BarcodeGeneratorHTML(); + $generated = $generator->getBarcode('12345678903', $generator::TYPE_IMB); + + $this->assertStringEqualsFile('tests/verified-files/12345678903-imb.html', $generated); + } } diff --git a/tests/verified-files/081231723897-dynamic-code128.html b/tests/verified-files/081231723897-dynamic-code128.html index b097b59..21a472b 100644 --- a/tests/verified-files/081231723897-dynamic-code128.html +++ b/tests/verified-files/081231723897-dynamic-code128.html @@ -1,30 +1,30 @@
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    diff --git a/tests/verified-files/12345678903-dynamic-imb.html b/tests/verified-files/12345678903-dynamic-imb.html new file mode 100644 index 0000000..88b9b43 --- /dev/null +++ b/tests/verified-files/12345678903-dynamic-imb.html @@ -0,0 +1,67 @@ +
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
    diff --git a/tests/verified-files/12345678903-imb.html b/tests/verified-files/12345678903-imb.html new file mode 100644 index 0000000..6e083b8 --- /dev/null +++ b/tests/verified-files/12345678903-imb.html @@ -0,0 +1,67 @@ +
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
    From acf32dab6c640905d49ae80a9f815d02a0d5a51d Mon Sep 17 00:00:00 2001 From: Casper Bakker Date: Sat, 27 Mar 2021 09:58:41 +0100 Subject: [PATCH 061/155] Shorten generated HTML --- src/BarcodeGeneratorDynamicHTML.php | 2 +- src/BarcodeGeneratorHTML.php | 2 +- .../verified-files/081231723897-code128.html | 56 ++++---- .../081231723897-dynamic-code128.html | 56 ++++---- .../12345678903-dynamic-imb.html | 130 +++++++++--------- tests/verified-files/12345678903-imb.html | 130 +++++++++--------- 6 files changed, 188 insertions(+), 188 deletions(-) diff --git a/src/BarcodeGeneratorDynamicHTML.php b/src/BarcodeGeneratorDynamicHTML.php index 687e459..5b1ab5c 100644 --- a/src/BarcodeGeneratorDynamicHTML.php +++ b/src/BarcodeGeneratorDynamicHTML.php @@ -30,7 +30,7 @@ public function getBarcode($barcode, $type, string $foregroundColor = 'black') $positionVertical = round(($bar->getPositionVertical() / $barcodeData->getHeight() * 100), 3); // draw a vertical bar - $html .= '
     
    ' . PHP_EOL; + $html .= '
     
    ' . PHP_EOL; } $positionHorizontal += $barWidth; diff --git a/src/BarcodeGeneratorHTML.php b/src/BarcodeGeneratorHTML.php index dc2fef0..45ec035 100644 --- a/src/BarcodeGeneratorHTML.php +++ b/src/BarcodeGeneratorHTML.php @@ -30,7 +30,7 @@ public function getBarcode($barcode, $type, int $widthFactor = 2, int $height = $positionVertical = round(($bar->getPositionVertical() * $height / $barcodeData->getHeight()), 3); // draw a vertical bar - $html .= '
     
    ' . PHP_EOL; + $html .= '
     
    ' . PHP_EOL; } $positionHorizontal += $barWidth; diff --git a/tests/verified-files/081231723897-code128.html b/tests/verified-files/081231723897-code128.html index 503081a..e9cf37a 100644 --- a/tests/verified-files/081231723897-code128.html +++ b/tests/verified-files/081231723897-code128.html @@ -1,30 +1,30 @@
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    diff --git a/tests/verified-files/081231723897-dynamic-code128.html b/tests/verified-files/081231723897-dynamic-code128.html index 21a472b..cdf2d94 100644 --- a/tests/verified-files/081231723897-dynamic-code128.html +++ b/tests/verified-files/081231723897-dynamic-code128.html @@ -1,30 +1,30 @@
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    diff --git a/tests/verified-files/12345678903-dynamic-imb.html b/tests/verified-files/12345678903-dynamic-imb.html index 88b9b43..c06f1fb 100644 --- a/tests/verified-files/12345678903-dynamic-imb.html +++ b/tests/verified-files/12345678903-dynamic-imb.html @@ -1,67 +1,67 @@
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    diff --git a/tests/verified-files/12345678903-imb.html b/tests/verified-files/12345678903-imb.html index 6e083b8..eba3400 100644 --- a/tests/verified-files/12345678903-imb.html +++ b/tests/verified-files/12345678903-imb.html @@ -1,67 +1,67 @@
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    From aca473c9f39607afe12f189eddd958f22ca73b4b Mon Sep 17 00:00:00 2001 From: Casper Bakker Date: Sat, 27 Mar 2021 10:01:46 +0100 Subject: [PATCH 062/155] Explain difference to HTML and Dynamic HTML --- Readme.md | 10 +++++----- src/BarcodeGeneratorDynamicHTML.php | 1 + src/BarcodeGeneratorHTML.php | 1 + 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/Readme.md b/Readme.md index 54a9315..a00482b 100644 --- a/Readme.md +++ b/Readme.md @@ -53,11 +53,11 @@ file_put_contents('barcode.png', $generator->getBarcode('081231723897', $generat ## Image types ```php -$generatorSVG = new Picqer\Barcode\BarcodeGeneratorSVG(); -$generatorPNG = new Picqer\Barcode\BarcodeGeneratorPNG(); -$generatorJPG = new Picqer\Barcode\BarcodeGeneratorJPG(); -$generatorHTML = new Picqer\Barcode\BarcodeGeneratorHTML(); -$generatorHTML = new Picqer\Barcode\BarcodeGeneratorDynamicHTML(); +$generatorSVG = new Picqer\Barcode\BarcodeGeneratorSVG(); // Vector based SVG +$generatorPNG = new Picqer\Barcode\BarcodeGeneratorPNG(); // Pixel based PNG +$generatorJPG = new Picqer\Barcode\BarcodeGeneratorJPG(); // Pixel based JPG +$generatorHTML = new Picqer\Barcode\BarcodeGeneratorHTML(); // Pixel based HTML +$generatorHTML = new Picqer\Barcode\BarcodeGeneratorDynamicHTML(); // Vector based HTML ``` ## Accepted barcode types diff --git a/src/BarcodeGeneratorDynamicHTML.php b/src/BarcodeGeneratorDynamicHTML.php index 5b1ab5c..d2e7cb5 100644 --- a/src/BarcodeGeneratorDynamicHTML.php +++ b/src/BarcodeGeneratorDynamicHTML.php @@ -8,6 +8,7 @@ class BarcodeGeneratorDynamicHTML extends BarcodeGenerator /** * Return an HTML representation of barcode. + * This 'dynamic' version uses percentage based widths and heights, resulting in a vector-y qualitative result. * * @param string $barcode code to print * @param string $type type of barcode diff --git a/src/BarcodeGeneratorHTML.php b/src/BarcodeGeneratorHTML.php index 45ec035..25a8820 100644 --- a/src/BarcodeGeneratorHTML.php +++ b/src/BarcodeGeneratorHTML.php @@ -6,6 +6,7 @@ class BarcodeGeneratorHTML extends BarcodeGenerator { /** * Return an HTML representation of barcode. + * This original version uses pixel based widths and heights. Use Dynamic HTML version for better quality representation. * * @param string $barcode code to print * @param string $type type of barcode From aefe3237448a35e169feb980acce85e59be4b1a7 Mon Sep 17 00:00:00 2001 From: Casper Bakker Date: Sat, 27 Mar 2021 10:04:45 +0100 Subject: [PATCH 063/155] Run tests on pull request --- .github/workflows/phpunit.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/phpunit.yml b/.github/workflows/phpunit.yml index 58d5ed1..cbe1a6a 100644 --- a/.github/workflows/phpunit.yml +++ b/.github/workflows/phpunit.yml @@ -1,6 +1,6 @@ name: phpunit -on: [push] +on: [push, pull_request] jobs: build: From 671552e99755be7340027fd0ab48787770b0b749 Mon Sep 17 00:00:00 2001 From: Igor Tverdokhleb Date: Thu, 26 Aug 2021 01:14:59 +0000 Subject: [PATCH 064/155] Add ITF-14 barcode type --- src/BarcodeGenerator.php | 5 ++ src/Types/TypeTF14.php | 121 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 126 insertions(+) create mode 100644 src/Types/TypeTF14.php diff --git a/src/BarcodeGenerator.php b/src/BarcodeGenerator.php index 8a57e1e..828bf84 100644 --- a/src/BarcodeGenerator.php +++ b/src/BarcodeGenerator.php @@ -46,6 +46,7 @@ use Picqer\Barcode\Types\TypeIntelligentMailBarcode; use Picqer\Barcode\Types\TypeInterleaved25; use Picqer\Barcode\Types\TypeInterleaved25Checksum; +use Picqer\Barcode\Types\TypeITF14; use Picqer\Barcode\Types\TypeKix; use Picqer\Barcode\Types\TypeMsi; use Picqer\Barcode\Types\TypeMsiChecksum; @@ -72,6 +73,7 @@ abstract class BarcodeGenerator const TYPE_STANDARD_2_5_CHECKSUM = 'S25+'; const TYPE_INTERLEAVED_2_5 = 'I25'; const TYPE_INTERLEAVED_2_5_CHECKSUM = 'I25+'; + const TYPE_ITF_14 = 'ITF14'; const TYPE_CODE_128 = 'C128'; const TYPE_CODE_128_A = 'C128A'; const TYPE_CODE_128_B = 'C128B'; @@ -131,6 +133,9 @@ protected function createDataBuilderForType(string $type) case self::TYPE_INTERLEAVED_2_5_CHECKSUM: return new TypeInterleaved25Checksum(); + case self::TYPE_ITF_14: + return new TypeITF14(); + case self::TYPE_CODE_128: return new TypeCode128(); diff --git a/src/Types/TypeTF14.php b/src/Types/TypeTF14.php new file mode 100644 index 0000000..0f59f86 --- /dev/null +++ b/src/Types/TypeTF14.php @@ -0,0 +1,121 @@ + 14 || strlen($code) < 13) { + throw new InvalidLengthException(); + } + + $k = 0; + $pbegin = "1010"; + $pbeginarr = str_split($pbegin); + + foreach ($pbeginarr as $x) { + $t = $x === '1'; + $w = 1; + + $barcode->addBar(new BarcodeBar($w, 1, $t)); + ++$k; + } + + for ($i = 0; $i < strlen($code); $i += 2) { + + if (!isset($chr[$code{$i}]) || !isset($chr[$code{$i + 1}])) { + throw new InvalidCharacterException(); + } + + $bars = true; + $pbars = $chr[$code[$i]]; + $pspaces = $chr[$code[$i + 1]]; + $pmixed = ""; + + + while (strlen($pbars) > 0) { + $pmixed .= $pbars[0] . $pspaces[0]; + $pbars = substr($pbars, 1); + $pspaces = substr($pspaces, 1); + } + + $pmixedarr = str_split($pmixed); + + foreach ($pmixedarr as $x) { + if ($bars) { + $t = true; + $w = ($x === '1') ? '1' : '2'; + } else { + $t = false; + $w = ($x === '1') ? '1' : '2'; + } + + $barcode->addBar(new BarcodeBar($w, 1, $t)); + $bars = !$bars; + ++$k; + } + } + + $pend = "1101"; + $pendarr = str_split($pend); + + foreach ($pendarr as $x) { + $t = $x == '1'; + $w = 1; + + $barcode->addBar(new BarcodeBar($w, 1, $t)); + ++$k; + } + + return $barcode; + } +} From d8354c1e096ae4cd92fdf864d779bebb95df903a Mon Sep 17 00:00:00 2001 From: Igor Tverdokhleb Date: Wed, 15 Sep 2021 17:34:35 +0000 Subject: [PATCH 065/155] fix(TypeITF14): codestyle --- src/Types/TypeTF14.php | 27 ++++++++------------------- 1 file changed, 8 insertions(+), 19 deletions(-) diff --git a/src/Types/TypeTF14.php b/src/Types/TypeTF14.php index 0f59f86..f1a9635 100644 --- a/src/Types/TypeTF14.php +++ b/src/Types/TypeTF14.php @@ -1,6 +1,4 @@ -addBar(new BarcodeBar($w, 1, $t)); - $bars = !$bars; + $bars = !$bars; ++$k; } } From ae5908389c8a59f49575437b5ab88554888bd3a8 Mon Sep 17 00:00:00 2001 From: Str4to Date: Thu, 30 Sep 2021 16:34:10 +0200 Subject: [PATCH 066/155] TypeCode32 class definition --- src/Types/TypeCode32.php | 61 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 src/Types/TypeCode32.php diff --git a/src/Types/TypeCode32.php b/src/Types/TypeCode32.php new file mode 100644 index 0000000..8dd3ed6 --- /dev/null +++ b/src/Types/TypeCode32.php @@ -0,0 +1,61 @@ + '0', + '1' => '1', + '2' => '2', + '3' => '3', + '4' => '4', + '5' => '5', + '6' => '6', + '7' => '7', + '8' => '8', + '9' => '9', + '10' => 'B', + '11' => 'C', + '12' => 'D', + '13' => 'F', + '14' => 'G', + '15' => 'H', + '16' => 'J', + '17' => 'K', + '18' => 'L', + '19' => 'M', + '20' => 'N', + '21' => 'P', + '22' => 'Q', + '23' => 'R', + '24' => 'S', + '25' => 'T', + '26' => 'U', + '27' => 'V', + '28' => 'W', + '29' => 'X', + '30' => 'Y', + '31' => 'Z' + ]; + + + + public function getBarcodeData(string $code): Barcode + { + $code39=""; + $codeElab=$code; + for($e=5;$e>=0;$e--) + { + $code39.=$this->conversionTable32[intval($codeElab / pow(32,$e))]; + $codeElab=intval($codeElab % pow(32,$e)); + } + return parent::getBarcodeData($code39); + } +} From 53b3172dfa42b10efa4128f81f2b50fc8e395b14 Mon Sep 17 00:00:00 2001 From: Str4to Date: Thu, 30 Sep 2021 16:35:46 +0200 Subject: [PATCH 067/155] TypeCode32 use declaration in abstract class --- src/BarcodeGenerator.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/BarcodeGenerator.php b/src/BarcodeGenerator.php index 8a57e1e..69974e2 100644 --- a/src/BarcodeGenerator.php +++ b/src/BarcodeGenerator.php @@ -36,6 +36,7 @@ use Picqer\Barcode\Types\TypeCode128A; use Picqer\Barcode\Types\TypeCode128B; use Picqer\Barcode\Types\TypeCode128C; +use Picqer\Barcode\Types\TypeCode32; use Picqer\Barcode\Types\TypeCode39; use Picqer\Barcode\Types\TypeCode39Checksum; use Picqer\Barcode\Types\TypeCode39Extended; @@ -63,6 +64,7 @@ abstract class BarcodeGenerator { + const TYPE_CODE_32 = 'C32'; const TYPE_CODE_39 = 'C39'; const TYPE_CODE_39_CHECKSUM = 'C39+'; const TYPE_CODE_39E = 'C39E'; // CODE 39 EXTENDED @@ -104,6 +106,9 @@ protected function getBarcodeData(string $code, string $type): Barcode protected function createDataBuilderForType(string $type) { switch (strtoupper($type)) { + case self::TYPE_CODE_32: + return new TypeCode32(); + case self::TYPE_CODE_39: return new TypeCode39(); From 01f34d31cc0aa20cd416cb4b1fda6904aeab2e1e Mon Sep 17 00:00:00 2001 From: Str4to Date: Thu, 30 Sep 2021 16:42:17 +0200 Subject: [PATCH 068/155] Added TypeCode32 definition --- Readme.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Readme.md b/Readme.md index 3c3c104..a395de3 100644 --- a/Readme.md +++ b/Readme.md @@ -64,6 +64,7 @@ These barcode types are supported. All types support different character sets or Most used types are TYPE_CODE_128 and TYPE_CODE_39. Because of the best scanner support, variable length and most chars supported. +- TYPE_CODE_32 (italian pharmaceutical code 'MINSAN') - TYPE_CODE_39 - TYPE_CODE_39_CHECKSUM - TYPE_CODE_39E @@ -117,4 +118,4 @@ file_put_contents('barcode.jpg', $generator->getBarcode('081231723897', $generat ### Oneliner SVG output to disk ```php file_put_contents('barcode.svg', (new Picqer\Barcode\BarcodeGeneratorSVG())->getBarcode('6825ME601', Picqer\Barcode\BarcodeGeneratorSVG::TYPE_KIX)); -``` \ No newline at end of file +``` From 4bd841e774e5b3cdfeb386c66499b6d94c278b9e Mon Sep 17 00:00:00 2001 From: turbopixel Date: Fri, 24 Dec 2021 14:37:38 +0100 Subject: [PATCH 069/155] fix deprecated: strtoupper(): Passing null to parameter (#144) --- src/Types/TypeCode128.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Types/TypeCode128.php b/src/Types/TypeCode128.php index 3bf04f2..0f4c5a5 100644 --- a/src/Types/TypeCode128.php +++ b/src/Types/TypeCode128.php @@ -156,7 +156,7 @@ public function getBarcodeData(string $code): Barcode // length of the code $len = strlen($code); - switch (strtoupper($this->type)) { + switch (strtoupper($this->type ?? "")) { case 'A': $startid = 103; for ($i = 0; $i < $len; ++$i) { From 86880749ebfc260ff8c4885d172c0b3a019efac3 Mon Sep 17 00:00:00 2001 From: Casper Bakker Date: Fri, 6 May 2022 08:56:04 +0200 Subject: [PATCH 070/155] Fix encoding of char 11 in Code39e --- src/Types/TypeCode39.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Types/TypeCode39.php b/src/Types/TypeCode39.php index 4cc32b8..cce4e82 100644 --- a/src/Types/TypeCode39.php +++ b/src/Types/TypeCode39.php @@ -132,7 +132,7 @@ protected function encode_code39_ext($code) chr(8) => '$H', chr(9) => '$I', chr(10) => '$J', - chr(11) => '£K', + chr(11) => '$K', chr(12) => '$L', chr(13) => '$M', chr(14) => '$N', From a55b483932772416296741b311ce48c828f9ff03 Mon Sep 17 00:00:00 2001 From: Casper Bakker Date: Fri, 6 May 2022 09:11:26 +0200 Subject: [PATCH 071/155] Fix conversion of code 93 --- .github/workflows/phpunit.yml | 2 +- src/Types/TypeCode93.php | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/.github/workflows/phpunit.yml b/.github/workflows/phpunit.yml index cbe1a6a..afa6d9c 100644 --- a/.github/workflows/phpunit.yml +++ b/.github/workflows/phpunit.yml @@ -9,7 +9,7 @@ jobs: strategy: matrix: - php-versions: ['7.3', '7.4', '8.0'] + php-versions: ['7.3', '7.4', '8.0', '8.1'] steps: - uses: actions/checkout@v2 diff --git a/src/Types/TypeCode93.php b/src/Types/TypeCode93.php index e91b2c1..24bebca 100644 --- a/src/Types/TypeCode93.php +++ b/src/Types/TypeCode93.php @@ -105,18 +105,18 @@ public function getBarcodeData(string $code): Barcode chr(33) => chr(129) . 'A', chr(34) => chr(129) . 'B', chr(35) => chr(129) . 'C', - chr(36) => chr(129) . 'D', - chr(37) => chr(129) . 'E', + chr(36) => chr(36), // "$" + chr(37) => chr(37), // "%" chr(38) => chr(129) . 'F', chr(39) => chr(129) . 'G', chr(40) => chr(129) . 'H', chr(41) => chr(129) . 'I', chr(42) => chr(129) . 'J', - chr(43) => chr(129) . 'K', + chr(43) => chr(43), // "+" chr(44) => chr(129) . 'L', chr(45) => '-', chr(46) => '.', - chr(47) => chr(129) . 'O', + chr(47) => chr(47), // "/" chr(48) => '0', chr(49) => '1', chr(50) => '2', From d9414c34ec98bccae462ff9f90e76ef9040dbac3 Mon Sep 17 00:00:00 2001 From: Casper Bakker Date: Fri, 6 May 2022 09:11:53 +0200 Subject: [PATCH 072/155] Add reference to header --- src/Types/TypeCode93.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Types/TypeCode93.php b/src/Types/TypeCode93.php index 24bebca..e89c9bd 100644 --- a/src/Types/TypeCode93.php +++ b/src/Types/TypeCode93.php @@ -9,6 +9,8 @@ /* * CODE 93 - USS-93 * Compact code similar to Code 39 + * + * reference: https://en.wikipedia.org/wiki/Code_93#Full_ASCII_Code_93 */ class TypeCode93 implements TypeInterface From a875c84f95b4b43047111fbd804f63b0e3d4a31b Mon Sep 17 00:00:00 2001 From: Casper Bakker Date: Fri, 6 May 2022 09:19:17 +0200 Subject: [PATCH 073/155] Fix codestyle --- src/Types/TypeCode32.php | 29 ++++++++++++++--------------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/src/Types/TypeCode32.php b/src/Types/TypeCode32.php index 8dd3ed6..a9ad515 100644 --- a/src/Types/TypeCode32.php +++ b/src/Types/TypeCode32.php @@ -3,14 +3,14 @@ namespace Picqer\Barcode\Types; use Picqer\Barcode\Barcode; + /* * CODE 32 - italian pharmaceutical * General-purpose code in very wide use world-wide */ - class TypeCode32 extends TypeCode39 { - protected $conversionTable32 = [ + protected $conversionTable32 = [ '0' => '0', '1' => '1', '2' => '2', @@ -44,18 +44,17 @@ class TypeCode32 extends TypeCode39 '30' => 'Y', '31' => 'Z' ]; - - - - public function getBarcodeData(string $code): Barcode - { - $code39=""; - $codeElab=$code; - for($e=5;$e>=0;$e--) - { - $code39.=$this->conversionTable32[intval($codeElab / pow(32,$e))]; - $codeElab=intval($codeElab % pow(32,$e)); - } - return parent::getBarcodeData($code39); + + public function getBarcodeData(string $code): Barcode + { + $code39 = ''; + $codeElab = $code; + + for ($e = 5; $e >= 0; $e--) { + $code39 .= $this->conversionTable32[intval($codeElab / pow(32,$e))]; + $codeElab = intval($codeElab % pow(32,$e)); + } + + return parent::getBarcodeData($code39); } } From 938fd4e80505cb681a8c7de47e23a96ad17267b8 Mon Sep 17 00:00:00 2001 From: Casper Bakker Date: Tue, 31 May 2022 09:43:54 +0200 Subject: [PATCH 074/155] Use white as default background in JPG images with Imagick --- src/BarcodeGeneratorJPG.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/BarcodeGeneratorJPG.php b/src/BarcodeGeneratorJPG.php index bdd88cc..0e33d70 100644 --- a/src/BarcodeGeneratorJPG.php +++ b/src/BarcodeGeneratorJPG.php @@ -9,7 +9,7 @@ class BarcodeGeneratorJPG extends BarcodeGeneratorPNG protected function createImagickImageObject(int $width, int $height): Imagick { $image = new Imagick(); - $image->newImage($width, $height, 'none', 'JPG'); + $image->newImage($width, $height, 'white', 'JPG'); return $image; } From 9e773e1ca0c20d88b72e07986b8e9d129671d78b Mon Sep 17 00:00:00 2001 From: bkr Date: Mon, 13 Jun 2022 12:00:26 +0200 Subject: [PATCH 075/155] * Fixed an error in \Picqer\Barcode\Types\TypeCode11::getCheckDigitK(), which was thrown every time a code longer than 10 characters (including the first check digit) has been used. --- src/Types/TypeCode11.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Types/TypeCode11.php b/src/Types/TypeCode11.php index ffba8d5..7352e99 100644 --- a/src/Types/TypeCode11.php +++ b/src/Types/TypeCode11.php @@ -91,7 +91,7 @@ private function getCheckDigitK(string $code): string $p = 1; $check = 0; - for ($i = strlen($code); $i >= 0; --$i) { + for ($i = (strlen($code) - 1); $i >= 0; --$i) { $digit = $code[$i]; if ($digit == '-') { $dval = 10; From 29daa92b97b1ab9f66e60031918a5b31827abfa4 Mon Sep 17 00:00:00 2001 From: bkr Date: Mon, 13 Jun 2022 14:48:20 +0200 Subject: [PATCH 076/155] * Fixed an error by replacing the escape characters. Before, they were chr(128-131) (which did return an unknown character in some environments); now, they are "a", "b", "c" and "d". * Fixed wrong conversions for characters "($)", "(%)", "(/)" and "(+)" (because they were not in order, but conversions have been copied in order). * Fixed wrong encodings for some characters. * Fixed a bug, that lower case letters were impossible to use with Code 93. * * Even though \Picqer\Barcode\Types\TypeCode93 is implemented to work with Code 93 Extended, at the start of ::getBarcodeData() was a strtoupper(). * Removed translation of special characters in ::checksum_code93(), because it is no longer necessary. --- src/Types/TypeCode93.php | 198 +++++++++++++++++++-------------------- 1 file changed, 95 insertions(+), 103 deletions(-) diff --git a/src/Types/TypeCode93.php b/src/Types/TypeCode93.php index e89c9bd..bca30be 100644 --- a/src/Types/TypeCode93.php +++ b/src/Types/TypeCode93.php @@ -59,66 +59,64 @@ class TypeCode93 implements TypeInterface 47 => '112131', // / 43 => '113121', // + 37 => '211131', // % - 128 => '121221', // ($) - 129 => '311121', // (/) - 130 => '122211', // (+) - 131 => '312111', // (%) + 97 => '121221', // ($) + 98 => '312111', // (%) + 99 => '311121', // (/) + 100 => '122211', // (+) 42 => '111141', // start-stop ]; public function getBarcodeData(string $code): Barcode { - $code = strtoupper($code); - $encode = [ - chr(0) => chr(131) . 'U', - chr(1) => chr(128) . 'A', - chr(2) => chr(128) . 'B', - chr(3) => chr(128) . 'C', - chr(4) => chr(128) . 'D', - chr(5) => chr(128) . 'E', - chr(6) => chr(128) . 'F', - chr(7) => chr(128) . 'G', - chr(8) => chr(128) . 'H', - chr(9) => chr(128) . 'I', - chr(10) => chr(128) . 'J', - chr(11) => '£K', - chr(12) => chr(128) . 'L', - chr(13) => chr(128) . 'M', - chr(14) => chr(128) . 'N', - chr(15) => chr(128) . 'O', - chr(16) => chr(128) . 'P', - chr(17) => chr(128) . 'Q', - chr(18) => chr(128) . 'R', - chr(19) => chr(128) . 'S', - chr(20) => chr(128) . 'T', - chr(21) => chr(128) . 'U', - chr(22) => chr(128) . 'V', - chr(23) => chr(128) . 'W', - chr(24) => chr(128) . 'X', - chr(25) => chr(128) . 'Y', - chr(26) => chr(128) . 'Z', - chr(27) => chr(131) . 'A', - chr(28) => chr(131) . 'B', - chr(29) => chr(131) . 'C', - chr(30) => chr(131) . 'D', - chr(31) => chr(131) . 'E', + chr(0) => 'bU', + chr(1) => 'aA', + chr(2) => 'aB', + chr(3) => 'aC', + chr(4) => 'aD', + chr(5) => 'aE', + chr(6) => 'aF', + chr(7) => 'aG', + chr(8) => 'aH', + chr(9) => 'aI', + chr(10) => 'aJ', + chr(11) => 'aK', + chr(12) => 'aL', + chr(13) => 'aM', + chr(14) => 'aN', + chr(15) => 'aO', + chr(16) => 'aP', + chr(17) => 'aQ', + chr(18) => 'aR', + chr(19) => 'aS', + chr(20) => 'aT', + chr(21) => 'aU', + chr(22) => 'aV', + chr(23) => 'aW', + chr(24) => 'aX', + chr(25) => 'aY', + chr(26) => 'aZ', + chr(27) => 'bA', + chr(28) => 'bB', + chr(29) => 'bC', + chr(30) => 'bD', + chr(31) => 'bE', chr(32) => ' ', - chr(33) => chr(129) . 'A', - chr(34) => chr(129) . 'B', - chr(35) => chr(129) . 'C', - chr(36) => chr(36), // "$" - chr(37) => chr(37), // "%" - chr(38) => chr(129) . 'F', - chr(39) => chr(129) . 'G', - chr(40) => chr(129) . 'H', - chr(41) => chr(129) . 'I', - chr(42) => chr(129) . 'J', - chr(43) => chr(43), // "+" - chr(44) => chr(129) . 'L', + chr(33) => 'cA', + chr(34) => 'cB', + chr(35) => 'cC', + chr(36) => '$', + chr(37) => '%', + chr(38) => 'cF', + chr(39) => 'cG', + chr(40) => 'cH', + chr(41) => 'cI', + chr(42) => 'cJ', + chr(43) => '+', + chr(44) => 'cL', chr(45) => '-', chr(46) => '.', - chr(47) => chr(47), // "/" + chr(47) => '/', chr(48) => '0', chr(49) => '1', chr(50) => '2', @@ -129,13 +127,13 @@ public function getBarcodeData(string $code): Barcode chr(55) => '7', chr(56) => '8', chr(57) => '9', - chr(58) => chr(129) . 'Z', - chr(59) => chr(131) . 'F', - chr(60) => chr(131) . 'G', - chr(61) => chr(131) . 'H', - chr(62) => chr(131) . 'I', - chr(63) => chr(131) . 'J', - chr(64) => chr(131) . 'V', + chr(58) => 'cZ', + chr(59) => 'bF', + chr(60) => 'bG', + chr(61) => 'bH', + chr(62) => 'bI', + chr(63) => 'bJ', + chr(64) => 'bV', chr(65) => 'A', chr(66) => 'B', chr(67) => 'C', @@ -162,43 +160,43 @@ public function getBarcodeData(string $code): Barcode chr(88) => 'X', chr(89) => 'Y', chr(90) => 'Z', - chr(91) => chr(131) . 'K', - chr(92) => chr(131) . 'L', - chr(93) => chr(131) . 'M', - chr(94) => chr(131) . 'N', - chr(95) => chr(131) . 'O', - chr(96) => chr(131) . 'W', - chr(97) => chr(130) . 'A', - chr(98) => chr(130) . 'B', - chr(99) => chr(130) . 'C', - chr(100) => chr(130) . 'D', - chr(101) => chr(130) . 'E', - chr(102) => chr(130) . 'F', - chr(103) => chr(130) . 'G', - chr(104) => chr(130) . 'H', - chr(105) => chr(130) . 'I', - chr(106) => chr(130) . 'J', - chr(107) => chr(130) . 'K', - chr(108) => chr(130) . 'L', - chr(109) => chr(130) . 'M', - chr(110) => chr(130) . 'N', - chr(111) => chr(130) . 'O', - chr(112) => chr(130) . 'P', - chr(113) => chr(130) . 'Q', - chr(114) => chr(130) . 'R', - chr(115) => chr(130) . 'S', - chr(116) => chr(130) . 'T', - chr(117) => chr(130) . 'U', - chr(118) => chr(130) . 'V', - chr(119) => chr(130) . 'W', - chr(120) => chr(130) . 'X', - chr(121) => chr(130) . 'Y', - chr(122) => chr(130) . 'Z', - chr(123) => chr(131) . 'P', - chr(124) => chr(131) . 'Q', - chr(125) => chr(131) . 'R', - chr(126) => chr(131) . 'S', - chr(127) => chr(131) . 'T', + chr(91) => 'bK', + chr(92) => 'bL', + chr(93) => 'bM', + chr(94) => 'bN', + chr(95) => 'bO', + chr(96) => 'bW', + chr(97) => 'dA', + chr(98) => 'dB', + chr(99) => 'dC', + chr(100) => 'dD', + chr(101) => 'dE', + chr(102) => 'dF', + chr(103) => 'dG', + chr(104) => 'dH', + chr(105) => 'dI', + chr(106) => 'dJ', + chr(107) => 'dK', + chr(108) => 'dL', + chr(109) => 'dM', + chr(110) => 'dN', + chr(111) => 'dO', + chr(112) => 'dP', + chr(113) => 'dQ', + chr(114) => 'dR', + chr(115) => 'dS', + chr(116) => 'dT', + chr(117) => 'dU', + chr(118) => 'dV', + chr(119) => 'dW', + chr(120) => 'dX', + chr(121) => 'dY', + chr(122) => 'dZ', + chr(123) => 'bP', + chr(124) => 'bQ', + chr(125) => 'bR', + chr(126) => 'bS', + chr(127) => 'bT', ]; $code_ext = ''; @@ -250,13 +248,10 @@ public function getBarcodeData(string $code): Barcode */ protected function checksum_code93($code) { - $chars = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '-', '.', ' ', '$', '/', '+', '%', '<', '=', '>', '?']; - - // translate special characters - $code = strtr($code, chr(128) . chr(131) . chr(129) . chr(130), '<=>?'); - $len = strlen($code); + $chars = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '-', '.', ' ', '$', '/', '+', '%', 'a', 'b', 'c', 'd']; // calculate check digit C + $len = strlen($code); $p = 1; $check = 0; for ($i = ($len - 1); $i >= 0; --$i) { @@ -287,9 +282,6 @@ protected function checksum_code93($code) $checksum = $c . $k; - // resto respecial characters - $checksum = strtr($checksum, '<=>?', chr(128) . chr(131) . chr(129) . chr(130)); - return $checksum; } } From bd730cf408f050372dd4d5934207ab99272116ae Mon Sep 17 00:00:00 2001 From: Casper Bakker Date: Tue, 14 Jun 2022 09:09:07 +0200 Subject: [PATCH 077/155] Recreate verify file --- .gitignore | 3 +- tests/verified-files/C93-1234567890abcABC.svg | 51 +++++++++++-------- 2 files changed, 32 insertions(+), 22 deletions(-) diff --git a/.gitignore b/.gitignore index ac4bc3c..71981a6 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ vendor composer.lock composer.phar -.phpunit.result.cache \ No newline at end of file +.phpunit.result.cache +.DS_Store diff --git a/tests/verified-files/C93-1234567890abcABC.svg b/tests/verified-files/C93-1234567890abcABC.svg index 2e18423..120c36f 100644 --- a/tests/verified-files/C93-1234567890abcABC.svg +++ b/tests/verified-files/C93-1234567890abcABC.svg @@ -1,7 +1,7 @@ - - *1234567890ABCABC2N* + + *1234567890dAdBdCABC6-* @@ -36,33 +36,42 @@ - - - + + + - - - + + + - - - - + + + + - - - - - - - - - + + + + + + + + + + + + + + + + + + From cf5b1d7bcc49a11ebb33daf42bf9d1a4bada5146 Mon Sep 17 00:00:00 2001 From: bkr Date: Wed, 29 Jun 2022 13:57:05 +0200 Subject: [PATCH 078/155] * Fixed a bug, that lower case letters were impossible to use with Code 39 Extended. --- src/Types/TypeCode39.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/Types/TypeCode39.php b/src/Types/TypeCode39.php index cce4e82..5a4a2d2 100644 --- a/src/Types/TypeCode39.php +++ b/src/Types/TypeCode39.php @@ -70,8 +70,6 @@ public function getBarcodeData(string $code): Barcode throw new InvalidLengthException('You should provide a barcode string.'); } - $code = strtoupper($code); - if ($this->extended) { // extended mode $code = $this->encode_code39_ext($code); From 8d8c358a3095b0791d89ceba34b19f6c064c5311 Mon Sep 17 00:00:00 2001 From: bkr Date: Wed, 29 Jun 2022 14:28:40 +0200 Subject: [PATCH 079/155] * Fixed failing tests. --- tests/TypesTest.php | 14 +++- tests/VerifiedBarcodeTest.php | 5 +- ...67890abcABC.svg => C39+-1234567890ABC.svg} | 37 +++------ tests/verified-files/C39-1234567890ABC.svg | 82 +++++++++++++++++++ ...90abcABC.svg => C39E-1234567890abcABC.svg} | 0 5 files changed, 107 insertions(+), 31 deletions(-) rename tests/verified-files/{C39+-1234567890abcABC.svg => C39+-1234567890ABC.svg} (74%) create mode 100644 tests/verified-files/C39-1234567890ABC.svg rename tests/verified-files/{C39-1234567890abcABC.svg => C39E-1234567890abcABC.svg} (100%) diff --git a/tests/TypesTest.php b/tests/TypesTest.php index 16b3083..7e7658b 100644 --- a/tests/TypesTest.php +++ b/tests/TypesTest.php @@ -7,19 +7,27 @@ class TypesTest extends TestCase public function test_generator_can_generate_code_39_barcode() { $generator = new Picqer\Barcode\BarcodeGeneratorSVG(); - $result = $generator->getBarcode('1234567890abcABC', $generator::TYPE_CODE_39); + $result = $generator->getBarcode('1234567890ABC', $generator::TYPE_CODE_39); - $this->assertStringEqualsFile('tests/verified-files/C39-1234567890abcABC.svg', $result); + $this->assertStringEqualsFile('tests/verified-files/C39-1234567890ABC.svg', $result); } public function test_generator_can_generate_code_39_checksum_barcode() { $generator = new Picqer\Barcode\BarcodeGeneratorSVG(); - $result = $generator->getBarcode('1234567890abcABC', $generator::TYPE_CODE_39_CHECKSUM); + $result = $generator->getBarcode('1234567890ABC', $generator::TYPE_CODE_39_CHECKSUM); $this->assertGreaterThan(100, strlen($result)); } + public function test_generator_can_generate_code_39_extended_barcode() + { + $generator = new Picqer\Barcode\BarcodeGeneratorSVG(); + $result = $generator->getBarcode('1234567890abcABC', $generator::TYPE_CODE_39); + + $this->assertStringEqualsFile('tests/verified-files/C39E-1234567890abcABC.svg', $result); + } + public function test_generator_can_generate_code_39_extended_checksum_barcode() { $generator = new Picqer\Barcode\BarcodeGeneratorSVG(); diff --git a/tests/VerifiedBarcodeTest.php b/tests/VerifiedBarcodeTest.php index 95e4ae3..9c6ac3e 100644 --- a/tests/VerifiedBarcodeTest.php +++ b/tests/VerifiedBarcodeTest.php @@ -14,8 +14,9 @@ class VerifiedBarcodeTest extends TestCase { public static $supportedBarcodes = [ - ['type' => BarcodeGenerator::TYPE_CODE_39, 'barcodes' => ['1234567890abcABC']], - ['type' => BarcodeGenerator::TYPE_CODE_39_CHECKSUM, 'barcodes' => ['1234567890abcABC']], + ['type' => BarcodeGenerator::TYPE_CODE_39, 'barcodes' => ['1234567890ABC']], + ['type' => BarcodeGenerator::TYPE_CODE_39_CHECKSUM, 'barcodes' => ['1234567890ABC']], + ['type' => BarcodeGenerator::TYPE_CODE_39E, 'barcodes' => ['1234567890abcABC']], ['type' => BarcodeGenerator::TYPE_CODE_39E_CHECKSUM, 'barcodes' => ['1234567890abcABC']], ['type' => BarcodeGenerator::TYPE_CODE_93, 'barcodes' => ['1234567890abcABC']], ['type' => BarcodeGenerator::TYPE_STANDARD_2_5, 'barcodes' => ['1234567890']], diff --git a/tests/verified-files/C39+-1234567890abcABC.svg b/tests/verified-files/C39+-1234567890ABC.svg similarity index 74% rename from tests/verified-files/C39+-1234567890abcABC.svg rename to tests/verified-files/C39+-1234567890ABC.svg index f5ae78d..280551a 100644 --- a/tests/verified-files/C39+-1234567890abcABC.svg +++ b/tests/verified-files/C39+-1234567890ABC.svg @@ -1,7 +1,7 @@ - - *1234567890ABCABCP* + + *1234567890ABCZ* @@ -73,30 +73,15 @@ - - - - - + + + + + - - - - - - - - - - - - - - - - - - - + + + + diff --git a/tests/verified-files/C39-1234567890ABC.svg b/tests/verified-files/C39-1234567890ABC.svg new file mode 100644 index 0000000..448792d --- /dev/null +++ b/tests/verified-files/C39-1234567890ABC.svg @@ -0,0 +1,82 @@ + + + + *1234567890ABC* + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/tests/verified-files/C39-1234567890abcABC.svg b/tests/verified-files/C39E-1234567890abcABC.svg similarity index 100% rename from tests/verified-files/C39-1234567890abcABC.svg rename to tests/verified-files/C39E-1234567890abcABC.svg From 85d0feb8976f93cd65e7d5a0a4a736201a70cde1 Mon Sep 17 00:00:00 2001 From: bkr Date: Wed, 29 Jun 2022 14:34:15 +0200 Subject: [PATCH 080/155] * Fixed failing tests. --- tests/TypesTest.php | 14 ++- tests/VerifiedBarcodeTest.php | 5 +- ...67890abcABC.svg => C39+-1234567890ABC.svg} | 32 ++--- ...567890abcABC.svg => C39-1234567890ABC.svg} | 32 +---- .../verified-files/C39E+-1234567890abcABC.svg | 59 +++++---- .../verified-files/C39E-1234567890abcABC.svg | 112 ++++++++++++++++++ 6 files changed, 180 insertions(+), 74 deletions(-) rename tests/verified-files/{C39-1234567890abcABC.svg => C39+-1234567890ABC.svg} (78%) rename tests/verified-files/{C39+-1234567890abcABC.svg => C39-1234567890ABC.svg} (74%) create mode 100644 tests/verified-files/C39E-1234567890abcABC.svg diff --git a/tests/TypesTest.php b/tests/TypesTest.php index 16b3083..6bc7860 100644 --- a/tests/TypesTest.php +++ b/tests/TypesTest.php @@ -7,19 +7,27 @@ class TypesTest extends TestCase public function test_generator_can_generate_code_39_barcode() { $generator = new Picqer\Barcode\BarcodeGeneratorSVG(); - $result = $generator->getBarcode('1234567890abcABC', $generator::TYPE_CODE_39); + $result = $generator->getBarcode('1234567890ABC', $generator::TYPE_CODE_39); - $this->assertStringEqualsFile('tests/verified-files/C39-1234567890abcABC.svg', $result); + $this->assertStringEqualsFile('tests/verified-files/C39-1234567890ABC.svg', $result); } public function test_generator_can_generate_code_39_checksum_barcode() { $generator = new Picqer\Barcode\BarcodeGeneratorSVG(); - $result = $generator->getBarcode('1234567890abcABC', $generator::TYPE_CODE_39_CHECKSUM); + $result = $generator->getBarcode('1234567890ABC', $generator::TYPE_CODE_39_CHECKSUM); $this->assertGreaterThan(100, strlen($result)); } + public function test_generator_can_generate_code_39_extended_barcode() + { + $generator = new Picqer\Barcode\BarcodeGeneratorSVG(); + $result = $generator->getBarcode('1234567890abcABC', $generator::TYPE_CODE_39E); + + $this->assertStringEqualsFile('tests/verified-files/C39E-1234567890abcABC.svg', $result); + } + public function test_generator_can_generate_code_39_extended_checksum_barcode() { $generator = new Picqer\Barcode\BarcodeGeneratorSVG(); diff --git a/tests/VerifiedBarcodeTest.php b/tests/VerifiedBarcodeTest.php index 95e4ae3..9c6ac3e 100644 --- a/tests/VerifiedBarcodeTest.php +++ b/tests/VerifiedBarcodeTest.php @@ -14,8 +14,9 @@ class VerifiedBarcodeTest extends TestCase { public static $supportedBarcodes = [ - ['type' => BarcodeGenerator::TYPE_CODE_39, 'barcodes' => ['1234567890abcABC']], - ['type' => BarcodeGenerator::TYPE_CODE_39_CHECKSUM, 'barcodes' => ['1234567890abcABC']], + ['type' => BarcodeGenerator::TYPE_CODE_39, 'barcodes' => ['1234567890ABC']], + ['type' => BarcodeGenerator::TYPE_CODE_39_CHECKSUM, 'barcodes' => ['1234567890ABC']], + ['type' => BarcodeGenerator::TYPE_CODE_39E, 'barcodes' => ['1234567890abcABC']], ['type' => BarcodeGenerator::TYPE_CODE_39E_CHECKSUM, 'barcodes' => ['1234567890abcABC']], ['type' => BarcodeGenerator::TYPE_CODE_93, 'barcodes' => ['1234567890abcABC']], ['type' => BarcodeGenerator::TYPE_STANDARD_2_5, 'barcodes' => ['1234567890']], diff --git a/tests/verified-files/C39-1234567890abcABC.svg b/tests/verified-files/C39+-1234567890ABC.svg similarity index 78% rename from tests/verified-files/C39-1234567890abcABC.svg rename to tests/verified-files/C39+-1234567890ABC.svg index a1b9792..280551a 100644 --- a/tests/verified-files/C39-1234567890abcABC.svg +++ b/tests/verified-files/C39+-1234567890ABC.svg @@ -1,7 +1,7 @@ - - *1234567890ABCABC* + + *1234567890ABCZ* @@ -73,25 +73,15 @@ - - - - - + + + + + - - - - - - - - - - - - - - + + + + diff --git a/tests/verified-files/C39+-1234567890abcABC.svg b/tests/verified-files/C39-1234567890ABC.svg similarity index 74% rename from tests/verified-files/C39+-1234567890abcABC.svg rename to tests/verified-files/C39-1234567890ABC.svg index f5ae78d..448792d 100644 --- a/tests/verified-files/C39+-1234567890abcABC.svg +++ b/tests/verified-files/C39-1234567890ABC.svg @@ -1,7 +1,7 @@ - - *1234567890ABCABCP* + + *1234567890ABC* @@ -73,30 +73,10 @@ - + - - - - - - - - - - - - - - - - - - - - - - - + + + diff --git a/tests/verified-files/C39E+-1234567890abcABC.svg b/tests/verified-files/C39E+-1234567890abcABC.svg index f5ae78d..a1a87c5 100644 --- a/tests/verified-files/C39E+-1234567890abcABC.svg +++ b/tests/verified-files/C39E+-1234567890abcABC.svg @@ -1,7 +1,7 @@ - - *1234567890ABCABCP* + + *1234567890+A+B+CABCJ* @@ -58,45 +58,60 @@ - + - - - + + + - - - - + + + + - - + + - + - + - - - + + + - + - - - - + + + + + + + + + + + + + + + + + + + diff --git a/tests/verified-files/C39E-1234567890abcABC.svg b/tests/verified-files/C39E-1234567890abcABC.svg new file mode 100644 index 0000000..d85cc71 --- /dev/null +++ b/tests/verified-files/C39E-1234567890abcABC.svg @@ -0,0 +1,112 @@ + + + + *1234567890+A+B+CABC* + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + From fe45e74a2916c6bfdde2076546777a0d4aa29c66 Mon Sep 17 00:00:00 2001 From: bkr Date: Wed, 29 Jun 2022 14:35:09 +0200 Subject: [PATCH 081/155] Revert "* Fixed failing tests." This reverts commit 8d8c358a3095b0791d89ceba34b19f6c064c5311. --- tests/TypesTest.php | 14 +--- tests/VerifiedBarcodeTest.php | 5 +- ...67890ABC.svg => C39+-1234567890abcABC.svg} | 37 ++++++--- tests/verified-files/C39-1234567890ABC.svg | 82 ------------------- ...890abcABC.svg => C39-1234567890abcABC.svg} | 0 5 files changed, 31 insertions(+), 107 deletions(-) rename tests/verified-files/{C39+-1234567890ABC.svg => C39+-1234567890abcABC.svg} (74%) delete mode 100644 tests/verified-files/C39-1234567890ABC.svg rename tests/verified-files/{C39E-1234567890abcABC.svg => C39-1234567890abcABC.svg} (100%) diff --git a/tests/TypesTest.php b/tests/TypesTest.php index 7e7658b..16b3083 100644 --- a/tests/TypesTest.php +++ b/tests/TypesTest.php @@ -7,27 +7,19 @@ class TypesTest extends TestCase public function test_generator_can_generate_code_39_barcode() { $generator = new Picqer\Barcode\BarcodeGeneratorSVG(); - $result = $generator->getBarcode('1234567890ABC', $generator::TYPE_CODE_39); + $result = $generator->getBarcode('1234567890abcABC', $generator::TYPE_CODE_39); - $this->assertStringEqualsFile('tests/verified-files/C39-1234567890ABC.svg', $result); + $this->assertStringEqualsFile('tests/verified-files/C39-1234567890abcABC.svg', $result); } public function test_generator_can_generate_code_39_checksum_barcode() { $generator = new Picqer\Barcode\BarcodeGeneratorSVG(); - $result = $generator->getBarcode('1234567890ABC', $generator::TYPE_CODE_39_CHECKSUM); + $result = $generator->getBarcode('1234567890abcABC', $generator::TYPE_CODE_39_CHECKSUM); $this->assertGreaterThan(100, strlen($result)); } - public function test_generator_can_generate_code_39_extended_barcode() - { - $generator = new Picqer\Barcode\BarcodeGeneratorSVG(); - $result = $generator->getBarcode('1234567890abcABC', $generator::TYPE_CODE_39); - - $this->assertStringEqualsFile('tests/verified-files/C39E-1234567890abcABC.svg', $result); - } - public function test_generator_can_generate_code_39_extended_checksum_barcode() { $generator = new Picqer\Barcode\BarcodeGeneratorSVG(); diff --git a/tests/VerifiedBarcodeTest.php b/tests/VerifiedBarcodeTest.php index 9c6ac3e..95e4ae3 100644 --- a/tests/VerifiedBarcodeTest.php +++ b/tests/VerifiedBarcodeTest.php @@ -14,9 +14,8 @@ class VerifiedBarcodeTest extends TestCase { public static $supportedBarcodes = [ - ['type' => BarcodeGenerator::TYPE_CODE_39, 'barcodes' => ['1234567890ABC']], - ['type' => BarcodeGenerator::TYPE_CODE_39_CHECKSUM, 'barcodes' => ['1234567890ABC']], - ['type' => BarcodeGenerator::TYPE_CODE_39E, 'barcodes' => ['1234567890abcABC']], + ['type' => BarcodeGenerator::TYPE_CODE_39, 'barcodes' => ['1234567890abcABC']], + ['type' => BarcodeGenerator::TYPE_CODE_39_CHECKSUM, 'barcodes' => ['1234567890abcABC']], ['type' => BarcodeGenerator::TYPE_CODE_39E_CHECKSUM, 'barcodes' => ['1234567890abcABC']], ['type' => BarcodeGenerator::TYPE_CODE_93, 'barcodes' => ['1234567890abcABC']], ['type' => BarcodeGenerator::TYPE_STANDARD_2_5, 'barcodes' => ['1234567890']], diff --git a/tests/verified-files/C39+-1234567890ABC.svg b/tests/verified-files/C39+-1234567890abcABC.svg similarity index 74% rename from tests/verified-files/C39+-1234567890ABC.svg rename to tests/verified-files/C39+-1234567890abcABC.svg index 280551a..f5ae78d 100644 --- a/tests/verified-files/C39+-1234567890ABC.svg +++ b/tests/verified-files/C39+-1234567890abcABC.svg @@ -1,7 +1,7 @@ - - *1234567890ABCZ* + + *1234567890ABCABCP* @@ -73,15 +73,30 @@ - - - - - + + + + + - - - - + + + + + + + + + + + + + + + + + + + diff --git a/tests/verified-files/C39-1234567890ABC.svg b/tests/verified-files/C39-1234567890ABC.svg deleted file mode 100644 index 448792d..0000000 --- a/tests/verified-files/C39-1234567890ABC.svg +++ /dev/null @@ -1,82 +0,0 @@ - - - - *1234567890ABC* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/tests/verified-files/C39E-1234567890abcABC.svg b/tests/verified-files/C39-1234567890abcABC.svg similarity index 100% rename from tests/verified-files/C39E-1234567890abcABC.svg rename to tests/verified-files/C39-1234567890abcABC.svg From 02693c9774176064d7d977559be2d19b268a09ad Mon Sep 17 00:00:00 2001 From: bkr Date: Thu, 30 Jun 2022 13:31:16 +0200 Subject: [PATCH 082/155] * Removed leading zero for barcodes of type "Standard 2 of 5", in case the code (including checksum, if present) was odd. * Fixed wrong encoding for character '8' (was encoded as '0'). --- src/Types/TypeStandard2of5.php | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/Types/TypeStandard2of5.php b/src/Types/TypeStandard2of5.php index 59a5958..592f59a 100644 --- a/src/Types/TypeStandard2of5.php +++ b/src/Types/TypeStandard2of5.php @@ -26,16 +26,12 @@ public function getBarcodeData(string $code): Barcode $chr['5'] = '11101011101010'; $chr['6'] = '10111011101010'; $chr['7'] = '10101011101110'; - $chr['8'] = '10101110111010'; + $chr['8'] = '11101010111010'; $chr['9'] = '10111010111010'; if ($this->checksum) { // add checksum $code .= $this->checksum_s25($code); } - if ((strlen($code) % 2) != 0) { - // add leading zero if code-length is odd - $code = '0' . $code; - } $seq = '11011010'; for ($i = 0; $i < strlen($code); ++$i) { From 326113c1bf5933a66685a1950c2788d0d535f46a Mon Sep 17 00:00:00 2001 From: bkr Date: Thu, 30 Jun 2022 13:33:27 +0200 Subject: [PATCH 083/155] * Fixed failing tests. --- tests/verified-files/S25+-1234567890.svg | 87 +++++++++++------------- tests/verified-files/S25-1234567890.svg | 6 +- 2 files changed, 44 insertions(+), 49 deletions(-) diff --git a/tests/verified-files/S25+-1234567890.svg b/tests/verified-files/S25+-1234567890.svg index 4271d9f..dcdc32a 100644 --- a/tests/verified-files/S25+-1234567890.svg +++ b/tests/verified-files/S25+-1234567890.svg @@ -1,73 +1,68 @@ - - 012345678905 + + 12345678905 - - - - - - - + + + + + + + - - - + + - - + + + + - - - - - - - - - + + + + + + + + - + + - - - - + + - - + + + - - + + - - + + - - - - + + + + - - - - - - - - + + + diff --git a/tests/verified-files/S25-1234567890.svg b/tests/verified-files/S25-1234567890.svg index 2a6051d..26d48c5 100644 --- a/tests/verified-files/S25-1234567890.svg +++ b/tests/verified-files/S25-1234567890.svg @@ -41,9 +41,9 @@ - - - + + + From b98f110cc5a79f723688fb17fd90b9325300d844 Mon Sep 17 00:00:00 2001 From: Casper Bakker Date: Fri, 1 Jul 2022 10:27:26 +0200 Subject: [PATCH 084/155] Update Readme.md --- Readme.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Readme.md b/Readme.md index a7e2453..edb76f3 100644 --- a/Readme.md +++ b/Readme.md @@ -8,7 +8,8 @@ It creates SVG, PNG, JPG and HTML images, from the most used 1D barcode standard *The codebase is based on the [TCPDF barcode generator](https://github.com/tecnickcom/TCPDF) by Nicola Asuni. This code is therefor licensed under LGPLv3.* ## No support for... -We do not support any 2D barcodes, like QR codes. We also only generate the 'bars' part of a barcode. If you want text of the code below the barcode, you could add it later to the output of this package. +- No support for any **2D** barcodes, like QR codes. +- We only generate the 'bars' part of a barcode, without text below the barcode. If you want text of the code below the barcode, you could add it later to the output of this package. ## Installation Install through [composer](https://getcomposer.org/doc/00-intro.md): From fbc6395be3c06cbabfa7205aa37ad68ec0476552 Mon Sep 17 00:00:00 2001 From: bkr Date: Wed, 6 Jul 2022 12:06:26 +0200 Subject: [PATCH 085/155] * Partly reworked barcode type code 32. * * The code is now validated, properly. * * If the code is less than 8 characters long, '0's are padded to the left of it. * * If the checksum digit is missing, it will be added. Else, it will be validated. --- src/Types/TypeCode32.php | 70 +++++++++++++++++++++++++++++++++++----- 1 file changed, 62 insertions(+), 8 deletions(-) diff --git a/src/Types/TypeCode32.php b/src/Types/TypeCode32.php index a9ad515..e5df479 100644 --- a/src/Types/TypeCode32.php +++ b/src/Types/TypeCode32.php @@ -3,6 +3,9 @@ namespace Picqer\Barcode\Types; use Picqer\Barcode\Barcode; +use Picqer\Barcode\Exceptions\InvalidCharacterException; +use Picqer\Barcode\Exceptions\InvalidCheckDigitException; +use Picqer\Barcode\Exceptions\InvalidLengthException; /* * CODE 32 - italian pharmaceutical @@ -47,14 +50,65 @@ class TypeCode32 extends TypeCode39 public function getBarcodeData(string $code): Barcode { - $code39 = ''; + // Validate code 32. + $stringLength = strlen($code); + + for ($i = 0; $i < $stringLength; ++$i) { + if (!is_numeric($code[$i])) { + throw new InvalidCharacterException('Character "' . $code[$i] . '" is not supported.'); + } + } + + // Prepare code 32. + $code = str_pad($code, 8, '0', STR_PAD_LEFT); + $checksumDigit = $this->checksum_code32(substr($code, 0, 8)); + $stringLength = max($stringLength, 8); + + if ($stringLength === 8) { + $code .= $checksumDigit; + ++$stringLength; + } + if ($stringLength !== 9) { + throw new InvalidLengthException('Only a code consisting of no more than 9 numbers is supported.'); + } + if ($code[8] !== $checksumDigit) { + throw new InvalidCheckDigitException('Provided checksum digit is wrong for provided code.'); + } + + // Convert code 32 into code 39. + $code39 = ''; $codeElab = $code; - - for ($e = 5; $e >= 0; $e--) { - $code39 .= $this->conversionTable32[intval($codeElab / pow(32,$e))]; - $codeElab = intval($codeElab % pow(32,$e)); - } - + + for ($e = 5; $e >= 0; --$e) { + $code39 .= $this->conversionTable32[intval($codeElab / pow(32, $e))]; + $codeElab = $codeElab % pow(32, $e); + } + + // Return barcode data for code 39. return parent::getBarcodeData($code39); - } + } + + + /** + * Calculate CODE 32 checksum (modulo 10). + * + * @param string $code code to represent. + * @return string char checksum. + * @protected + */ + protected function checksum_code32(string $code): string + { + $s = 0; + + foreach (str_split($code) as $i => $c) { + if (0 === $i % 2) { + $s += (int)$c; + } else { + $c = 2 * (int)$c; + $s += (int)floor($c / 10) + ($c % 10); + } + } + + return (string)$s % 10; + } } From 0bc1dd19a1cca10231a158ee6324e57b8e089562 Mon Sep 17 00:00:00 2001 From: Joe Date: Wed, 21 Sep 2022 21:33:52 +0200 Subject: [PATCH 086/155] SVG: widthFactor can be real number To allow arbitrary barcode width, widthFactor can be float, everything works as expected. --- src/BarcodeGeneratorSVG.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/BarcodeGeneratorSVG.php b/src/BarcodeGeneratorSVG.php index 11652ea..bb54468 100644 --- a/src/BarcodeGeneratorSVG.php +++ b/src/BarcodeGeneratorSVG.php @@ -9,13 +9,13 @@ class BarcodeGeneratorSVG extends BarcodeGenerator * * @param $barcode (string) code to print * @param $type (const) type of barcode - * @param $widthFactor (int) Minimum width of a single bar in user units. + * @param $widthFactor (float) Minimum width of a single bar in user units. * @param $height (int) Height of barcode in user units. * @param $foregroundColor (string) Foreground color (in SVG format) for bar elements (background is transparent). * @return string SVG code. * @public */ - public function getBarcode($barcode, $type, int $widthFactor = 2, int $height = 30, string $foregroundColor = 'black') + public function getBarcode($barcode, $type, float $widthFactor = 2, int $height = 30, string $foregroundColor = 'black') { $barcodeData = $this->getBarcodeData($barcode, $type); From 939f3cdaf3f63250118ebdcb742c1e2ab681f1ff Mon Sep 17 00:00:00 2001 From: Casper Bakker Date: Fri, 23 Dec 2022 13:50:19 +0100 Subject: [PATCH 087/155] Add PHP 8.2 to tests --- .github/workflows/phpunit.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/phpunit.yml b/.github/workflows/phpunit.yml index afa6d9c..8410680 100644 --- a/.github/workflows/phpunit.yml +++ b/.github/workflows/phpunit.yml @@ -9,7 +9,7 @@ jobs: strategy: matrix: - php-versions: ['7.3', '7.4', '8.0', '8.1'] + php-versions: ['7.3', '7.4', '8.0', '8.1', '8.2'] steps: - uses: actions/checkout@v2 From 77da17382cce6b292cc3d5549520d1c8a9ab8058 Mon Sep 17 00:00:00 2001 From: Casper Bakker Date: Fri, 23 Dec 2022 13:51:52 +0100 Subject: [PATCH 088/155] Upgrade actions/checkout to v3 --- .github/workflows/phpunit.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/phpunit.yml b/.github/workflows/phpunit.yml index 8410680..712364a 100644 --- a/.github/workflows/phpunit.yml +++ b/.github/workflows/phpunit.yml @@ -12,7 +12,7 @@ jobs: php-versions: ['7.3', '7.4', '8.0', '8.1', '8.2'] steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v3 - name: Setup PHP uses: shivammathur/setup-php@v2 From af7f65e25a66daed16aa80546312d9dd1544ce4d Mon Sep 17 00:00:00 2001 From: Alexandre Bertrand Date: Thu, 9 Mar 2023 11:15:35 +0100 Subject: [PATCH 089/155] Update PHP doc types --- src/BarcodeGeneratorSVG.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/BarcodeGeneratorSVG.php b/src/BarcodeGeneratorSVG.php index 11652ea..66607bb 100644 --- a/src/BarcodeGeneratorSVG.php +++ b/src/BarcodeGeneratorSVG.php @@ -8,14 +8,14 @@ class BarcodeGeneratorSVG extends BarcodeGenerator * Return a SVG string representation of barcode. * * @param $barcode (string) code to print - * @param $type (const) type of barcode + * @param BarcodeGenerator::Type_* $type (string) type of barcode * @param $widthFactor (int) Minimum width of a single bar in user units. * @param $height (int) Height of barcode in user units. * @param $foregroundColor (string) Foreground color (in SVG format) for bar elements (background is transparent). * @return string SVG code. * @public */ - public function getBarcode($barcode, $type, int $widthFactor = 2, int $height = 30, string $foregroundColor = 'black') + public function getBarcode($barcode, string $type, int $widthFactor = 2, int $height = 30, string $foregroundColor = 'black'): string { $barcodeData = $this->getBarcodeData($barcode, $type); From 8e6e9367103760fe863c91a0657cd7cd9166f36c Mon Sep 17 00:00:00 2001 From: Alexandre Bertrand Date: Thu, 9 Mar 2023 11:16:23 +0100 Subject: [PATCH 090/155] Update PHP doc types --- src/BarcodeGeneratorSVG.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/BarcodeGeneratorSVG.php b/src/BarcodeGeneratorSVG.php index 66607bb..03c9550 100644 --- a/src/BarcodeGeneratorSVG.php +++ b/src/BarcodeGeneratorSVG.php @@ -15,7 +15,7 @@ class BarcodeGeneratorSVG extends BarcodeGenerator * @return string SVG code. * @public */ - public function getBarcode($barcode, string $type, int $widthFactor = 2, int $height = 30, string $foregroundColor = 'black'): string + public function getBarcode(string $barcode, $type, int $widthFactor = 2, int $height = 30, string $foregroundColor = 'black'): string { $barcodeData = $this->getBarcodeData($barcode, $type); From 9fc3fa3c48289d30479d99d683ba27c0eae2a46f Mon Sep 17 00:00:00 2001 From: dvrtech Date: Wed, 14 Jun 2023 12:52:10 -0500 Subject: [PATCH 091/155] save --- src/Types/{TypeTF14.php => TypeITF14.php} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename src/Types/{TypeTF14.php => TypeITF14.php} (100%) diff --git a/src/Types/TypeTF14.php b/src/Types/TypeITF14.php similarity index 100% rename from src/Types/TypeTF14.php rename to src/Types/TypeITF14.php From 7986d876722f632e68b6d2e3aaa686b90c02004f Mon Sep 17 00:00:00 2001 From: dvrtech Date: Wed, 14 Jun 2023 12:54:05 -0500 Subject: [PATCH 092/155] Update TypeITF14.php --- src/Types/TypeITF14.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Types/TypeITF14.php b/src/Types/TypeITF14.php index f1a9635..7b1cbaf 100644 --- a/src/Types/TypeITF14.php +++ b/src/Types/TypeITF14.php @@ -1,4 +1,6 @@ - Date: Wed, 14 Jun 2023 12:57:02 -0500 Subject: [PATCH 093/155] Update TypeITF14.php --- src/Types/TypeITF14.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Types/TypeITF14.php b/src/Types/TypeITF14.php index 7b1cbaf..57b8c81 100644 --- a/src/Types/TypeITF14.php +++ b/src/Types/TypeITF14.php @@ -8,7 +8,7 @@ use Picqer\Barcode\Exceptions\InvalidLengthException; use Picqer\Barcode\Types\TypeInterface; -class TypeCodeITF14 implements TypeInterface +class TypeITF14 implements TypeInterface { /** * @throws InvalidLengthException From b9ac477dbb92113da6acdc4ac1dd5d0923112ad7 Mon Sep 17 00:00:00 2001 From: dvrtech Date: Wed, 14 Jun 2023 14:47:13 -0500 Subject: [PATCH 094/155] save --- Readme.md | 1 + tests/VerifiedBarcodeTest.php | 4 +++- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/Readme.md b/Readme.md index a00482b..e3409a3 100644 --- a/Readme.md +++ b/Readme.md @@ -82,6 +82,7 @@ Most used types are TYPE_CODE_128 and TYPE_CODE_39. Because of the best scanner - TYPE_EAN_5 - TYPE_EAN_8 - TYPE_EAN_13 +- TYPE_ITF14 (Also known as GTIN-14) - TYPE_UPC_A - TYPE_UPC_E - TYPE_MSI diff --git a/tests/VerifiedBarcodeTest.php b/tests/VerifiedBarcodeTest.php index 95e4ae3..decb4a5 100644 --- a/tests/VerifiedBarcodeTest.php +++ b/tests/VerifiedBarcodeTest.php @@ -23,6 +23,7 @@ class VerifiedBarcodeTest extends TestCase ['type' => BarcodeGenerator::TYPE_INTERLEAVED_2_5, 'barcodes' => ['1234567890']], ['type' => BarcodeGenerator::TYPE_INTERLEAVED_2_5_CHECKSUM, 'barcodes' => ['1234567890']], ['type' => BarcodeGenerator::TYPE_EAN_13, 'barcodes' => ['081231723897', '0049000004632', '004900000463']], + ['type' => BarcodeGenerator::TYPE_ITF_14, 'barcodes' => ['00012345600012']], ['type' => BarcodeGenerator::TYPE_CODE_128, 'barcodes' => ['081231723897', '1234567890abcABC-283*33']], ['type' => BarcodeGenerator::TYPE_CODE_128_A, 'barcodes' => ['1234567890']], ['type' => BarcodeGenerator::TYPE_CODE_128_B, 'barcodes' => ['081231723897', '1234567890abcABC-283*33']], @@ -61,7 +62,8 @@ public function testAllSupportedBarcodeTypes() } } - protected function getSaveFilename($value) { + protected function getSaveFilename($value) + { return preg_replace('/[^a-zA-Z0-9_ \-+]/s', '-', $value); } } From cc09761b03de10034df4043f88e3e697a4912e53 Mon Sep 17 00:00:00 2001 From: Casper Bakker Date: Mon, 24 Jul 2023 19:57:39 +0200 Subject: [PATCH 095/155] Add itf14 test file --- tests/verified-files/ITF14-00012345600012.svg | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 tests/verified-files/ITF14-00012345600012.svg diff --git a/tests/verified-files/ITF14-00012345600012.svg b/tests/verified-files/ITF14-00012345600012.svg new file mode 100644 index 0000000..fe36016 --- /dev/null +++ b/tests/verified-files/ITF14-00012345600012.svg @@ -0,0 +1,47 @@ + + + + 00012345600012 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + From 28b3b8cf28367dd3e6c8ea53c35d5ddb53d5afe8 Mon Sep 17 00:00:00 2001 From: Casper Bakker Date: Mon, 24 Jul 2023 20:24:50 +0200 Subject: [PATCH 096/155] Update phpunit.xml --- phpunit.xml | 34 ++++++++++++---------------------- 1 file changed, 12 insertions(+), 22 deletions(-) diff --git a/phpunit.xml b/phpunit.xml index ea81c2b..9f49e2c 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -1,23 +1,13 @@ - - - - ./tests/ - - - - - ./src - - - \ No newline at end of file + + + + ./src + + + + + ./tests/ + + + From 7567f6da285724e6461416cd0047a680d975c84e Mon Sep 17 00:00:00 2001 From: Casper Bakker Date: Mon, 24 Jul 2023 20:40:00 +0200 Subject: [PATCH 097/155] Fix codestyle of ITF-14 --- src/Types/TypeITF14.php | 81 ++++++++++--------------- src/Types/TypeInterleaved25Checksum.php | 1 + 2 files changed, 33 insertions(+), 49 deletions(-) diff --git a/src/Types/TypeITF14.php b/src/Types/TypeITF14.php index 57b8c81..188faf7 100644 --- a/src/Types/TypeITF14.php +++ b/src/Types/TypeITF14.php @@ -6,7 +6,6 @@ use Picqer\Barcode\BarcodeBar; use Picqer\Barcode\Exceptions\InvalidCharacterException; use Picqer\Barcode\Exceptions\InvalidLengthException; -use Picqer\Barcode\Types\TypeInterface; class TypeITF14 implements TypeInterface { @@ -16,9 +15,7 @@ class TypeITF14 implements TypeInterface */ public function getBarcodeData(string $code): Barcode { - $barcode = new Barcode($code); - - $chr = array(); + $chr = []; $chr['0'] = '11221'; $chr['1'] = '21112'; $chr['2'] = '12112'; @@ -29,54 +26,35 @@ public function getBarcodeData(string $code): Barcode $chr['7'] = '11122'; $chr['8'] = '21121'; $chr['9'] = '12121'; + $chr['A'] = '11'; + $chr['Z'] = '21'; if (strlen($code) === 13) { - $total = 0; - - for ($i = 0; $i <= strlen($code) - 1; $i++) { - $temp = intval($code . substr($i, 1)); - $total += $temp * (($i === 0 || $i % 2 === 0) ? 3 : 1); - } - - $cs = $total % 10; - $cs = 10 - $cs; - if ($cs === 10) { - $cs = 0; - } - - $code .= (string) $cs; + $code .= $this->getChecksum($code); } if (strlen($code) > 14 || strlen($code) < 13) { throw new InvalidLengthException(); } - $k = 0; - $pbegin = "1010"; - $pbeginarr = str_split($pbegin); + $barcode = new Barcode($code); - foreach ($pbeginarr as $x) { - $t = $x === '1'; - $w = 1; + // Add start and stop codes + $code = 'AA' . strtolower($code) . 'ZA'; - $barcode->addBar(new BarcodeBar($w, 1, $t)); - ++$k; - } - - for ($i = 0; $i < strlen($code); $i += 2) { - if (!isset($chr[$code[$i]]) || !isset($chr[$code[$i + 1]])) { + for ($charIndex = 0; $charIndex < strlen($code); $charIndex += 2) { + if (! isset($chr[$code[$charIndex]]) || ! isset($chr[$code[$charIndex + 1]])) { throw new InvalidCharacterException(); } - $bars = true; - $pbars = $chr[$code[$i]]; - $pspaces = $chr[$code[$i + 1]]; - $pmixed = ""; - + $bars = true; + $pbars = $chr[$code[$charIndex]]; + $pspaces = $chr[$code[$charIndex + 1]]; + $pmixed = ''; while (strlen($pbars) > 0) { - $pmixed .= $pbars[0] . $pspaces[0]; - $pbars = substr($pbars, 1); + $pmixed .= $pbars[0] . $pspaces[0]; + $pbars = substr($pbars, 1); $pspaces = substr($pspaces, 1); } @@ -88,25 +66,30 @@ public function getBarcodeData(string $code): Barcode } else { $t = false; } - $w = ($x === '1') ? '1' : '2'; + $width = ($x === '1') ? '1' : '2'; - $barcode->addBar(new BarcodeBar($w, 1, $t)); - $bars = !$bars; - ++$k; + $barcode->addBar(new BarcodeBar($width, 1, $t)); + $bars = ! $bars; } } - $pend = "1101"; - $pendarr = str_split($pend); + return $barcode; + } + + private function getChecksum(string $code): string + { + $total = 0; - foreach ($pendarr as $x) { - $t = $x == '1'; - $w = 1; + for ($charIndex = 0; $charIndex <= (strlen($code) - 1); $charIndex++) { + $integerOfChar = intval($code . substr($charIndex, 1)); + $total += $integerOfChar * (($charIndex === 0 || $charIndex % 2 === 0) ? 3 : 1); + } - $barcode->addBar(new BarcodeBar($w, 1, $t)); - ++$k; + $checksum = 10 - ($total % 10); + if ($checksum === 10) { + $checksum = 0; } - return $barcode; + return (string)$checksum; } } diff --git a/src/Types/TypeInterleaved25Checksum.php b/src/Types/TypeInterleaved25Checksum.php index 127efc0..01ece0f 100644 --- a/src/Types/TypeInterleaved25Checksum.php +++ b/src/Types/TypeInterleaved25Checksum.php @@ -16,6 +16,7 @@ class TypeInterleaved25Checksum implements TypeInterface { public function getBarcodeData(string $code): Barcode { + $chr = []; $chr['0'] = '11221'; $chr['1'] = '21112'; $chr['2'] = '12112'; From affa22775e3b74b20ab20b3a185ef6db09bb2461 Mon Sep 17 00:00:00 2001 From: Casper Bakker Date: Mon, 24 Jul 2023 21:05:22 +0200 Subject: [PATCH 098/155] Improve ITF-14 code --- src/Types/TypeITF14.php | 26 ++++------- src/Types/TypeInterleaved25Checksum.php | 1 + tests/VerifiedBarcodeTest.php | 4 +- tests/verified-files/ITF14-00012345600012.svg | 5 +- tests/verified-files/ITF14-05400141288766.svg | 46 +++++++++++++++++++ 5 files changed, 60 insertions(+), 22 deletions(-) create mode 100644 tests/verified-files/ITF14-05400141288766.svg diff --git a/src/Types/TypeITF14.php b/src/Types/TypeITF14.php index 188faf7..ff395d9 100644 --- a/src/Types/TypeITF14.php +++ b/src/Types/TypeITF14.php @@ -29,12 +29,12 @@ public function getBarcodeData(string $code): Barcode $chr['A'] = '11'; $chr['Z'] = '21'; - if (strlen($code) === 13) { - $code .= $this->getChecksum($code); + if (strlen($code) < 13 || strlen($code) > 14) { + throw new InvalidLengthException(); } - if (strlen($code) > 14 || strlen($code) < 13) { - throw new InvalidLengthException(); + if (strlen($code) === 13) { + $code .= $this->getChecksum($code); } $barcode = new Barcode($code); @@ -42,12 +42,13 @@ public function getBarcodeData(string $code): Barcode // Add start and stop codes $code = 'AA' . strtolower($code) . 'ZA'; + // Loop through 2 chars at once for ($charIndex = 0; $charIndex < strlen($code); $charIndex += 2) { if (! isset($chr[$code[$charIndex]]) || ! isset($chr[$code[$charIndex + 1]])) { throw new InvalidCharacterException(); } - $bars = true; + $drawBar = true; $pbars = $chr[$code[$charIndex]]; $pspaces = $chr[$code[$charIndex + 1]]; $pmixed = ''; @@ -58,18 +59,9 @@ public function getBarcodeData(string $code): Barcode $pspaces = substr($pspaces, 1); } - $pmixedarr = str_split($pmixed); - - foreach ($pmixedarr as $x) { - if ($bars) { - $t = true; - } else { - $t = false; - } - $width = ($x === '1') ? '1' : '2'; - - $barcode->addBar(new BarcodeBar($width, 1, $t)); - $bars = ! $bars; + foreach (str_split($pmixed) as $width) { + $barcode->addBar(new BarcodeBar($width, 1, $drawBar)); + $drawBar = ! $drawBar; } } diff --git a/src/Types/TypeInterleaved25Checksum.php b/src/Types/TypeInterleaved25Checksum.php index 01ece0f..1af0439 100644 --- a/src/Types/TypeInterleaved25Checksum.php +++ b/src/Types/TypeInterleaved25Checksum.php @@ -37,6 +37,7 @@ public function getBarcodeData(string $code): Barcode // add leading zero if code-length is odd $code = '0' . $code; } + // add start and stop codes $code = 'AA' . strtolower($code) . 'ZA'; diff --git a/tests/VerifiedBarcodeTest.php b/tests/VerifiedBarcodeTest.php index ff1f626..30af657 100644 --- a/tests/VerifiedBarcodeTest.php +++ b/tests/VerifiedBarcodeTest.php @@ -5,7 +5,7 @@ /* * Test all supported barcodes types, with as much different but supported input strings. - * Verified files can be build with generate-verified-files.php file. + * Verified files can be built with generate-verified-files.php file. * Only run that file if you added new types or new strings to test. * * We use SVG because that output is vector and should be the same on every host system. @@ -24,7 +24,7 @@ class VerifiedBarcodeTest extends TestCase ['type' => BarcodeGenerator::TYPE_INTERLEAVED_2_5, 'barcodes' => ['1234567890']], ['type' => BarcodeGenerator::TYPE_INTERLEAVED_2_5_CHECKSUM, 'barcodes' => ['1234567890']], ['type' => BarcodeGenerator::TYPE_EAN_13, 'barcodes' => ['081231723897', '0049000004632', '004900000463']], - ['type' => BarcodeGenerator::TYPE_ITF_14, 'barcodes' => ['00012345600012']], + ['type' => BarcodeGenerator::TYPE_ITF_14, 'barcodes' => ['00012345600012', '05400141288766']], ['type' => BarcodeGenerator::TYPE_CODE_128, 'barcodes' => ['081231723897', '1234567890abcABC-283*33']], ['type' => BarcodeGenerator::TYPE_CODE_128_A, 'barcodes' => ['1234567890']], ['type' => BarcodeGenerator::TYPE_CODE_128_B, 'barcodes' => ['081231723897', '1234567890abcABC-283*33']], diff --git a/tests/verified-files/ITF14-00012345600012.svg b/tests/verified-files/ITF14-00012345600012.svg index fe36016..25553f0 100644 --- a/tests/verified-files/ITF14-00012345600012.svg +++ b/tests/verified-files/ITF14-00012345600012.svg @@ -1,6 +1,6 @@ - + 00012345600012 @@ -40,8 +40,7 @@ - - + diff --git a/tests/verified-files/ITF14-05400141288766.svg b/tests/verified-files/ITF14-05400141288766.svg new file mode 100644 index 0000000..ece67b7 --- /dev/null +++ b/tests/verified-files/ITF14-05400141288766.svg @@ -0,0 +1,46 @@ + + + + 05400141288766 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + From b9e88b2e8496465786c91ea5a9c23adbac924a8d Mon Sep 17 00:00:00 2001 From: Casper Bakker Date: Tue, 25 Jul 2023 07:42:31 +0200 Subject: [PATCH 099/155] New badges on readme --- Readme.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Readme.md b/Readme.md index c9a2697..372b35f 100644 --- a/Readme.md +++ b/Readme.md @@ -1,5 +1,7 @@ # PHP Barcode Generator -[![Build Status](https://travis-ci.org/picqer/php-barcode-generator.svg?branch=main)](https://travis-ci.org/picqer/php-barcode-generator) [![Github Actions](https://github.com/picqer/php-barcode-generator/workflows/phpunit/badge.svg)](https://travis-ci.org/picqer/php-barcode-generator) [![Total Downloads](https://poser.pugx.org/picqer/php-barcode-generator/downloads)](https://packagist.org/packages/picqer/php-barcode-generator) +Build Status +Total Downloads +Latest Stable Version This is an easy to use, non-bloated, framework independent, barcode generator in PHP. From 3d77915f939e1d4dff68c2235a7e9873afbc7d98 Mon Sep 17 00:00:00 2001 From: Casper Bakker Date: Tue, 25 Jul 2023 07:46:25 +0200 Subject: [PATCH 100/155] Update Readme.md --- Readme.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Readme.md b/Readme.md index 372b35f..1a42d48 100644 --- a/Readme.md +++ b/Readme.md @@ -3,7 +3,7 @@ Total Downloads Latest Stable Version -This is an easy to use, non-bloated, framework independent, barcode generator in PHP. +This is an easy to use, non-bloated, framework independent, barcode generator in PHP. It uses zero(!) composer dependencies and is only a handful of files. Probably the reason that this is the most downloaded barcode generator for PHP on Packagist. ;) It creates SVG, PNG, JPG and HTML images, from the most used 1D barcode standards. From 622a0c7576b75f65a4c7ce56251a3adb66fc481d Mon Sep 17 00:00:00 2001 From: Casper Bakker Date: Tue, 25 Jul 2023 08:28:16 +0200 Subject: [PATCH 101/155] Add tests for fractional width in SVGs --- generate-verified-files.php | 1 + src/BarcodeGeneratorSVG.php | 12 ++++-- tests/BarcodeSvgTest.php | 8 ++++ .../081231723897-ean13-fractional-width.svg | 37 +++++++++++++++++++ 4 files changed, 55 insertions(+), 3 deletions(-) create mode 100644 tests/verified-files/081231723897-ean13-fractional-width.svg diff --git a/generate-verified-files.php b/generate-verified-files.php index 4ec5bc2..ea627b4 100644 --- a/generate-verified-files.php +++ b/generate-verified-files.php @@ -8,6 +8,7 @@ function getSaveFilename($value) { $generatorSVG = new Picqer\Barcode\BarcodeGeneratorSVG(); file_put_contents('tests/verified-files/081231723897-ean13.svg', $generatorSVG->getBarcode('081231723897', $generatorSVG::TYPE_EAN_13)); +file_put_contents('tests/verified-files/081231723897-ean13-fractional-width.svg', $generatorSVG->getBarcode('081231723897', $generatorSVG::TYPE_EAN_13, 0.25, 25.75)); $generatorHTML = new Picqer\Barcode\BarcodeGeneratorHTML(); file_put_contents('tests/verified-files/081231723897-code128.html', $generatorHTML->getBarcode('081231723897', $generatorHTML::TYPE_CODE_128)); diff --git a/src/BarcodeGeneratorSVG.php b/src/BarcodeGeneratorSVG.php index e460412..6f8fb16 100644 --- a/src/BarcodeGeneratorSVG.php +++ b/src/BarcodeGeneratorSVG.php @@ -10,17 +10,22 @@ class BarcodeGeneratorSVG extends BarcodeGenerator * @param $barcode (string) code to print * @param BarcodeGenerator::Type_* $type (string) type of barcode * @param $widthFactor (float) Minimum width of a single bar in user units. - * @param $height (int) Height of barcode in user units. + * @param $height (float) Height of barcode in user units. * @param $foregroundColor (string) Foreground color (in SVG format) for bar elements (background is transparent). * @return string SVG code. * @public */ - public function getBarcode(string $barcode, $type, float $widthFactor = 2, int $height = 30, string $foregroundColor = 'black') + public function getBarcode(string $barcode, $type, float $widthFactor = 2, float $height = 30, string $foregroundColor = 'black') { $barcodeData = $this->getBarcodeData($barcode, $type); // replace table for special characters - $repstr = ["\0" => '', '&' => '&', '<' => '<', '>' => '>']; + $repstr = [ + "\0" => '', + '&' => '&', + '<' => '<', + '>' => '>', + ]; $width = round(($barcodeData->getWidth() * $widthFactor), 3); @@ -45,6 +50,7 @@ public function getBarcode(string $barcode, $type, float $widthFactor = 2, int $ $positionHorizontal += $barWidth; } + $svg .= "\t" . PHP_EOL; $svg .= '' . PHP_EOL; diff --git a/tests/BarcodeSvgTest.php b/tests/BarcodeSvgTest.php index 14c0a21..5a080ff 100644 --- a/tests/BarcodeSvgTest.php +++ b/tests/BarcodeSvgTest.php @@ -11,4 +11,12 @@ public function test_svg_barcode_generator_can_generate_ean_13_barcode() $this->assertStringEqualsFile('tests/verified-files/081231723897-ean13.svg', $generated); } + + public function test_svg_barcode_generator_can_generate_ean_13_barcode_with_fractional_width() + { + $generator = new Picqer\Barcode\BarcodeGeneratorSVG(); + $generated = $generator->getBarcode('081231723897', $generator::TYPE_EAN_13, 0.25, 25.75); + + $this->assertStringEqualsFile('tests/verified-files/081231723897-ean13-fractional-width.svg', $generated); + } } diff --git a/tests/verified-files/081231723897-ean13-fractional-width.svg b/tests/verified-files/081231723897-ean13-fractional-width.svg new file mode 100644 index 0000000..d616490 --- /dev/null +++ b/tests/verified-files/081231723897-ean13-fractional-width.svg @@ -0,0 +1,37 @@ + + + + 0812317238973 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + From d30174b9cd4f08164686d803d22e80929e461c7a Mon Sep 17 00:00:00 2001 From: Casper Bakker Date: Tue, 25 Jul 2023 08:30:35 +0200 Subject: [PATCH 102/155] More typehinting --- src/BarcodeGeneratorDynamicHTML.php | 4 ++-- src/BarcodeGeneratorHTML.php | 4 ++-- src/BarcodeGeneratorPNG.php | 7 +++++-- src/BarcodeGeneratorSVG.php | 2 +- 4 files changed, 10 insertions(+), 7 deletions(-) diff --git a/src/BarcodeGeneratorDynamicHTML.php b/src/BarcodeGeneratorDynamicHTML.php index d2e7cb5..52809d3 100644 --- a/src/BarcodeGeneratorDynamicHTML.php +++ b/src/BarcodeGeneratorDynamicHTML.php @@ -11,11 +11,11 @@ class BarcodeGeneratorDynamicHTML extends BarcodeGenerator * This 'dynamic' version uses percentage based widths and heights, resulting in a vector-y qualitative result. * * @param string $barcode code to print - * @param string $type type of barcode + * @param BarcodeGenerator::Type_* $type (string) type of barcode * @param string $foregroundColor Foreground color for bar elements as '#333' or 'orange' for example (background is transparent). * @return string HTML code. */ - public function getBarcode($barcode, $type, string $foregroundColor = 'black') + public function getBarcode(string $barcode, $type, string $foregroundColor = 'black'): string { $barcodeData = $this->getBarcodeData($barcode, $type); diff --git a/src/BarcodeGeneratorHTML.php b/src/BarcodeGeneratorHTML.php index 25a8820..294b680 100644 --- a/src/BarcodeGeneratorHTML.php +++ b/src/BarcodeGeneratorHTML.php @@ -9,13 +9,13 @@ class BarcodeGeneratorHTML extends BarcodeGenerator * This original version uses pixel based widths and heights. Use Dynamic HTML version for better quality representation. * * @param string $barcode code to print - * @param string $type type of barcode + * @param BarcodeGenerator::Type_* $type (string) type of barcode * @param int $widthFactor Width of a single bar element in pixels. * @param int $height Height of a single bar element in pixels. * @param string $foregroundColor Foreground color for bar elements as '#333' or 'orange' for example (background is transparent). * @return string HTML code. */ - public function getBarcode($barcode, $type, int $widthFactor = 2, int $height = 30, string $foregroundColor = 'black') + public function getBarcode($barcode, $type, int $widthFactor = 2, int $height = 30, string $foregroundColor = 'black'): string { $barcodeData = $this->getBarcodeData($barcode, $type); diff --git a/src/BarcodeGeneratorPNG.php b/src/BarcodeGeneratorPNG.php index e73a054..bc7943c 100644 --- a/src/BarcodeGeneratorPNG.php +++ b/src/BarcodeGeneratorPNG.php @@ -11,6 +11,9 @@ class BarcodeGeneratorPNG extends BarcodeGenerator { protected $useImagick = true; + /** + * @throws BarcodeException + */ public function __construct() { // Auto switch between GD and Imagick based on what is installed @@ -43,13 +46,13 @@ public function useGd() * Return a PNG image representation of barcode (requires GD or Imagick library). * * @param string $barcode code to print - * @param string $type type of barcode: + * @param BarcodeGenerator::Type_* $type (string) type of barcode * @param int $widthFactor Width of a single bar element in pixels. * @param int $height Height of a single bar element in pixels. * @param array $foregroundColor RGB (0-255) foreground color for bar elements (background is transparent). * @return string image data or false in case of error. */ - public function getBarcode($barcode, $type, int $widthFactor = 2, int $height = 30, array $foregroundColor = [0, 0, 0]) + public function getBarcode(string $barcode, $type, int $widthFactor = 2, int $height = 30, array $foregroundColor = [0, 0, 0]): string { $barcodeData = $this->getBarcodeData($barcode, $type); $width = round($barcodeData->getWidth() * $widthFactor); diff --git a/src/BarcodeGeneratorSVG.php b/src/BarcodeGeneratorSVG.php index 6f8fb16..e67527f 100644 --- a/src/BarcodeGeneratorSVG.php +++ b/src/BarcodeGeneratorSVG.php @@ -15,7 +15,7 @@ class BarcodeGeneratorSVG extends BarcodeGenerator * @return string SVG code. * @public */ - public function getBarcode(string $barcode, $type, float $widthFactor = 2, float $height = 30, string $foregroundColor = 'black') + public function getBarcode(string $barcode, $type, float $widthFactor = 2, float $height = 30, string $foregroundColor = 'black'): string { $barcodeData = $this->getBarcodeData($barcode, $type); From cfd423fd20cb3988aaa2b64c8e93ec029e5714e9 Mon Sep 17 00:00:00 2001 From: Casper Bakker Date: Tue, 25 Jul 2023 08:35:08 +0200 Subject: [PATCH 103/155] Update examples.md --- examples.md | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/examples.md b/examples.md index 4e0bec2..00329a5 100644 --- a/examples.md +++ b/examples.md @@ -6,11 +6,15 @@ These are examples of supported barcodes with this library. ### C39 -![Barcode 1234567890abcABC as C39](tests/verified-files/C39-1234567890abcABC.svg) +![Barcode 1234567890ABC as C39](tests/verified-files/C39-1234567890ABC.svg) ### C39+ -![Barcode 1234567890abcABC as C39+](tests/verified-files/C39+-1234567890abcABC.svg) +![Barcode 1234567890ABC as C39+](tests/verified-files/C39+-1234567890ABC.svg) + +### C39E + +![Barcode 1234567890abcABC as C39E](tests/verified-files/C39E-1234567890abcABC.svg) ### C39E+ @@ -44,6 +48,12 @@ These are examples of supported barcodes with this library. ![Barcode 004900000463 as EAN13](tests/verified-files/EAN13-004900000463.svg) +### ITF14 + +![Barcode 00012345600012 as ITF14](tests/verified-files/ITF14-00012345600012.svg) + +![Barcode 05400141288766 as ITF14](tests/verified-files/ITF14-05400141288766.svg) + ### C128 ![Barcode 081231723897 as C128](tests/verified-files/C128-081231723897.svg) From dab19f2afe42d2e3dda1504f5834e36f2ecf0553 Mon Sep 17 00:00:00 2001 From: Casper Bakker Date: Tue, 25 Jul 2023 08:37:46 +0200 Subject: [PATCH 104/155] Update Readme.md --- Readme.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Readme.md b/Readme.md index 1a42d48..dfdfea2 100644 --- a/Readme.md +++ b/Readme.md @@ -34,6 +34,9 @@ $generator = new Picqer\Barcode\BarcodeGeneratorHTML(); echo $generator->getBarcode('081231723897', $generator::TYPE_CODE_128); ``` +Will result in this beauty:
    +![Barcode 081231723897 as Code 128](tests/verified-files/081231723897-ean13.svg) + The `getBarcode()` method accepts the following parameters: - `$barcode` String needed to encode in the barcode - `$type` Type of barcode, use the constants defined in the class From a0b9c7127fcb56e970acfa7c357dfe5fdd507cf7 Mon Sep 17 00:00:00 2001 From: Micha Ahrweiler Date: Tue, 8 Aug 2023 17:09:37 +0200 Subject: [PATCH 105/155] Fix phpdoc type case for usage with vimeo/psalm The phpdoc type BarcodeGenerator::Type_* (with wrong case) is not recognized by vimeo/psalm, but BarcodeGenerator::TYPE_* (with correct case) is. --- src/BarcodeGeneratorDynamicHTML.php | 2 +- src/BarcodeGeneratorHTML.php | 2 +- src/BarcodeGeneratorPNG.php | 2 +- src/BarcodeGeneratorSVG.php | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/BarcodeGeneratorDynamicHTML.php b/src/BarcodeGeneratorDynamicHTML.php index 52809d3..57ce23e 100644 --- a/src/BarcodeGeneratorDynamicHTML.php +++ b/src/BarcodeGeneratorDynamicHTML.php @@ -11,7 +11,7 @@ class BarcodeGeneratorDynamicHTML extends BarcodeGenerator * This 'dynamic' version uses percentage based widths and heights, resulting in a vector-y qualitative result. * * @param string $barcode code to print - * @param BarcodeGenerator::Type_* $type (string) type of barcode + * @param BarcodeGenerator::TYPE_* $type (string) type of barcode * @param string $foregroundColor Foreground color for bar elements as '#333' or 'orange' for example (background is transparent). * @return string HTML code. */ diff --git a/src/BarcodeGeneratorHTML.php b/src/BarcodeGeneratorHTML.php index 294b680..75a98fe 100644 --- a/src/BarcodeGeneratorHTML.php +++ b/src/BarcodeGeneratorHTML.php @@ -9,7 +9,7 @@ class BarcodeGeneratorHTML extends BarcodeGenerator * This original version uses pixel based widths and heights. Use Dynamic HTML version for better quality representation. * * @param string $barcode code to print - * @param BarcodeGenerator::Type_* $type (string) type of barcode + * @param BarcodeGenerator::TYPE_* $type (string) type of barcode * @param int $widthFactor Width of a single bar element in pixels. * @param int $height Height of a single bar element in pixels. * @param string $foregroundColor Foreground color for bar elements as '#333' or 'orange' for example (background is transparent). diff --git a/src/BarcodeGeneratorPNG.php b/src/BarcodeGeneratorPNG.php index bc7943c..05ac413 100644 --- a/src/BarcodeGeneratorPNG.php +++ b/src/BarcodeGeneratorPNG.php @@ -46,7 +46,7 @@ public function useGd() * Return a PNG image representation of barcode (requires GD or Imagick library). * * @param string $barcode code to print - * @param BarcodeGenerator::Type_* $type (string) type of barcode + * @param BarcodeGenerator::TYPE_* $type (string) type of barcode * @param int $widthFactor Width of a single bar element in pixels. * @param int $height Height of a single bar element in pixels. * @param array $foregroundColor RGB (0-255) foreground color for bar elements (background is transparent). diff --git a/src/BarcodeGeneratorSVG.php b/src/BarcodeGeneratorSVG.php index e67527f..4d0d477 100644 --- a/src/BarcodeGeneratorSVG.php +++ b/src/BarcodeGeneratorSVG.php @@ -8,7 +8,7 @@ class BarcodeGeneratorSVG extends BarcodeGenerator * Return a SVG string representation of barcode. * * @param $barcode (string) code to print - * @param BarcodeGenerator::Type_* $type (string) type of barcode + * @param BarcodeGenerator::TYPE_* $type (string) type of barcode * @param $widthFactor (float) Minimum width of a single bar in user units. * @param $height (float) Height of barcode in user units. * @param $foregroundColor (string) Foreground color (in SVG format) for bar elements (background is transparent). From a98997e976e193fb3aee8c7a4020990165336be6 Mon Sep 17 00:00:00 2001 From: Darren Stephens Date: Thu, 7 Sep 2023 18:07:04 +0100 Subject: [PATCH 106/155] Add Telepen/Telepn Numeric --- src/BarcodeGenerator.php | 10 + src/Types/TypeTelepen.php | 187 ++++++++++++++++++ tests/TypesTest.php | 16 ++ tests/VerifiedBarcodeTest.php | 2 + .../TELEPENALPHA-1234567890ASCD.svg | 95 +++++++++ .../TELEPENNUMERIC-1234567890.svg | 54 +++++ 6 files changed, 364 insertions(+) create mode 100644 src/Types/TypeTelepen.php create mode 100644 tests/verified-files/TELEPENALPHA-1234567890ASCD.svg create mode 100644 tests/verified-files/TELEPENNUMERIC-1234567890.svg diff --git a/src/BarcodeGenerator.php b/src/BarcodeGenerator.php index 9c5f2f4..d7c1b2e 100644 --- a/src/BarcodeGenerator.php +++ b/src/BarcodeGenerator.php @@ -58,6 +58,7 @@ use Picqer\Barcode\Types\TypeRms4cc; use Picqer\Barcode\Types\TypeStandard2of5; use Picqer\Barcode\Types\TypeStandard2of5Checksum; +use Picqer\Barcode\Types\TypeTelepen; use Picqer\Barcode\Types\TypeUpcA; use Picqer\Barcode\Types\TypeUpcE; use Picqer\Barcode\Types\TypeUpcExtension2; @@ -90,6 +91,8 @@ abstract class BarcodeGenerator const TYPE_MSI_CHECKSUM = 'MSI+'; // MSI + CHECKSUM (modulo 11) const TYPE_POSTNET = 'POSTNET'; const TYPE_PLANET = 'PLANET'; + const TYPE_TELEPEN_ALPHA = 'TELEPENALPHA'; + const TYPE_TELEPEN_NUMERIC = 'TELEPENNUMERIC'; const TYPE_RMS4CC = 'RMS4CC'; // RMS4CC (Royal Mail 4-state Customer Code) - CBC (Customer Bar Code) const TYPE_KIX = 'KIX'; // KIX (Klant index - Customer index) const TYPE_IMB = 'IMB'; // IMB - Intelligent Mail Barcode - Onecode - USPS-B-3200 @@ -203,6 +206,13 @@ protected function createDataBuilderForType(string $type) case self::TYPE_PHARMA_CODE_TWO_TRACKS: return new TypePharmacodeTwoCode(); + + case self::TYPE_TELEPEN_ALPHA: + return new TypeTelepen(); + + case self::TYPE_TELEPEN_NUMERIC: + return new TypeTelepen('numeric'); + } throw new UnknownTypeException(); diff --git a/src/Types/TypeTelepen.php b/src/Types/TypeTelepen.php new file mode 100644 index 0000000..97f1600 --- /dev/null +++ b/src/Types/TypeTelepen.php @@ -0,0 +1,187 @@ + Robin Stuart by + * Darren Stephens + * at https://github.com/woo-j/OkapiBarcode which used the + * Apache License 2.0 http://www.apache.org/licenses/LICENSE-2.0 + * + * Implements Telepen (also known as Telepen Alpha), and Telepen Numeric. + * + * Telepen can encode ASCII text input and includes a modulo-127 check digit. + * Telepen Numeric allows compression of numeric data into a Telepen symbol. Data + * can consist of pairs of numbers or pairs consisting of a numerical digit followed + * by an X character. Telepen Numeric also includes a mod-127 check digit. + */ + +namespace Picqer\Barcode\Types; + +use Picqer\Barcode\Barcode; +use Picqer\Barcode\BarcodeBar; +use Picqer\Barcode\Exceptions\InvalidFormatException; + +define('TELEPEN_START_CHAR', '_'); +define('TELEPEN_STOP_CHAR', 'z'); +define('TELEPEN_ALPHA', ''); +define('TELEPEN_NUMERIC', 'numeric'); + +class TypeTelepen implements TypeInterface +{ + private $telepen_lookup_table; + private $mode; + + public function __construct($m = 'alpha') + { + $this->mode = TELEPEN_ALPHA; + if (strtolower($m) == 'numeric') { + $this->mode = TELEPEN_NUMERIC; + } + $this->createTelepenConversionTable(); + } + + public function getBarcodeData(string $code): Barcode + { + /* The stream we get from the telepen output gives us the + * width of alternating black/white stripes + */ + + $encoded = $this->encode($code); //binary string + $barcode = new Barcode($code); + + $drawBar = true; + for ($i = 0; $i < strlen($encoded); ++$i) { + $barWidth = $encoded[$i]; + $barcode->addBar(new BarcodeBar($barWidth, 250, $drawBar)); + $drawBar = !$drawBar; //flip to other colour + } + + return $barcode; + } + + protected function encode($code) : string + { + $result = null; + if ($this->mode == TELEPEN_ALPHA) { + $result = $this->encodeAlpha($code); + } else { + $result = $this->encodeNumeric($code); + } + + return $result; + } + + protected function encodeAlpha($code) : string + { + + if (!preg_match('/[ -~]+/', $code)) { // everything from ASCII0-ASCII127 + throw new InvalidFormatException("Invalid characters in data"); + } + + $count = 0; + + /* other implementations use the byte-chr-int type equivalence to work + * with array indices in the conversion/lookup table. It's probably + * better to be more explicit with php, hence the use of ord and chr here. + */ + + // begin with start char + $dest = $this->telepen_lookup_table[ord(TELEPEN_START_CHAR)]; + + for ($i = 0; $i < strlen($code); $i++) { + //$ascii_code = ord(substr($code, $i, 1)); + $ascii_code = ord($code[$i]); + $dest .= ($this->telepen_lookup_table[$ascii_code]); + $count += $ascii_code; + } + + // Now add check and terminator + $check_digit = 127 - ($count % 127); + if ($check_digit == 127) { + $check_digit = 0; + } + + $dest .= $this->telepen_lookup_table[ord($check_digit)]; + $dest .= $this->telepen_lookup_table[ord(TELEPEN_STOP_CHAR)]; // Stop + + return $dest; + } + + private function encodeNumeric(string $code) : string + { + + /* If input contains non-numeric or X, exit */ + if (!preg_match('/^[0-9X]+$/', $code)) { + throw new InvalidFormatException("Invalid characters in data"); + } + + /* If input is an odd length, exit */ + $t = ''; + if (strlen($code) % 2 > 0) { + throw new InvalidFormatException("There must be an even number of digits"); + } + + $count = 0; + $dest = $this->telepen_lookup_table[ord(TELEPEN_START_CHAR)]; // begin with the start character _ + + for ($i = 0; $i < strlen($code); $i += 2) { + $c1 = $code[$i]; + $c2 = $code[$i+1]; + /* Input nX is allowed, but Xn is not */ + if ($c1 == 'X') { + throw new InvalidFormatException("Invalid position of X in data"); + } + $glyph = null; + if ($c2 == 'X') { + $glyph = (ord($c1) - ord('0')) + 17; + } else { + $glyph = ((10 * (ord($c1) - ord('0'))) + (ord($c2) - ord('0'))) + 27; + } + $count += $glyph; + $dest .= $this->telepen_lookup_table[$glyph]; + } + + $check_digit = 127 - ($count % 127); + if ($check_digit == 127) { + $check_digit = 0; + } + + $dest .= $this->telepen_lookup_table[$check_digit]; + $dest .= $this->telepen_lookup_table[ord(TELEPEN_STOP_CHAR)]; // Stop + + return $dest; + } + + private function createTelepenConversionTable() + { + $this->telepen_lookup_table = [ + "1111111111111111", "1131313111", "33313111", "1111313131", + "3111313111", "11333131", "13133131", "111111313111", "31333111", + "1131113131", "33113131", "1111333111", "3111113131", "1113133111", + "1311133111", "111111113131", "3131113111", "11313331", "333331", + "111131113111", "31113331", "1133113111", "1313113111", "1111113331", + "31131331", "113111113111", "3311113111", "1111131331", "311111113111", + "1113111331", "1311111331", "11111111113111", "31313311", "1131311131", + "33311131", "1111313311", "3111311131", "11333311", "13133311", + "111111311131", "31331131", "1131113311", "33113311", "1111331131", + "3111113311", "1113131131", "1311131131", "111111113311", "3131111131", + "1131131311", "33131311", "111131111131", "3111131311", "1133111131", + "1313111131", "111111131311", "3113111311", "113111111131", + "3311111131", "111113111311", "311111111131", "111311111311", + "131111111311", "11111111111131", "3131311111", "11313133", "333133", + "111131311111", "31113133", "1133311111", "1313311111", "1111113133", + "313333", "113111311111", "3311311111", "11113333", "311111311111", + "11131333", "13111333", "11111111311111", "31311133", "1131331111", + "33331111", "1111311133", "3111331111", "11331133", "13131133", + "111111331111", "3113131111", "1131111133", "33111133", "111113131111", + "3111111133", "111311131111", "131111131111", "111111111133", + "31311313", "113131111111", "3331111111", "1111311313", "311131111111", + "11331313", "13131313", "11111131111111", "3133111111", "1131111313", + "33111313", "111133111111", "3111111313", "111313111111", + "131113111111", "111111111313", "313111111111", "1131131113", + "33131113", "11113111111111", "3111131113", "113311111111", + "131311111111", "111111131113", "3113111113", "11311111111111", + "331111111111", "111113111113", "31111111111111", "111311111113", + "131111111113" + ]; + } +} diff --git a/tests/TypesTest.php b/tests/TypesTest.php index 6bc7860..aaca1ad 100644 --- a/tests/TypesTest.php +++ b/tests/TypesTest.php @@ -235,4 +235,20 @@ public function test_generator_can_generate_pharma_code_2_tracks_barcode() $this->assertGreaterThan(100, strlen($result)); } + + public function test_generator_can_generate_telepen_alpha_barcode() + { + $generator = new Picqer\Barcode\BarcodeGeneratorSVG(); + $result = $generator->getBarcode('1234567890ASCD', $generator::TYPE_TELEPEN_ALPHA); + + $this->assertGreaterThan(100, strlen($result)); + } + + public function test_generator_can_generate_telepen_numeric_barcode() + { + $generator = new Picqer\Barcode\BarcodeGeneratorSVG(); + $result = $generator->getBarcode('1234567890', $generator::TYPE_TELEPEN_NUMERIC); + + $this->assertGreaterThan(100, strlen($result)); + } } \ No newline at end of file diff --git a/tests/VerifiedBarcodeTest.php b/tests/VerifiedBarcodeTest.php index 30af657..bc7f9f4 100644 --- a/tests/VerifiedBarcodeTest.php +++ b/tests/VerifiedBarcodeTest.php @@ -44,6 +44,8 @@ class VerifiedBarcodeTest extends TestCase ['type' => BarcodeGenerator::TYPE_CODE_11, 'barcodes' => ['123456789']], ['type' => BarcodeGenerator::TYPE_PHARMA_CODE, 'barcodes' => ['123456789']], ['type' => BarcodeGenerator::TYPE_PHARMA_CODE_TWO_TRACKS, 'barcodes' => ['123456789']], + ['type' => BarcodeGenerator::TYPE_TELEPEN_ALPHA, 'barcodes' => ['1234567890ASCD']], + ['type' => BarcodeGenerator::TYPE_TELEPEN_NUMERIC, 'barcodes' => ['1234567890']] ]; public function testAllSupportedBarcodeTypes() diff --git a/tests/verified-files/TELEPENALPHA-1234567890ASCD.svg b/tests/verified-files/TELEPENALPHA-1234567890ASCD.svg new file mode 100644 index 0000000..d44f436 --- /dev/null +++ b/tests/verified-files/TELEPENALPHA-1234567890ASCD.svg @@ -0,0 +1,95 @@ + + + + 1234567890ASCD + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/tests/verified-files/TELEPENNUMERIC-1234567890.svg b/tests/verified-files/TELEPENNUMERIC-1234567890.svg new file mode 100644 index 0000000..079766b --- /dev/null +++ b/tests/verified-files/TELEPENNUMERIC-1234567890.svg @@ -0,0 +1,54 @@ + + + + 1234567890 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + From 6695b3925b850ac514b86ac90dbaa2e22df77f85 Mon Sep 17 00:00:00 2001 From: Darren Stephens Date: Fri, 8 Sep 2023 11:27:04 +0100 Subject: [PATCH 107/155] Update TypeTelepen.php Minor corrections to construction, and comments --- src/Types/TypeTelepen.php | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/Types/TypeTelepen.php b/src/Types/TypeTelepen.php index 97f1600..65c3484 100644 --- a/src/Types/TypeTelepen.php +++ b/src/Types/TypeTelepen.php @@ -1,9 +1,8 @@ Robin Stuart by - * Darren Stephens - * at https://github.com/woo-j/OkapiBarcode which used the + * Adapted by Darren Stephens + * from Java implementation of Telepen by Robin Stuart + * at https://github.com/woo-j/OkapiBarcode which uses the * Apache License 2.0 http://www.apache.org/licenses/LICENSE-2.0 * * Implements Telepen (also known as Telepen Alpha), and Telepen Numeric. @@ -22,7 +21,7 @@ define('TELEPEN_START_CHAR', '_'); define('TELEPEN_STOP_CHAR', 'z'); -define('TELEPEN_ALPHA', ''); +define('TELEPEN_ALPHA', 'alpha'); define('TELEPEN_NUMERIC', 'numeric'); class TypeTelepen implements TypeInterface @@ -73,7 +72,7 @@ protected function encode($code) : string protected function encodeAlpha($code) : string { - if (!preg_match('/[ -~]+/', $code)) { // everything from ASCII0-ASCII127 + if (!preg_match('/[ -~]+/', $code)) { // everything from ASCII32-ASCII127 throw new InvalidFormatException("Invalid characters in data"); } From 8279d3def5bd13261a7f697db5fa29048fb00875 Mon Sep 17 00:00:00 2001 From: Darren Stephens Date: Fri, 15 Sep 2023 17:18:50 +0100 Subject: [PATCH 108/155] updates/adjustments in response to review --- src/Types/TypeTelepen.php | 98 ++++++++++++++++++++++----------------- 1 file changed, 56 insertions(+), 42 deletions(-) diff --git a/src/Types/TypeTelepen.php b/src/Types/TypeTelepen.php index 65c3484..bf43170 100644 --- a/src/Types/TypeTelepen.php +++ b/src/Types/TypeTelepen.php @@ -19,21 +19,21 @@ use Picqer\Barcode\BarcodeBar; use Picqer\Barcode\Exceptions\InvalidFormatException; -define('TELEPEN_START_CHAR', '_'); -define('TELEPEN_STOP_CHAR', 'z'); -define('TELEPEN_ALPHA', 'alpha'); -define('TELEPEN_NUMERIC', 'numeric'); - class TypeTelepen implements TypeInterface { + private const TELEPEN_START_CHAR = '_'; + private const TELEPEN_STOP_CHAR = 'z'; + private const TELEPEN_ALPHA = 'alpha'; + private const TELEPEN_NUMERIC = 'numeric'; + private $telepen_lookup_table; private $mode; public function __construct($m = 'alpha') { - $this->mode = TELEPEN_ALPHA; + $this->mode = self::TELEPEN_ALPHA; if (strtolower($m) == 'numeric') { - $this->mode = TELEPEN_NUMERIC; + $this->mode = self::TELEPEN_NUMERIC; } $this->createTelepenConversionTable(); } @@ -60,7 +60,7 @@ public function getBarcodeData(string $code): Barcode protected function encode($code) : string { $result = null; - if ($this->mode == TELEPEN_ALPHA) { + if ($this->mode == self::TELEPEN_ALPHA) { $result = $this->encodeAlpha($code); } else { $result = $this->encodeNumeric($code); @@ -71,8 +71,10 @@ protected function encode($code) : string protected function encodeAlpha($code) : string { - - if (!preg_match('/[ -~]+/', $code)) { // everything from ASCII32-ASCII127 + // We aren't interested in the non-printable parts of the ASCII set + if ( + !preg_match('/[ -~]+/', $code) + ) { // everything from ASCII32-ASCII127 throw new InvalidFormatException("Invalid characters in data"); } @@ -84,7 +86,7 @@ protected function encodeAlpha($code) : string */ // begin with start char - $dest = $this->telepen_lookup_table[ord(TELEPEN_START_CHAR)]; + $dest = $this->telepen_lookup_table[ord(self::TELEPEN_START_CHAR)]; for ($i = 0; $i < strlen($code); $i++) { //$ascii_code = ord(substr($code, $i, 1)); @@ -100,7 +102,7 @@ protected function encodeAlpha($code) : string } $dest .= $this->telepen_lookup_table[ord($check_digit)]; - $dest .= $this->telepen_lookup_table[ord(TELEPEN_STOP_CHAR)]; // Stop + $dest .= $this->telepen_lookup_table[ord(self::TELEPEN_STOP_CHAR)]; // Stop return $dest; } @@ -120,7 +122,7 @@ private function encodeNumeric(string $code) : string } $count = 0; - $dest = $this->telepen_lookup_table[ord(TELEPEN_START_CHAR)]; // begin with the start character _ + $dest = $this->telepen_lookup_table[ord(self::TELEPEN_START_CHAR)]; // begin with the start character _ for ($i = 0; $i < strlen($code); $i += 2) { $c1 = $code[$i]; @@ -145,42 +147,54 @@ private function encodeNumeric(string $code) : string } $dest .= $this->telepen_lookup_table[$check_digit]; - $dest .= $this->telepen_lookup_table[ord(TELEPEN_STOP_CHAR)]; // Stop + $dest .= $this->telepen_lookup_table[ord(self::TELEPEN_STOP_CHAR)]; // Stop return $dest; } + /** + * The table provides a representation of barcode patterns + * for each character in the ASCII set. from ASCII0-127 + * Each barcode starts with "_" - ASCII95 0x5F, + * and ends with "z" - ASCII122 0xFA. + * More information about Telepen symbology is available from + * https://v4l237.n3cdn1.secureserver.net/wp-content/uploads/2022/05/Barcode-Symbology-information-and-History.pdf + */ private function createTelepenConversionTable() { $this->telepen_lookup_table = [ - "1111111111111111", "1131313111", "33313111", "1111313131", - "3111313111", "11333131", "13133131", "111111313111", "31333111", - "1131113131", "33113131", "1111333111", "3111113131", "1113133111", - "1311133111", "111111113131", "3131113111", "11313331", "333331", - "111131113111", "31113331", "1133113111", "1313113111", "1111113331", - "31131331", "113111113111", "3311113111", "1111131331", "311111113111", - "1113111331", "1311111331", "11111111113111", "31313311", "1131311131", - "33311131", "1111313311", "3111311131", "11333311", "13133311", - "111111311131", "31331131", "1131113311", "33113311", "1111331131", - "3111113311", "1113131131", "1311131131", "111111113311", "3131111131", - "1131131311", "33131311", "111131111131", "3111131311", "1133111131", - "1313111131", "111111131311", "3113111311", "113111111131", - "3311111131", "111113111311", "311111111131", "111311111311", - "131111111311", "11111111111131", "3131311111", "11313133", "333133", - "111131311111", "31113133", "1133311111", "1313311111", "1111113133", - "313333", "113111311111", "3311311111", "11113333", "311111311111", - "11131333", "13111333", "11111111311111", "31311133", "1131331111", - "33331111", "1111311133", "3111331111", "11331133", "13131133", - "111111331111", "3113131111", "1131111133", "33111133", "111113131111", - "3111111133", "111311131111", "131111131111", "111111111133", - "31311313", "113131111111", "3331111111", "1111311313", "311131111111", - "11331313", "13131313", "11111131111111", "3133111111", "1131111313", - "33111313", "111133111111", "3111111313", "111313111111", - "131113111111", "111111111313", "313111111111", "1131131113", - "33131113", "11113111111111", "3111131113", "113311111111", - "131311111111", "111111131113", "3113111113", "11311111111111", - "331111111111", "111113111113", "31111111111111", "111311111113", - "131111111113" + "1111111111111111", "1131313111", "33313111", "1111313131", + "3111313111", "11333131", "13133131", "111111313111", + "31333111", "1131113131", "33113131", "1111333111", + "3111113131", "1113133111", "1311133111", "111111113131", + "3131113111", "11313331", "333331", "111131113111", + "31113331", "1133113111", "1313113111", "1111113331", + "31131331", "113111113111", "3311113111", "1111131331", + "311111113111", "1113111331", "1311111331", "11111111113111", + "31313311", "1131311131", "33311131", "1111313311", + "3111311131", "11333311", "13133311", "111111311131", + "31331131", "1131113311", "33113311", "1111331131", + "3111113311", "1113131131", "1311131131", "111111113311", + "3131111131", "1131131311", "33131311", "111131111131", + "3111131311", "1133111131", "1313111131", "111111131311", + "3113111311", "113111111131", "3311111131", "111113111311", + "311111111131", "111311111311", "131111111311", "11111111111131", + "3131311111", "11313133", "333133", "111131311111", + "31113133", "1133311111", "1313311111", "1111113133", + "313333", "113111311111", "3311311111", "11113333", + "311111311111", "11131333", "13111333", "11111111311111", + "31311133", "1131331111", "33331111", "1111311133", + "3111331111", "11331133", "13131133", "111111331111", + "3113131111", "1131111133", "33111133", "111113131111", + "3111111133", "111311131111", "131111131111", "111111111133", + "31311313", "113131111111", "3331111111", "1111311313", + "311131111111", "11331313", "13131313", "11111131111111", + "3133111111", "1131111313", "33111313", "111133111111", + "3111111313", "111313111111", "131113111111", "111111111313", + "313111111111", "1131131113", "33131113", "11113111111111", + "3111131113", "113311111111", "131311111111", "111111131113", + "3113111113", "11311111111111", "331111111111", "111113111113", + "31111111111111", "111311111113", "131111111113" ]; } } From 961c78f1978ff90a5d337b33953015767c0a38b5 Mon Sep 17 00:00:00 2001 From: Casper Bakker Date: Sat, 16 Sep 2023 09:38:56 +0200 Subject: [PATCH 109/155] Delete CHANGELOG.md --- CHANGELOG.md | 56 ---------------------------------------------------- 1 file changed, 56 deletions(-) delete mode 100644 CHANGELOG.md diff --git a/CHANGELOG.md b/CHANGELOG.md deleted file mode 100644 index 63777c2..0000000 --- a/CHANGELOG.md +++ /dev/null @@ -1,56 +0,0 @@ -# Changelog -All notable changes to this project will be documented in this file. - -The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), -and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). - -## [Unreleased] - -## [2.0.1] - 2020-01-28 -### Fixed -- Removed special chars from filenames we use for test validation #94 - -## [2.0.0] - 2020-01-11 -### Added -- Introduced Barcode and BarcodeBar classes to standardise generator output. -- Introduced methods to force use of GD or Imagick, see readme. -- Loads of new tests added, including tests on Github Actions. - -### Changed -- Splitted all barcode types to different files. -- Refactored a lot of code for better readability, stricter checking, and to be more efficient. -- Merged JPG and PNG generators, because of duplicate code. - -### Fixed -- Fixed a bug in Codabar generation 2d1128f5222d9368fc6151d2b51801ea29ba1052 -- Do not draw multiple bars on the same position #74 -- Do not try to draw barcodes for empty strings #42 -- Fixed possible casting issue in Codabar #92 - -## [0.4.0] - 2019-12-31 -### Added -- Added support for PHP 7.4, thanks to @pilif #80 - -## [0.3.0] - 2019-01-12 -### Added -- SVG: Add viewBox attribute to allow svg scaling #68 by @cuchac -- Adjust CODE_128 to handle odd number of digits #55 by @richayles - -### Fixed -- Bugfix update imagick function #51 by @Keinbockwurst - -## [0.2.2] - 2017-09-28 -### Added -- Raising exceptions if we cannot generate JPG or PNG because of missing libraries. Thanks @OskarStark - -## [0.2.1] - 2016-10-24 -### Fixed -- Bugfixes for wrong constant values. - -## [0.2.0] - 2016-05-14 -### Added -- This release adds exceptions to this package. Now it is easier to detect if the generated barcode is correct or not. - -## [0.1.0] - 2015-08-13 -### Added -- Everything. First release of this package. From 784de4b29fca99b58f3cb0148e92693852bea3d2 Mon Sep 17 00:00:00 2001 From: Casper Bakker Date: Sat, 16 Sep 2023 09:40:53 +0200 Subject: [PATCH 110/155] Update github actions to v4 --- .github/workflows/phpunit.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/phpunit.yml b/.github/workflows/phpunit.yml index 712364a..785311c 100644 --- a/.github/workflows/phpunit.yml +++ b/.github/workflows/phpunit.yml @@ -12,7 +12,7 @@ jobs: php-versions: ['7.3', '7.4', '8.0', '8.1', '8.2'] steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 - name: Setup PHP uses: shivammathur/setup-php@v2 From 76a15ec324523c141f30b706db5c52c055900f65 Mon Sep 17 00:00:00 2001 From: Casper Bakker Date: Sat, 16 Sep 2023 09:54:08 +0200 Subject: [PATCH 111/155] Fix phpstan errors level 1 --- src/BarcodeGeneratorPNG.php | 4 ++-- src/Types/TypeCode128.php | 8 ++++++++ src/Types/TypePharmacodeTwoCode.php | 5 +++++ 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/src/BarcodeGeneratorPNG.php b/src/BarcodeGeneratorPNG.php index 05ac413..150e1e4 100644 --- a/src/BarcodeGeneratorPNG.php +++ b/src/BarcodeGeneratorPNG.php @@ -76,7 +76,7 @@ public function getBarcode(string $barcode, $type, int $widthFactor = 2, int $he $barHeight = round(($bar->getHeight() * $height / $barcodeData->getHeight()), 3); // draw a vertical bar - if ($this->useImagick && isset($imagickBarsShape)) { + if ($this->useImagick) { $imagickBarsShape->rectangle($positionHorizontal, $y, ($positionHorizontal + $barWidth - 1), ($y + $barHeight)); } else { imagefilledrectangle($image, $positionHorizontal, $y, ($positionHorizontal + $barWidth - 1), ($y + $barHeight), $gdForegroundColor); @@ -85,7 +85,7 @@ public function getBarcode(string $barcode, $type, int $widthFactor = 2, int $he $positionHorizontal += $barWidth; } - if ($this->useImagick && isset($imagickBarsShape)) { + if ($this->useImagick) { $image = $this->createImagickImageObject($width, $height); $image->drawImage($imagickBarsShape); return $image->getImageBlob(); diff --git a/src/Types/TypeCode128.php b/src/Types/TypeCode128.php index 0f4c5a5..27d3ea6 100644 --- a/src/Types/TypeCode128.php +++ b/src/Types/TypeCode128.php @@ -4,6 +4,7 @@ use Picqer\Barcode\Barcode; use Picqer\Barcode\BarcodeBar; +use Picqer\Barcode\Exceptions\BarcodeException; use Picqer\Barcode\Exceptions\InvalidCharacterException; use Picqer\Barcode\Exceptions\InvalidLengthException; @@ -331,11 +332,18 @@ public function getBarcodeData(string $code): Barcode $code_data[] = intval($chrnum); } break; + + default: + throw new InvalidCharacterException('Do not support different mode then A, B or C.'); } } } // calculate check character + if (! isset($startid)) { + throw new BarcodeException('Could not determine start char for barcode.'); + } + $sum = $startid; foreach ($code_data as $key => $val) { $sum += ($val * ($key + 1)); diff --git a/src/Types/TypePharmacodeTwoCode.php b/src/Types/TypePharmacodeTwoCode.php index 7c1959a..3d7c2fa 100644 --- a/src/Types/TypePharmacodeTwoCode.php +++ b/src/Types/TypePharmacodeTwoCode.php @@ -9,6 +9,8 @@ use Picqer\Barcode\Barcode; use Picqer\Barcode\BarcodeBar; +use Picqer\Barcode\Exceptions\BarcodeException; +use Picqer\Barcode\Exceptions\InvalidCharacterException; use Picqer\Barcode\Exceptions\InvalidLengthException; class TypePharmacodeTwoCode implements TypeInterface @@ -62,6 +64,9 @@ public function getBarcodeData(string $code): Barcode $p = 0; $h = 2; break; + + default: + throw new InvalidCharacterException('Could not find bar for char.'); } $barcode->addBar(new BarcodeBar(1, $h, 1, $p)); From 326792a2b8a4bcef7fa972a63e0fc85796eff348 Mon Sep 17 00:00:00 2001 From: Casper Bakker Date: Sat, 16 Sep 2023 10:32:58 +0200 Subject: [PATCH 112/155] Run static analysis in Github Actions --- .github/workflows/phpstan.yml | 25 +++++++++++++++++++++++++ .github/workflows/phpunit.yml | 10 ++++++---- composer.json | 3 ++- 3 files changed, 33 insertions(+), 5 deletions(-) create mode 100644 .github/workflows/phpstan.yml diff --git a/.github/workflows/phpstan.yml b/.github/workflows/phpstan.yml new file mode 100644 index 0000000..831b110 --- /dev/null +++ b/.github/workflows/phpstan.yml @@ -0,0 +1,25 @@ +name: Static analysis (phpstan) + +on: [push, pull_request] + +jobs: + build: + runs-on: ubuntu-latest + + steps: + - name: Checkout code + uses: actions/checkout@v4 + with: + fetch-depth: 0 + + - name: Setup PHP + uses: shivammathur/setup-php@v2 + with: + php-version: 8.2 + extensions: mbstring, gd, bcmath, imagick + + - name: Install dependencies + run: composer install --prefer-dist --no-progress --no-interaction + + - name: Run analysis + run: vendor/bin/phpstan analyse src --level 1 diff --git a/.github/workflows/phpunit.yml b/.github/workflows/phpunit.yml index 785311c..2c3d4e3 100644 --- a/.github/workflows/phpunit.yml +++ b/.github/workflows/phpunit.yml @@ -1,10 +1,9 @@ -name: phpunit +name: Unit tests (phpunit) on: [push, pull_request] jobs: build: - runs-on: ubuntu-latest strategy: @@ -12,7 +11,10 @@ jobs: php-versions: ['7.3', '7.4', '8.0', '8.1', '8.2'] steps: - - uses: actions/checkout@v4 + - name: Checkout code + uses: actions/checkout@v4 + with: + fetch-depth: 0 - name: Setup PHP uses: shivammathur/setup-php@v2 @@ -24,7 +26,7 @@ jobs: run: composer validate - name: Install dependencies - run: composer install --prefer-dist --no-progress + run: composer install --prefer-dist --no-progress --no-interaction - name: Run test suite run: composer run-script test diff --git a/composer.json b/composer.json index 180639e..3328695 100644 --- a/composer.json +++ b/composer.json @@ -22,7 +22,8 @@ "ext-mbstring": "*" }, "require-dev": { - "phpunit/phpunit": "^9.5" + "phpunit/phpunit": "^9.5", + "phpstan/phpstan": "^1.10" }, "suggest": { "ext-bcmath": "Barcode IMB (Intelligent Mail Barcode) needs bcmath extension", From 3d6b3400cbc25fe12bed65407c7fa211ffb05add Mon Sep 17 00:00:00 2001 From: Casper Bakker Date: Sat, 16 Sep 2023 10:45:08 +0200 Subject: [PATCH 113/155] Phpstan fixed to level 2 --- .github/workflows/phpstan.yml | 2 +- src/Types/TypeIntelligentMailBarcode.php | 8 ++++---- src/Types/TypeUpcExtension2.php | 8 ++++---- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/.github/workflows/phpstan.yml b/.github/workflows/phpstan.yml index 831b110..db0f34d 100644 --- a/.github/workflows/phpstan.yml +++ b/.github/workflows/phpstan.yml @@ -22,4 +22,4 @@ jobs: run: composer install --prefer-dist --no-progress --no-interaction - name: Run analysis - run: vendor/bin/phpstan analyse src --level 1 + run: vendor/bin/phpstan analyse src --level 2 diff --git a/src/Types/TypeIntelligentMailBarcode.php b/src/Types/TypeIntelligentMailBarcode.php index 8c4f8c4..e574cf7 100644 --- a/src/Types/TypeIntelligentMailBarcode.php +++ b/src/Types/TypeIntelligentMailBarcode.php @@ -378,11 +378,11 @@ public function getBarcodeData(string $code): Barcode // convert codewords to characters $characters = []; $bitmask = 512; - foreach ($codewords as $k => $val) { + foreach ($codewords as $val) { if ($val <= 1286) { - $chrcode = $table5of13[$val]; + $chrcode = (int)$table5of13[$val]; } else { - $chrcode = $table2of13[($val - 1287)]; + $chrcode = (int)$table2of13[($val - 1287)]; } if (($fcs & $bitmask) > 0) { // bitwise invert @@ -517,7 +517,7 @@ protected function hex_to_dec($hex) * @return array requested table * @protected */ - protected function imb_tables($n, $size) + protected function imb_tables(int $n, int $size): array { $table = []; $lli = 0; // LUT lower index diff --git a/src/Types/TypeUpcExtension2.php b/src/Types/TypeUpcExtension2.php index 98ee3c6..cebbfa8 100644 --- a/src/Types/TypeUpcExtension2.php +++ b/src/Types/TypeUpcExtension2.php @@ -20,20 +20,20 @@ public function getBarcodeData(string $code): Barcode { $len = $this->length; - //Padding + // Padding $code = str_pad($code, $len, '0', STR_PAD_LEFT); - // calculate check digit + // Calculate check digit if ($len == 2) { $r = $code % 4; } elseif ($len == 5) { - $r = (3 * ($code[0] + $code[2] + $code[4])) + (9 * ($code[1] + $code[3])); + $r = (3 * intval($code[0] . $code[2] . $code[4])) + (9 * intval($code[1] . $code[3])); $r %= 10; } else { throw new InvalidCheckDigitException(); } - //Convert digits to bars + // Convert digits to bars $codes = [ 'A' => [ // left odd parity '0' => '0001101', From da543987d6a083ffa03366e005eb9c90579c92c8 Mon Sep 17 00:00:00 2001 From: Casper Bakker Date: Sat, 16 Sep 2023 10:46:25 +0200 Subject: [PATCH 114/155] Phpstan level 3 --- src/Types/TypeCode11.php | 2 +- src/Types/TypeCode32.php | 2 +- src/Types/TypeInterleaved25Checksum.php | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Types/TypeCode11.php b/src/Types/TypeCode11.php index 7352e99..2857cb3 100644 --- a/src/Types/TypeCode11.php +++ b/src/Types/TypeCode11.php @@ -106,6 +106,6 @@ private function getCheckDigitK(string $code): string } $check %= 11; - return $check; + return (string)$check; } } diff --git a/src/Types/TypeCode32.php b/src/Types/TypeCode32.php index e5df479..6ca6816 100644 --- a/src/Types/TypeCode32.php +++ b/src/Types/TypeCode32.php @@ -109,6 +109,6 @@ protected function checksum_code32(string $code): string } } - return (string)$s % 10; + return (string)($s % 10); } } diff --git a/src/Types/TypeInterleaved25Checksum.php b/src/Types/TypeInterleaved25Checksum.php index 1af0439..6192126 100644 --- a/src/Types/TypeInterleaved25Checksum.php +++ b/src/Types/TypeInterleaved25Checksum.php @@ -86,6 +86,6 @@ protected function getChecksum(string $code): string $r = (10 - $r); } - return $r; + return (string)$r; } } From c72e5c801c1b8ae1f284b804fd5111e6da4ec373 Mon Sep 17 00:00:00 2001 From: Casper Bakker Date: Sat, 16 Sep 2023 10:51:06 +0200 Subject: [PATCH 115/155] Phpstan level 4 --- .github/workflows/phpstan.yml | 2 +- src/Types/TypeCode128.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/phpstan.yml b/.github/workflows/phpstan.yml index db0f34d..70afc49 100644 --- a/.github/workflows/phpstan.yml +++ b/.github/workflows/phpstan.yml @@ -22,4 +22,4 @@ jobs: run: composer install --prefer-dist --no-progress --no-interaction - name: Run analysis - run: vendor/bin/phpstan analyse src --level 2 + run: vendor/bin/phpstan analyse src --level 4 diff --git a/src/Types/TypeCode128.php b/src/Types/TypeCode128.php index 27d3ea6..38fc618 100644 --- a/src/Types/TypeCode128.php +++ b/src/Types/TypeCode128.php @@ -165,7 +165,7 @@ public function getBarcodeData(string $code): Barcode $char_id = ord($char); if (($char_id >= 241) AND ($char_id <= 244)) { $code_data[] = $fnc_a[$char_id]; - } elseif (($char_id >= 0) AND ($char_id <= 95)) { + } elseif ($char_id <= 95) { $code_data[] = strpos($keys_a, $char); } else { throw new InvalidCharacterException('Char ' . $char . ' is unsupported'); From 02365e8e279c1f5414c769dcfc5dcaf65966d8f9 Mon Sep 17 00:00:00 2001 From: Casper Bakker Date: Sat, 16 Sep 2023 10:56:21 +0200 Subject: [PATCH 116/155] Tweak phpstan running --- .github/workflows/phpstan.yml | 2 +- phpstan.neon.dist | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) create mode 100644 phpstan.neon.dist diff --git a/.github/workflows/phpstan.yml b/.github/workflows/phpstan.yml index 70afc49..d2e5686 100644 --- a/.github/workflows/phpstan.yml +++ b/.github/workflows/phpstan.yml @@ -22,4 +22,4 @@ jobs: run: composer install --prefer-dist --no-progress --no-interaction - name: Run analysis - run: vendor/bin/phpstan analyse src --level 4 + run: vendor/bin/phpstan --error-format=github --no-progress diff --git a/phpstan.neon.dist b/phpstan.neon.dist new file mode 100644 index 0000000..0405354 --- /dev/null +++ b/phpstan.neon.dist @@ -0,0 +1,4 @@ +parameters: + paths: + - src + level: 4 From b64906f233792c031aabe10512eb34d05f824e9d Mon Sep 17 00:00:00 2001 From: Casper Bakker Date: Sat, 16 Sep 2023 10:57:37 +0200 Subject: [PATCH 117/155] No double runs on PR's --- .github/workflows/phpstan.yml | 6 +++++- .github/workflows/phpunit.yml | 6 +++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/.github/workflows/phpstan.yml b/.github/workflows/phpstan.yml index d2e5686..7528e81 100644 --- a/.github/workflows/phpstan.yml +++ b/.github/workflows/phpstan.yml @@ -1,6 +1,10 @@ name: Static analysis (phpstan) -on: [push, pull_request] +on: + push: + branches: + - main + pull_request: jobs: build: diff --git a/.github/workflows/phpunit.yml b/.github/workflows/phpunit.yml index 2c3d4e3..cfd73af 100644 --- a/.github/workflows/phpunit.yml +++ b/.github/workflows/phpunit.yml @@ -1,6 +1,10 @@ name: Unit tests (phpunit) -on: [push, pull_request] +on: + push: + branches: + - main + pull_request: jobs: build: From 4e35979ca5210ebc581974557562793c5428d915 Mon Sep 17 00:00:00 2001 From: Casper Bakker Date: Thu, 25 Jan 2024 08:53:48 +0100 Subject: [PATCH 118/155] Update phpstan.yml --- .github/workflows/phpstan.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/phpstan.yml b/.github/workflows/phpstan.yml index 7528e81..1e4ca51 100644 --- a/.github/workflows/phpstan.yml +++ b/.github/workflows/phpstan.yml @@ -19,7 +19,7 @@ jobs: - name: Setup PHP uses: shivammathur/setup-php@v2 with: - php-version: 8.2 + php-version: 8.3 extensions: mbstring, gd, bcmath, imagick - name: Install dependencies From a0bf54caed6d36f4f7a2476b25dfde89e3a1993b Mon Sep 17 00:00:00 2001 From: Casper Bakker Date: Thu, 25 Jan 2024 08:54:04 +0100 Subject: [PATCH 119/155] Update phpunit.yml --- .github/workflows/phpunit.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/phpunit.yml b/.github/workflows/phpunit.yml index cfd73af..08c393d 100644 --- a/.github/workflows/phpunit.yml +++ b/.github/workflows/phpunit.yml @@ -12,7 +12,7 @@ jobs: strategy: matrix: - php-versions: ['7.3', '7.4', '8.0', '8.1', '8.2'] + php-versions: ['7.3', '7.4', '8.0', '8.1', '8.2', '8.3'] steps: - name: Checkout code From 6c8860dce3c7c0c6ed04dbcf86d9dace02d4f34b Mon Sep 17 00:00:00 2001 From: Casper Bakker Date: Thu, 25 Jan 2024 08:55:38 +0100 Subject: [PATCH 120/155] Update composer.json --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 3328695..dadc0d2 100644 --- a/composer.json +++ b/composer.json @@ -18,7 +18,7 @@ } ], "require": { - "php": "^7.3|^8.0", + "php": "^8.1", "ext-mbstring": "*" }, "require-dev": { From 132e5ebab01e7f012060b0f517ca1da24a66c739 Mon Sep 17 00:00:00 2001 From: Casper Bakker Date: Thu, 25 Jan 2024 08:55:59 +0100 Subject: [PATCH 121/155] Update phpunit.yml --- .github/workflows/phpunit.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/phpunit.yml b/.github/workflows/phpunit.yml index 08c393d..5739810 100644 --- a/.github/workflows/phpunit.yml +++ b/.github/workflows/phpunit.yml @@ -12,7 +12,7 @@ jobs: strategy: matrix: - php-versions: ['7.3', '7.4', '8.0', '8.1', '8.2', '8.3'] + php-versions: ['8.1', '8.2', '8.3'] steps: - name: Checkout code From 1c4bc99178b5a9b53d60a604b924cea39f532c2a Mon Sep 17 00:00:00 2001 From: Casper Bakker Date: Sun, 31 Mar 2024 12:09:51 +0200 Subject: [PATCH 122/155] New renderer style --- .github/workflows/phpunit.yml | 2 +- composer.json | 2 +- src/Barcode.php | 10 ++--- src/BarcodeBar.php | 10 ++--- src/BarcodeGenerator.php | 2 +- src/BarcodeGeneratorSVG.php | 36 ++------------- src/Renderers/SvgRenderer.php | 57 ++++++++++++++++++++++++ src/Types/TypeCodabar.php | 2 +- src/Types/TypeCode11.php | 2 +- src/Types/TypeCode128.php | 2 +- src/Types/TypeCode32.php | 4 +- src/Types/TypeCode39.php | 2 +- src/Types/TypeCode93.php | 2 +- src/Types/TypeEan13.php | 6 +-- src/Types/TypeEan8.php | 6 +-- src/Types/TypeEanUpcBase.php | 10 ++--- src/Types/TypeITF14.php | 2 +- src/Types/TypeIntelligentMailBarcode.php | 2 +- src/Types/TypeInterface.php | 2 +- src/Types/TypeInterleaved25Checksum.php | 2 +- src/Types/TypeMsiChecksum.php | 2 +- src/Types/TypePharmacode.php | 2 +- src/Types/TypePharmacodeTwoCode.php | 2 +- src/Types/TypePostnet.php | 2 +- src/Types/TypeRms4cc.php | 2 +- src/Types/TypeStandard2of5.php | 2 +- src/Types/TypeTelepen.php | 2 +- src/Types/TypeUpcA.php | 6 +-- src/Types/TypeUpcE.php | 6 +-- src/Types/TypeUpcExtension2.php | 2 +- tests/BarcodeSvgv3Test.php | 26 +++++++++++ tests/PharmacodeTest.php | 2 +- tests/VerifiedBarcodeTest.php | 2 +- 33 files changed, 137 insertions(+), 84 deletions(-) create mode 100644 src/Renderers/SvgRenderer.php create mode 100644 tests/BarcodeSvgv3Test.php diff --git a/.github/workflows/phpunit.yml b/.github/workflows/phpunit.yml index 5739810..33b5a11 100644 --- a/.github/workflows/phpunit.yml +++ b/.github/workflows/phpunit.yml @@ -12,7 +12,7 @@ jobs: strategy: matrix: - php-versions: ['8.1', '8.2', '8.3'] + php-versions: ['8.2', '8.3'] steps: - name: Checkout code diff --git a/composer.json b/composer.json index dadc0d2..e65b8e8 100644 --- a/composer.json +++ b/composer.json @@ -18,7 +18,7 @@ } ], "require": { - "php": "^8.1", + "php": "^8.2", "ext-mbstring": "*" }, "require-dev": { diff --git a/src/Barcode.php b/src/Barcode.php index 4ab9f20..dbb27f1 100644 --- a/src/Barcode.php +++ b/src/Barcode.php @@ -4,10 +4,10 @@ class Barcode { - protected $barcode; - protected $width = 0; - protected $height = 0; - protected $bars = []; + protected string $barcode; + protected int $width = 0; + protected int $height = 0; + protected array $bars = []; public function __construct(string $barcode) { @@ -40,4 +40,4 @@ public function getBars(): array { return $this->bars; } -} \ No newline at end of file +} diff --git a/src/BarcodeBar.php b/src/BarcodeBar.php index 332a661..61a415f 100644 --- a/src/BarcodeBar.php +++ b/src/BarcodeBar.php @@ -4,10 +4,10 @@ class BarcodeBar { - protected $width; - protected $height; - protected $positionVertical; - protected $type; + protected int $width; + protected int $height; + protected int $positionVertical; + protected int $type; const TYPE_BAR = 1; const TYPE_SPACING = 0; @@ -39,4 +39,4 @@ public function isBar(): bool { return $this->type === self::TYPE_BAR; } -} \ No newline at end of file +} diff --git a/src/BarcodeGenerator.php b/src/BarcodeGenerator.php index d7c1b2e..9dd7f76 100644 --- a/src/BarcodeGenerator.php +++ b/src/BarcodeGenerator.php @@ -105,7 +105,7 @@ protected function getBarcodeData(string $code, string $type): Barcode { $barcodeDataBuilder = $this->createDataBuilderForType($type); - return $barcodeDataBuilder->getBarcodeData($code); + return $barcodeDataBuilder->getBarcode($code); } protected function createDataBuilderForType(string $type) diff --git a/src/BarcodeGeneratorSVG.php b/src/BarcodeGeneratorSVG.php index 4d0d477..aab6a15 100644 --- a/src/BarcodeGeneratorSVG.php +++ b/src/BarcodeGeneratorSVG.php @@ -19,41 +19,11 @@ public function getBarcode(string $barcode, $type, float $widthFactor = 2, float { $barcodeData = $this->getBarcodeData($barcode, $type); - // replace table for special characters - $repstr = [ - "\0" => '', - '&' => '&', - '<' => '<', - '>' => '>', - ]; - $width = round(($barcodeData->getWidth() * $widthFactor), 3); - $svg = '' . PHP_EOL; - $svg .= '' . PHP_EOL; - $svg .= '' . PHP_EOL; - $svg .= "\t" . '' . strtr($barcodeData->getBarcode(), $repstr) . '' . PHP_EOL; - $svg .= "\t" . '' . PHP_EOL; - - // print bars - $positionHorizontal = 0; - /** @var BarcodeBar $bar */ - foreach ($barcodeData->getBars() as $bar) { - $barWidth = round(($bar->getWidth() * $widthFactor), 3); - $barHeight = round(($bar->getHeight() * $height / $barcodeData->getHeight()), 3); - - if ($bar->isBar() && $barWidth > 0) { - $positionVertical = round(($bar->getPositionVertical() * $height / $barcodeData->getHeight()), 3); - // draw a vertical bar - $svg .= "\t\t" . '' . PHP_EOL; - } - - $positionHorizontal += $barWidth; - } - - $svg .= "\t" . PHP_EOL; - $svg .= '' . PHP_EOL; + $renderer = new \Picqer\Barcode\Renderers\SvgRenderer(); + $renderer->setForegroundColor($foregroundColor); - return $svg; + return $renderer->render($barcodeData, $width, $height); } } diff --git a/src/Renderers/SvgRenderer.php b/src/Renderers/SvgRenderer.php new file mode 100644 index 0000000..72dd993 --- /dev/null +++ b/src/Renderers/SvgRenderer.php @@ -0,0 +1,57 @@ + '', + '&' => '&', + '<' => '<', + '>' => '>', + ]; + +// $width = round(($barcode->getWidth() * $widthFactor), 3); + $widthFactor = $width / $barcode->getWidth(); + + $svg = '' . PHP_EOL; + $svg .= '' . PHP_EOL; + $svg .= '' . PHP_EOL; + $svg .= "\t" . '' . strtr($barcode->getBarcode(), $repstr) . '' . PHP_EOL; + $svg .= "\t" . '' . PHP_EOL; + + // print bars + $positionHorizontal = 0; + /** @var BarcodeBar $bar */ + foreach ($barcode->getBars() as $bar) { + $barWidth = round(($bar->getWidth() * $widthFactor), 3); + $barHeight = round(($bar->getHeight() * $height / $barcode->getHeight()), 3); + + if ($bar->isBar() && $barWidth > 0) { + $positionVertical = round(($bar->getPositionVertical() * $height / $barcode->getHeight()), 3); + // draw a vertical bar + $svg .= "\t\t" . '' . PHP_EOL; + } + + $positionHorizontal += $barWidth; + } + + $svg .= "\t" . PHP_EOL; + $svg .= '' . PHP_EOL; + + return $svg; + } + + public function setForegroundColor(string $color) + { + $this->foregroundColor = $color; + } +} diff --git a/src/Types/TypeCodabar.php b/src/Types/TypeCodabar.php index 0bf40e1..62c78f8 100644 --- a/src/Types/TypeCodabar.php +++ b/src/Types/TypeCodabar.php @@ -36,7 +36,7 @@ class TypeCodabar implements TypeInterface 'D' => '11122211' ]; - public function getBarcodeData(string $code): Barcode + public function getBarcode(string $code): Barcode { $barcode = new Barcode($code); diff --git a/src/Types/TypeCode11.php b/src/Types/TypeCode11.php index 2857cb3..1886364 100644 --- a/src/Types/TypeCode11.php +++ b/src/Types/TypeCode11.php @@ -28,7 +28,7 @@ class TypeCode11 implements TypeInterface 'S' => '112211', ]; - public function getBarcodeData(string $code): Barcode + public function getBarcode(string $code): Barcode { $barcode = new Barcode($code); diff --git a/src/Types/TypeCode128.php b/src/Types/TypeCode128.php index 38fc618..3a6242d 100644 --- a/src/Types/TypeCode128.php +++ b/src/Types/TypeCode128.php @@ -131,7 +131,7 @@ class TypeCode128 implements TypeInterface '200000' /* END */ ]; - public function getBarcodeData(string $code): Barcode + public function getBarcode(string $code): Barcode { if (strlen(trim($code)) === 0) { throw new InvalidLengthException('You should provide a barcode string.'); diff --git a/src/Types/TypeCode32.php b/src/Types/TypeCode32.php index 6ca6816..5c50395 100644 --- a/src/Types/TypeCode32.php +++ b/src/Types/TypeCode32.php @@ -48,7 +48,7 @@ class TypeCode32 extends TypeCode39 '31' => 'Z' ]; - public function getBarcodeData(string $code): Barcode + public function getBarcode(string $code): Barcode { // Validate code 32. $stringLength = strlen($code); @@ -85,7 +85,7 @@ public function getBarcodeData(string $code): Barcode } // Return barcode data for code 39. - return parent::getBarcodeData($code39); + return parent::getBarcode($code39); } diff --git a/src/Types/TypeCode39.php b/src/Types/TypeCode39.php index 5a4a2d2..53cbaf9 100644 --- a/src/Types/TypeCode39.php +++ b/src/Types/TypeCode39.php @@ -64,7 +64,7 @@ class TypeCode39 implements TypeInterface '*' => '131131311', ]; - public function getBarcodeData(string $code): Barcode + public function getBarcode(string $code): Barcode { if (strlen(trim($code)) === 0) { throw new InvalidLengthException('You should provide a barcode string.'); diff --git a/src/Types/TypeCode93.php b/src/Types/TypeCode93.php index bca30be..0bd9317 100644 --- a/src/Types/TypeCode93.php +++ b/src/Types/TypeCode93.php @@ -66,7 +66,7 @@ class TypeCode93 implements TypeInterface 42 => '111141', // start-stop ]; - public function getBarcodeData(string $code): Barcode + public function getBarcode(string $code): Barcode { $encode = [ chr(0) => 'bU', diff --git a/src/Types/TypeEan13.php b/src/Types/TypeEan13.php index 194d3ee..3f2fe75 100644 --- a/src/Types/TypeEan13.php +++ b/src/Types/TypeEan13.php @@ -14,7 +14,7 @@ class TypeEan13 extends TypeEanUpcBase { - protected $length = 13; - protected $upca = false; - protected $upce = false; + protected int $length = 13; + protected bool $upca = false; + protected bool $upce = false; } diff --git a/src/Types/TypeEan8.php b/src/Types/TypeEan8.php index 6020761..e605e34 100644 --- a/src/Types/TypeEan8.php +++ b/src/Types/TypeEan8.php @@ -14,7 +14,7 @@ class TypeEan8 extends TypeEanUpcBase { - protected $length = 8; - protected $upca = false; - protected $upce = false; + protected int $length = 8; + protected bool $upca = false; + protected bool $upce = false; } diff --git a/src/Types/TypeEanUpcBase.php b/src/Types/TypeEanUpcBase.php index 2ad218e..fe8948a 100644 --- a/src/Types/TypeEanUpcBase.php +++ b/src/Types/TypeEanUpcBase.php @@ -20,11 +20,11 @@ abstract class TypeEanUpcBase implements TypeInterface { - protected $length = 13; - protected $upca = false; - protected $upce = false; + protected int $length = 13; + protected bool $upca = false; + protected bool $upce = false; - public function getBarcodeData(string $code): Barcode + public function getBarcode(string $code): Barcode { if (strlen(trim($code)) === 0) { throw new InvalidLengthException('You should provide a barcode string.'); @@ -206,7 +206,7 @@ public function getBarcodeData(string $code): Barcode return $barcode; } - protected function calculateChecksumDigit(string $code) + protected function calculateChecksumDigit(string $code): int { // calculate check digit $sum_a = 0; diff --git a/src/Types/TypeITF14.php b/src/Types/TypeITF14.php index ff395d9..50f7b2e 100644 --- a/src/Types/TypeITF14.php +++ b/src/Types/TypeITF14.php @@ -13,7 +13,7 @@ class TypeITF14 implements TypeInterface * @throws InvalidLengthException * @throws InvalidCharacterException */ - public function getBarcodeData(string $code): Barcode + public function getBarcode(string $code): Barcode { $chr = []; $chr['0'] = '11221'; diff --git a/src/Types/TypeIntelligentMailBarcode.php b/src/Types/TypeIntelligentMailBarcode.php index e574cf7..7d74829 100644 --- a/src/Types/TypeIntelligentMailBarcode.php +++ b/src/Types/TypeIntelligentMailBarcode.php @@ -34,7 +34,7 @@ class TypeIntelligentMailBarcode implements TypeInterface { - public function getBarcodeData(string $code): Barcode + public function getBarcode(string $code): Barcode { $asc_chr = [ 4, diff --git a/src/Types/TypeInterface.php b/src/Types/TypeInterface.php index f380c38..1fa946d 100644 --- a/src/Types/TypeInterface.php +++ b/src/Types/TypeInterface.php @@ -6,5 +6,5 @@ interface TypeInterface { - public function getBarcodeData(string $code): Barcode; + public function getBarcode(string $code): Barcode; } diff --git a/src/Types/TypeInterleaved25Checksum.php b/src/Types/TypeInterleaved25Checksum.php index 6192126..0cb9aa6 100644 --- a/src/Types/TypeInterleaved25Checksum.php +++ b/src/Types/TypeInterleaved25Checksum.php @@ -14,7 +14,7 @@ class TypeInterleaved25Checksum implements TypeInterface { - public function getBarcodeData(string $code): Barcode + public function getBarcode(string $code): Barcode { $chr = []; $chr['0'] = '11221'; diff --git a/src/Types/TypeMsiChecksum.php b/src/Types/TypeMsiChecksum.php index cb3a0ff..c421391 100644 --- a/src/Types/TypeMsiChecksum.php +++ b/src/Types/TypeMsiChecksum.php @@ -19,7 +19,7 @@ class TypeMsiChecksum implements TypeInterface { protected $checksum = true; - public function getBarcodeData(string $code): Barcode + public function getBarcode(string $code): Barcode { $chr['0'] = '100100100100'; $chr['1'] = '100100100110'; diff --git a/src/Types/TypePharmacode.php b/src/Types/TypePharmacode.php index 5f1f128..66a628b 100644 --- a/src/Types/TypePharmacode.php +++ b/src/Types/TypePharmacode.php @@ -12,7 +12,7 @@ class TypePharmacode implements TypeInterface { - public function getBarcodeData(string $code): Barcode + public function getBarcode(string $code): Barcode { $code = intval($code); diff --git a/src/Types/TypePharmacodeTwoCode.php b/src/Types/TypePharmacodeTwoCode.php index 3d7c2fa..cde23c0 100644 --- a/src/Types/TypePharmacodeTwoCode.php +++ b/src/Types/TypePharmacodeTwoCode.php @@ -15,7 +15,7 @@ class TypePharmacodeTwoCode implements TypeInterface { - public function getBarcodeData(string $code): Barcode + public function getBarcode(string $code): Barcode { $code = intval($code); diff --git a/src/Types/TypePostnet.php b/src/Types/TypePostnet.php index 1f99ae2..a43d093 100644 --- a/src/Types/TypePostnet.php +++ b/src/Types/TypePostnet.php @@ -29,7 +29,7 @@ class TypePostnet implements TypeInterface 9 => [2, 1, 2, 1, 1] ]; - public function getBarcodeData(string $code): Barcode + public function getBarcode(string $code): Barcode { $code = str_replace(['-', ' '], '', $code); $len = strlen($code); diff --git a/src/Types/TypeRms4cc.php b/src/Types/TypeRms4cc.php index c728bcb..8dac3b4 100644 --- a/src/Types/TypeRms4cc.php +++ b/src/Types/TypeRms4cc.php @@ -17,7 +17,7 @@ class TypeRms4cc implements TypeInterface { protected $kix = false; - public function getBarcodeData(string $code): Barcode + public function getBarcode(string $code): Barcode { // bar mode // 1 = pos 1, length 2 diff --git a/src/Types/TypeStandard2of5.php b/src/Types/TypeStandard2of5.php index 592f59a..906445d 100644 --- a/src/Types/TypeStandard2of5.php +++ b/src/Types/TypeStandard2of5.php @@ -16,7 +16,7 @@ class TypeStandard2of5 implements TypeInterface { protected $checksum = false; - public function getBarcodeData(string $code): Barcode + public function getBarcode(string $code): Barcode { $chr['0'] = '10101110111010'; $chr['1'] = '11101010101110'; diff --git a/src/Types/TypeTelepen.php b/src/Types/TypeTelepen.php index bf43170..6ec1158 100644 --- a/src/Types/TypeTelepen.php +++ b/src/Types/TypeTelepen.php @@ -38,7 +38,7 @@ public function __construct($m = 'alpha') $this->createTelepenConversionTable(); } - public function getBarcodeData(string $code): Barcode + public function getBarcode(string $code): Barcode { /* The stream we get from the telepen output gives us the * width of alternating black/white stripes diff --git a/src/Types/TypeUpcA.php b/src/Types/TypeUpcA.php index 11d45b9..11c21a8 100644 --- a/src/Types/TypeUpcA.php +++ b/src/Types/TypeUpcA.php @@ -14,7 +14,7 @@ class TypeUpcA extends TypeEanUpcBase { - protected $length = 12; - protected $upca = true; - protected $upce = false; + protected int $length = 12; + protected bool $upca = true; + protected bool $upce = false; } diff --git a/src/Types/TypeUpcE.php b/src/Types/TypeUpcE.php index 4fd8a7e..95ada11 100644 --- a/src/Types/TypeUpcE.php +++ b/src/Types/TypeUpcE.php @@ -14,7 +14,7 @@ class TypeUpcE extends TypeEanUpcBase { - protected $length = 12; - protected $upca = false; - protected $upce = true; + protected int $length = 12; + protected bool $upca = false; + protected bool $upce = true; } diff --git a/src/Types/TypeUpcExtension2.php b/src/Types/TypeUpcExtension2.php index cebbfa8..f0620e1 100644 --- a/src/Types/TypeUpcExtension2.php +++ b/src/Types/TypeUpcExtension2.php @@ -16,7 +16,7 @@ class TypeUpcExtension2 implements TypeInterface { protected $length = 2; - public function getBarcodeData(string $code): Barcode + public function getBarcode(string $code): Barcode { $len = $this->length; diff --git a/tests/BarcodeSvgv3Test.php b/tests/BarcodeSvgv3Test.php new file mode 100644 index 0000000..5af16b7 --- /dev/null +++ b/tests/BarcodeSvgv3Test.php @@ -0,0 +1,26 @@ +getBarcode('081231723897'); + + $renderer = new Picqer\Barcode\Renderers\SvgRenderer(); + $generated = $renderer->render($barcode, 190); + + $this->assertStringEqualsFile('tests/verified-files/081231723897-ean13.svg', $generated); + } + + public function test_svg_barcode_generator_can_generate_ean_13_barcode_with_fractional_width() + { + $barcode = (new Picqer\Barcode\Types\TypeEan13())->getBarcode('081231723897'); + + $renderer = new Picqer\Barcode\Renderers\SvgRenderer(); + $generated = $renderer->render($barcode, 23.75, 25.75); + + $this->assertStringEqualsFile('tests/verified-files/081231723897-ean13-fractional-width.svg', $generated); + } +} diff --git a/tests/PharmacodeTest.php b/tests/PharmacodeTest.php index c068ffb..e2f4ed5 100644 --- a/tests/PharmacodeTest.php +++ b/tests/PharmacodeTest.php @@ -10,6 +10,6 @@ public function test_validation_triggerd_when_generating_zero_code() $this->expectException(Picqer\Barcode\Exceptions\InvalidLengthException::class); - $pharmacode->getBarcodeData('0'); + $pharmacode->getBarcode('0'); } } diff --git a/tests/VerifiedBarcodeTest.php b/tests/VerifiedBarcodeTest.php index bc7f9f4..e3b496f 100644 --- a/tests/VerifiedBarcodeTest.php +++ b/tests/VerifiedBarcodeTest.php @@ -13,7 +13,7 @@ class VerifiedBarcodeTest extends TestCase { - public static $supportedBarcodes = [ + public static array $supportedBarcodes = [ ['type' => BarcodeGenerator::TYPE_CODE_39, 'barcodes' => ['1234567890ABC']], ['type' => BarcodeGenerator::TYPE_CODE_39_CHECKSUM, 'barcodes' => ['1234567890ABC']], ['type' => BarcodeGenerator::TYPE_CODE_39E, 'barcodes' => ['1234567890abcABC']], From 9aba4bc232e5c4f75393b9532d5a9b1c1de82f9a Mon Sep 17 00:00:00 2001 From: Casper Bakker Date: Sun, 31 Mar 2024 12:57:35 +0200 Subject: [PATCH 123/155] Add support for inline SVG's --- src/BarcodeGeneratorJPG.php | 50 +++++++-- src/BarcodeGeneratorPNG.php | 107 ++++---------------- src/Exceptions/InvalidOptionException.php | 5 + src/Renderers/JpgRenderer.php | 22 ++++ src/Renderers/PngRenderer.php | 118 ++++++++++++++++++++++ src/Renderers/SvgRenderer.php | 34 ++++--- tests/BarcodeSvgv3Test.php | 26 ----- tests/SvgRendererTest.php | 59 +++++++++++ 8 files changed, 284 insertions(+), 137 deletions(-) create mode 100644 src/Exceptions/InvalidOptionException.php create mode 100644 src/Renderers/JpgRenderer.php create mode 100644 src/Renderers/PngRenderer.php delete mode 100644 tests/BarcodeSvgv3Test.php create mode 100644 tests/SvgRendererTest.php diff --git a/src/BarcodeGeneratorJPG.php b/src/BarcodeGeneratorJPG.php index 0e33d70..ba89b14 100644 --- a/src/BarcodeGeneratorJPG.php +++ b/src/BarcodeGeneratorJPG.php @@ -2,21 +2,51 @@ namespace Picqer\Barcode; -use Imagick; - -class BarcodeGeneratorJPG extends BarcodeGeneratorPNG +class BarcodeGeneratorJPG extends BarcodeGenerator { - protected function createImagickImageObject(int $width, int $height): Imagick + protected ?bool $useImagick = null; + + /** + * Return a PNG image representation of barcode (requires GD or Imagick library). + * + * @param string $barcode code to print + * @param BarcodeGenerator::TYPE_* $type (string) type of barcode + * @param int $widthFactor Width of a single bar element in pixels. + * @param int $height Height of a single bar element in pixels. + * @param array $foregroundColor RGB (0-255) foreground color for bar elements (background is transparent). + * @return string image data or false in case of error. + */ + public function getBarcode(string $barcode, $type, int $widthFactor = 2, int $height = 30, array $foregroundColor = [0, 0, 0]): string { - $image = new Imagick(); - $image->newImage($width, $height, 'white', 'JPG'); + $barcodeData = $this->getBarcodeData($barcode, $type); + + $renderer = new \Picqer\Barcode\Renderers\JpgRenderer(); + $renderer->setForegroundColor($foregroundColor); + + if (! is_null($this->useImagick)) { + if ($this->useImagick) { + $renderer->useImagick(); + } else { + $renderer->useGd(); + } + } - return $image; + return $renderer->render($barcodeData, $widthFactor, $height); + } + + /** + * Force the use of Imagick image extension + */ + public function useImagick() + { + $this->useImagick = true; } - protected function generateGdImage($image) + /** + * Force the use of the GD image library + */ + public function useGd() { - imagejpeg($image); - imagedestroy($image); + $this->useImagick = false; } } diff --git a/src/BarcodeGeneratorPNG.php b/src/BarcodeGeneratorPNG.php index 150e1e4..f2c0b39 100644 --- a/src/BarcodeGeneratorPNG.php +++ b/src/BarcodeGeneratorPNG.php @@ -2,45 +2,9 @@ namespace Picqer\Barcode; -use Imagick; -use imagickdraw; -use imagickpixel; -use Picqer\Barcode\Exceptions\BarcodeException; - class BarcodeGeneratorPNG extends BarcodeGenerator { - protected $useImagick = true; - - /** - * @throws BarcodeException - */ - public function __construct() - { - // Auto switch between GD and Imagick based on what is installed - if (extension_loaded('imagick')) { - $this->useImagick = true; - } elseif (function_exists('imagecreate')) { - $this->useImagick = false; - } else { - throw new BarcodeException('Neither gd-lib or imagick are installed!'); - } - } - - /** - * Force the use of Imagick image extension - */ - public function useImagick() - { - $this->useImagick = true; - } - - /** - * Force the use of the GD image library - */ - public function useGd() - { - $this->useImagick = false; - } + protected ?bool $useImagick = null; /** * Return a PNG image representation of barcode (requires GD or Imagick library). @@ -55,67 +19,34 @@ public function useGd() public function getBarcode(string $barcode, $type, int $widthFactor = 2, int $height = 30, array $foregroundColor = [0, 0, 0]): string { $barcodeData = $this->getBarcodeData($barcode, $type); - $width = round($barcodeData->getWidth() * $widthFactor); - - if ($this->useImagick) { - $imagickBarsShape = new imagickdraw(); - $imagickBarsShape->setFillColor(new imagickpixel('rgb(' . implode(',', $foregroundColor) .')')); - } else { - $image = $this->createGdImageObject($width, $height); - $gdForegroundColor = imagecolorallocate($image, $foregroundColor[0], $foregroundColor[1], $foregroundColor[2]); - } - // print bars - $positionHorizontal = 0; - /** @var BarcodeBar $bar */ - foreach ($barcodeData->getBars() as $bar) { - $barWidth = round(($bar->getWidth() * $widthFactor), 3); + $renderer = new \Picqer\Barcode\Renderers\PngRenderer(); + $renderer->setForegroundColor($foregroundColor); - if ($bar->isBar() && $barWidth > 0) { - $y = round(($bar->getPositionVertical() * $height / $barcodeData->getHeight()), 3); - $barHeight = round(($bar->getHeight() * $height / $barcodeData->getHeight()), 3); - - // draw a vertical bar - if ($this->useImagick) { - $imagickBarsShape->rectangle($positionHorizontal, $y, ($positionHorizontal + $barWidth - 1), ($y + $barHeight)); - } else { - imagefilledrectangle($image, $positionHorizontal, $y, ($positionHorizontal + $barWidth - 1), ($y + $barHeight), $gdForegroundColor); - } + if (! is_null($this->useImagick)) { + if ($this->useImagick) { + $renderer->useImagick(); + } else { + $renderer->useGd(); } - $positionHorizontal += $barWidth; - } - - if ($this->useImagick) { - $image = $this->createImagickImageObject($width, $height); - $image->drawImage($imagickBarsShape); - return $image->getImageBlob(); } - ob_start(); - $this->generateGdImage($image); - return ob_get_clean(); + return $renderer->render($barcodeData, $widthFactor, $height); } - protected function createGdImageObject(int $width, int $height) - { - $image = imagecreate($width, $height); - $colorBackground = imagecolorallocate($image, 255, 255, 255); - imagecolortransparent($image, $colorBackground); - - return $image; - } - - protected function createImagickImageObject(int $width, int $height): Imagick + /** + * Force the use of Imagick image extension + */ + public function useImagick() { - $image = new Imagick(); - $image->newImage($width, $height, 'none', 'PNG'); - - return $image; + $this->useImagick = true; } - protected function generateGdImage($image) + /** + * Force the use of the GD image library + */ + public function useGd() { - imagepng($image); - imagedestroy($image); + $this->useImagick = false; } } diff --git a/src/Exceptions/InvalidOptionException.php b/src/Exceptions/InvalidOptionException.php new file mode 100644 index 0000000..6a9b5ad --- /dev/null +++ b/src/Exceptions/InvalidOptionException.php @@ -0,0 +1,5 @@ +newImage($width, $height, 'none', 'JPG'); + + return $image; + } + + protected function generateGdImage($image) + { + \imagejpeg($image); + \imagedestroy($image); + } +} diff --git a/src/Renderers/PngRenderer.php b/src/Renderers/PngRenderer.php new file mode 100644 index 0000000..2009635 --- /dev/null +++ b/src/Renderers/PngRenderer.php @@ -0,0 +1,118 @@ +useImagick = true; + } elseif (function_exists('imagecreate')) { + $this->useImagick = false; + } else { + throw new BarcodeException('Neither gd-lib or imagick are installed!'); + } + } + + /** + * Force the use of Imagick image extension + */ + public function useImagick() + { + $this->useImagick = true; + } + + /** + * Force the use of the GD image library + */ + public function useGd() + { + $this->useImagick = false; + } + + public function render(Barcode $barcode, int $widthFactor = 2, int $height = 30): string + { + $width = round($barcode->getWidth() * $widthFactor); + + if ($this->useImagick) { + $imagickBarsShape = new ImagickDraw(); + $imagickBarsShape->setFillColor(new ImagickPixel('rgb(' . implode(',', $this->foregroundColor) .')')); + } else { + $image = $this->createGdImageObject($width, $height); + $gdForegroundColor = \imagecolorallocate($image, $this->foregroundColor[0], $this->foregroundColor[1], $this->foregroundColor[2]); + } + + // print bars + $positionHorizontal = 0; + /** @var BarcodeBar $bar */ + foreach ($barcode->getBars() as $bar) { + $barWidth = round(($bar->getWidth() * $widthFactor), 3); + + if ($bar->isBar() && $barWidth > 0) { + $y = round(($bar->getPositionVertical() * $height / $barcode->getHeight()), 3); + $barHeight = round(($bar->getHeight() * $height / $barcode->getHeight()), 3); + + // draw a vertical bar + if ($this->useImagick) { + $imagickBarsShape->rectangle($positionHorizontal, $y, ($positionHorizontal + $barWidth - 1), ($y + $barHeight)); + } else { + \imagefilledrectangle($image, $positionHorizontal, $y, ($positionHorizontal + $barWidth - 1), ($y + $barHeight), $gdForegroundColor); + } + } + $positionHorizontal += $barWidth; + } + + if ($this->useImagick) { + $image = $this->createImagickImageObject($width, $height); + $image->drawImage($imagickBarsShape); + return $image->getImageBlob(); + } + + ob_start(); + $this->generateGdImage($image); + return ob_get_clean(); + } + + public function setForegroundColor(array $color) + { + $this->foregroundColor = $color; + } + + protected function createGdImageObject(int $width, int $height) + { + $image = \imagecreate($width, $height); + $colorBackground = \imagecolorallocate($image, 255, 255, 255); + \imagecolortransparent($image, $colorBackground); + + return $image; + } + + protected function createImagickImageObject(int $width, int $height): Imagick + { + $image = new Imagick(); + $image->newImage($width, $height, 'none', 'PNG'); + + return $image; + } + + protected function generateGdImage($image) + { + \imagepng($image); + \imagedestroy($image); + } +} diff --git a/src/Renderers/SvgRenderer.php b/src/Renderers/SvgRenderer.php index 72dd993..6587d99 100644 --- a/src/Renderers/SvgRenderer.php +++ b/src/Renderers/SvgRenderer.php @@ -4,28 +4,27 @@ use Picqer\Barcode\Barcode; use Picqer\Barcode\BarcodeBar; +use Picqer\Barcode\Exceptions\InvalidOptionException; class SvgRenderer { protected string $foregroundColor = 'black'; + protected string $svgType = self::TYPE_SVG_STANDALONE; + + public const TYPE_SVG_STANDALONE = 'standalone'; + public const TYPE_SVG_INLINE = 'inline'; public function render(Barcode $barcode, float $width = 200, float $height = 30): string { - // replace table for special characters - $repstr = [ - "\0" => '', - '&' => '&', - '<' => '<', - '>' => '>', - ]; - -// $width = round(($barcode->getWidth() * $widthFactor), 3); $widthFactor = $width / $barcode->getWidth(); - $svg = '' . PHP_EOL; - $svg .= '' . PHP_EOL; + $svg = ''; + if ($this->svgType === self::TYPE_SVG_STANDALONE) { + $svg .= '' . PHP_EOL; + $svg .= '' . PHP_EOL; + } $svg .= '' . PHP_EOL; - $svg .= "\t" . '' . strtr($barcode->getBarcode(), $repstr) . '' . PHP_EOL; + $svg .= "\t" . '' . htmlspecialchars($barcode->getBarcode()) . '' . PHP_EOL; $svg .= "\t" . '' . PHP_EOL; // print bars @@ -50,8 +49,17 @@ public function render(Barcode $barcode, float $width = 200, float $height = 30) return $svg; } - public function setForegroundColor(string $color) + public function setForegroundColor(string $color): void { $this->foregroundColor = $color; } + + public function setSvgType(string $svgType): void + { + if (! in_array($svgType, [self::TYPE_SVG_INLINE, self::TYPE_SVG_STANDALONE])) { + throw new InvalidOptionException(); + } + + $this->svgType = $svgType; + } } diff --git a/tests/BarcodeSvgv3Test.php b/tests/BarcodeSvgv3Test.php deleted file mode 100644 index 5af16b7..0000000 --- a/tests/BarcodeSvgv3Test.php +++ /dev/null @@ -1,26 +0,0 @@ -getBarcode('081231723897'); - - $renderer = new Picqer\Barcode\Renderers\SvgRenderer(); - $generated = $renderer->render($barcode, 190); - - $this->assertStringEqualsFile('tests/verified-files/081231723897-ean13.svg', $generated); - } - - public function test_svg_barcode_generator_can_generate_ean_13_barcode_with_fractional_width() - { - $barcode = (new Picqer\Barcode\Types\TypeEan13())->getBarcode('081231723897'); - - $renderer = new Picqer\Barcode\Renderers\SvgRenderer(); - $generated = $renderer->render($barcode, 23.75, 25.75); - - $this->assertStringEqualsFile('tests/verified-files/081231723897-ean13-fractional-width.svg', $generated); - } -} diff --git a/tests/SvgRendererTest.php b/tests/SvgRendererTest.php new file mode 100644 index 0000000..aa1e97f --- /dev/null +++ b/tests/SvgRendererTest.php @@ -0,0 +1,59 @@ +getBarcode('081231723897'); + + $renderer = new Picqer\Barcode\Renderers\SvgRenderer(); + $generated = $renderer->render($barcode, 190); + + $this->assertStringEqualsFile('tests/verified-files/081231723897-ean13.svg', $generated); + } + + public function test_svg_barcode_generator_can_generate_ean_13_barcode_with_fractional_width() + { + $barcode = (new Picqer\Barcode\Types\TypeEan13())->getBarcode('081231723897'); + + $renderer = new Picqer\Barcode\Renderers\SvgRenderer(); + $generated = $renderer->render($barcode, 23.75, 25.75); + + $this->assertStringEqualsFile('tests/verified-files/081231723897-ean13-fractional-width.svg', $generated); + } + + public function test_svg_barcode_generator_as_standalone() + { + $barcode = (new Picqer\Barcode\Types\TypeEan13())->getBarcode('081231723897'); + + $renderer = new Picqer\Barcode\Renderers\SvgRenderer(); + $renderer->setSvgType(\Picqer\Barcode\Renderers\SvgRenderer::TYPE_SVG_STANDALONE); + $generated = $renderer->render($barcode); + + $this->assertStringStartsWith('assertStringContainsString('getBarcode('081231723897'); + + $renderer = new Picqer\Barcode\Renderers\SvgRenderer(); + $renderer->setSvgType(\Picqer\Barcode\Renderers\SvgRenderer::TYPE_SVG_INLINE); + $generated = $renderer->render($barcode); + + $this->assertStringStartsWith('assertStringNotContainsString('expectException(\Picqer\Barcode\Exceptions\InvalidOptionException::class); + + $renderer = new Picqer\Barcode\Renderers\SvgRenderer(); + $renderer->setSvgType('other'); + } +} From 29756f74371d55af8a3f5844ae8ba576a048eeea Mon Sep 17 00:00:00 2001 From: Casper Bakker Date: Sun, 31 Mar 2024 13:15:17 +0200 Subject: [PATCH 124/155] phpstan to level 5 --- phpstan.neon.dist | 2 +- src/Renderers/PngRenderer.php | 8 ++++---- src/Types/TypeITF14.php | 4 ++-- src/Types/TypeIntelligentMailBarcode.php | 18 +++++++++--------- src/Types/TypeInterleaved25Checksum.php | 2 +- src/Types/TypeKix.php | 2 +- src/Types/TypePharmacode.php | 2 +- src/Types/TypePharmacodeTwoCode.php | 7 +++---- src/Types/TypePostnet.php | 12 ++++++------ src/Types/TypeRms4cc.php | 12 ++++++------ src/Types/TypeTelepen.php | 4 ++-- 11 files changed, 36 insertions(+), 37 deletions(-) diff --git a/phpstan.neon.dist b/phpstan.neon.dist index 0405354..0a16ff9 100644 --- a/phpstan.neon.dist +++ b/phpstan.neon.dist @@ -1,4 +1,4 @@ parameters: paths: - src - level: 4 + level: 5 diff --git a/src/Renderers/PngRenderer.php b/src/Renderers/PngRenderer.php index 2009635..59c8269 100644 --- a/src/Renderers/PngRenderer.php +++ b/src/Renderers/PngRenderer.php @@ -47,7 +47,7 @@ public function useGd() public function render(Barcode $barcode, int $widthFactor = 2, int $height = 30): string { - $width = round($barcode->getWidth() * $widthFactor); + $width = (int)round($barcode->getWidth() * $widthFactor); if ($this->useImagick) { $imagickBarsShape = new ImagickDraw(); @@ -61,11 +61,11 @@ public function render(Barcode $barcode, int $widthFactor = 2, int $height = 30) $positionHorizontal = 0; /** @var BarcodeBar $bar */ foreach ($barcode->getBars() as $bar) { - $barWidth = round(($bar->getWidth() * $widthFactor), 3); + $barWidth = (int)round(($bar->getWidth() * $widthFactor)); if ($bar->isBar() && $barWidth > 0) { - $y = round(($bar->getPositionVertical() * $height / $barcode->getHeight()), 3); - $barHeight = round(($bar->getHeight() * $height / $barcode->getHeight()), 3); + $y = (int)round(($bar->getPositionVertical() * $height / $barcode->getHeight())); + $barHeight = (int)round(($bar->getHeight() * $height / $barcode->getHeight())); // draw a vertical bar if ($this->useImagick) { diff --git a/src/Types/TypeITF14.php b/src/Types/TypeITF14.php index 50f7b2e..7740efa 100644 --- a/src/Types/TypeITF14.php +++ b/src/Types/TypeITF14.php @@ -60,7 +60,7 @@ public function getBarcode(string $code): Barcode } foreach (str_split($pmixed) as $width) { - $barcode->addBar(new BarcodeBar($width, 1, $drawBar)); + $barcode->addBar(new BarcodeBar(intval($width), 1, $drawBar)); $drawBar = ! $drawBar; } } @@ -73,7 +73,7 @@ private function getChecksum(string $code): string $total = 0; for ($charIndex = 0; $charIndex <= (strlen($code) - 1); $charIndex++) { - $integerOfChar = intval($code . substr($charIndex, 1)); + $integerOfChar = intval($code . substr(strval($charIndex), 1)); $total += $integerOfChar * (($charIndex === 0 || $charIndex % 2 === 0) ? 3 : 1); } diff --git a/src/Types/TypeIntelligentMailBarcode.php b/src/Types/TypeIntelligentMailBarcode.php index 7d74829..9160e14 100644 --- a/src/Types/TypeIntelligentMailBarcode.php +++ b/src/Types/TypeIntelligentMailBarcode.php @@ -333,9 +333,9 @@ public function getBarcode(string $code): Barcode throw new BarcodeException('Routing code unknown'); } - $binary_code = bcmul($binary_code, 10); + $binary_code = bcmul($binary_code, strval(10)); $binary_code = bcadd($binary_code, $tracking_number[0]); - $binary_code = bcmul($binary_code, 5); + $binary_code = bcmul($binary_code, strval(5)); $binary_code = bcadd($binary_code, $tracking_number[1]); $binary_code .= substr($tracking_number, 2, 18); @@ -360,11 +360,11 @@ public function getBarcode(string $code): Barcode // convert binary data to codewords $codewords = []; $data = $this->hex_to_dec($binary_code_102bit); - $codewords[0] = bcmod($data, 636) * 2; - $data = bcdiv($data, 636); + $codewords[0] = bcmod($data, strval(636)) * 2; + $data = bcdiv($data, strval(636)); for ($i = 1; $i < 9; ++$i) { - $codewords[$i] = bcmod($data, 1365); - $data = bcdiv($data, 1365); + $codewords[$i] = bcmod($data, strval(1365)); + $data = bcdiv($data, strval(1365)); } $codewords[9] = $data; if (($fcs >> 10) == 1) { @@ -440,7 +440,7 @@ protected function dec_to_hex($number) $hex = []; while ($number > 0) { - array_push($hex, strtoupper(dechex(bcmod($number, '16')))); + $hex[] = strtoupper(dechex(intval(bcmod(strval($number), '16')))); $number = bcdiv($number, '16', 0); } $hex = array_reverse($hex); @@ -501,8 +501,8 @@ protected function hex_to_dec($hex) $bitval = 1; $len = strlen($hex); for ($pos = ($len - 1); $pos >= 0; --$pos) { - $dec = bcadd($dec, bcmul(hexdec($hex[$pos]), $bitval)); - $bitval = bcmul($bitval, 16); + $dec = bcadd($dec, bcmul(strval(hexdec($hex[$pos])), strval($bitval))); + $bitval = bcmul($bitval, '16'); } return $dec; diff --git a/src/Types/TypeInterleaved25Checksum.php b/src/Types/TypeInterleaved25Checksum.php index 0cb9aa6..5871e9b 100644 --- a/src/Types/TypeInterleaved25Checksum.php +++ b/src/Types/TypeInterleaved25Checksum.php @@ -62,7 +62,7 @@ public function getBarcode(string $code): Barcode } else { $t = false; // space } - $w = $seq[$j]; + $w = intval($seq[$j]); $barcode->addBar(new BarcodeBar($w, 1, $t)); } } diff --git a/src/Types/TypeKix.php b/src/Types/TypeKix.php index 08595a5..de20cf8 100644 --- a/src/Types/TypeKix.php +++ b/src/Types/TypeKix.php @@ -12,5 +12,5 @@ class TypeKix extends TypeRms4cc { - protected $kix = true; + protected bool $kix = true; } diff --git a/src/Types/TypePharmacode.php b/src/Types/TypePharmacode.php index 66a628b..80b3aee 100644 --- a/src/Types/TypePharmacode.php +++ b/src/Types/TypePharmacode.php @@ -31,6 +31,6 @@ public function getBarcode(string $code): Barcode $seq = substr($seq, 0, -2); $seq = strrev($seq); - return BinarySequenceConverter::convert($code, $seq); + return BinarySequenceConverter::convert(strval($code), $seq); } } diff --git a/src/Types/TypePharmacodeTwoCode.php b/src/Types/TypePharmacodeTwoCode.php index cde23c0..41de30f 100644 --- a/src/Types/TypePharmacodeTwoCode.php +++ b/src/Types/TypePharmacodeTwoCode.php @@ -9,7 +9,6 @@ use Picqer\Barcode\Barcode; use Picqer\Barcode\BarcodeBar; -use Picqer\Barcode\Exceptions\BarcodeException; use Picqer\Barcode\Exceptions\InvalidCharacterException; use Picqer\Barcode\Exceptions\InvalidLengthException; @@ -46,7 +45,7 @@ public function getBarcode(string $code): Barcode $seq = strrev($seq); - $barcode = new Barcode($code); + $barcode = new Barcode(strval($code)); for ($i = 0; $i < strlen($seq); ++$i) { switch ($seq[$i]) { @@ -69,9 +68,9 @@ public function getBarcode(string $code): Barcode throw new InvalidCharacterException('Could not find bar for char.'); } - $barcode->addBar(new BarcodeBar(1, $h, 1, $p)); + $barcode->addBar(new BarcodeBar(1, $h, true, $p)); if ($i < (strlen($seq) - 1)) { - $barcode->addBar(new BarcodeBar(1, 2, 0, 0)); + $barcode->addBar(new BarcodeBar(1, 2, false, 0)); } } diff --git a/src/Types/TypePostnet.php b/src/Types/TypePostnet.php index a43d093..2d136b3 100644 --- a/src/Types/TypePostnet.php +++ b/src/Types/TypePostnet.php @@ -49,20 +49,20 @@ public function getBarcode(string $code): Barcode $len = strlen($code); // start bar - $barcode->addBar(new BarcodeBar(1, 2, 1)); - $barcode->addBar(new BarcodeBar(1, 2, 0)); + $barcode->addBar(new BarcodeBar(1, 2, true)); + $barcode->addBar(new BarcodeBar(1, 2, false)); for ($i = 0; $i < $len; ++$i) { for ($j = 0; $j < 5; ++$j) { $h = $this->barlen[$code[$i]][$j]; - $p = floor(1 / $h); - $barcode->addBar(new BarcodeBar(1, $h, 1, $p)); - $barcode->addBar(new BarcodeBar(1, 2, 0)); + $p = (int)floor(1 / $h); + $barcode->addBar(new BarcodeBar(1, (int)$h, true, $p)); + $barcode->addBar(new BarcodeBar(1, 2, false)); } } // end bar - $barcode->addBar(new BarcodeBar(1, 2, 1)); + $barcode->addBar(new BarcodeBar(1, 2, true)); return $barcode; } diff --git a/src/Types/TypeRms4cc.php b/src/Types/TypeRms4cc.php index 8dac3b4..684a624 100644 --- a/src/Types/TypeRms4cc.php +++ b/src/Types/TypeRms4cc.php @@ -15,7 +15,7 @@ class TypeRms4cc implements TypeInterface { - protected $kix = false; + protected bool $kix = false; public function getBarcode(string $code): Barcode { @@ -122,8 +122,8 @@ public function getBarcode(string $code): Barcode ++$len; // start bar - $barcode->addBar(new BarcodeBar(1, 2, 1)); - $barcode->addBar(new BarcodeBar(1, 2, 0)); + $barcode->addBar(new BarcodeBar(1, 2, true)); + $barcode->addBar(new BarcodeBar(1, 2, false)); } for ($i = 0; $i < $len; ++$i) { @@ -150,14 +150,14 @@ public function getBarcode(string $code): Barcode break; } - $barcode->addBar(new BarcodeBar(1, $h, 1, $p)); - $barcode->addBar(new BarcodeBar(1, 2, 0)); + $barcode->addBar(new BarcodeBar(1, $h, true, $p)); + $barcode->addBar(new BarcodeBar(1, 2, false)); } } if (! $this->kix) { // stop bar - $barcode->addBar(new BarcodeBar(1, 3, 1)); + $barcode->addBar(new BarcodeBar(1, 3, true)); } return $barcode; diff --git a/src/Types/TypeTelepen.php b/src/Types/TypeTelepen.php index 6ec1158..378e342 100644 --- a/src/Types/TypeTelepen.php +++ b/src/Types/TypeTelepen.php @@ -50,7 +50,7 @@ public function getBarcode(string $code): Barcode $drawBar = true; for ($i = 0; $i < strlen($encoded); ++$i) { $barWidth = $encoded[$i]; - $barcode->addBar(new BarcodeBar($barWidth, 250, $drawBar)); + $barcode->addBar(new BarcodeBar(intval($barWidth), 250, $drawBar)); $drawBar = !$drawBar; //flip to other colour } @@ -101,7 +101,7 @@ protected function encodeAlpha($code) : string $check_digit = 0; } - $dest .= $this->telepen_lookup_table[ord($check_digit)]; + $dest .= $this->telepen_lookup_table[ord(strval($check_digit))]; $dest .= $this->telepen_lookup_table[ord(self::TELEPEN_STOP_CHAR)]; // Stop return $dest; From ec4502fad452dd7581989df982f128a26e6451e1 Mon Sep 17 00:00:00 2001 From: Casper Bakker Date: Sun, 31 Mar 2024 20:34:00 +0200 Subject: [PATCH 125/155] HTML renderers --- src/BarcodeGeneratorDynamicHTML.php | 25 +---- src/BarcodeGeneratorHTML.php | 25 +---- src/Renderers/DynamicHtmlRenderer.php | 43 ++++++++ src/Renderers/HtmlRenderer.php | 43 ++++++++ tests/DynamicHtmlRendererTest.php | 26 +++++ tests/HtmlRendererTest.php | 26 +++++ tests/JpgRendererTest.php | 152 ++++++++++++++++++++++++++ tests/PngRendererTest.php | 142 ++++++++++++++++++++++++ 8 files changed, 440 insertions(+), 42 deletions(-) create mode 100644 src/Renderers/DynamicHtmlRenderer.php create mode 100644 src/Renderers/HtmlRenderer.php create mode 100644 tests/DynamicHtmlRendererTest.php create mode 100644 tests/HtmlRendererTest.php create mode 100644 tests/JpgRendererTest.php create mode 100644 tests/PngRendererTest.php diff --git a/src/BarcodeGeneratorDynamicHTML.php b/src/BarcodeGeneratorDynamicHTML.php index 57ce23e..66845a2 100644 --- a/src/BarcodeGeneratorDynamicHTML.php +++ b/src/BarcodeGeneratorDynamicHTML.php @@ -4,8 +4,6 @@ class BarcodeGeneratorDynamicHTML extends BarcodeGenerator { - private const WIDTH_PRECISION = 6; - /** * Return an HTML representation of barcode. * This 'dynamic' version uses percentage based widths and heights, resulting in a vector-y qualitative result. @@ -19,26 +17,9 @@ public function getBarcode(string $barcode, $type, string $foregroundColor = 'bl { $barcodeData = $this->getBarcodeData($barcode, $type); - $html = '
    ' . PHP_EOL; - - $positionHorizontal = 0; - /** @var BarcodeBar $bar */ - foreach ($barcodeData->getBars() as $bar) { - $barWidth = $bar->getWidth() / $barcodeData->getWidth() * 100; - $barHeight = round(($bar->getHeight() / $barcodeData->getHeight() * 100), 3); - - if ($bar->isBar() && $barWidth > 0) { - $positionVertical = round(($bar->getPositionVertical() / $barcodeData->getHeight() * 100), 3); - - // draw a vertical bar - $html .= '
     
    ' . PHP_EOL; - } - - $positionHorizontal += $barWidth; - } - - $html .= '
    ' . PHP_EOL; + $renderer = new \Picqer\Barcode\Renderers\DynamicHtmlRenderer(); + $renderer->setForegroundColor($foregroundColor); - return $html; + return $renderer->render($barcodeData); } } diff --git a/src/BarcodeGeneratorHTML.php b/src/BarcodeGeneratorHTML.php index 75a98fe..c38a723 100644 --- a/src/BarcodeGeneratorHTML.php +++ b/src/BarcodeGeneratorHTML.php @@ -15,30 +15,15 @@ class BarcodeGeneratorHTML extends BarcodeGenerator * @param string $foregroundColor Foreground color for bar elements as '#333' or 'orange' for example (background is transparent). * @return string HTML code. */ - public function getBarcode($barcode, $type, int $widthFactor = 2, int $height = 30, string $foregroundColor = 'black'): string + public function getBarcode(string $barcode, $type, int $widthFactor = 2, int $height = 30, string $foregroundColor = 'black'): string { $barcodeData = $this->getBarcodeData($barcode, $type); - $html = '
    ' . PHP_EOL; + $width = round(($barcodeData->getWidth() * $widthFactor), 3); - $positionHorizontal = 0; - /** @var BarcodeBar $bar */ - foreach ($barcodeData->getBars() as $bar) { - $barWidth = round(($bar->getWidth() * $widthFactor), 3); - $barHeight = round(($bar->getHeight() * $height / $barcodeData->getHeight()), 3); + $renderer = new \Picqer\Barcode\Renderers\HtmlRenderer(); + $renderer->setForegroundColor($foregroundColor); - if ($bar->isBar() && $barWidth > 0) { - $positionVertical = round(($bar->getPositionVertical() * $height / $barcodeData->getHeight()), 3); - - // draw a vertical bar - $html .= '
     
    ' . PHP_EOL; - } - - $positionHorizontal += $barWidth; - } - - $html .= '
    ' . PHP_EOL; - - return $html; + return $renderer->render($barcodeData, $width, $height); } } diff --git a/src/Renderers/DynamicHtmlRenderer.php b/src/Renderers/DynamicHtmlRenderer.php new file mode 100644 index 0000000..51ea1a0 --- /dev/null +++ b/src/Renderers/DynamicHtmlRenderer.php @@ -0,0 +1,43 @@ +' . PHP_EOL; + + $positionHorizontal = 0; + /** @var BarcodeBar $bar */ + foreach ($barcode->getBars() as $bar) { + $barWidth = $bar->getWidth() / $barcode->getWidth() * 100; + $barHeight = round(($bar->getHeight() / $barcode->getHeight() * 100), 3); + + if ($bar->isBar() && $barWidth > 0) { + $positionVertical = round(($bar->getPositionVertical() / $barcode->getHeight() * 100), 3); + + // draw a vertical bar + $html .= '
     
    ' . PHP_EOL; + } + + $positionHorizontal += $barWidth; + } + + $html .= '
    ' . PHP_EOL; + + return $html; + } + + public function setForegroundColor(string $color): void + { + $this->foregroundColor = $color; + } +} diff --git a/src/Renderers/HtmlRenderer.php b/src/Renderers/HtmlRenderer.php new file mode 100644 index 0000000..517908e --- /dev/null +++ b/src/Renderers/HtmlRenderer.php @@ -0,0 +1,43 @@ +getWidth(); + + $html = '
    ' . PHP_EOL; + + $positionHorizontal = 0; + /** @var BarcodeBar $bar */ + foreach ($barcode->getBars() as $bar) { + $barWidth = round(($bar->getWidth() * $widthFactor), 3); + $barHeight = round(($bar->getHeight() * $height / $barcode->getHeight()), 3); + + if ($bar->isBar() && $barWidth > 0) { + $positionVertical = round(($bar->getPositionVertical() * $height / $barcode->getHeight()), 3); + + // draw a vertical bar + $html .= '
     
    ' . PHP_EOL; + } + + $positionHorizontal += $barWidth; + } + + $html .= '
    ' . PHP_EOL; + + return $html; + } + + public function setForegroundColor(string $color): void + { + $this->foregroundColor = $color; + } +} diff --git a/tests/DynamicHtmlRendererTest.php b/tests/DynamicHtmlRendererTest.php new file mode 100644 index 0000000..9597874 --- /dev/null +++ b/tests/DynamicHtmlRendererTest.php @@ -0,0 +1,26 @@ +getBarcode('081231723897'); + + $renderer = new Picqer\Barcode\Renderers\DynamicHtmlRenderer(); + $generated = $renderer->render($barcode); + + $this->assertStringEqualsFile('tests/verified-files/081231723897-dynamic-code128.html', $generated); + } + + public function test_dynamic_html_barcode_generator_can_generate_imb_barcode_to_test_heights() + { + $barcode = (new Picqer\Barcode\Types\TypeIntelligentMailBarcode())->getBarcode('12345678903'); + + $renderer = new Picqer\Barcode\Renderers\DynamicHtmlRenderer(); + $generated = $renderer->render($barcode); + + $this->assertStringEqualsFile('tests/verified-files/12345678903-dynamic-imb.html', $generated); + } +} diff --git a/tests/HtmlRendererTest.php b/tests/HtmlRendererTest.php new file mode 100644 index 0000000..803d2c0 --- /dev/null +++ b/tests/HtmlRendererTest.php @@ -0,0 +1,26 @@ +getBarcode('081231723897'); + + $renderer = new Picqer\Barcode\Renderers\HtmlRenderer(); + $generated = $renderer->render($barcode, $barcode->getWidth() * 2); + + $this->assertStringEqualsFile('tests/verified-files/081231723897-code128.html', $generated); + } + + public function test_html_barcode_generator_can_generate_imb_barcode_to_test_heights() + { + $barcode = (new Picqer\Barcode\Types\TypeIntelligentMailBarcode())->getBarcode('12345678903'); + + $renderer = new Picqer\Barcode\Renderers\HtmlRenderer(); + $generated = $renderer->render($barcode, $barcode->getWidth() * 2); + + $this->assertStringEqualsFile('tests/verified-files/12345678903-imb.html', $generated); + } +} diff --git a/tests/JpgRendererTest.php b/tests/JpgRendererTest.php new file mode 100644 index 0000000..46aa939 --- /dev/null +++ b/tests/JpgRendererTest.php @@ -0,0 +1,152 @@ +getBarcode('081231723897'); + + $renderer = new Picqer\Barcode\Renderers\JpgRenderer(); + $renderer->useGd(); + $generated = $renderer->render($barcode); + + $imageInfo = getimagesizefromstring($generated); + + $this->assertGreaterThan(100, strlen($generated)); + $this->assertEquals(202, $imageInfo[0]); // Image width + $this->assertEquals(30, $imageInfo[1]); // Image height + $this->assertEquals('image/jpeg', $imageInfo['mime']); + } + + + public function test_jpg_barcode_generator_can_generate_code_39_barcode() + { + $barcode = (new Picqer\Barcode\Types\TypeCode39())->getBarcode('081231723897'); + + $renderer = new Picqer\Barcode\Renderers\JpgRenderer(); + $renderer->useGd(); + $generated = $renderer->render($barcode, 1); + + $imageInfo = getimagesizefromstring($generated); + + $this->assertGreaterThan(100, strlen($generated)); + $this->assertEquals(224, $imageInfo[0]); // Image width + $this->assertEquals(30, $imageInfo[1]); // Image height + $this->assertEquals('image/jpeg', $imageInfo['mime']); + } + + public function test_jpg_barcode_generator_can_use_different_height() + { + $barcode = (new Picqer\Barcode\Types\TypeCode128())->getBarcode('081231723897'); + + $renderer = new Picqer\Barcode\Renderers\JpgRenderer(); + $renderer->useGd(); + $generated = $renderer->render($barcode, 2, 45); + + $imageInfo = getimagesizefromstring($generated); + + $this->assertGreaterThan(100, strlen($generated)); + $this->assertEquals(202, $imageInfo[0]); // Image width + $this->assertEquals(45, $imageInfo[1]); // Image height + $this->assertEquals('image/jpeg', $imageInfo['mime']); + } + + public function test_jpg_barcode_generator_can_use_different_width_factor() + { + $barcode = (new Picqer\Barcode\Types\TypeCode128())->getBarcode('081231723897'); + + $renderer = new Picqer\Barcode\Renderers\JpgRenderer(); + $renderer->useGd(); + $generated = $renderer->render($barcode, 5); + + $imageInfo = getimagesizefromstring($generated); + + $this->assertGreaterThan(100, strlen($generated)); + $this->assertEquals(505, $imageInfo[0]); // Image width + $this->assertEquals('image/jpeg', $imageInfo['mime']); + } + + + // Copied as Imagick + + public function test_jpg_barcode_generator_can_generate_code_128_barcode_imagick() + { + if (! extension_loaded('imagick')) { + $this->markTestSkipped(); + } + + $barcode = (new Picqer\Barcode\Types\TypeCode128())->getBarcode('081231723897'); + + $renderer = new Picqer\Barcode\Renderers\JpgRenderer(); + $renderer->useImagick(); + $generated = $renderer->render($barcode); + + $imageInfo = getimagesizefromstring($generated); + + $this->assertGreaterThan(100, strlen($generated)); + $this->assertEquals(202, $imageInfo[0]); // Image width + $this->assertEquals(30, $imageInfo[1]); // Image height + $this->assertEquals('image/jpeg', $imageInfo['mime']); + } + + public function test_jpg_barcode_generator_can_generate_code_39_barcode_imagick() + { + if (! extension_loaded('imagick')) { + $this->markTestSkipped(); + } + + $barcode = (new Picqer\Barcode\Types\TypeCode39())->getBarcode('081231723897'); + + $renderer = new Picqer\Barcode\Renderers\JpgRenderer(); + $renderer->useImagick(); + $generated = $renderer->render($barcode, 1); + + $imageInfo = getimagesizefromstring($generated); + + $this->assertGreaterThan(100, strlen($generated)); + $this->assertEquals(224, $imageInfo[0]); // Image width + $this->assertEquals(30, $imageInfo[1]); // Image height + $this->assertEquals('image/jpeg', $imageInfo['mime']); + } + + public function test_jpg_barcode_generator_can_use_different_height_imagick() + { + if (! extension_loaded('imagick')) { + $this->markTestSkipped(); + } + + $barcode = (new Picqer\Barcode\Types\TypeCode128())->getBarcode('081231723897'); + + $renderer = new Picqer\Barcode\Renderers\JpgRenderer(); + $renderer->useGd(); + $generated = $renderer->render($barcode, 2, 45); + + $imageInfo = getimagesizefromstring($generated); + + $this->assertGreaterThan(100, strlen($generated)); + $this->assertEquals(202, $imageInfo[0]); // Image width + $this->assertEquals(45, $imageInfo[1]); // Image height + $this->assertEquals('image/jpeg', $imageInfo['mime']); + } + + public function test_jpg_barcode_generator_can_use_different_width_factor_imagick() + { + if (! extension_loaded('imagick')) { + $this->markTestSkipped(); + } + + $barcode = (new Picqer\Barcode\Types\TypeCode128())->getBarcode('081231723897'); + + $renderer = new Picqer\Barcode\Renderers\JpgRenderer(); + $renderer->useGd(); + $generated = $renderer->render($barcode, 5); + + $imageInfo = getimagesizefromstring($generated); + + $this->assertGreaterThan(100, strlen($generated)); + $this->assertEquals(505, $imageInfo[0]); // Image width + $this->assertEquals('image/jpeg', $imageInfo['mime']); + } +} diff --git a/tests/PngRendererTest.php b/tests/PngRendererTest.php new file mode 100644 index 0000000..db253ce --- /dev/null +++ b/tests/PngRendererTest.php @@ -0,0 +1,142 @@ +getBarcode('081231723897'); + + $renderer = new Picqer\Barcode\Renderers\PngRenderer(); + $renderer->useGd(); + $generated = $renderer->render($barcode); + + $this->assertEquals('PNG', substr($generated, 1, 3)); + } + + + public function test_png_barcode_generator_can_generate_code_39_barcode() + { + $barcode = (new Picqer\Barcode\Types\TypeCode39())->getBarcode('081231723897'); + + $renderer = new Picqer\Barcode\Renderers\PngRenderer(); + $renderer->useGd(); + $generated = $renderer->render($barcode, 1); + + $imageInfo = getimagesizefromstring($generated); + + $this->assertGreaterThan(100, strlen($generated)); + $this->assertEquals(224, $imageInfo[0]); // Image width + $this->assertEquals(30, $imageInfo[1]); // Image height + $this->assertEquals('image/png', $imageInfo['mime']); + } + + public function test_png_barcode_generator_can_use_different_height() + { + $barcode = (new Picqer\Barcode\Types\TypeCode128())->getBarcode('081231723897'); + + $renderer = new Picqer\Barcode\Renderers\PngRenderer(); + $renderer->useGd(); + $generated = $renderer->render($barcode, 2, 45); + + $imageInfo = getimagesizefromstring($generated); + + $this->assertGreaterThan(100, strlen($generated)); + $this->assertEquals(202, $imageInfo[0]); // Image width + $this->assertEquals(45, $imageInfo[1]); // Image height + $this->assertEquals('image/png', $imageInfo['mime']); + } + + public function test_png_barcode_generator_can_use_different_width_factor() + { + $barcode = (new Picqer\Barcode\Types\TypeCode128())->getBarcode('081231723897'); + + $renderer = new Picqer\Barcode\Renderers\PngRenderer(); + $renderer->useGd(); + $generated = $renderer->render($barcode, 5); + + $imageInfo = getimagesizefromstring($generated); + + $this->assertGreaterThan(100, strlen($generated)); + $this->assertEquals(505, $imageInfo[0]); // Image width + $this->assertEquals('image/png', $imageInfo['mime']); + } + + + // Copied as Imagick + + public function test_png_barcode_generator_can_generate_code_128_barcode_imagick() + { + if (! extension_loaded('imagick')) { + $this->markTestSkipped(); + } + + $barcode = (new Picqer\Barcode\Types\TypeCode128())->getBarcode('081231723897'); + + $renderer = new Picqer\Barcode\Renderers\PngRenderer(); + $renderer->useImagick(); + $generated = $renderer->render($barcode); + + $this->assertEquals('PNG', substr($generated, 1, 3)); + } + + public function test_png_barcode_generator_can_generate_code_39_barcode_imagick() + { + if (! extension_loaded('imagick')) { + $this->markTestSkipped(); + } + + $barcode = (new Picqer\Barcode\Types\TypeCode39())->getBarcode('081231723897'); + + $renderer = new Picqer\Barcode\Renderers\PngRenderer(); + $renderer->useImagick(); + $generated = $renderer->render($barcode, 1); + + $imageInfo = getimagesizefromstring($generated); + + $this->assertGreaterThan(100, strlen($generated)); + $this->assertEquals(224, $imageInfo[0]); // Image width + $this->assertEquals(30, $imageInfo[1]); // Image height + $this->assertEquals('image/png', $imageInfo['mime']); + } + + public function test_png_barcode_generator_can_use_different_height_imagick() + { + if (! extension_loaded('imagick')) { + $this->markTestSkipped(); + } + + $barcode = (new Picqer\Barcode\Types\TypeCode128())->getBarcode('081231723897'); + + $renderer = new Picqer\Barcode\Renderers\PngRenderer(); + $renderer->useGd(); + $generated = $renderer->render($barcode, 2, 45); + + $imageInfo = getimagesizefromstring($generated); + + $this->assertGreaterThan(100, strlen($generated)); + $this->assertEquals(202, $imageInfo[0]); // Image width + $this->assertEquals(45, $imageInfo[1]); // Image height + $this->assertEquals('image/png', $imageInfo['mime']); + } + + public function test_png_barcode_generator_can_use_different_width_factor_imagick() + { + if (! extension_loaded('imagick')) { + $this->markTestSkipped(); + } + + $barcode = (new Picqer\Barcode\Types\TypeCode128())->getBarcode('081231723897'); + + $renderer = new Picqer\Barcode\Renderers\PngRenderer(); + $renderer->useGd(); + $generated = $renderer->render($barcode, 5); + + $imageInfo = getimagesizefromstring($generated); + + $this->assertGreaterThan(100, strlen($generated)); + $this->assertEquals(505, $imageInfo[0]); // Image width + $this->assertEquals('image/png', $imageInfo['mime']); + } +} From 460f3eee050caa0d19e293d0bd4001539051b75b Mon Sep 17 00:00:00 2001 From: Casper Bakker Date: Sun, 31 Mar 2024 21:24:21 +0200 Subject: [PATCH 126/155] More typehints --- .github/FUNDING.yml | 1 - src/Barcode.php | 2 +- src/BarcodeBar.php | 2 +- src/BarcodeGenerator.php | 6 +++++- src/BarcodeGeneratorDynamicHTML.php | 3 +++ src/BarcodeGeneratorHTML.php | 3 +++ src/BarcodeGeneratorJPG.php | 7 +++++-- src/BarcodeGeneratorPNG.php | 7 +++++-- src/BarcodeGeneratorSVG.php | 3 +++ src/Types/TypeCodabar.php | 2 +- src/Types/TypeCode11.php | 2 +- src/Types/TypeCode128.php | 6 +++--- src/Types/TypeCode128A.php | 2 +- src/Types/TypeCode128B.php | 2 +- src/Types/TypeCode128C.php | 2 +- src/Types/TypeCode32.php | 2 +- src/Types/TypeCode39.php | 11 ++++++----- src/Types/TypeCode39Checksum.php | 4 ++-- src/Types/TypeCode39Extended.php | 4 ++-- src/Types/TypeCode39ExtendedChecksum.php | 4 ++-- src/Types/TypeCode93.php | 8 +++----- src/Types/TypeEanUpcBase.php | 2 +- src/Types/TypeInterleaved25Checksum.php | 4 ++-- src/Types/TypeMsi.php | 2 +- src/Types/TypeMsiChecksum.php | 2 +- src/Types/TypePlanet.php | 2 +- src/Types/TypePostnet.php | 2 +- src/Types/TypeStandard2of5.php | 12 ++++++------ src/Types/TypeStandard2of5Checksum.php | 2 +- src/Types/TypeTelepen.php | 8 ++++---- src/Types/TypeUpcExtension2.php | 2 +- src/Types/TypeUpcExtension5.php | 2 +- 32 files changed, 70 insertions(+), 53 deletions(-) delete mode 100644 .github/FUNDING.yml diff --git a/.github/FUNDING.yml b/.github/FUNDING.yml deleted file mode 100644 index a8ff52a..0000000 --- a/.github/FUNDING.yml +++ /dev/null @@ -1 +0,0 @@ -github: casperbakker diff --git a/src/Barcode.php b/src/Barcode.php index dbb27f1..a39b382 100644 --- a/src/Barcode.php +++ b/src/Barcode.php @@ -14,7 +14,7 @@ public function __construct(string $barcode) $this->barcode = $barcode; } - public function addBar(BarcodeBar $bar) + public function addBar(BarcodeBar $bar): void { $this->bars[] = $bar; $this->width += $bar->getWidth(); diff --git a/src/BarcodeBar.php b/src/BarcodeBar.php index 61a415f..58a881f 100644 --- a/src/BarcodeBar.php +++ b/src/BarcodeBar.php @@ -2,7 +2,7 @@ namespace Picqer\Barcode; -class BarcodeBar +readonly class BarcodeBar { protected int $width; protected int $height; diff --git a/src/BarcodeGenerator.php b/src/BarcodeGenerator.php index 9dd7f76..8555b06 100644 --- a/src/BarcodeGenerator.php +++ b/src/BarcodeGenerator.php @@ -45,6 +45,7 @@ use Picqer\Barcode\Types\TypeEan13; use Picqer\Barcode\Types\TypeEan8; use Picqer\Barcode\Types\TypeIntelligentMailBarcode; +use Picqer\Barcode\Types\TypeInterface; use Picqer\Barcode\Types\TypeInterleaved25; use Picqer\Barcode\Types\TypeInterleaved25Checksum; use Picqer\Barcode\Types\TypeITF14; @@ -101,6 +102,9 @@ abstract class BarcodeGenerator const TYPE_PHARMA_CODE = 'PHARMA'; const TYPE_PHARMA_CODE_TWO_TRACKS = 'PHARMA2T'; + /** + * @throws UnknownTypeException + */ protected function getBarcodeData(string $code, string $type): Barcode { $barcodeDataBuilder = $this->createDataBuilderForType($type); @@ -108,7 +112,7 @@ protected function getBarcodeData(string $code, string $type): Barcode return $barcodeDataBuilder->getBarcode($code); } - protected function createDataBuilderForType(string $type) + protected function createDataBuilderForType(string $type): TypeInterface { switch (strtoupper($type)) { case self::TYPE_CODE_32: diff --git a/src/BarcodeGeneratorDynamicHTML.php b/src/BarcodeGeneratorDynamicHTML.php index 66845a2..c08a4ed 100644 --- a/src/BarcodeGeneratorDynamicHTML.php +++ b/src/BarcodeGeneratorDynamicHTML.php @@ -2,6 +2,8 @@ namespace Picqer\Barcode; +use Picqer\Barcode\Exceptions\UnknownTypeException; + class BarcodeGeneratorDynamicHTML extends BarcodeGenerator { /** @@ -12,6 +14,7 @@ class BarcodeGeneratorDynamicHTML extends BarcodeGenerator * @param BarcodeGenerator::TYPE_* $type (string) type of barcode * @param string $foregroundColor Foreground color for bar elements as '#333' or 'orange' for example (background is transparent). * @return string HTML code. + * @throws UnknownTypeException */ public function getBarcode(string $barcode, $type, string $foregroundColor = 'black'): string { diff --git a/src/BarcodeGeneratorHTML.php b/src/BarcodeGeneratorHTML.php index c38a723..bc30f25 100644 --- a/src/BarcodeGeneratorHTML.php +++ b/src/BarcodeGeneratorHTML.php @@ -2,6 +2,8 @@ namespace Picqer\Barcode; +use Picqer\Barcode\Exceptions\UnknownTypeException; + class BarcodeGeneratorHTML extends BarcodeGenerator { /** @@ -14,6 +16,7 @@ class BarcodeGeneratorHTML extends BarcodeGenerator * @param int $height Height of a single bar element in pixels. * @param string $foregroundColor Foreground color for bar elements as '#333' or 'orange' for example (background is transparent). * @return string HTML code. + * @throws UnknownTypeException */ public function getBarcode(string $barcode, $type, int $widthFactor = 2, int $height = 30, string $foregroundColor = 'black'): string { diff --git a/src/BarcodeGeneratorJPG.php b/src/BarcodeGeneratorJPG.php index ba89b14..2e27001 100644 --- a/src/BarcodeGeneratorJPG.php +++ b/src/BarcodeGeneratorJPG.php @@ -2,6 +2,8 @@ namespace Picqer\Barcode; +use Picqer\Barcode\Exceptions\UnknownTypeException; + class BarcodeGeneratorJPG extends BarcodeGenerator { protected ?bool $useImagick = null; @@ -15,6 +17,7 @@ class BarcodeGeneratorJPG extends BarcodeGenerator * @param int $height Height of a single bar element in pixels. * @param array $foregroundColor RGB (0-255) foreground color for bar elements (background is transparent). * @return string image data or false in case of error. + * @throws UnknownTypeException */ public function getBarcode(string $barcode, $type, int $widthFactor = 2, int $height = 30, array $foregroundColor = [0, 0, 0]): string { @@ -37,7 +40,7 @@ public function getBarcode(string $barcode, $type, int $widthFactor = 2, int $he /** * Force the use of Imagick image extension */ - public function useImagick() + public function useImagick(): void { $this->useImagick = true; } @@ -45,7 +48,7 @@ public function useImagick() /** * Force the use of the GD image library */ - public function useGd() + public function useGd(): void { $this->useImagick = false; } diff --git a/src/BarcodeGeneratorPNG.php b/src/BarcodeGeneratorPNG.php index f2c0b39..31edc56 100644 --- a/src/BarcodeGeneratorPNG.php +++ b/src/BarcodeGeneratorPNG.php @@ -2,6 +2,8 @@ namespace Picqer\Barcode; +use Picqer\Barcode\Exceptions\UnknownTypeException; + class BarcodeGeneratorPNG extends BarcodeGenerator { protected ?bool $useImagick = null; @@ -15,6 +17,7 @@ class BarcodeGeneratorPNG extends BarcodeGenerator * @param int $height Height of a single bar element in pixels. * @param array $foregroundColor RGB (0-255) foreground color for bar elements (background is transparent). * @return string image data or false in case of error. + * @throws UnknownTypeException */ public function getBarcode(string $barcode, $type, int $widthFactor = 2, int $height = 30, array $foregroundColor = [0, 0, 0]): string { @@ -37,7 +40,7 @@ public function getBarcode(string $barcode, $type, int $widthFactor = 2, int $he /** * Force the use of Imagick image extension */ - public function useImagick() + public function useImagick(): void { $this->useImagick = true; } @@ -45,7 +48,7 @@ public function useImagick() /** * Force the use of the GD image library */ - public function useGd() + public function useGd(): void { $this->useImagick = false; } diff --git a/src/BarcodeGeneratorSVG.php b/src/BarcodeGeneratorSVG.php index aab6a15..ef7d63f 100644 --- a/src/BarcodeGeneratorSVG.php +++ b/src/BarcodeGeneratorSVG.php @@ -2,6 +2,8 @@ namespace Picqer\Barcode; +use Picqer\Barcode\Exceptions\UnknownTypeException; + class BarcodeGeneratorSVG extends BarcodeGenerator { /** @@ -14,6 +16,7 @@ class BarcodeGeneratorSVG extends BarcodeGenerator * @param $foregroundColor (string) Foreground color (in SVG format) for bar elements (background is transparent). * @return string SVG code. * @public + * @throws UnknownTypeException */ public function getBarcode(string $barcode, $type, float $widthFactor = 2, float $height = 30, string $foregroundColor = 'black'): string { diff --git a/src/Types/TypeCodabar.php b/src/Types/TypeCodabar.php index 62c78f8..e782dc4 100644 --- a/src/Types/TypeCodabar.php +++ b/src/Types/TypeCodabar.php @@ -13,7 +13,7 @@ class TypeCodabar implements TypeInterface { - protected $conversionTable = [ + protected array $conversionTable = [ '0' => '11111221', '1' => '11112211', '2' => '11121121', diff --git a/src/Types/TypeCode11.php b/src/Types/TypeCode11.php index 1886364..ab469bd 100644 --- a/src/Types/TypeCode11.php +++ b/src/Types/TypeCode11.php @@ -13,7 +13,7 @@ class TypeCode11 implements TypeInterface { - protected $conversionTable = [ + protected array $conversionTable = [ '0' => '111121', '1' => '211121', '2' => '121121', diff --git a/src/Types/TypeCode128.php b/src/Types/TypeCode128.php index 3a6242d..ecc084e 100644 --- a/src/Types/TypeCode128.php +++ b/src/Types/TypeCode128.php @@ -18,9 +18,9 @@ class TypeCode128 implements TypeInterface { - protected $type = null; + protected ?string $type = null; - protected $conversionTable = [ + protected array $conversionTable = [ '212222', /* 00 */ '222122', /* 01 */ '222221', /* 02 */ @@ -383,7 +383,7 @@ public function getBarcode(string $code): Barcode * @return array sequence * @protected */ - protected function get128ABsequence($code) + protected function get128ABsequence($code): array { $len = strlen($code); $sequence = []; diff --git a/src/Types/TypeCode128A.php b/src/Types/TypeCode128A.php index ff2fe0c..78bb46e 100644 --- a/src/Types/TypeCode128A.php +++ b/src/Types/TypeCode128A.php @@ -12,5 +12,5 @@ class TypeCode128A extends TypeCode128 { - protected $type = 'A'; + protected ?string $type = 'A'; } diff --git a/src/Types/TypeCode128B.php b/src/Types/TypeCode128B.php index b462863..6dbc0d8 100644 --- a/src/Types/TypeCode128B.php +++ b/src/Types/TypeCode128B.php @@ -12,5 +12,5 @@ class TypeCode128B extends TypeCode128 { - protected $type = 'B'; + protected ?string $type = 'B'; } diff --git a/src/Types/TypeCode128C.php b/src/Types/TypeCode128C.php index f4d4460..82566a2 100644 --- a/src/Types/TypeCode128C.php +++ b/src/Types/TypeCode128C.php @@ -12,5 +12,5 @@ class TypeCode128C extends TypeCode128 { - protected $type = 'C'; + protected ?string $type = 'C'; } diff --git a/src/Types/TypeCode32.php b/src/Types/TypeCode32.php index 5c50395..54c9bde 100644 --- a/src/Types/TypeCode32.php +++ b/src/Types/TypeCode32.php @@ -13,7 +13,7 @@ */ class TypeCode32 extends TypeCode39 { - protected $conversionTable32 = [ + protected array $conversionTable32 = [ '0' => '0', '1' => '1', '2' => '2', diff --git a/src/Types/TypeCode39.php b/src/Types/TypeCode39.php index 53cbaf9..2be9cc3 100644 --- a/src/Types/TypeCode39.php +++ b/src/Types/TypeCode39.php @@ -14,10 +14,10 @@ class TypeCode39 implements TypeInterface { - protected $extended = false; - protected $checksum = false; + protected bool $extended = false; + protected bool $checksum = false; - protected $conversionTable = [ + protected array $conversionTable = [ '0' => '111331311', '1' => '311311113', '2' => '113311113', @@ -113,10 +113,11 @@ public function getBarcode(string $code): Barcode * Encode a string to be used for CODE 39 Extended mode. * * @param string $code code to represent. - * @return bool|string encoded string. + * @return string encoded string. * @protected + * @throws InvalidCharacterException */ - protected function encode_code39_ext($code) + protected function encode_code39_ext(string $code): string { $encode = [ chr(0) => '%U', diff --git a/src/Types/TypeCode39Checksum.php b/src/Types/TypeCode39Checksum.php index b129f28..7a47bb6 100644 --- a/src/Types/TypeCode39Checksum.php +++ b/src/Types/TypeCode39Checksum.php @@ -9,6 +9,6 @@ class TypeCode39Checksum extends TypeCode39 { - protected $extended = false; - protected $checksum = true; + protected bool $extended = false; + protected bool $checksum = true; } diff --git a/src/Types/TypeCode39Extended.php b/src/Types/TypeCode39Extended.php index d16cfd6..0d85043 100644 --- a/src/Types/TypeCode39Extended.php +++ b/src/Types/TypeCode39Extended.php @@ -9,6 +9,6 @@ class TypeCode39Extended extends TypeCode39 { - protected $extended = true; - protected $checksum = false; + protected bool $extended = true; + protected bool $checksum = false; } diff --git a/src/Types/TypeCode39ExtendedChecksum.php b/src/Types/TypeCode39ExtendedChecksum.php index 1d28a88..9e5292c 100644 --- a/src/Types/TypeCode39ExtendedChecksum.php +++ b/src/Types/TypeCode39ExtendedChecksum.php @@ -9,6 +9,6 @@ class TypeCode39ExtendedChecksum extends TypeCode39 { - protected $extended = true; - protected $checksum = true; + protected bool $extended = true; + protected bool $checksum = true; } diff --git a/src/Types/TypeCode93.php b/src/Types/TypeCode93.php index 0bd9317..f944cf7 100644 --- a/src/Types/TypeCode93.php +++ b/src/Types/TypeCode93.php @@ -15,7 +15,7 @@ class TypeCode93 implements TypeInterface { - protected $conversionTable = [ + protected array $conversionTable = [ 48 => '131112', // 0 49 => '111213', // 1 50 => '111312', // 2 @@ -246,7 +246,7 @@ public function getBarcode(string $code): Barcode * @return string checksum code. * @protected */ - protected function checksum_code93($code) + protected function checksum_code93(string $code): string { $chars = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '-', '.', ' ', '$', '/', '+', '%', 'a', 'b', 'c', 'd']; @@ -280,8 +280,6 @@ protected function checksum_code93($code) $check %= 47; $k = $chars[$check]; - $checksum = $c . $k; - - return $checksum; + return $c . $k; } } diff --git a/src/Types/TypeEanUpcBase.php b/src/Types/TypeEanUpcBase.php index fe8948a..fba0121 100644 --- a/src/Types/TypeEanUpcBase.php +++ b/src/Types/TypeEanUpcBase.php @@ -211,7 +211,7 @@ protected function calculateChecksumDigit(string $code): int // calculate check digit $sum_a = 0; for ($i = 1; $i < $this->length - 1; $i += 2) { - $sum_a += $code[$i]; + $sum_a += intval($code[$i]); } if ($this->length > 12) { $sum_a *= 3; diff --git a/src/Types/TypeInterleaved25Checksum.php b/src/Types/TypeInterleaved25Checksum.php index 5871e9b..10727e6 100644 --- a/src/Types/TypeInterleaved25Checksum.php +++ b/src/Types/TypeInterleaved25Checksum.php @@ -75,11 +75,11 @@ protected function getChecksum(string $code): string $len = strlen($code); $sum = 0; for ($i = 0; $i < $len; $i += 2) { - $sum += $code[$i]; + $sum += intval($code[$i]); } $sum *= 3; for ($i = 1; $i < $len; $i += 2) { - $sum += ($code[$i]); + $sum += intval($code[$i]); } $r = $sum % 10; if ($r > 0) { diff --git a/src/Types/TypeMsi.php b/src/Types/TypeMsi.php index d6e91cb..12f6ee6 100644 --- a/src/Types/TypeMsi.php +++ b/src/Types/TypeMsi.php @@ -13,5 +13,5 @@ class TypeMsi extends TypeMsiChecksum { - protected $checksum = false; + protected bool $checksum = false; } diff --git a/src/Types/TypeMsiChecksum.php b/src/Types/TypeMsiChecksum.php index c421391..3d06f07 100644 --- a/src/Types/TypeMsiChecksum.php +++ b/src/Types/TypeMsiChecksum.php @@ -17,7 +17,7 @@ class TypeMsiChecksum implements TypeInterface { - protected $checksum = true; + protected bool $checksum = true; public function getBarcode(string $code): Barcode { diff --git a/src/Types/TypePlanet.php b/src/Types/TypePlanet.php index 0752386..c3b7f5e 100644 --- a/src/Types/TypePlanet.php +++ b/src/Types/TypePlanet.php @@ -13,7 +13,7 @@ class TypePlanet extends TypePostnet { - protected $barlen = [ + protected array $barlen = [ 0 => [1, 1, 2, 2, 2], 1 => [2, 2, 2, 1, 1], 2 => [2, 2, 1, 2, 1], diff --git a/src/Types/TypePostnet.php b/src/Types/TypePostnet.php index 2d136b3..0d235d8 100644 --- a/src/Types/TypePostnet.php +++ b/src/Types/TypePostnet.php @@ -16,7 +16,7 @@ class TypePostnet implements TypeInterface { - protected $barlen = [ + protected array $barlen = [ 0 => [2, 2, 1, 1, 1], 1 => [1, 1, 1, 2, 2], 2 => [1, 1, 2, 1, 2], diff --git a/src/Types/TypeStandard2of5.php b/src/Types/TypeStandard2of5.php index 906445d..1445a3c 100644 --- a/src/Types/TypeStandard2of5.php +++ b/src/Types/TypeStandard2of5.php @@ -14,7 +14,7 @@ class TypeStandard2of5 implements TypeInterface { - protected $checksum = false; + protected bool $checksum = false; public function getBarcode(string $code): Barcode { @@ -50,25 +50,25 @@ public function getBarcode(string $code): Barcode * Checksum for standard 2 of 5 barcodes. * * @param $code (string) code to process. - * @return int checksum. + * @return string checksum. * @protected */ - protected function checksum_s25($code) + protected function checksum_s25(string $code): string { $len = strlen($code); $sum = 0; for ($i = 0; $i < $len; $i += 2) { - $sum += $code[$i]; + $sum += intval($code[$i]); } $sum *= 3; for ($i = 1; $i < $len; $i += 2) { - $sum += ($code[$i]); + $sum += intval($code[$i]); } $r = $sum % 10; if ($r > 0) { $r = (10 - $r); } - return $r; + return strval($r); } } diff --git a/src/Types/TypeStandard2of5Checksum.php b/src/Types/TypeStandard2of5Checksum.php index 25bc4a0..c1b6fc5 100644 --- a/src/Types/TypeStandard2of5Checksum.php +++ b/src/Types/TypeStandard2of5Checksum.php @@ -10,5 +10,5 @@ class TypeStandard2of5Checksum extends TypeStandard2of5 { - protected $checksum = true; + protected bool $checksum = true; } diff --git a/src/Types/TypeTelepen.php b/src/Types/TypeTelepen.php index 378e342..477d4b9 100644 --- a/src/Types/TypeTelepen.php +++ b/src/Types/TypeTelepen.php @@ -23,11 +23,12 @@ class TypeTelepen implements TypeInterface { private const TELEPEN_START_CHAR = '_'; private const TELEPEN_STOP_CHAR = 'z'; + private const TELEPEN_ALPHA = 'alpha'; private const TELEPEN_NUMERIC = 'numeric'; - private $telepen_lookup_table; - private $mode; + private array $telepen_lookup_table; + private string $mode; public function __construct($m = 'alpha') { @@ -59,7 +60,6 @@ public function getBarcode(string $code): Barcode protected function encode($code) : string { - $result = null; if ($this->mode == self::TELEPEN_ALPHA) { $result = $this->encodeAlpha($code); } else { @@ -160,7 +160,7 @@ private function encodeNumeric(string $code) : string * More information about Telepen symbology is available from * https://v4l237.n3cdn1.secureserver.net/wp-content/uploads/2022/05/Barcode-Symbology-information-and-History.pdf */ - private function createTelepenConversionTable() + private function createTelepenConversionTable(): void { $this->telepen_lookup_table = [ "1111111111111111", "1131313111", "33313111", "1111313131", diff --git a/src/Types/TypeUpcExtension2.php b/src/Types/TypeUpcExtension2.php index f0620e1..22ecbca 100644 --- a/src/Types/TypeUpcExtension2.php +++ b/src/Types/TypeUpcExtension2.php @@ -14,7 +14,7 @@ class TypeUpcExtension2 implements TypeInterface { - protected $length = 2; + protected int $length = 2; public function getBarcode(string $code): Barcode { diff --git a/src/Types/TypeUpcExtension5.php b/src/Types/TypeUpcExtension5.php index b1d3424..c9e3065 100644 --- a/src/Types/TypeUpcExtension5.php +++ b/src/Types/TypeUpcExtension5.php @@ -10,5 +10,5 @@ class TypeUpcExtension5 extends TypeUpcExtension2 { - protected $length = 5; + protected int $length = 5; } From 6813baf371dd08aeed75a395c52bd35ee8981266 Mon Sep 17 00:00:00 2001 From: Casper Bakker Date: Sun, 31 Mar 2024 21:24:31 +0200 Subject: [PATCH 127/155] Updated readme for v3 --- Readme.md | 134 ++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 106 insertions(+), 28 deletions(-) diff --git a/Readme.md b/Readme.md index dfdfea2..9433736 100644 --- a/Readme.md +++ b/Readme.md @@ -7,8 +7,6 @@ This is an easy to use, non-bloated, framework independent, barcode generator in It creates SVG, PNG, JPG and HTML images, from the most used 1D barcode standards. -*The codebase is based on the [TCPDF barcode generator](https://github.com/tecnickcom/TCPDF) by Nicola Asuni. This code is therefor licensed under LGPLv3.* - ## No support for... - No support for any **2D** barcodes, like QR codes. - We only generate the 'bars' part of a barcode, without text below the barcode. If you want text of the code below the barcode, you could add it later to the output of this package. @@ -23,51 +21,56 @@ composer require picqer/php-barcode-generator If you want to generate PNG or JPG images, you need the GD library or Imagick installed on your system as well. ## Usage -Initiate the barcode generator for the output you want, then call the ->getBarcode() routine as many times as you want. +You want a barcode for a specific "type" (for example Code 128 or UPC) in a specific image format (for example PNG or SVG). + +> The "type" is a standard that defines which characters you can encode and which bars represent which character. The most used types are [code 128](https://en.wikipedia.org/wiki/Code_128) and [EAN/UPC](https://en.wikipedia.org/wiki/International_Article_Number). Not all characters can be encoded into each barcode type, and not all barcode scanners can read all types. + +First, encode the string you want the barcode of into a `Barcode` object with one of the barcode types. +Then, use one of the renderers to render the image of the bars in the `Barcode` object. ```php getBarcode('081231723897', $generator::TYPE_CODE_128); +// Make Barcode object of Code128 encoding. +$barcode = (new Picqer\Barcode\Types\TypeCode128())->getBarcode('081231723897'); + +// Output the barcode as HTML in the browser with a HTML Renderer +$renderer = new Picqer\Barcode\Renderers\HtmlRenderer(); +echo $renderer->render($barcode); ``` Will result in this beauty:
    ![Barcode 081231723897 as Code 128](tests/verified-files/081231723897-ean13.svg) -The `getBarcode()` method accepts the following parameters: -- `$barcode` String needed to encode in the barcode -- `$type` Type of barcode, use the constants defined in the class -- `$widthFactor` Width is based on the length of the data, with this factor you can make the barcode bars wider than default -- `$height` The total height of the barcode in pixels -- `$foregroundColor` Hex code as string, or array of RGB, of the colors of the bars (the foreground color) - -Example of usage of all parameters: - +Each renderer has their own options. For example, you can set the height, width and color of a PNG: ```php getBarcode('081231723897', $generator::TYPE_CODE_128, 3, 50, $redColor)); +$barcode = (new Picqer\Barcode\Types\TypeCode128())->getBarcode('081231723897'); +$renderer = new Picqer\Barcode\Renderers\PngRenderer(); +$renderer->setForegroundColor($colorRed); + +// Save PNG to the filesystem, with widthFactor 3 and height of 50 pixels +file_put_contents('barcode.png', $renderer->render($barcode, 3, 50)); ``` ## Image types ```php -$generatorSVG = new Picqer\Barcode\BarcodeGeneratorSVG(); // Vector based SVG -$generatorPNG = new Picqer\Barcode\BarcodeGeneratorPNG(); // Pixel based PNG -$generatorJPG = new Picqer\Barcode\BarcodeGeneratorJPG(); // Pixel based JPG -$generatorHTML = new Picqer\Barcode\BarcodeGeneratorHTML(); // Pixel based HTML -$generatorHTML = new Picqer\Barcode\BarcodeGeneratorDynamicHTML(); // Vector based HTML +$renderer = new Picqer\Barcode\Renderers\SvgRenderer(); // Vector based SVG +$renderer = new Picqer\Barcode\Renderers\PngRenderer(); // Pixel based PNG +$renderer = new Picqer\Barcode\Renderers\JpgRenderer(); // Pixel based JPG +$renderer = new Picqer\Barcode\Renderers\HtmlRenderer(); // Pixel based HTML +$renderer = new Picqer\Barcode\Renderers\DynamicHtmlRenderer(); // Vector based HTML (full 'page' width and height) ``` ## Accepted barcode types -These barcode types are supported. All types support different character sets or have mandatory lengths. Please see wikipedia for supported chars and lengths per type. +These barcode types are supported. All types support different character sets and some have mandatory lengths. Please see wikipedia for supported chars and lengths per type. + +You can find all supported types in the [src/Types](src/Types) folder. Most used types are TYPE_CODE_128 and TYPE_CODE_39. Because of the best scanner support, variable length and most chars supported. @@ -107,23 +110,98 @@ Most used types are TYPE_CODE_128 and TYPE_CODE_39. Because of the best scanner [See example images for all supported barcode types](examples.md) ## A note about PNG and JPG images -If you want to use PNG or JPG images, you need to install [Imagick](https://www.php.net/manual/en/intro.imagick.php) or the [GD library](https://www.php.net/manual/en/intro.image.php). This package will use Imagick if that is installed, or fall back to GD. If you have both installed but you want a specific method, you can use `$generator->useGd()` or `$generator->useImagick()` to force your preference. +If you want to use PNG or JPG images, you need to install [Imagick](https://www.php.net/manual/en/intro.imagick.php) or the [GD library](https://www.php.net/manual/en/intro.image.php). This package will use Imagick if that is installed, or fall back to GD. If you have both installed, but you want a specific method, you can use `$renderer->useGd()` or `$renderer->useImagick()` to force your preference. ## Examples ### Embedded PNG image in HTML ```php +$barcode = (new Picqer\Barcode\Types\TypeCode128())->getBarcode('081231723897'); +$renderer = new Picqer\Barcode\Renderers\PngRenderer(); +echo ''; +``` + +### Save JPG barcode to disk +```php +$barcode = (new Picqer\Barcode\Types\TypeCodabar())->getBarcode('081231723897'); +$renderer = new Picqer\Barcode\Renderers\JpgRenderer(); + +file_put_contents('barcode.jpg', $renderer->render($barcode)); +``` + +### Oneliner SVG output to disk +```php +file_put_contents('barcode.svg', (new Picqer\Barcode\Renderers\SvgRenderer())->render((new Picqer\Barcode\Types\TypeKix())->getBarcode('6825ME601'))); +``` + +## Upgrading to v3 +There is no need to change anything when upgrading from v2 to v3. Above you find the new preferred way of using this library since v3. But the old style still works. + +--- + +## Previous style generators +In version 3 the barcode type encoders and image renderers are completely separate. This makes building your own renderer way easier. The old way was using "generators". Below are the old examples of these generators, which still works in v3 as well. + +### Usage +Initiate the barcode generator for the output you want, then call the ->getBarcode() routine as many times as you want. + +```php +getBarcode('081231723897', $generator::TYPE_CODE_128); +``` + +Will result in this beauty:
    +![Barcode 081231723897 as Code 128](tests/verified-files/081231723897-ean13.svg) + +The `getBarcode()` method accepts the following parameters: +- `$barcode` String needed to encode in the barcode +- `$type` Type of barcode, use the constants defined in the class +- `$widthFactor` Width is based on the length of the data, with this factor you can make the barcode bars wider than default +- `$height` The total height of the barcode in pixels +- `$foregroundColor` Hex code as string, or array of RGB, of the colors of the bars (the foreground color) + +Example of usage of all parameters: + +```php +getBarcode('081231723897', $generator::TYPE_CODE_128, 3, 50, $redColor)); +``` + +### Image types +```php +$generatorSVG = new Picqer\Barcode\BarcodeGeneratorSVG(); // Vector based SVG +$generatorPNG = new Picqer\Barcode\BarcodeGeneratorPNG(); // Pixel based PNG +$generatorJPG = new Picqer\Barcode\BarcodeGeneratorJPG(); // Pixel based JPG +$generatorHTML = new Picqer\Barcode\BarcodeGeneratorHTML(); // Pixel based HTML +$generatorHTML = new Picqer\Barcode\BarcodeGeneratorDynamicHTML(); // Vector based HTML +``` + +#### Embedded PNG image in HTML +```php $generator = new Picqer\Barcode\BarcodeGeneratorPNG(); echo ''; ``` -### Save JPG barcode to disk +#### Save JPG barcode to disk ```php $generator = new Picqer\Barcode\BarcodeGeneratorJPG(); file_put_contents('barcode.jpg', $generator->getBarcode('081231723897', $generator::TYPE_CODABAR)); ``` -### Oneliner SVG output to disk +#### Oneliner SVG output to disk ```php file_put_contents('barcode.svg', (new Picqer\Barcode\BarcodeGeneratorSVG())->getBarcode('6825ME601', Picqer\Barcode\BarcodeGeneratorSVG::TYPE_KIX)); ``` + +--- +*The codebase is based on the [TCPDF barcode generator](https://github.com/tecnickcom/TCPDF) by Nicola Asuni. This code is therefor licensed under LGPLv3.* From f20b1ef6793462c4216e66e6186d8919b90193b6 Mon Sep 17 00:00:00 2001 From: Casper Bakker Date: Sun, 31 Mar 2024 21:25:30 +0200 Subject: [PATCH 128/155] Update Readme.md --- Readme.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Readme.md b/Readme.md index 9433736..2eaa1ea 100644 --- a/Readme.md +++ b/Readme.md @@ -23,10 +23,10 @@ If you want to generate PNG or JPG images, you need the GD library or Imagick in ## Usage You want a barcode for a specific "type" (for example Code 128 or UPC) in a specific image format (for example PNG or SVG). -> The "type" is a standard that defines which characters you can encode and which bars represent which character. The most used types are [code 128](https://en.wikipedia.org/wiki/Code_128) and [EAN/UPC](https://en.wikipedia.org/wiki/International_Article_Number). Not all characters can be encoded into each barcode type, and not all barcode scanners can read all types. +- First, encode the string you want the barcode of into a `Barcode` object with one of the barcode types. +- Then, use one of the renderers to render the image of the bars in the `Barcode` object. -First, encode the string you want the barcode of into a `Barcode` object with one of the barcode types. -Then, use one of the renderers to render the image of the bars in the `Barcode` object. +> The "type" is a standard that defines which characters you can encode and which bars represent which character. The most used types are [code 128](https://en.wikipedia.org/wiki/Code_128) and [EAN/UPC](https://en.wikipedia.org/wiki/International_Article_Number). Not all characters can be encoded into each barcode type, and not all barcode scanners can read all types. ```php Date: Sun, 31 Mar 2024 21:42:45 +0200 Subject: [PATCH 129/155] Update readme --- Readme.md | 48 ++++++++++++++++++++++++++++++++++- src/Renderers/JpgRenderer.php | 2 +- src/Renderers/PngRenderer.php | 16 ++++++------ 3 files changed, 56 insertions(+), 10 deletions(-) diff --git a/Readme.md b/Readme.md index 2eaa1ea..861a2ca 100644 --- a/Readme.md +++ b/Readme.md @@ -58,7 +58,8 @@ $renderer->setForegroundColor($colorRed); file_put_contents('barcode.png', $renderer->render($barcode, 3, 50)); ``` -## Image types +## Image renderers +These are the available renderers: ```php $renderer = new Picqer\Barcode\Renderers\SvgRenderer(); // Vector based SVG $renderer = new Picqer\Barcode\Renderers\PngRenderer(); // Pixel based PNG @@ -67,6 +68,51 @@ $renderer = new Picqer\Barcode\Renderers\HtmlRenderer(); // Pixel based HTML $renderer = new Picqer\Barcode\Renderers\DynamicHtmlRenderer(); // Vector based HTML (full 'page' width and height) ``` +Each renderer has their own options. Only the barcode is required, the rest is optional. Here are all the options for each renderers: + +### SVG +```php +$renderer = new Picqer\Barcode\Renderers\SvgRenderer(); +$renderer->setForegroundColor('red'); // Give a color for the bars, the background is always white +$renderer->setSvgType($renderer::TYPE_SVG_INLINE); // Changes the output to be used inline inside HTML documents, instead of a standalone SVG image (default) +$renderer->setSvgType($renderer::TYPE_SVG_STANDALONE); // If you want to force the default, create a stand alone SVG image + +$renderer->render($barcode, 450.20, 75); // Width and height support floats +```` + +### PNG + JPG +All options for PNG and JPG are the same. +```php +$renderer = new Picqer\Barcode\Renderers\PngRenderer(); +$renderer->setForegroundColor([255, 0, 0]); // Give a color for the bars, the background is always white. Give it as 3 times 0-255 values for red, green and blue. +$renderer->useGd(); // If you have Imagick and GD installed, but want to use GD +$renderer->useImagick(); // If you have Imagick and GD installed, but want to use Imagick +$renderer->render($barcode, 5, 40); // Width factor (how many pixel wide every bar is), and the height in pixels +```` + +### HTML +Gives HTML to use inline in a full HTML document. +```php +$renderer = new Picqer\Barcode\Renderers\HtmlRenderer(); +$renderer->setForegroundColor('red'); // Give a color for the bars, the background is always white + +$renderer->render($barcode, 450.20, 75); // Width and height support floats +```` + +### Dynamic HTML +Give HTML here the barcode is using the full width and height, to put inside a container/div that has a fixed size. +```php +$renderer = new Picqer\Barcode\Renderers\DynamicHtmlRenderer(); +$renderer->setForegroundColor('red'); // Give a color for the bars, the background is always white + +$renderer->render($barcode); +```` + +You can put the rendered HTML inside a div like this: +```html +
    +``` + ## Accepted barcode types These barcode types are supported. All types support different character sets and some have mandatory lengths. Please see wikipedia for supported chars and lengths per type. diff --git a/src/Renderers/JpgRenderer.php b/src/Renderers/JpgRenderer.php index 30a6b6e..132ec10 100644 --- a/src/Renderers/JpgRenderer.php +++ b/src/Renderers/JpgRenderer.php @@ -14,7 +14,7 @@ protected function createImagickImageObject(int $width, int $height): Imagick return $image; } - protected function generateGdImage($image) + protected function generateGdImage($image): void { \imagejpeg($image); \imagedestroy($image); diff --git a/src/Renderers/PngRenderer.php b/src/Renderers/PngRenderer.php index 59c8269..22cfcc8 100644 --- a/src/Renderers/PngRenderer.php +++ b/src/Renderers/PngRenderer.php @@ -32,7 +32,7 @@ public function __construct() /** * Force the use of Imagick image extension */ - public function useImagick() + public function useImagick(): void { $this->useImagick = true; } @@ -40,7 +40,7 @@ public function useImagick() /** * Force the use of the GD image library */ - public function useGd() + public function useGd(): void { $this->useImagick = false; } @@ -81,14 +81,14 @@ public function render(Barcode $barcode, int $widthFactor = 2, int $height = 30) $image = $this->createImagickImageObject($width, $height); $image->drawImage($imagickBarsShape); return $image->getImageBlob(); + } else { + ob_start(); + $this->generateGdImage($image); + return ob_get_clean(); } - - ob_start(); - $this->generateGdImage($image); - return ob_get_clean(); } - public function setForegroundColor(array $color) + public function setForegroundColor(array $color): void { $this->foregroundColor = $color; } @@ -110,7 +110,7 @@ protected function createImagickImageObject(int $width, int $height): Imagick return $image; } - protected function generateGdImage($image) + protected function generateGdImage($image): void { \imagepng($image); \imagedestroy($image); From 82659561909fb135d86016e2c7f59df2e3c23a2b Mon Sep 17 00:00:00 2001 From: Casper Bakker Date: Sun, 31 Mar 2024 21:44:25 +0200 Subject: [PATCH 130/155] Update Readme.md --- Readme.md | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/Readme.md b/Readme.md index 861a2ca..8923037 100644 --- a/Readme.md +++ b/Readme.md @@ -59,18 +59,12 @@ file_put_contents('barcode.png', $renderer->render($barcode, 3, 50)); ``` ## Image renderers -These are the available renderers: -```php -$renderer = new Picqer\Barcode\Renderers\SvgRenderer(); // Vector based SVG -$renderer = new Picqer\Barcode\Renderers\PngRenderer(); // Pixel based PNG -$renderer = new Picqer\Barcode\Renderers\JpgRenderer(); // Pixel based JPG -$renderer = new Picqer\Barcode\Renderers\HtmlRenderer(); // Pixel based HTML -$renderer = new Picqer\Barcode\Renderers\DynamicHtmlRenderer(); // Vector based HTML (full 'page' width and height) -``` +Available image renderers: SVG, PNG, JPG and HTML. Each renderer has their own options. Only the barcode is required, the rest is optional. Here are all the options for each renderers: ### SVG +A vector based SVG image. Gives the best quality to print. ```php $renderer = new Picqer\Barcode\Renderers\SvgRenderer(); $renderer->setForegroundColor('red'); // Give a color for the bars, the background is always white From d7d9890d8e5462e1af2f0fbe68f199b7837e099b Mon Sep 17 00:00:00 2001 From: Casper Bakker Date: Sun, 31 Mar 2024 21:45:48 +0200 Subject: [PATCH 131/155] Update Readme.md --- Readme.md | 1 + 1 file changed, 1 insertion(+) diff --git a/Readme.md b/Readme.md index 8923037..28fc583 100644 --- a/Readme.md +++ b/Readme.md @@ -81,6 +81,7 @@ $renderer = new Picqer\Barcode\Renderers\PngRenderer(); $renderer->setForegroundColor([255, 0, 0]); // Give a color for the bars, the background is always white. Give it as 3 times 0-255 values for red, green and blue. $renderer->useGd(); // If you have Imagick and GD installed, but want to use GD $renderer->useImagick(); // If you have Imagick and GD installed, but want to use Imagick + $renderer->render($barcode, 5, 40); // Width factor (how many pixel wide every bar is), and the height in pixels ```` From faae548fbf52e58344a2f30632898d56624a1b4f Mon Sep 17 00:00:00 2001 From: Casper Bakker Date: Mon, 1 Apr 2024 16:03:50 +0200 Subject: [PATCH 132/155] New verified files and examples --- examples.md | 140 ++++++++++-------- generate-examples.php | 16 +- generate-verified-files.php | 44 +++--- src/Helpers/StringHelpers.php | 12 ++ src/Types/TypePharmacode.php | 4 +- src/Types/TypePharmacodeTwoCode.php | 4 +- src/Types/TypeTelepenNumeric.php | 24 +++ tests/TypesTest.php | 12 +- tests/VerifiedBarcodeTest.php | 82 +++++----- ...23456789.svg => TypeCodabar-123456789.svg} | 0 ...123456789.svg => TypeCode11-123456789.svg} | 0 ...23897.svg => TypeCode128-081231723897.svg} | 0 ...> TypeCode128-1234567890abcABC-283-33.svg} | 0 ...567890.svg => TypeCode128A-1234567890.svg} | 0 ...3897.svg => TypeCode128B-081231723897.svg} | 0 ... TypeCode128B-1234567890abcABC-283-33.svg} | 0 ...90ABC.svg => TypeCode39-1234567890ABC.svg} | 0 ...g => TypeCode39Checksum-1234567890ABC.svg} | 0 ...> TypeCode39Extended-1234567890abcABC.svg} | 0 ...de39ExtendedChecksum-1234567890abcABC.svg} | 0 ...BC.svg => TypeCode93-1234567890abcABC.svg} | 0 ...0000463.svg => TypeEan13-004900000463.svg} | 0 ...004632.svg => TypeEan13-0049000004632.svg} | 0 ...1723897.svg => TypeEan13-081231723897.svg} | 0 ...{EAN8-1234568.svg => TypeEan8-1234568.svg} | 0 ...00012.svg => TypeITF14-00012345600012.svg} | 0 ...88766.svg => TypeITF14-05400141288766.svg} | 0 ... TypeIntelligentMailBarcode-123456789.svg} | 0 ...0.svg => TypeInterleaved25-1234567890.svg} | 0 ... TypeInterleaved25Checksum-1234567890.svg} | 0 ...IX-123456789.svg => TypeKix-123456789.svg} | 0 ...SI-123456789.svg => TypeMsi-123456789.svg} | 0 ...6789.svg => TypeMsiChecksum-123456789.svg} | 0 ...56789.svg => TypePharmacode-123456789.svg} | 2 +- ...vg => TypePharmacodeTwoCode-123456789.svg} | 2 +- ...123456789.svg => TypePlanet-123456789.svg} | 0 ...23456789.svg => TypePostnet-123456789.svg} | 0 ...123456789.svg => TypeRms4cc-123456789.svg} | 0 ...90.svg => TypeStandard2of5-1234567890.svg} | 0 ...> TypeStandard2of5Checksum-1234567890.svg} | 0 ...SCD.svg => TypeTelepen-1234567890ASCD.svg} | 0 ....svg => TypeTelepenNumeric-1234567890.svg} | 0 ...A-123456789.svg => TypeUpcA-123456789.svg} | 0 ...E-123456789.svg => TypeUpcE-123456789.svg} | 0 .../{EAN2-22.svg => TypeUpcExtension2-22.svg} | 0 ...UpcExtension5-1234567890abcABC-283-33.svg} | 0 46 files changed, 199 insertions(+), 143 deletions(-) create mode 100644 src/Helpers/StringHelpers.php create mode 100644 src/Types/TypeTelepenNumeric.php rename tests/verified-files/{CODABAR-123456789.svg => TypeCodabar-123456789.svg} (100%) rename tests/verified-files/{CODE11-123456789.svg => TypeCode11-123456789.svg} (100%) rename tests/verified-files/{C128-081231723897.svg => TypeCode128-081231723897.svg} (100%) rename tests/verified-files/{C128-1234567890abcABC-283-33.svg => TypeCode128-1234567890abcABC-283-33.svg} (100%) rename tests/verified-files/{C128A-1234567890.svg => TypeCode128A-1234567890.svg} (100%) rename tests/verified-files/{C128B-081231723897.svg => TypeCode128B-081231723897.svg} (100%) rename tests/verified-files/{C128B-1234567890abcABC-283-33.svg => TypeCode128B-1234567890abcABC-283-33.svg} (100%) rename tests/verified-files/{C39-1234567890ABC.svg => TypeCode39-1234567890ABC.svg} (100%) rename tests/verified-files/{C39+-1234567890ABC.svg => TypeCode39Checksum-1234567890ABC.svg} (100%) rename tests/verified-files/{C39E-1234567890abcABC.svg => TypeCode39Extended-1234567890abcABC.svg} (100%) rename tests/verified-files/{C39E+-1234567890abcABC.svg => TypeCode39ExtendedChecksum-1234567890abcABC.svg} (100%) rename tests/verified-files/{C93-1234567890abcABC.svg => TypeCode93-1234567890abcABC.svg} (100%) rename tests/verified-files/{EAN13-004900000463.svg => TypeEan13-004900000463.svg} (100%) rename tests/verified-files/{EAN13-0049000004632.svg => TypeEan13-0049000004632.svg} (100%) rename tests/verified-files/{EAN13-081231723897.svg => TypeEan13-081231723897.svg} (100%) rename tests/verified-files/{EAN8-1234568.svg => TypeEan8-1234568.svg} (100%) rename tests/verified-files/{ITF14-00012345600012.svg => TypeITF14-00012345600012.svg} (100%) rename tests/verified-files/{ITF14-05400141288766.svg => TypeITF14-05400141288766.svg} (100%) rename tests/verified-files/{IMB-123456789.svg => TypeIntelligentMailBarcode-123456789.svg} (100%) rename tests/verified-files/{I25-1234567890.svg => TypeInterleaved25-1234567890.svg} (100%) rename tests/verified-files/{I25+-1234567890.svg => TypeInterleaved25Checksum-1234567890.svg} (100%) rename tests/verified-files/{KIX-123456789.svg => TypeKix-123456789.svg} (100%) rename tests/verified-files/{MSI-123456789.svg => TypeMsi-123456789.svg} (100%) rename tests/verified-files/{MSI+-123456789.svg => TypeMsiChecksum-123456789.svg} (100%) rename tests/verified-files/{PHARMA-123456789.svg => TypePharmacode-123456789.svg} (98%) rename tests/verified-files/{PHARMA2T-123456789.svg => TypePharmacodeTwoCode-123456789.svg} (97%) rename tests/verified-files/{PLANET-123456789.svg => TypePlanet-123456789.svg} (100%) rename tests/verified-files/{POSTNET-123456789.svg => TypePostnet-123456789.svg} (100%) rename tests/verified-files/{RMS4CC-123456789.svg => TypeRms4cc-123456789.svg} (100%) rename tests/verified-files/{S25-1234567890.svg => TypeStandard2of5-1234567890.svg} (100%) rename tests/verified-files/{S25+-1234567890.svg => TypeStandard2of5Checksum-1234567890.svg} (100%) rename tests/verified-files/{TELEPENALPHA-1234567890ASCD.svg => TypeTelepen-1234567890ASCD.svg} (100%) rename tests/verified-files/{TELEPENNUMERIC-1234567890.svg => TypeTelepenNumeric-1234567890.svg} (100%) rename tests/verified-files/{UPCA-123456789.svg => TypeUpcA-123456789.svg} (100%) rename tests/verified-files/{UPCE-123456789.svg => TypeUpcE-123456789.svg} (100%) rename tests/verified-files/{EAN2-22.svg => TypeUpcExtension2-22.svg} (100%) rename tests/verified-files/{EAN5-1234567890abcABC-283-33.svg => TypeUpcExtension5-1234567890abcABC-283-33.svg} (100%) diff --git a/examples.md b/examples.md index 00329a5..689c192 100644 --- a/examples.md +++ b/examples.md @@ -2,137 +2,147 @@ These are examples of supported barcodes with this library. +All types can be found in the src/Types directory. -### C39 -![Barcode 1234567890ABC as C39](tests/verified-files/C39-1234567890ABC.svg) +### TypeCode39 -### C39+ +![Barcode 1234567890ABC as Picqer\Barcode\Types\TypeCode39](tests/verified-files/TypeCode39-1234567890ABC.svg) -![Barcode 1234567890ABC as C39+](tests/verified-files/C39+-1234567890ABC.svg) +### TypeCode39Checksum -### C39E +![Barcode 1234567890ABC as Picqer\Barcode\Types\TypeCode39Checksum](tests/verified-files/TypeCode39Checksum-1234567890ABC.svg) -![Barcode 1234567890abcABC as C39E](tests/verified-files/C39E-1234567890abcABC.svg) +### TypeCode39Extended -### C39E+ +![Barcode 1234567890abcABC as Picqer\Barcode\Types\TypeCode39Extended](tests/verified-files/TypeCode39Extended-1234567890abcABC.svg) -![Barcode 1234567890abcABC as C39E+](tests/verified-files/C39E+-1234567890abcABC.svg) +### TypeCode39ExtendedChecksum -### C93 +![Barcode 1234567890abcABC as Picqer\Barcode\Types\TypeCode39ExtendedChecksum](tests/verified-files/TypeCode39ExtendedChecksum-1234567890abcABC.svg) -![Barcode 1234567890abcABC as C93](tests/verified-files/C93-1234567890abcABC.svg) +### TypeCode93 -### S25 +![Barcode 1234567890abcABC as Picqer\Barcode\Types\TypeCode93](tests/verified-files/TypeCode93-1234567890abcABC.svg) -![Barcode 1234567890 as S25](tests/verified-files/S25-1234567890.svg) +### TypeStandard2of5 -### S25+ +![Barcode 1234567890 as Picqer\Barcode\Types\TypeStandard2of5](tests/verified-files/TypeStandard2of5-1234567890.svg) -![Barcode 1234567890 as S25+](tests/verified-files/S25+-1234567890.svg) +### TypeStandard2of5Checksum -### I25 +![Barcode 1234567890 as Picqer\Barcode\Types\TypeStandard2of5Checksum](tests/verified-files/TypeStandard2of5Checksum-1234567890.svg) -![Barcode 1234567890 as I25](tests/verified-files/I25-1234567890.svg) +### TypeInterleaved25 -### I25+ +![Barcode 1234567890 as Picqer\Barcode\Types\TypeInterleaved25](tests/verified-files/TypeInterleaved25-1234567890.svg) -![Barcode 1234567890 as I25+](tests/verified-files/I25+-1234567890.svg) +### TypeInterleaved25Checksum -### EAN13 +![Barcode 1234567890 as Picqer\Barcode\Types\TypeInterleaved25Checksum](tests/verified-files/TypeInterleaved25Checksum-1234567890.svg) -![Barcode 081231723897 as EAN13](tests/verified-files/EAN13-081231723897.svg) +### TypeEan13 -![Barcode 0049000004632 as EAN13](tests/verified-files/EAN13-0049000004632.svg) +![Barcode 081231723897 as Picqer\Barcode\Types\TypeEan13](tests/verified-files/TypeEan13-081231723897.svg) -![Barcode 004900000463 as EAN13](tests/verified-files/EAN13-004900000463.svg) +![Barcode 0049000004632 as Picqer\Barcode\Types\TypeEan13](tests/verified-files/TypeEan13-0049000004632.svg) -### ITF14 +![Barcode 004900000463 as Picqer\Barcode\Types\TypeEan13](tests/verified-files/TypeEan13-004900000463.svg) -![Barcode 00012345600012 as ITF14](tests/verified-files/ITF14-00012345600012.svg) +### TypeITF14 -![Barcode 05400141288766 as ITF14](tests/verified-files/ITF14-05400141288766.svg) +![Barcode 00012345600012 as Picqer\Barcode\Types\TypeITF14](tests/verified-files/TypeITF14-00012345600012.svg) -### C128 +![Barcode 05400141288766 as Picqer\Barcode\Types\TypeITF14](tests/verified-files/TypeITF14-05400141288766.svg) -![Barcode 081231723897 as C128](tests/verified-files/C128-081231723897.svg) +### TypeCode128 -![Barcode 1234567890abcABC-283*33 as C128](tests/verified-files/C128-1234567890abcABC-283-33.svg) +![Barcode 081231723897 as Picqer\Barcode\Types\TypeCode128](tests/verified-files/TypeCode128-081231723897.svg) -### C128A +![Barcode 1234567890abcABC-283*33 as Picqer\Barcode\Types\TypeCode128](tests/verified-files/TypeCode128-1234567890abcABC-283-33.svg) -![Barcode 1234567890 as C128A](tests/verified-files/C128A-1234567890.svg) +### TypeCode128A -### C128B +![Barcode 1234567890 as Picqer\Barcode\Types\TypeCode128A](tests/verified-files/TypeCode128A-1234567890.svg) -![Barcode 081231723897 as C128B](tests/verified-files/C128B-081231723897.svg) +### TypeCode128B -![Barcode 1234567890abcABC-283*33 as C128B](tests/verified-files/C128B-1234567890abcABC-283-33.svg) +![Barcode 081231723897 as Picqer\Barcode\Types\TypeCode128B](tests/verified-files/TypeCode128B-081231723897.svg) -### EAN2 +![Barcode 1234567890abcABC-283*33 as Picqer\Barcode\Types\TypeCode128B](tests/verified-files/TypeCode128B-1234567890abcABC-283-33.svg) -![Barcode 22 as EAN2](tests/verified-files/EAN2-22.svg) +### TypeUpcExtension2 -### EAN5 +![Barcode 22 as Picqer\Barcode\Types\TypeUpcExtension2](tests/verified-files/TypeUpcExtension2-22.svg) -![Barcode 1234567890abcABC-283*33 as EAN5](tests/verified-files/EAN5-1234567890abcABC-283-33.svg) +### TypeUpcExtension5 -### EAN8 +![Barcode 1234567890abcABC-283*33 as Picqer\Barcode\Types\TypeUpcExtension5](tests/verified-files/TypeUpcExtension5-1234567890abcABC-283-33.svg) -![Barcode 1234568 as EAN8](tests/verified-files/EAN8-1234568.svg) +### TypeEan8 -### UPCA +![Barcode 1234568 as Picqer\Barcode\Types\TypeEan8](tests/verified-files/TypeEan8-1234568.svg) -![Barcode 123456789 as UPCA](tests/verified-files/UPCA-123456789.svg) +### TypeUpcA -### UPCE +![Barcode 123456789 as Picqer\Barcode\Types\TypeUpcA](tests/verified-files/TypeUpcA-123456789.svg) -![Barcode 123456789 as UPCE](tests/verified-files/UPCE-123456789.svg) +### TypeUpcE -### MSI +![Barcode 123456789 as Picqer\Barcode\Types\TypeUpcE](tests/verified-files/TypeUpcE-123456789.svg) -![Barcode 123456789 as MSI](tests/verified-files/MSI-123456789.svg) +### TypeMsi -### MSI+ +![Barcode 123456789 as Picqer\Barcode\Types\TypeMsi](tests/verified-files/TypeMsi-123456789.svg) -![Barcode 123456789 as MSI+](tests/verified-files/MSI+-123456789.svg) +### TypeMsiChecksum -### POSTNET +![Barcode 123456789 as Picqer\Barcode\Types\TypeMsiChecksum](tests/verified-files/TypeMsiChecksum-123456789.svg) -![Barcode 123456789 as POSTNET](tests/verified-files/POSTNET-123456789.svg) +### TypePostnet -### PLANET +![Barcode 123456789 as Picqer\Barcode\Types\TypePostnet](tests/verified-files/TypePostnet-123456789.svg) -![Barcode 123456789 as PLANET](tests/verified-files/PLANET-123456789.svg) +### TypePlanet -### RMS4CC +![Barcode 123456789 as Picqer\Barcode\Types\TypePlanet](tests/verified-files/TypePlanet-123456789.svg) -![Barcode 123456789 as RMS4CC](tests/verified-files/RMS4CC-123456789.svg) +### TypeRms4cc -### KIX +![Barcode 123456789 as Picqer\Barcode\Types\TypeRms4cc](tests/verified-files/TypeRms4cc-123456789.svg) -![Barcode 123456789 as KIX](tests/verified-files/KIX-123456789.svg) +### TypeKix -### IMB +![Barcode 123456789 as Picqer\Barcode\Types\TypeKix](tests/verified-files/TypeKix-123456789.svg) -![Barcode 123456789 as IMB](tests/verified-files/IMB-123456789.svg) +### TypeIntelligentMailBarcode -### CODABAR +![Barcode 123456789 as Picqer\Barcode\Types\TypeIntelligentMailBarcode](tests/verified-files/TypeIntelligentMailBarcode-123456789.svg) -![Barcode 123456789 as CODABAR](tests/verified-files/CODABAR-123456789.svg) +### TypeCodabar -### CODE11 +![Barcode 123456789 as Picqer\Barcode\Types\TypeCodabar](tests/verified-files/TypeCodabar-123456789.svg) -![Barcode 123456789 as CODE11](tests/verified-files/CODE11-123456789.svg) +### TypeCode11 -### PHARMA +![Barcode 123456789 as Picqer\Barcode\Types\TypeCode11](tests/verified-files/TypeCode11-123456789.svg) -![Barcode 123456789 as PHARMA](tests/verified-files/PHARMA-123456789.svg) +### TypePharmacode -### PHARMA2T +![Barcode 123456789 as Picqer\Barcode\Types\TypePharmacode](tests/verified-files/TypePharmacode-123456789.svg) -![Barcode 123456789 as PHARMA2T](tests/verified-files/PHARMA2T-123456789.svg) +### TypePharmacodeTwoCode + +![Barcode 123456789 as Picqer\Barcode\Types\TypePharmacodeTwoCode](tests/verified-files/TypePharmacodeTwoCode-123456789.svg) + +### TypeTelepen + +![Barcode 1234567890ASCD as Picqer\Barcode\Types\TypeTelepen](tests/verified-files/TypeTelepen-1234567890ASCD.svg) + +### TypeTelepenNumeric + +![Barcode 1234567890 as Picqer\Barcode\Types\TypeTelepenNumeric](tests/verified-files/TypeTelepenNumeric-1234567890.svg) diff --git a/generate-examples.php b/generate-examples.php index 5929908..7be0f00 100644 --- a/generate-examples.php +++ b/generate-examples.php @@ -1,9 +1,5 @@ getBarcode('081231723897', $generatorSVG::TYPE_EAN_13)); -file_put_contents('tests/verified-files/081231723897-ean13-fractional-width.svg', $generatorSVG->getBarcode('081231723897', $generatorSVG::TYPE_EAN_13, 0.25, 25.75)); +$barcode = $typeEncoderEan13->getBarcode('081231723897'); +file_put_contents('tests/verified-files/081231723897-ean13.svg', $svgRenderer->render($barcode, $barcode->getWidth() * 2)); +file_put_contents('tests/verified-files/081231723897-ean13-fractional-width.svg', $svgRenderer->render($barcode, $barcode->getWidth() * 0.25, 25.75)); -$generatorHTML = new Picqer\Barcode\BarcodeGeneratorHTML(); -file_put_contents('tests/verified-files/081231723897-code128.html', $generatorHTML->getBarcode('081231723897', $generatorHTML::TYPE_CODE_128)); +$barcode = $typeEncoderCode128->getBarcode('081231723897'); +file_put_contents('tests/verified-files/081231723897-code128.html', $htmlRenderer->render($barcode, $barcode->getWidth() * 2)); -file_put_contents('tests/verified-files/12345678903-imb.html', $generatorHTML->getBarcode('12345678903', $generatorHTML::TYPE_IMB)); +$barcode = $typeEncoderIMB->getBarcode('12345678903'); +file_put_contents('tests/verified-files/12345678903-imb.html', $htmlRenderer->render($barcode, $barcode->getWidth() * 2)); -$generatorDynamicHTML = new Picqer\Barcode\BarcodeGeneratorDynamicHTML(); -file_put_contents('tests/verified-files/081231723897-dynamic-code128.html', $generatorDynamicHTML->getBarcode('081231723897', $generatorDynamicHTML::TYPE_CODE_128)); +$barcode = $typeEncoderCode128->getBarcode('081231723897'); +file_put_contents('tests/verified-files/081231723897-dynamic-code128.html', $dynamicHtmlRenderer->render($barcode)); -file_put_contents('tests/verified-files/12345678903-dynamic-imb.html', $generatorDynamicHTML->getBarcode('12345678903', $generatorDynamicHTML::TYPE_IMB)); +$barcode = $typeEncoderIMB->getBarcode('12345678903'); +file_put_contents('tests/verified-files/12345678903-dynamic-imb.html', $dynamicHtmlRenderer->render($barcode)); -$generatorSVG = new Picqer\Barcode\BarcodeGeneratorSVG(); -file_put_contents('tests/verified-files/0049000004632-ean13.svg', $generatorSVG->getBarcode('0049000004632', $generatorSVG::TYPE_EAN_13)); +$barcode = $typeEncoderEan13->getBarcode('0049000004632'); +file_put_contents('tests/verified-files/0049000004632-ean13.svg', $svgRenderer->render($barcode, $barcode->getWidth() * 2)); -// New style of verified files +// New style of verified files, defined in VerifiedBarcodeTest.php require(__DIR__ . '/tests/VerifiedBarcodeTest.php'); $verifiedFiles = VerifiedBarcodeTest::$supportedBarcodes; -$generatorSVG = new Picqer\Barcode\BarcodeGeneratorSVG(); foreach ($verifiedFiles as $verifiedFile) { - foreach ($verifiedFile['barcodes'] as $barcode) { - file_put_contents('tests/verified-files/' . getSaveFilename($verifiedFile['type'] . '-' . $barcode) . '.svg', $generatorSVG->getBarcode($barcode, $verifiedFile['type'])); + foreach ($verifiedFile['barcodes'] as $barcodeText) { + $barcode = (new $verifiedFile['type']())->getBarcode($barcodeText); + $result = $svgRenderer->render($barcode, $barcode->getWidth() * 2); + + file_put_contents('tests/verified-files/' . Picqer\Barcode\Helpers\StringHelpers::getSafeFilenameFrom($verifiedFile['type'] . '-' . $barcodeText) . '.svg', $result); } } diff --git a/src/Helpers/StringHelpers.php b/src/Helpers/StringHelpers.php new file mode 100644 index 0000000..c9857d0 --- /dev/null +++ b/src/Helpers/StringHelpers.php @@ -0,0 +1,12 @@ + + * from Java implementation of Telepen by Robin Stuart + * at https://github.com/woo-j/OkapiBarcode which uses the + * Apache License 2.0 http://www.apache.org/licenses/LICENSE-2.0 + * + * Implements Telepen (also known as Telepen Alpha), and Telepen Numeric. + * + * Telepen can encode ASCII text input and includes a modulo-127 check digit. + * Telepen Numeric allows compression of numeric data into a Telepen symbol. Data + * can consist of pairs of numbers or pairs consisting of a numerical digit followed + * by an X character. Telepen Numeric also includes a mod-127 check digit. + */ + +namespace Picqer\Barcode\Types; + +class TypeTelepenNumeric extends TypeTelepen +{ + public function __construct() + { + parent::__construct('numeric'); + } +} diff --git a/tests/TypesTest.php b/tests/TypesTest.php index aaca1ad..05b305e 100644 --- a/tests/TypesTest.php +++ b/tests/TypesTest.php @@ -9,7 +9,7 @@ public function test_generator_can_generate_code_39_barcode() $generator = new Picqer\Barcode\BarcodeGeneratorSVG(); $result = $generator->getBarcode('1234567890ABC', $generator::TYPE_CODE_39); - $this->assertStringEqualsFile('tests/verified-files/C39-1234567890ABC.svg', $result); + $this->assertStringEqualsFile('tests/verified-files/TypeCode39-1234567890ABC.svg', $result); } public function test_generator_can_generate_code_39_checksum_barcode() @@ -25,7 +25,7 @@ public function test_generator_can_generate_code_39_extended_barcode() $generator = new Picqer\Barcode\BarcodeGeneratorSVG(); $result = $generator->getBarcode('1234567890abcABC', $generator::TYPE_CODE_39E); - $this->assertStringEqualsFile('tests/verified-files/C39E-1234567890abcABC.svg', $result); + $this->assertStringEqualsFile('tests/verified-files/TypeCode39Extended-1234567890abcABC.svg', $result); } public function test_generator_can_generate_code_39_extended_checksum_barcode() @@ -81,7 +81,7 @@ public function test_generator_can_generate_code_128_barcode() $generator = new Picqer\Barcode\BarcodeGeneratorSVG(); $result = $generator->getBarcode('1234567890abcABC-283*33', $generator::TYPE_CODE_128); - $this->assertStringEqualsFile('tests/verified-files/C128-1234567890abcABC-283-33.svg', $result); + $this->assertStringEqualsFile('tests/verified-files/TypeCode128-1234567890abcABC-283-33.svg', $result); } public function test_generator_can_generate_code_128_a_barcode() @@ -89,7 +89,7 @@ public function test_generator_can_generate_code_128_a_barcode() $generator = new Picqer\Barcode\BarcodeGeneratorSVG(); $result = $generator->getBarcode('1234567890', $generator::TYPE_CODE_128_A); - $this->assertStringEqualsFile('tests/verified-files/C128A-1234567890.svg', $result); + $this->assertStringEqualsFile('tests/verified-files/TypeCode128A-1234567890.svg', $result); } public function test_generator_can_generate_code_128_b_barcode() @@ -97,7 +97,7 @@ public function test_generator_can_generate_code_128_b_barcode() $generator = new Picqer\Barcode\BarcodeGeneratorSVG(); $result = $generator->getBarcode('1234567890abcABC-283*33', $generator::TYPE_CODE_128_B); - $this->assertStringEqualsFile('tests/verified-files/C128B-1234567890abcABC-283-33.svg', $result); + $this->assertStringEqualsFile('tests/verified-files/TypeCode128B-1234567890abcABC-283-33.svg', $result); } public function test_generator_can_generate_ean_2_barcode() @@ -251,4 +251,4 @@ public function test_generator_can_generate_telepen_numeric_barcode() $this->assertGreaterThan(100, strlen($result)); } -} \ No newline at end of file +} diff --git a/tests/VerifiedBarcodeTest.php b/tests/VerifiedBarcodeTest.php index e3b496f..0e0267e 100644 --- a/tests/VerifiedBarcodeTest.php +++ b/tests/VerifiedBarcodeTest.php @@ -1,7 +1,7 @@ BarcodeGenerator::TYPE_CODE_39, 'barcodes' => ['1234567890ABC']], - ['type' => BarcodeGenerator::TYPE_CODE_39_CHECKSUM, 'barcodes' => ['1234567890ABC']], - ['type' => BarcodeGenerator::TYPE_CODE_39E, 'barcodes' => ['1234567890abcABC']], - ['type' => BarcodeGenerator::TYPE_CODE_39E_CHECKSUM, 'barcodes' => ['1234567890abcABC']], - ['type' => BarcodeGenerator::TYPE_CODE_93, 'barcodes' => ['1234567890abcABC']], - ['type' => BarcodeGenerator::TYPE_STANDARD_2_5, 'barcodes' => ['1234567890']], - ['type' => BarcodeGenerator::TYPE_STANDARD_2_5_CHECKSUM, 'barcodes' => ['1234567890']], - ['type' => BarcodeGenerator::TYPE_INTERLEAVED_2_5, 'barcodes' => ['1234567890']], - ['type' => BarcodeGenerator::TYPE_INTERLEAVED_2_5_CHECKSUM, 'barcodes' => ['1234567890']], - ['type' => BarcodeGenerator::TYPE_EAN_13, 'barcodes' => ['081231723897', '0049000004632', '004900000463']], - ['type' => BarcodeGenerator::TYPE_ITF_14, 'barcodes' => ['00012345600012', '05400141288766']], - ['type' => BarcodeGenerator::TYPE_CODE_128, 'barcodes' => ['081231723897', '1234567890abcABC-283*33']], - ['type' => BarcodeGenerator::TYPE_CODE_128_A, 'barcodes' => ['1234567890']], - ['type' => BarcodeGenerator::TYPE_CODE_128_B, 'barcodes' => ['081231723897', '1234567890abcABC-283*33']], - ['type' => BarcodeGenerator::TYPE_EAN_2, 'barcodes' => ['22']], - ['type' => BarcodeGenerator::TYPE_EAN_5, 'barcodes' => ['1234567890abcABC-283*33']], - ['type' => BarcodeGenerator::TYPE_EAN_8, 'barcodes' => ['1234568']], - ['type' => BarcodeGenerator::TYPE_UPC_A, 'barcodes' => ['123456789']], - ['type' => BarcodeGenerator::TYPE_UPC_E, 'barcodes' => ['123456789']], - ['type' => BarcodeGenerator::TYPE_MSI, 'barcodes' => ['123456789']], - ['type' => BarcodeGenerator::TYPE_MSI_CHECKSUM, 'barcodes' => ['123456789']], - ['type' => BarcodeGenerator::TYPE_POSTNET, 'barcodes' => ['123456789']], - ['type' => BarcodeGenerator::TYPE_PLANET, 'barcodes' => ['123456789']], - ['type' => BarcodeGenerator::TYPE_RMS4CC, 'barcodes' => ['123456789']], - ['type' => BarcodeGenerator::TYPE_KIX, 'barcodes' => ['123456789']], - ['type' => BarcodeGenerator::TYPE_IMB, 'barcodes' => ['123456789']], - ['type' => BarcodeGenerator::TYPE_CODABAR, 'barcodes' => ['123456789']], - ['type' => BarcodeGenerator::TYPE_CODE_11, 'barcodes' => ['123456789']], - ['type' => BarcodeGenerator::TYPE_PHARMA_CODE, 'barcodes' => ['123456789']], - ['type' => BarcodeGenerator::TYPE_PHARMA_CODE_TWO_TRACKS, 'barcodes' => ['123456789']], - ['type' => BarcodeGenerator::TYPE_TELEPEN_ALPHA, 'barcodes' => ['1234567890ASCD']], - ['type' => BarcodeGenerator::TYPE_TELEPEN_NUMERIC, 'barcodes' => ['1234567890']] + ['type' => \Picqer\Barcode\Types\TypeCode39::class, 'barcodes' => ['1234567890ABC']], + ['type' => \Picqer\Barcode\Types\TypeCode39Checksum::class, 'barcodes' => ['1234567890ABC']], + ['type' => \Picqer\Barcode\Types\TypeCode39Extended::class, 'barcodes' => ['1234567890abcABC']], + ['type' => \Picqer\Barcode\Types\TypeCode39ExtendedChecksum::class, 'barcodes' => ['1234567890abcABC']], + ['type' => \Picqer\Barcode\Types\TypeCode93::class, 'barcodes' => ['1234567890abcABC']], + ['type' => \Picqer\Barcode\Types\TypeStandard2of5::class, 'barcodes' => ['1234567890']], + ['type' => \Picqer\Barcode\Types\TypeStandard2of5Checksum::class, 'barcodes' => ['1234567890']], + ['type' => \Picqer\Barcode\Types\TypeInterleaved25::class, 'barcodes' => ['1234567890']], + ['type' => \Picqer\Barcode\Types\TypeInterleaved25Checksum::class, 'barcodes' => ['1234567890']], + ['type' => \Picqer\Barcode\Types\TypeEan13::class, 'barcodes' => ['081231723897', '0049000004632', '004900000463']], + ['type' => \Picqer\Barcode\Types\TypeITF14::class, 'barcodes' => ['00012345600012', '05400141288766']], + ['type' => \Picqer\Barcode\Types\TypeCode128::class, 'barcodes' => ['081231723897', '1234567890abcABC-283*33']], + ['type' => \Picqer\Barcode\Types\TypeCode128A::class, 'barcodes' => ['1234567890']], + ['type' => \Picqer\Barcode\Types\TypeCode128B::class, 'barcodes' => ['081231723897', '1234567890abcABC-283*33']], + ['type' => \Picqer\Barcode\Types\TypeUpcExtension2::class, 'barcodes' => ['22']], + ['type' => \Picqer\Barcode\Types\TypeUpcExtension5::class, 'barcodes' => ['1234567890abcABC-283*33']], + ['type' => \Picqer\Barcode\Types\TypeEan8::class, 'barcodes' => ['1234568']], + ['type' => \Picqer\Barcode\Types\TypeUpcA::class, 'barcodes' => ['123456789']], + ['type' => \Picqer\Barcode\Types\TypeUpcE::class, 'barcodes' => ['123456789']], + ['type' => \Picqer\Barcode\Types\TypeMsi::class, 'barcodes' => ['123456789']], + ['type' => \Picqer\Barcode\Types\TypeMsiChecksum::class, 'barcodes' => ['123456789']], + ['type' => \Picqer\Barcode\Types\TypePostnet::class, 'barcodes' => ['123456789']], + ['type' => \Picqer\Barcode\Types\TypePlanet::class, 'barcodes' => ['123456789']], + ['type' => \Picqer\Barcode\Types\TypeRms4cc::class, 'barcodes' => ['123456789']], + ['type' => \Picqer\Barcode\Types\TypeKix::class, 'barcodes' => ['123456789']], + ['type' => \Picqer\Barcode\Types\TypeIntelligentMailBarcode::class, 'barcodes' => ['123456789']], + ['type' => \Picqer\Barcode\Types\TypeCodabar::class, 'barcodes' => ['123456789']], + ['type' => \Picqer\Barcode\Types\TypeCode11::class, 'barcodes' => ['123456789']], + ['type' => \Picqer\Barcode\Types\TypePharmacode::class, 'barcodes' => ['123456789']], + ['type' => \Picqer\Barcode\Types\TypePharmacodeTwoCode::class, 'barcodes' => ['123456789']], + ['type' => \Picqer\Barcode\Types\TypeTelepen::class, 'barcodes' => ['1234567890ASCD']], + ['type' => \Picqer\Barcode\Types\TypeTelepenNumeric::class, 'barcodes' => ['1234567890']] ]; public function testAllSupportedBarcodeTypes() { - $generator = new Picqer\Barcode\BarcodeGeneratorSVG(); + $renderer = new Picqer\Barcode\Renderers\SvgRenderer(); foreach ($this::$supportedBarcodes as $barcodeTestSet) { - foreach ($barcodeTestSet['barcodes'] as $barcode) { - $result = $generator->getBarcode($barcode, $barcodeTestSet['type']); + foreach ($barcodeTestSet['barcodes'] as $barcodeText) { + $barcode = (new $barcodeTestSet['type']())->getBarcode($barcodeText); + $result = $renderer->render($barcode, $barcode->getWidth() * 2); $this->assertStringEqualsFile( - sprintf('tests/verified-files/%s.svg', $this->getSaveFilename($barcodeTestSet['type'] . '-' . $barcode)), + sprintf('tests/verified-files/%s.svg', StringHelpers::getSafeFilenameFrom($barcodeTestSet['type'] . '-' . $barcodeText)), $result, - sprintf('%s x %s dynamic test failed', $barcodeTestSet['type'], $barcode) + sprintf('%s x %s dynamic test failed', $barcodeTestSet['type'], $barcodeText) ); } } } - - protected function getSaveFilename($value) - { - return preg_replace('/[^a-zA-Z0-9_ \-+]/s', '-', $value); - } } diff --git a/tests/verified-files/CODABAR-123456789.svg b/tests/verified-files/TypeCodabar-123456789.svg similarity index 100% rename from tests/verified-files/CODABAR-123456789.svg rename to tests/verified-files/TypeCodabar-123456789.svg diff --git a/tests/verified-files/CODE11-123456789.svg b/tests/verified-files/TypeCode11-123456789.svg similarity index 100% rename from tests/verified-files/CODE11-123456789.svg rename to tests/verified-files/TypeCode11-123456789.svg diff --git a/tests/verified-files/C128-081231723897.svg b/tests/verified-files/TypeCode128-081231723897.svg similarity index 100% rename from tests/verified-files/C128-081231723897.svg rename to tests/verified-files/TypeCode128-081231723897.svg diff --git a/tests/verified-files/C128-1234567890abcABC-283-33.svg b/tests/verified-files/TypeCode128-1234567890abcABC-283-33.svg similarity index 100% rename from tests/verified-files/C128-1234567890abcABC-283-33.svg rename to tests/verified-files/TypeCode128-1234567890abcABC-283-33.svg diff --git a/tests/verified-files/C128A-1234567890.svg b/tests/verified-files/TypeCode128A-1234567890.svg similarity index 100% rename from tests/verified-files/C128A-1234567890.svg rename to tests/verified-files/TypeCode128A-1234567890.svg diff --git a/tests/verified-files/C128B-081231723897.svg b/tests/verified-files/TypeCode128B-081231723897.svg similarity index 100% rename from tests/verified-files/C128B-081231723897.svg rename to tests/verified-files/TypeCode128B-081231723897.svg diff --git a/tests/verified-files/C128B-1234567890abcABC-283-33.svg b/tests/verified-files/TypeCode128B-1234567890abcABC-283-33.svg similarity index 100% rename from tests/verified-files/C128B-1234567890abcABC-283-33.svg rename to tests/verified-files/TypeCode128B-1234567890abcABC-283-33.svg diff --git a/tests/verified-files/C39-1234567890ABC.svg b/tests/verified-files/TypeCode39-1234567890ABC.svg similarity index 100% rename from tests/verified-files/C39-1234567890ABC.svg rename to tests/verified-files/TypeCode39-1234567890ABC.svg diff --git a/tests/verified-files/C39+-1234567890ABC.svg b/tests/verified-files/TypeCode39Checksum-1234567890ABC.svg similarity index 100% rename from tests/verified-files/C39+-1234567890ABC.svg rename to tests/verified-files/TypeCode39Checksum-1234567890ABC.svg diff --git a/tests/verified-files/C39E-1234567890abcABC.svg b/tests/verified-files/TypeCode39Extended-1234567890abcABC.svg similarity index 100% rename from tests/verified-files/C39E-1234567890abcABC.svg rename to tests/verified-files/TypeCode39Extended-1234567890abcABC.svg diff --git a/tests/verified-files/C39E+-1234567890abcABC.svg b/tests/verified-files/TypeCode39ExtendedChecksum-1234567890abcABC.svg similarity index 100% rename from tests/verified-files/C39E+-1234567890abcABC.svg rename to tests/verified-files/TypeCode39ExtendedChecksum-1234567890abcABC.svg diff --git a/tests/verified-files/C93-1234567890abcABC.svg b/tests/verified-files/TypeCode93-1234567890abcABC.svg similarity index 100% rename from tests/verified-files/C93-1234567890abcABC.svg rename to tests/verified-files/TypeCode93-1234567890abcABC.svg diff --git a/tests/verified-files/EAN13-004900000463.svg b/tests/verified-files/TypeEan13-004900000463.svg similarity index 100% rename from tests/verified-files/EAN13-004900000463.svg rename to tests/verified-files/TypeEan13-004900000463.svg diff --git a/tests/verified-files/EAN13-0049000004632.svg b/tests/verified-files/TypeEan13-0049000004632.svg similarity index 100% rename from tests/verified-files/EAN13-0049000004632.svg rename to tests/verified-files/TypeEan13-0049000004632.svg diff --git a/tests/verified-files/EAN13-081231723897.svg b/tests/verified-files/TypeEan13-081231723897.svg similarity index 100% rename from tests/verified-files/EAN13-081231723897.svg rename to tests/verified-files/TypeEan13-081231723897.svg diff --git a/tests/verified-files/EAN8-1234568.svg b/tests/verified-files/TypeEan8-1234568.svg similarity index 100% rename from tests/verified-files/EAN8-1234568.svg rename to tests/verified-files/TypeEan8-1234568.svg diff --git a/tests/verified-files/ITF14-00012345600012.svg b/tests/verified-files/TypeITF14-00012345600012.svg similarity index 100% rename from tests/verified-files/ITF14-00012345600012.svg rename to tests/verified-files/TypeITF14-00012345600012.svg diff --git a/tests/verified-files/ITF14-05400141288766.svg b/tests/verified-files/TypeITF14-05400141288766.svg similarity index 100% rename from tests/verified-files/ITF14-05400141288766.svg rename to tests/verified-files/TypeITF14-05400141288766.svg diff --git a/tests/verified-files/IMB-123456789.svg b/tests/verified-files/TypeIntelligentMailBarcode-123456789.svg similarity index 100% rename from tests/verified-files/IMB-123456789.svg rename to tests/verified-files/TypeIntelligentMailBarcode-123456789.svg diff --git a/tests/verified-files/I25-1234567890.svg b/tests/verified-files/TypeInterleaved25-1234567890.svg similarity index 100% rename from tests/verified-files/I25-1234567890.svg rename to tests/verified-files/TypeInterleaved25-1234567890.svg diff --git a/tests/verified-files/I25+-1234567890.svg b/tests/verified-files/TypeInterleaved25Checksum-1234567890.svg similarity index 100% rename from tests/verified-files/I25+-1234567890.svg rename to tests/verified-files/TypeInterleaved25Checksum-1234567890.svg diff --git a/tests/verified-files/KIX-123456789.svg b/tests/verified-files/TypeKix-123456789.svg similarity index 100% rename from tests/verified-files/KIX-123456789.svg rename to tests/verified-files/TypeKix-123456789.svg diff --git a/tests/verified-files/MSI-123456789.svg b/tests/verified-files/TypeMsi-123456789.svg similarity index 100% rename from tests/verified-files/MSI-123456789.svg rename to tests/verified-files/TypeMsi-123456789.svg diff --git a/tests/verified-files/MSI+-123456789.svg b/tests/verified-files/TypeMsiChecksum-123456789.svg similarity index 100% rename from tests/verified-files/MSI+-123456789.svg rename to tests/verified-files/TypeMsiChecksum-123456789.svg diff --git a/tests/verified-files/PHARMA-123456789.svg b/tests/verified-files/TypePharmacode-123456789.svg similarity index 98% rename from tests/verified-files/PHARMA-123456789.svg rename to tests/verified-files/TypePharmacode-123456789.svg index f3561bb..46044b0 100644 --- a/tests/verified-files/PHARMA-123456789.svg +++ b/tests/verified-files/TypePharmacode-123456789.svg @@ -1,7 +1,7 @@ - 0 + 123456789 diff --git a/tests/verified-files/PHARMA2T-123456789.svg b/tests/verified-files/TypePharmacodeTwoCode-123456789.svg similarity index 97% rename from tests/verified-files/PHARMA2T-123456789.svg rename to tests/verified-files/TypePharmacodeTwoCode-123456789.svg index 6f9368f..39975d4 100644 --- a/tests/verified-files/PHARMA2T-123456789.svg +++ b/tests/verified-files/TypePharmacodeTwoCode-123456789.svg @@ -1,7 +1,7 @@ - 0 + 123456789 diff --git a/tests/verified-files/PLANET-123456789.svg b/tests/verified-files/TypePlanet-123456789.svg similarity index 100% rename from tests/verified-files/PLANET-123456789.svg rename to tests/verified-files/TypePlanet-123456789.svg diff --git a/tests/verified-files/POSTNET-123456789.svg b/tests/verified-files/TypePostnet-123456789.svg similarity index 100% rename from tests/verified-files/POSTNET-123456789.svg rename to tests/verified-files/TypePostnet-123456789.svg diff --git a/tests/verified-files/RMS4CC-123456789.svg b/tests/verified-files/TypeRms4cc-123456789.svg similarity index 100% rename from tests/verified-files/RMS4CC-123456789.svg rename to tests/verified-files/TypeRms4cc-123456789.svg diff --git a/tests/verified-files/S25-1234567890.svg b/tests/verified-files/TypeStandard2of5-1234567890.svg similarity index 100% rename from tests/verified-files/S25-1234567890.svg rename to tests/verified-files/TypeStandard2of5-1234567890.svg diff --git a/tests/verified-files/S25+-1234567890.svg b/tests/verified-files/TypeStandard2of5Checksum-1234567890.svg similarity index 100% rename from tests/verified-files/S25+-1234567890.svg rename to tests/verified-files/TypeStandard2of5Checksum-1234567890.svg diff --git a/tests/verified-files/TELEPENALPHA-1234567890ASCD.svg b/tests/verified-files/TypeTelepen-1234567890ASCD.svg similarity index 100% rename from tests/verified-files/TELEPENALPHA-1234567890ASCD.svg rename to tests/verified-files/TypeTelepen-1234567890ASCD.svg diff --git a/tests/verified-files/TELEPENNUMERIC-1234567890.svg b/tests/verified-files/TypeTelepenNumeric-1234567890.svg similarity index 100% rename from tests/verified-files/TELEPENNUMERIC-1234567890.svg rename to tests/verified-files/TypeTelepenNumeric-1234567890.svg diff --git a/tests/verified-files/UPCA-123456789.svg b/tests/verified-files/TypeUpcA-123456789.svg similarity index 100% rename from tests/verified-files/UPCA-123456789.svg rename to tests/verified-files/TypeUpcA-123456789.svg diff --git a/tests/verified-files/UPCE-123456789.svg b/tests/verified-files/TypeUpcE-123456789.svg similarity index 100% rename from tests/verified-files/UPCE-123456789.svg rename to tests/verified-files/TypeUpcE-123456789.svg diff --git a/tests/verified-files/EAN2-22.svg b/tests/verified-files/TypeUpcExtension2-22.svg similarity index 100% rename from tests/verified-files/EAN2-22.svg rename to tests/verified-files/TypeUpcExtension2-22.svg diff --git a/tests/verified-files/EAN5-1234567890abcABC-283-33.svg b/tests/verified-files/TypeUpcExtension5-1234567890abcABC-283-33.svg similarity index 100% rename from tests/verified-files/EAN5-1234567890abcABC-283-33.svg rename to tests/verified-files/TypeUpcExtension5-1234567890abcABC-283-33.svg From 55c82bd1ae6eee9a07113b99fecba9a16507441d Mon Sep 17 00:00:00 2001 From: Casper Bakker Date: Mon, 1 Apr 2024 16:28:00 +0200 Subject: [PATCH 133/155] More v3 info in readme --- Readme.md | 24 ++++++++++++++++++++++++ tests/TypesTest.php | 19 +++++++++++++------ 2 files changed, 37 insertions(+), 6 deletions(-) diff --git a/Readme.md b/Readme.md index 28fc583..2692cca 100644 --- a/Readme.md +++ b/Readme.md @@ -178,6 +178,30 @@ file_put_contents('barcode.svg', (new Picqer\Barcode\Renderers\SvgRenderer())->r ## Upgrading to v3 There is no need to change anything when upgrading from v2 to v3. Above you find the new preferred way of using this library since v3. But the old style still works. +If you want to convert to the new style, here is an example: +```php +// Old style +$generator = new Picqer\Barcode\BarcodeGeneratorSVG(); +echo $generator->getBarcode('081231723897', $generator::TYPE_CODE_128); + +// New style +$barcode = (new Picqer\Barcode\Types\TypeCode128())->getBarcode('081231723897'); +$renderer = new Picqer\Barcode\Renderers\SvgRenderer(); +echo $renderer->render($barcode); +``` + +The width in the SVG and HTML renderer is now the width of the end result, instead of the widthFactor. If you want to keep dynamic widths, you can get the width of the encoded Barcode and multiply it by the widthFactor to get the same result as before. See here an example for a widthFactor of 2: +```php +// Old style +$generator = new Picqer\Barcode\BarcodeGeneratorSVG(); +echo $generator->getBarcode('081231723897', $generator::TYPE_CODE_128, 2. 30); + +// New style +$barcode = (new Picqer\Barcode\Types\TypeCode128())->getBarcode('081231723897'); +$renderer = new Picqer\Barcode\Renderers\SvgRenderer(); +echo $renderer->render($barcode, $barcode->getWidth() * 2, 30); +``` + --- ## Previous style generators diff --git a/tests/TypesTest.php b/tests/TypesTest.php index 05b305e..2c6ef81 100644 --- a/tests/TypesTest.php +++ b/tests/TypesTest.php @@ -1,29 +1,36 @@ getBarcode('1234567890ABC', $generator::TYPE_CODE_39); + $barcode = (new TypeCode39())->getBarcode('1234567890ABC'); + $renderer = new SvgRenderer(); + $result = $renderer->render($barcode, $barcode->getWidth() * 2); $this->assertStringEqualsFile('tests/verified-files/TypeCode39-1234567890ABC.svg', $result); } public function test_generator_can_generate_code_39_checksum_barcode() { - $generator = new Picqer\Barcode\BarcodeGeneratorSVG(); - $result = $generator->getBarcode('1234567890ABC', $generator::TYPE_CODE_39_CHECKSUM); + $barcode = (new TypeCode39Checksum())->getBarcode('1234567890ABC'); + $renderer = new SvgRenderer(); + $result = $renderer->render($barcode, $barcode->getWidth() * 2); $this->assertGreaterThan(100, strlen($result)); } public function test_generator_can_generate_code_39_extended_barcode() { - $generator = new Picqer\Barcode\BarcodeGeneratorSVG(); - $result = $generator->getBarcode('1234567890abcABC', $generator::TYPE_CODE_39E); + $barcode = (new TypeCode39Extended())->getBarcode('1234567890abcABC'); + $renderer = new SvgRenderer(); + $result = $renderer->render($barcode, $barcode->getWidth() * 2); $this->assertStringEqualsFile('tests/verified-files/TypeCode39Extended-1234567890abcABC.svg', $result); } From 341db4f85ed028afd1deb36ea02d081b562a9aaa Mon Sep 17 00:00:00 2001 From: Bert-Jan de Lange Date: Mon, 8 Apr 2024 17:35:15 +0200 Subject: [PATCH 134/155] Fix incorrect nullable return type --- src/Types/TypeCode39.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Types/TypeCode39.php b/src/Types/TypeCode39.php index 5a4a2d2..9ec689e 100644 --- a/src/Types/TypeCode39.php +++ b/src/Types/TypeCode39.php @@ -113,7 +113,7 @@ public function getBarcodeData(string $code): Barcode * Encode a string to be used for CODE 39 Extended mode. * * @param string $code code to represent. - * @return bool|string encoded string. + * @return string encoded string. * @protected */ protected function encode_code39_ext($code) From 0cc7fbd7e000e3874fc3ed2583c550eb2f002a02 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADt=20Kutn=C3=BD?= Date: Wed, 17 Jul 2024 20:27:49 +0200 Subject: [PATCH 135/155] Added failing test of ITF14 checksum --- tests/ChecksumBarcodeTest.php | 37 +++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 tests/ChecksumBarcodeTest.php diff --git a/tests/ChecksumBarcodeTest.php b/tests/ChecksumBarcodeTest.php new file mode 100644 index 0000000..2bbf0db --- /dev/null +++ b/tests/ChecksumBarcodeTest.php @@ -0,0 +1,37 @@ + TypeEan13::class, 'barcodes' => [ + '081231723897' => '0812317238973', + '004900000463' => '0049000004632', + ]], + ['type' => TypeITF14::class, 'barcodes' => [ + '0001234560001' => '00012345600012', + '0540014128876' => '05400141288766', + ]], + ]; + + public function testAllSupportedBarcodeTypes() + { + foreach ($this::$supportedBarcodes as $barcodeTestSet) { + $barcodeType = $this->getBarcodeType($barcodeTestSet['type']); + + foreach ($barcodeTestSet['barcodes'] as $testBarcode => $expectedBarcode) { + $this->assertEquals($expectedBarcode, $barcodeType->getBarcodeData($testBarcode)->getBarcode()); + } + } + } + + + private function getBarcodeType(string $typeClass): TypeInterface + { + return new $typeClass; + } +} From acaf80faf87a183ce285d276ea443ad5ecee6aaa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADt=20Kutn=C3=BD?= Date: Wed, 17 Jul 2024 20:31:21 +0200 Subject: [PATCH 136/155] Fixed checksum calculation for ITF14 barcode type source: How to calculate a check digit manually https://www.gs1.org/services/how-calculate-check-digit-manually --- src/Types/TypeITF14.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Types/TypeITF14.php b/src/Types/TypeITF14.php index ff395d9..12d6054 100644 --- a/src/Types/TypeITF14.php +++ b/src/Types/TypeITF14.php @@ -73,8 +73,8 @@ private function getChecksum(string $code): string $total = 0; for ($charIndex = 0; $charIndex <= (strlen($code) - 1); $charIndex++) { - $integerOfChar = intval($code . substr($charIndex, 1)); - $total += $integerOfChar * (($charIndex === 0 || $charIndex % 2 === 0) ? 3 : 1); + $integerOfChar = intval($code[$charIndex]); + $total += $integerOfChar * ($charIndex % 2 === 0 ? 3 : 1); } $checksum = 10 - ($total % 10); From 5e76e1c79a44c4391dc9c7f0c25747498c5e4958 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADt=20Kutn=C3=BD?= Date: Wed, 17 Jul 2024 22:41:16 +0200 Subject: [PATCH 137/155] Add failint test for invalid ITF14 checksum --- tests/ChecksumBarcodeTest.php | 34 +++++++++++++++++++++++++++++----- 1 file changed, 29 insertions(+), 5 deletions(-) diff --git a/tests/ChecksumBarcodeTest.php b/tests/ChecksumBarcodeTest.php index 2bbf0db..eff35ab 100644 --- a/tests/ChecksumBarcodeTest.php +++ b/tests/ChecksumBarcodeTest.php @@ -1,13 +1,15 @@ TypeEan13::class, 'barcodes' => [ '081231723897' => '0812317238973', '004900000463' => '0049000004632', @@ -17,14 +19,36 @@ class ChecksumBarcodeTest extends TestCase '0540014128876' => '05400141288766', ]], ]; + private const INVALID_BARCODES = [ + ['type' => TypeEan13::class, 'barcodes' => ['0812317238972', '0049000004633']], + ['type' => TypeITF14::class, 'barcodes' => ['00012345600016', '05400141288762']], + ]; - public function testAllSupportedBarcodeTypes() + public function testAllValidBarcodeTypes() { - foreach ($this::$supportedBarcodes as $barcodeTestSet) { + foreach (self::VALID_BARCODES as $barcodeTestSet) { $barcodeType = $this->getBarcodeType($barcodeTestSet['type']); - foreach ($barcodeTestSet['barcodes'] as $testBarcode => $expectedBarcode) { - $this->assertEquals($expectedBarcode, $barcodeType->getBarcodeData($testBarcode)->getBarcode()); + foreach ($barcodeTestSet['barcodes'] as $testBarcode => $validBarcode) { + $this->assertEquals($validBarcode, $barcodeType->getBarcodeData($testBarcode)->getBarcode()); + } + } + } + + public function testAllInvalidBarcodeTypes() + { + foreach (self::INVALID_BARCODES as $barcodeTestSet) { + $barcodeType = $this->getBarcodeType($barcodeTestSet['type']); + + foreach ($barcodeTestSet['barcodes'] as $invalidBarcode) { + try { + $barcodeType->getBarcodeData($invalidBarcode)->getBarcode(); + } catch (BarcodeException $exception) { + $this->assertInstanceOf(InvalidCheckDigitException::class, $exception); + continue; + } + + $this->assertTrue(false, sprintf('Exception was not thrown for barcode "%s".', $invalidBarcode)); } } } From a1397928ccd111d01105b3517abcbb6611bc90fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADt=20Kutn=C3=BD?= Date: Wed, 17 Jul 2024 22:58:36 +0200 Subject: [PATCH 138/155] ITF14 throws InvalidCheckDigitException when checksum is invalid --- src/Types/TypeITF14.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/Types/TypeITF14.php b/src/Types/TypeITF14.php index 12d6054..d2f87bb 100644 --- a/src/Types/TypeITF14.php +++ b/src/Types/TypeITF14.php @@ -5,6 +5,7 @@ use Picqer\Barcode\Barcode; use Picqer\Barcode\BarcodeBar; use Picqer\Barcode\Exceptions\InvalidCharacterException; +use Picqer\Barcode\Exceptions\InvalidCheckDigitException; use Picqer\Barcode\Exceptions\InvalidLengthException; class TypeITF14 implements TypeInterface @@ -12,6 +13,7 @@ class TypeITF14 implements TypeInterface /** * @throws InvalidLengthException * @throws InvalidCharacterException + * @throws InvalidCheckDigitException */ public function getBarcodeData(string $code): Barcode { @@ -35,6 +37,10 @@ public function getBarcodeData(string $code): Barcode if (strlen($code) === 13) { $code .= $this->getChecksum($code); + } elseif (substr($code, -1) !== $this->getChecksum(substr($code, 0, -1))) { + // If length of given barcode is same as final length, barcode is including checksum + // Make sure that checksum is the same as we calculated + throw new InvalidCheckDigitException(); } $barcode = new Barcode($code); From e9d39681f617705492b609fc3fc58465ef88e8fd Mon Sep 17 00:00:00 2001 From: Casper Bakker Date: Wed, 18 Sep 2024 10:09:33 +0200 Subject: [PATCH 139/155] Fix phpstan errors --- src/Types/TypeCode128.php | 4 ++-- src/Types/TypeUpcExtension2.php | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Types/TypeCode128.php b/src/Types/TypeCode128.php index 38fc618..c8b8253 100644 --- a/src/Types/TypeCode128.php +++ b/src/Types/TypeCode128.php @@ -214,7 +214,7 @@ public function getBarcodeData(string $code): Barcode // get numeric sequences (if any) $numseq = []; preg_match_all('/([0-9]{4,})/', $code, $numseq, PREG_OFFSET_CAPTURE); - if (isset($numseq[1]) AND ! empty($numseq[1])) { + if (! empty($numseq[1])) { $end_offset = 0; foreach ($numseq[1] as $val) { $offset = $val[1]; @@ -390,7 +390,7 @@ protected function get128ABsequence($code) // get A sequences (if any) $numseq = []; preg_match_all('/([\x00-\x1f])/', $code, $numseq, PREG_OFFSET_CAPTURE); - if (isset($numseq[1]) AND ! empty($numseq[1])) { + if (empty($numseq[1])) { $end_offset = 0; foreach ($numseq[1] as $val) { $offset = $val[1]; diff --git a/src/Types/TypeUpcExtension2.php b/src/Types/TypeUpcExtension2.php index cebbfa8..f8416f0 100644 --- a/src/Types/TypeUpcExtension2.php +++ b/src/Types/TypeUpcExtension2.php @@ -25,7 +25,7 @@ public function getBarcodeData(string $code): Barcode // Calculate check digit if ($len == 2) { - $r = $code % 4; + $r = (int)$code % 4; } elseif ($len == 5) { $r = (3 * intval($code[0] . $code[2] . $code[4])) + (9 * intval($code[1] . $code[3])); $r %= 10; From 871daeb8704f4216c595bb5b308bdbe0ce292a96 Mon Sep 17 00:00:00 2001 From: Casper Bakker Date: Fri, 20 Sep 2024 17:42:50 +0200 Subject: [PATCH 140/155] Update ChecksumBarcodeTest.php --- tests/ChecksumBarcodeTest.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/tests/ChecksumBarcodeTest.php b/tests/ChecksumBarcodeTest.php index eff35ab..c9daf2d 100644 --- a/tests/ChecksumBarcodeTest.php +++ b/tests/ChecksumBarcodeTest.php @@ -30,7 +30,7 @@ public function testAllValidBarcodeTypes() $barcodeType = $this->getBarcodeType($barcodeTestSet['type']); foreach ($barcodeTestSet['barcodes'] as $testBarcode => $validBarcode) { - $this->assertEquals($validBarcode, $barcodeType->getBarcodeData($testBarcode)->getBarcode()); + $this->assertEquals($validBarcode, $barcodeType->getBarcode($testBarcode)->getBarcode()); } } } @@ -42,7 +42,7 @@ public function testAllInvalidBarcodeTypes() foreach ($barcodeTestSet['barcodes'] as $invalidBarcode) { try { - $barcodeType->getBarcodeData($invalidBarcode)->getBarcode(); + $barcodeType->getBarcode($invalidBarcode)->getBarcode(); } catch (BarcodeException $exception) { $this->assertInstanceOf(InvalidCheckDigitException::class, $exception); continue; @@ -53,7 +53,6 @@ public function testAllInvalidBarcodeTypes() } } - private function getBarcodeType(string $typeClass): TypeInterface { return new $typeClass; From b9a237759fb845c231567a400f53f71a9eb69a19 Mon Sep 17 00:00:00 2001 From: Casper Bakker Date: Sat, 21 Sep 2024 08:58:53 +0200 Subject: [PATCH 141/155] Improve variable names --- src/Types/TypeCode11.php | 44 +++++++++++++++++++------------------- src/Types/TypeCode32.php | 8 +++---- src/Types/TypeCode39.php | 12 +++++------ src/Types/TypeCode93.php | 46 ++++++++++++++++++++-------------------- 4 files changed, 55 insertions(+), 55 deletions(-) diff --git a/src/Types/TypeCode11.php b/src/Types/TypeCode11.php index ab469bd..d70cf5f 100644 --- a/src/Types/TypeCode11.php +++ b/src/Types/TypeCode11.php @@ -60,27 +60,27 @@ public function getBarcode(string $code): Barcode private function getCheckDigitC(string $code): string { - $p = 1; - $check = 0; + $weight = 1; + $checksum = 0; for ($i = (strlen($code) - 1); $i >= 0; --$i) { $digit = $code[$i]; if ($digit == '-') { - $dval = 10; + $digitValue = 10; } else { - $dval = intval($digit); + $digitValue = intval($digit); } - $check += ($dval * $p); - ++$p; - if ($p > 10) { - $p = 1; + $checksum += ($digitValue * $weight); + ++$weight; + if ($weight > 10) { + $weight = 1; } } - $check %= 11; - if ($check == 10) { - $check = '-'; + $checksum %= 11; + if ($checksum == 10) { + $checksum = '-'; } - return $check; + return $checksum; } private function getCheckDigitK(string $code): string @@ -89,23 +89,23 @@ private function getCheckDigitK(string $code): string return ''; } - $p = 1; - $check = 0; + $weight = 1; + $checksum = 0; for ($i = (strlen($code) - 1); $i >= 0; --$i) { $digit = $code[$i]; if ($digit == '-') { - $dval = 10; + $digitValue = 10; } else { - $dval = intval($digit); + $digitValue = intval($digit); } - $check += ($dval * $p); - ++$p; - if ($p > 9) { - $p = 1; + $checksum += ($digitValue * $weight); + ++$weight; + if ($weight > 9) { + $weight = 1; } } - $check %= 11; + $checksum %= 11; - return (string)$check; + return (string)$checksum; } } diff --git a/src/Types/TypeCode32.php b/src/Types/TypeCode32.php index 54c9bde..0f8a550 100644 --- a/src/Types/TypeCode32.php +++ b/src/Types/TypeCode32.php @@ -98,17 +98,17 @@ public function getBarcode(string $code): Barcode */ protected function checksum_code32(string $code): string { - $s = 0; + $checksum = 0; foreach (str_split($code) as $i => $c) { if (0 === $i % 2) { - $s += (int)$c; + $checksum += (int)$c; } else { $c = 2 * (int)$c; - $s += (int)floor($c / 10) + ($c % 10); + $checksum += (int)floor($c / 10) + ($c % 10); } } - return (string)($s % 10); + return (string)($checksum % 10); } } diff --git a/src/Types/TypeCode39.php b/src/Types/TypeCode39.php index 2be9cc3..bf16994 100644 --- a/src/Types/TypeCode39.php +++ b/src/Types/TypeCode39.php @@ -318,13 +318,13 @@ protected function checksum_code39($code) '%' ]; - $sum = 0; - for ($i = 0; $i < strlen($code); ++$i) { - $k = array_keys($chars, $code[$i]); - $sum += $k[0]; + $checksum = 0; + for ($index = 0; $index < strlen($code); ++$index) { + $charPosition = array_keys($chars, $code[$index]); + $checksum += $charPosition[0]; } - $j = ($sum % 43); + $checksumIndex = ($checksum % 43); - return $chars[$j]; + return $chars[$checksumIndex]; } } diff --git a/src/Types/TypeCode93.php b/src/Types/TypeCode93.php index f944cf7..5ff4a67 100644 --- a/src/Types/TypeCode93.php +++ b/src/Types/TypeCode93.php @@ -251,35 +251,35 @@ protected function checksum_code93(string $code): string $chars = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '-', '.', ' ', '$', '/', '+', '%', 'a', 'b', 'c', 'd']; // calculate check digit C - $len = strlen($code); - $p = 1; - $check = 0; - for ($i = ($len - 1); $i >= 0; --$i) { - $k = array_keys($chars, $code[$i]); - $check += ($k[0] * $p); - ++$p; - if ($p > 20) { - $p = 1; + $codeLength = strlen($code); + $weight = 1; + $checksum = 0; + for ($i = ($codeLength - 1); $i >= 0; --$i) { + $charIndex = array_keys($chars, $code[$i]); + $checksum += ($charIndex[0] * $weight); + ++$weight; + if ($weight > 20) { + $weight = 1; } } - $check %= 47; - $c = $chars[$check]; - $code .= $c; + $checksumC = $checksum % 47; + $checkDigitC = $chars[$checksumC]; + $codeWithC = $code . $checkDigitC; // calculate check digit K - $p = 1; - $check = 0; - for ($i = $len; $i >= 0; --$i) { - $k = array_keys($chars, $code[$i]); - $check += ($k[0] * $p); - ++$p; - if ($p > 15) { - $p = 1; + $weight = 1; + $checksum = 0; + for ($i = $codeLength; $i >= 0; --$i) { + $charIndex = array_keys($chars, $codeWithC[$i]); + $checksum += ($charIndex[0] * $weight); + ++$weight; + if ($weight > 15) { + $weight = 1; } } - $check %= 47; - $k = $chars[$check]; + $checksumK = $checksum % 47; + $checkDigitK = $chars[$checksumK]; - return $c . $k; + return $checkDigitC . $checkDigitK; } } From 8a7e79df4bcd9b9b38227821f63958a587ce005a Mon Sep 17 00:00:00 2001 From: Casper Bakker Date: Sat, 21 Sep 2024 09:20:43 +0200 Subject: [PATCH 142/155] Make methods fluent --- src/Barcode.php | 1 + src/BarcodeBar.php | 1 + src/Renderers/DynamicHtmlRenderer.php | 4 +++- src/Renderers/HtmlRenderer.php | 4 +++- src/Renderers/PngRenderer.php | 10 +++++++--- src/Renderers/SvgRenderer.php | 6 ++++-- 6 files changed, 19 insertions(+), 7 deletions(-) diff --git a/src/Barcode.php b/src/Barcode.php index a39b382..5b210b3 100644 --- a/src/Barcode.php +++ b/src/Barcode.php @@ -14,6 +14,7 @@ public function __construct(string $barcode) $this->barcode = $barcode; } + // Add a bar to the barcode, either a bar or a space, at the right side of the barcode public function addBar(BarcodeBar $bar): void { $this->bars[] = $bar; diff --git a/src/BarcodeBar.php b/src/BarcodeBar.php index 58a881f..28bd595 100644 --- a/src/BarcodeBar.php +++ b/src/BarcodeBar.php @@ -2,6 +2,7 @@ namespace Picqer\Barcode; +// Represents a single bar or space in a barcode readonly class BarcodeBar { protected int $width; diff --git a/src/Renderers/DynamicHtmlRenderer.php b/src/Renderers/DynamicHtmlRenderer.php index 51ea1a0..66a3e52 100644 --- a/src/Renderers/DynamicHtmlRenderer.php +++ b/src/Renderers/DynamicHtmlRenderer.php @@ -36,8 +36,10 @@ public function render(Barcode $barcode): string return $html; } - public function setForegroundColor(string $color): void + public function setForegroundColor(string $color): self { $this->foregroundColor = $color; + + return $this; } } diff --git a/src/Renderers/HtmlRenderer.php b/src/Renderers/HtmlRenderer.php index 517908e..77ba389 100644 --- a/src/Renderers/HtmlRenderer.php +++ b/src/Renderers/HtmlRenderer.php @@ -36,8 +36,10 @@ public function render(Barcode $barcode, float $width = 200, float $height = 30) return $html; } - public function setForegroundColor(string $color): void + public function setForegroundColor(string $color): self { $this->foregroundColor = $color; + + return $this; } } diff --git a/src/Renderers/PngRenderer.php b/src/Renderers/PngRenderer.php index 22cfcc8..8297d88 100644 --- a/src/Renderers/PngRenderer.php +++ b/src/Renderers/PngRenderer.php @@ -32,17 +32,19 @@ public function __construct() /** * Force the use of Imagick image extension */ - public function useImagick(): void + public function useImagick(): self { $this->useImagick = true; + return $this; } /** * Force the use of the GD image library */ - public function useGd(): void + public function useGd(): self { $this->useImagick = false; + return $this; } public function render(Barcode $barcode, int $widthFactor = 2, int $height = 30): string @@ -88,9 +90,11 @@ public function render(Barcode $barcode, int $widthFactor = 2, int $height = 30) } } - public function setForegroundColor(array $color): void + public function setForegroundColor(array $color): self { $this->foregroundColor = $color; + + return $this; } protected function createGdImageObject(int $width, int $height) diff --git a/src/Renderers/SvgRenderer.php b/src/Renderers/SvgRenderer.php index 6587d99..6af44b3 100644 --- a/src/Renderers/SvgRenderer.php +++ b/src/Renderers/SvgRenderer.php @@ -49,17 +49,19 @@ public function render(Barcode $barcode, float $width = 200, float $height = 30) return $svg; } - public function setForegroundColor(string $color): void + public function setForegroundColor(string $color): self { $this->foregroundColor = $color; + return $this; } - public function setSvgType(string $svgType): void + public function setSvgType(string $svgType): self { if (! in_array($svgType, [self::TYPE_SVG_INLINE, self::TYPE_SVG_STANDALONE])) { throw new InvalidOptionException(); } $this->svgType = $svgType; + return $this; } } From fb4480b0cefb02af664843f865e8b5d48d921d85 Mon Sep 17 00:00:00 2001 From: Casper Bakker Date: Sat, 21 Sep 2024 13:56:23 +0200 Subject: [PATCH 143/155] Updates in Github Actions definitions --- .github/workflows/phpstan.yml | 2 -- .github/workflows/phpunit.yml | 4 +--- composer.json | 10 +++++----- 3 files changed, 6 insertions(+), 10 deletions(-) diff --git a/.github/workflows/phpstan.yml b/.github/workflows/phpstan.yml index 1e4ca51..20fe4d1 100644 --- a/.github/workflows/phpstan.yml +++ b/.github/workflows/phpstan.yml @@ -13,8 +13,6 @@ jobs: steps: - name: Checkout code uses: actions/checkout@v4 - with: - fetch-depth: 0 - name: Setup PHP uses: shivammathur/setup-php@v2 diff --git a/.github/workflows/phpunit.yml b/.github/workflows/phpunit.yml index 33b5a11..cc4a0d5 100644 --- a/.github/workflows/phpunit.yml +++ b/.github/workflows/phpunit.yml @@ -12,13 +12,11 @@ jobs: strategy: matrix: - php-versions: ['8.2', '8.3'] + php-versions: ['8.2', '8.3', '8.4'] steps: - name: Checkout code uses: actions/checkout@v4 - with: - fetch-depth: 0 - name: Setup PHP uses: shivammathur/setup-php@v2 diff --git a/composer.json b/composer.json index e65b8e8..a67a4e8 100644 --- a/composer.json +++ b/composer.json @@ -6,15 +6,15 @@ "homepage": "https://github.com/picqer/php-barcode-generator", "license": "LGPL-3.0-or-later", "authors": [ - { - "name": "Nicola Asuni", - "email": "info@tecnick.com", - "homepage": "http://nicolaasuni.tecnick.com/" - }, { "name": "Casper Bakker", "email": "info@picqer.com", "homepage": "https://picqer.com/" + }, + { + "name": "Nicola Asuni", + "email": "info@tecnick.com", + "homepage": "http://nicolaasuni.tecnick.com/" } ], "require": { From a9339a5384fffdf24fbaa7ddeb1ccb94ff2b5c60 Mon Sep 17 00:00:00 2001 From: Casper Bakker Date: Sat, 21 Sep 2024 14:41:23 +0200 Subject: [PATCH 144/155] Add support for background colors --- generate-verified-files.php | 7 ++++ src/Renderers/DynamicHtmlRenderer.php | 10 ++++- src/Renderers/HtmlRenderer.php | 12 +++++- src/Renderers/JpgRenderer.php | 9 ++++- src/Renderers/PngRenderer.php | 35 ++++++++++++++--- src/Renderers/SvgRenderer.php | 13 +++++++ tests/HtmlRendererTest.php | 11 ++++++ tests/SvgRendererTest.php | 11 ++++++ .../081231723897-code128-red-background.html | 30 +++++++++++++++ .../081231723897-ean13-red-background.svg | 38 +++++++++++++++++++ 10 files changed, 167 insertions(+), 9 deletions(-) create mode 100644 tests/verified-files/081231723897-code128-red-background.html create mode 100644 tests/verified-files/081231723897-ean13-red-background.svg diff --git a/generate-verified-files.php b/generate-verified-files.php index 386da50..3d02f76 100644 --- a/generate-verified-files.php +++ b/generate-verified-files.php @@ -14,8 +14,15 @@ file_put_contents('tests/verified-files/081231723897-ean13.svg', $svgRenderer->render($barcode, $barcode->getWidth() * 2)); file_put_contents('tests/verified-files/081231723897-ean13-fractional-width.svg', $svgRenderer->render($barcode, $barcode->getWidth() * 0.25, 25.75)); +$svgRendererRed = new Picqer\Barcode\Renderers\SvgRenderer(); +$svgRendererRed->setBackgroundColor('red'); +file_put_contents('tests/verified-files/081231723897-ean13-red-background.svg', $svgRendererRed->render($barcode, $barcode->getWidth() * 2)); + $barcode = $typeEncoderCode128->getBarcode('081231723897'); file_put_contents('tests/verified-files/081231723897-code128.html', $htmlRenderer->render($barcode, $barcode->getWidth() * 2)); +$htmlRendererRed = new Picqer\Barcode\Renderers\HtmlRenderer(); +$htmlRendererRed->setBackgroundColor('red'); +file_put_contents('tests/verified-files/081231723897-code128-red-background.html', $htmlRendererRed->render($barcode, $barcode->getWidth() * 2)); $barcode = $typeEncoderIMB->getBarcode('12345678903'); file_put_contents('tests/verified-files/12345678903-imb.html', $htmlRenderer->render($barcode, $barcode->getWidth() * 2)); diff --git a/src/Renderers/DynamicHtmlRenderer.php b/src/Renderers/DynamicHtmlRenderer.php index 66a3e52..45500e5 100644 --- a/src/Renderers/DynamicHtmlRenderer.php +++ b/src/Renderers/DynamicHtmlRenderer.php @@ -10,10 +10,11 @@ class DynamicHtmlRenderer protected const WIDTH_PRECISION = 6; protected string $foregroundColor = 'black'; + protected ?string $backgroundColor = null; public function render(Barcode $barcode): string { - $html = '
    ' . PHP_EOL; + $html = '
    ' . PHP_EOL; $positionHorizontal = 0; /** @var BarcodeBar $bar */ @@ -36,10 +37,17 @@ public function render(Barcode $barcode): string return $html; } + // Use HTML color definitions, like 'red' or '#ff0000' public function setForegroundColor(string $color): self { $this->foregroundColor = $color; + return $this; + } + // Use HTML color definitions, like 'red' or '#ff0000' + public function setBackgroundColor(?string $color): self + { + $this->backgroundColor = $color; return $this; } } diff --git a/src/Renderers/HtmlRenderer.php b/src/Renderers/HtmlRenderer.php index 77ba389..fae8ffa 100644 --- a/src/Renderers/HtmlRenderer.php +++ b/src/Renderers/HtmlRenderer.php @@ -8,12 +8,13 @@ class HtmlRenderer { protected string $foregroundColor = 'black'; + protected ?string $backgroundColor = null; public function render(Barcode $barcode, float $width = 200, float $height = 30): string { $widthFactor = $width / $barcode->getWidth(); - $html = '
    ' . PHP_EOL; + $html = '
    ' . PHP_EOL; $positionHorizontal = 0; /** @var BarcodeBar $bar */ @@ -36,10 +37,17 @@ public function render(Barcode $barcode, float $width = 200, float $height = 30) return $html; } + // Use HTML color definitions, like 'red' or '#ff0000' public function setForegroundColor(string $color): self { $this->foregroundColor = $color; - + return $this; + } + + // Use HTML color definitions, like 'red' or '#ff0000' + public function setBackgroundColor(?string $color): self + { + $this->backgroundColor = $color; return $this; } } diff --git a/src/Renderers/JpgRenderer.php b/src/Renderers/JpgRenderer.php index 132ec10..1c835f7 100644 --- a/src/Renderers/JpgRenderer.php +++ b/src/Renderers/JpgRenderer.php @@ -9,7 +9,14 @@ class JpgRenderer extends PngRenderer protected function createImagickImageObject(int $width, int $height): Imagick { $image = new Imagick(); - $image->newImage($width, $height, 'none', 'JPG'); + if ($this->backgroundColor !== null) { + // Colored background + $backgroundColor = new ImagickPixel('rgb(' . implode(',', $this->backgroundColor) . ')'); + } else { + // Use transparent background + $backgroundColor = new ImagickPixel('none'); + } + $image->newImage($width, $height, $backgroundColor, 'JPG'); return $image; } diff --git a/src/Renderers/PngRenderer.php b/src/Renderers/PngRenderer.php index 8297d88..226a7b7 100644 --- a/src/Renderers/PngRenderer.php +++ b/src/Renderers/PngRenderer.php @@ -12,6 +12,8 @@ class PngRenderer { protected array $foregroundColor = [0, 0, 0]; + protected ?array $backgroundColor = null; + protected bool $useImagick; /** @@ -52,6 +54,7 @@ public function render(Barcode $barcode, int $widthFactor = 2, int $height = 30) $width = (int)round($barcode->getWidth() * $widthFactor); if ($this->useImagick) { + $image = $this->createImagickImageObject($width, $height); $imagickBarsShape = new ImagickDraw(); $imagickBarsShape->setFillColor(new ImagickPixel('rgb(' . implode(',', $this->foregroundColor) .')')); } else { @@ -80,7 +83,6 @@ public function render(Barcode $barcode, int $widthFactor = 2, int $height = 30) } if ($this->useImagick) { - $image = $this->createImagickImageObject($width, $height); $image->drawImage($imagickBarsShape); return $image->getImageBlob(); } else { @@ -90,18 +92,34 @@ public function render(Barcode $barcode, int $widthFactor = 2, int $height = 30) } } + // Use RGB color definitions, like [0, 0, 0] or [255, 255, 255] public function setForegroundColor(array $color): self { $this->foregroundColor = $color; - + return $this; + } + + // Use RGB color definitions, like [0, 0, 0] or [255, 255, 255] + // If no color is set, the background will be transparent + public function setBackgroundColor(?array $color): self + { + $this->backgroundColor = $color; return $this; } protected function createGdImageObject(int $width, int $height) { $image = \imagecreate($width, $height); - $colorBackground = \imagecolorallocate($image, 255, 255, 255); - \imagecolortransparent($image, $colorBackground); + + if ($this->backgroundColor !== null) { + // Colored background + $backgroundColor = \imagecolorallocate($image, $this->backgroundColor[0], $this->backgroundColor[1], $this->backgroundColor[2]); + \imagefill($image, 0, 0, $backgroundColor); + } else { + // Use transparent background + $backgroundColor = \imagecolorallocate($image, 255, 255, 255); + \imagecolortransparent($image, $backgroundColor); + } return $image; } @@ -109,7 +127,14 @@ protected function createGdImageObject(int $width, int $height) protected function createImagickImageObject(int $width, int $height): Imagick { $image = new Imagick(); - $image->newImage($width, $height, 'none', 'PNG'); + if ($this->backgroundColor !== null) { + // Colored background + $backgroundColor = new ImagickPixel('rgb(' . implode(',', $this->backgroundColor) . ')'); + } else { + // Use transparent background + $backgroundColor = new ImagickPixel('none'); + } + $image->newImage($width, $height, $backgroundColor, 'PNG'); return $image; } diff --git a/src/Renderers/SvgRenderer.php b/src/Renderers/SvgRenderer.php index 6af44b3..418333a 100644 --- a/src/Renderers/SvgRenderer.php +++ b/src/Renderers/SvgRenderer.php @@ -9,6 +9,7 @@ class SvgRenderer { protected string $foregroundColor = 'black'; + protected ?string $backgroundColor = null; protected string $svgType = self::TYPE_SVG_STANDALONE; public const TYPE_SVG_STANDALONE = 'standalone'; @@ -25,6 +26,12 @@ public function render(Barcode $barcode, float $width = 200, float $height = 30) } $svg .= '' . PHP_EOL; $svg .= "\t" . '' . htmlspecialchars($barcode->getBarcode()) . '' . PHP_EOL; + + // Add background rectangle if backgroundColor is set + if ($this->backgroundColor !== null) { + $svg .= "\t" . '' . PHP_EOL; + } + $svg .= "\t" . '' . PHP_EOL; // print bars @@ -55,6 +62,12 @@ public function setForegroundColor(string $color): self return $this; } + public function setBackgroundColor(?string $color): self + { + $this->backgroundColor = $color; + return $this; + } + public function setSvgType(string $svgType): self { if (! in_array($svgType, [self::TYPE_SVG_INLINE, self::TYPE_SVG_STANDALONE])) { diff --git a/tests/HtmlRendererTest.php b/tests/HtmlRendererTest.php index 803d2c0..387a43e 100644 --- a/tests/HtmlRendererTest.php +++ b/tests/HtmlRendererTest.php @@ -23,4 +23,15 @@ public function test_html_barcode_generator_can_generate_imb_barcode_to_test_hei $this->assertStringEqualsFile('tests/verified-files/12345678903-imb.html', $generated); } + + public function test_html_barcode_generator_with_background() + { + $barcode = (new Picqer\Barcode\Types\TypeCode128())->getBarcode('081231723897'); + + $renderer = new Picqer\Barcode\Renderers\HtmlRenderer(); + $renderer->setBackgroundColor('red'); + $generated = $renderer->render($barcode, $barcode->getWidth() * 2); + + $this->assertStringEqualsFile('tests/verified-files/081231723897-code128-red-background.html', $generated); + } } diff --git a/tests/SvgRendererTest.php b/tests/SvgRendererTest.php index aa1e97f..a8d73e6 100644 --- a/tests/SvgRendererTest.php +++ b/tests/SvgRendererTest.php @@ -56,4 +56,15 @@ public function test_svg_renderer_throws_exception_wrong_type() $renderer = new Picqer\Barcode\Renderers\SvgRenderer(); $renderer->setSvgType('other'); } + + public function test_svg_barcode_generator_can_use_background_color() + { + $barcode = (new Picqer\Barcode\Types\TypeEan13())->getBarcode('081231723897'); + + $renderer = new Picqer\Barcode\Renderers\SvgRenderer(); + $renderer->setBackgroundColor('red'); + $generated = $renderer->render($barcode, 190); + + $this->assertStringEqualsFile('tests/verified-files/081231723897-ean13-red-background.svg', $generated); + } } diff --git a/tests/verified-files/081231723897-code128-red-background.html b/tests/verified-files/081231723897-code128-red-background.html new file mode 100644 index 0000000..6023c22 --- /dev/null +++ b/tests/verified-files/081231723897-code128-red-background.html @@ -0,0 +1,30 @@ +
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
    diff --git a/tests/verified-files/081231723897-ean13-red-background.svg b/tests/verified-files/081231723897-ean13-red-background.svg new file mode 100644 index 0000000..406af57 --- /dev/null +++ b/tests/verified-files/081231723897-ean13-red-background.svg @@ -0,0 +1,38 @@ + + + + 0812317238973 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + From c48fd714d39cd6ebc7e9d4471e89c965093f3252 Mon Sep 17 00:00:00 2001 From: Casper Bakker Date: Sat, 21 Sep 2024 14:43:45 +0200 Subject: [PATCH 145/155] Fix import --- src/Renderers/JpgRenderer.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Renderers/JpgRenderer.php b/src/Renderers/JpgRenderer.php index 1c835f7..0b30226 100644 --- a/src/Renderers/JpgRenderer.php +++ b/src/Renderers/JpgRenderer.php @@ -3,6 +3,7 @@ namespace Picqer\Barcode\Renderers; use Imagick; +use ImagickPixel; class JpgRenderer extends PngRenderer { From 110b2ce1086fa427409f15cb586c000a0b763366 Mon Sep 17 00:00:00 2001 From: Casper Bakker Date: Sat, 21 Sep 2024 14:47:20 +0200 Subject: [PATCH 146/155] Docs --- Readme.md | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/Readme.md b/Readme.md index 2692cca..2a5b960 100644 --- a/Readme.md +++ b/Readme.md @@ -67,7 +67,8 @@ Each renderer has their own options. Only the barcode is required, the rest is o A vector based SVG image. Gives the best quality to print. ```php $renderer = new Picqer\Barcode\Renderers\SvgRenderer(); -$renderer->setForegroundColor('red'); // Give a color for the bars, the background is always white +$renderer->setForegroundColor('red'); // Give a color for the bars, default is black +$renderer->setBackgroundColor('blue'); // Give a color for the background, default is transparent $renderer->setSvgType($renderer::TYPE_SVG_INLINE); // Changes the output to be used inline inside HTML documents, instead of a standalone SVG image (default) $renderer->setSvgType($renderer::TYPE_SVG_STANDALONE); // If you want to force the default, create a stand alone SVG image @@ -78,7 +79,8 @@ $renderer->render($barcode, 450.20, 75); // Width and height support floats All options for PNG and JPG are the same. ```php $renderer = new Picqer\Barcode\Renderers\PngRenderer(); -$renderer->setForegroundColor([255, 0, 0]); // Give a color for the bars, the background is always white. Give it as 3 times 0-255 values for red, green and blue. +$renderer->setForegroundColor([255, 0, 0]); // Give a color for the bars, default is black. Give it as 3 times 0-255 values for red, green and blue. +$renderer->setBackgroundColor([0, 255, 255]); // Give a color for the background, default is transparent (in PNG) or white (in JPG). Give it as 3 times 0-255 values for red, green and blue. $renderer->useGd(); // If you have Imagick and GD installed, but want to use GD $renderer->useImagick(); // If you have Imagick and GD installed, but want to use Imagick @@ -89,7 +91,8 @@ $renderer->render($barcode, 5, 40); // Width factor (how many pixel wide every b Gives HTML to use inline in a full HTML document. ```php $renderer = new Picqer\Barcode\Renderers\HtmlRenderer(); -$renderer->setForegroundColor('red'); // Give a color for the bars, the background is always white +$renderer->setForegroundColor('red'); // Give a color for the bars, default is black +$renderer->setBackgroundColor('blue'); // Give a color for the background, default is transparent $renderer->render($barcode, 450.20, 75); // Width and height support floats ```` @@ -98,7 +101,8 @@ $renderer->render($barcode, 450.20, 75); // Width and height support floats Give HTML here the barcode is using the full width and height, to put inside a container/div that has a fixed size. ```php $renderer = new Picqer\Barcode\Renderers\DynamicHtmlRenderer(); -$renderer->setForegroundColor('red'); // Give a color for the bars, the background is always white +$renderer->setForegroundColor('red'); // Give a color for the bars, default is black +$renderer->setBackgroundColor('blue'); // Give a color for the background, default is transparent $renderer->render($barcode); ```` From 8fdf289416a520f746af3363335da5cbeac97a20 Mon Sep 17 00:00:00 2001 From: Casper Bakker Date: Sun, 22 Sep 2024 09:52:34 +0200 Subject: [PATCH 147/155] Add a RendererInterface --- generate-verified-files.php | 4 +- src/Renderers/DynamicHtmlRenderer.php | 19 ++- src/Renderers/HtmlRenderer.php | 16 +-- src/Renderers/PngRenderer.php | 18 ++- src/Renderers/RendererInterface.php | 14 ++ src/Renderers/SvgRenderer.php | 14 +- tests/verified-files/0049000004632-ean13.svg | 2 +- .../081231723897-code128-red-background.html | 58 ++++---- .../verified-files/081231723897-code128.html | 56 ++++---- .../081231723897-dynamic-code128.html | 56 ++++---- .../081231723897-ean13-fractional-width.svg | 2 +- .../081231723897-ean13-red-background.svg | 4 +- tests/verified-files/081231723897-ean13.svg | 2 +- .../12345678903-dynamic-imb.html | 130 +++++++++--------- tests/verified-files/12345678903-imb.html | 130 +++++++++--------- .../verified-files/TypeCodabar-123456789.svg | 2 +- tests/verified-files/TypeCode11-123456789.svg | 2 +- .../TypeCode128-081231723897.svg | 2 +- .../TypeCode128-1234567890abcABC-283-33.svg | 2 +- .../TypeCode128A-1234567890.svg | 2 +- .../TypeCode128B-081231723897.svg | 2 +- .../TypeCode128B-1234567890abcABC-283-33.svg | 2 +- .../TypeCode39-1234567890ABC.svg | 2 +- .../TypeCode39Checksum-1234567890ABC.svg | 2 +- .../TypeCode39Extended-1234567890abcABC.svg | 2 +- ...ode39ExtendedChecksum-1234567890abcABC.svg | 2 +- .../TypeCode93-1234567890abcABC.svg | 2 +- .../verified-files/TypeEan13-004900000463.svg | 2 +- .../TypeEan13-0049000004632.svg | 2 +- .../verified-files/TypeEan13-081231723897.svg | 2 +- tests/verified-files/TypeEan8-1234568.svg | 2 +- .../TypeITF14-00012345600012.svg | 2 +- .../TypeITF14-05400141288766.svg | 2 +- .../TypeIntelligentMailBarcode-123456789.svg | 2 +- .../TypeInterleaved25-1234567890.svg | 2 +- .../TypeInterleaved25Checksum-1234567890.svg | 2 +- tests/verified-files/TypeKix-123456789.svg | 2 +- tests/verified-files/TypeMsi-123456789.svg | 2 +- .../TypeMsiChecksum-123456789.svg | 2 +- .../TypePharmacode-123456789.svg | 2 +- .../TypePharmacodeTwoCode-123456789.svg | 2 +- tests/verified-files/TypePlanet-123456789.svg | 2 +- .../verified-files/TypePostnet-123456789.svg | 2 +- tests/verified-files/TypeRms4cc-123456789.svg | 2 +- .../TypeStandard2of5-1234567890.svg | 2 +- .../TypeStandard2of5Checksum-1234567890.svg | 2 +- .../TypeTelepen-1234567890ASCD.svg | 2 +- .../TypeTelepenNumeric-1234567890.svg | 2 +- tests/verified-files/TypeUpcA-123456789.svg | 2 +- tests/verified-files/TypeUpcE-123456789.svg | 2 +- tests/verified-files/TypeUpcExtension2-22.svg | 2 +- ...eUpcExtension5-1234567890abcABC-283-33.svg | 2 +- 52 files changed, 308 insertions(+), 291 deletions(-) create mode 100644 src/Renderers/RendererInterface.php diff --git a/generate-verified-files.php b/generate-verified-files.php index 3d02f76..d513546 100644 --- a/generate-verified-files.php +++ b/generate-verified-files.php @@ -15,13 +15,13 @@ file_put_contents('tests/verified-files/081231723897-ean13-fractional-width.svg', $svgRenderer->render($barcode, $barcode->getWidth() * 0.25, 25.75)); $svgRendererRed = new Picqer\Barcode\Renderers\SvgRenderer(); -$svgRendererRed->setBackgroundColor('red'); +$svgRendererRed->setBackgroundColor([255, 0, 0]); file_put_contents('tests/verified-files/081231723897-ean13-red-background.svg', $svgRendererRed->render($barcode, $barcode->getWidth() * 2)); $barcode = $typeEncoderCode128->getBarcode('081231723897'); file_put_contents('tests/verified-files/081231723897-code128.html', $htmlRenderer->render($barcode, $barcode->getWidth() * 2)); $htmlRendererRed = new Picqer\Barcode\Renderers\HtmlRenderer(); -$htmlRendererRed->setBackgroundColor('red'); +$htmlRendererRed->setBackgroundColor([255, 0, 0]); file_put_contents('tests/verified-files/081231723897-code128-red-background.html', $htmlRendererRed->render($barcode, $barcode->getWidth() * 2)); $barcode = $typeEncoderIMB->getBarcode('12345678903'); diff --git a/src/Renderers/DynamicHtmlRenderer.php b/src/Renderers/DynamicHtmlRenderer.php index 45500e5..c19de85 100644 --- a/src/Renderers/DynamicHtmlRenderer.php +++ b/src/Renderers/DynamicHtmlRenderer.php @@ -5,16 +5,17 @@ use Picqer\Barcode\Barcode; use Picqer\Barcode\BarcodeBar; -class DynamicHtmlRenderer +class DynamicHtmlRenderer implements RendererInterface { protected const WIDTH_PRECISION = 6; - protected string $foregroundColor = 'black'; - protected ?string $backgroundColor = null; + protected array $foregroundColor = [0, 0, 0]; + protected ?array $backgroundColor = null; - public function render(Barcode $barcode): string + // Width and height are ignored in this renderer + public function render(Barcode $barcode, float $width = 200, float $height = 30): string { - $html = '
    ' . PHP_EOL; + $html = '
    ' . PHP_EOL; $positionHorizontal = 0; /** @var BarcodeBar $bar */ @@ -26,7 +27,7 @@ public function render(Barcode $barcode): string $positionVertical = round(($bar->getPositionVertical() / $barcode->getHeight() * 100), 3); // draw a vertical bar - $html .= '
     
    ' . PHP_EOL; + $html .= '
     
    ' . PHP_EOL; } $positionHorizontal += $barWidth; @@ -37,15 +38,13 @@ public function render(Barcode $barcode): string return $html; } - // Use HTML color definitions, like 'red' or '#ff0000' - public function setForegroundColor(string $color): self + public function setForegroundColor(array $color): self { $this->foregroundColor = $color; return $this; } - // Use HTML color definitions, like 'red' or '#ff0000' - public function setBackgroundColor(?string $color): self + public function setBackgroundColor(?array $color): self { $this->backgroundColor = $color; return $this; diff --git a/src/Renderers/HtmlRenderer.php b/src/Renderers/HtmlRenderer.php index fae8ffa..9b93651 100644 --- a/src/Renderers/HtmlRenderer.php +++ b/src/Renderers/HtmlRenderer.php @@ -5,16 +5,16 @@ use Picqer\Barcode\Barcode; use Picqer\Barcode\BarcodeBar; -class HtmlRenderer +class HtmlRenderer implements RendererInterface { - protected string $foregroundColor = 'black'; - protected ?string $backgroundColor = null; + protected array $foregroundColor = [0, 0, 0]; + protected ?array $backgroundColor = null; public function render(Barcode $barcode, float $width = 200, float $height = 30): string { $widthFactor = $width / $barcode->getWidth(); - $html = '
    ' . PHP_EOL; + $html = '
    ' . PHP_EOL; $positionHorizontal = 0; /** @var BarcodeBar $bar */ @@ -26,7 +26,7 @@ public function render(Barcode $barcode, float $width = 200, float $height = 30) $positionVertical = round(($bar->getPositionVertical() * $height / $barcode->getHeight()), 3); // draw a vertical bar - $html .= '
     
    ' . PHP_EOL; + $html .= '
     
    ' . PHP_EOL; } $positionHorizontal += $barWidth; @@ -37,15 +37,13 @@ public function render(Barcode $barcode, float $width = 200, float $height = 30) return $html; } - // Use HTML color definitions, like 'red' or '#ff0000' - public function setForegroundColor(string $color): self + public function setForegroundColor(array $color): self { $this->foregroundColor = $color; return $this; } - // Use HTML color definitions, like 'red' or '#ff0000' - public function setBackgroundColor(?string $color): self + public function setBackgroundColor(?array $color): self { $this->backgroundColor = $color; return $this; diff --git a/src/Renderers/PngRenderer.php b/src/Renderers/PngRenderer.php index 226a7b7..4fd6e68 100644 --- a/src/Renderers/PngRenderer.php +++ b/src/Renderers/PngRenderer.php @@ -9,7 +9,7 @@ use Picqer\Barcode\BarcodeBar; use Picqer\Barcode\Exceptions\BarcodeException; -class PngRenderer +class PngRenderer implements RendererInterface { protected array $foregroundColor = [0, 0, 0]; protected ?array $backgroundColor = null; @@ -49,9 +49,15 @@ public function useGd(): self return $this; } - public function render(Barcode $barcode, int $widthFactor = 2, int $height = 30): string + // Floats in width and height will be rounded to integers + // For best (and valid) result, use a width as a factor of the width of the Barcode object + // Example: $width = $barcode->getWidth() * 3 + public function render(Barcode $barcode, float $width = 200, float $height = 30): string { - $width = (int)round($barcode->getWidth() * $widthFactor); + $width = round($width); + $height = round($height); + + $widthFactor = $width / $barcode->getWidth(); if ($this->useImagick) { $image = $this->createImagickImageObject($width, $height); @@ -66,7 +72,7 @@ public function render(Barcode $barcode, int $widthFactor = 2, int $height = 30) $positionHorizontal = 0; /** @var BarcodeBar $bar */ foreach ($barcode->getBars() as $bar) { - $barWidth = (int)round(($bar->getWidth() * $widthFactor)); + $barWidth = $bar->getWidth() * $widthFactor; if ($bar->isBar() && $barWidth > 0) { $y = (int)round(($bar->getPositionVertical() * $height / $barcode->getHeight())); @@ -74,9 +80,9 @@ public function render(Barcode $barcode, int $widthFactor = 2, int $height = 30) // draw a vertical bar if ($this->useImagick) { - $imagickBarsShape->rectangle($positionHorizontal, $y, ($positionHorizontal + $barWidth - 1), ($y + $barHeight)); + $imagickBarsShape->rectangle(round($positionHorizontal), $y, round($positionHorizontal + $barWidth - 1), ($y + $barHeight)); } else { - \imagefilledrectangle($image, $positionHorizontal, $y, ($positionHorizontal + $barWidth - 1), ($y + $barHeight), $gdForegroundColor); + \imagefilledrectangle($image, round($positionHorizontal), $y, round($positionHorizontal + $barWidth - 1), ($y + $barHeight), $gdForegroundColor); } } $positionHorizontal += $barWidth; diff --git a/src/Renderers/RendererInterface.php b/src/Renderers/RendererInterface.php new file mode 100644 index 0000000..eaba33d --- /dev/null +++ b/src/Renderers/RendererInterface.php @@ -0,0 +1,14 @@ +backgroundColor !== null) { - $svg .= "\t" . '' . PHP_EOL; + $svg .= "\t" . '' . PHP_EOL; } - $svg .= "\t" . '' . PHP_EOL; + $svg .= "\t" . '' . PHP_EOL; // print bars $positionHorizontal = 0; @@ -56,13 +56,13 @@ public function render(Barcode $barcode, float $width = 200, float $height = 30) return $svg; } - public function setForegroundColor(string $color): self + public function setForegroundColor(array $color): self { $this->foregroundColor = $color; return $this; } - public function setBackgroundColor(?string $color): self + public function setBackgroundColor(?array $color): self { $this->backgroundColor = $color; return $this; diff --git a/tests/verified-files/0049000004632-ean13.svg b/tests/verified-files/0049000004632-ean13.svg index 427ff40..5c46159 100644 --- a/tests/verified-files/0049000004632-ean13.svg +++ b/tests/verified-files/0049000004632-ean13.svg @@ -2,7 +2,7 @@ 0049000004632 - + diff --git a/tests/verified-files/081231723897-code128-red-background.html b/tests/verified-files/081231723897-code128-red-background.html index 6023c22..45f6b31 100644 --- a/tests/verified-files/081231723897-code128-red-background.html +++ b/tests/verified-files/081231723897-code128-red-background.html @@ -1,30 +1,30 @@ -
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    +
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    diff --git a/tests/verified-files/081231723897-code128.html b/tests/verified-files/081231723897-code128.html index e9cf37a..7b3f9f2 100644 --- a/tests/verified-files/081231723897-code128.html +++ b/tests/verified-files/081231723897-code128.html @@ -1,30 +1,30 @@
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    diff --git a/tests/verified-files/081231723897-dynamic-code128.html b/tests/verified-files/081231723897-dynamic-code128.html index cdf2d94..6d88107 100644 --- a/tests/verified-files/081231723897-dynamic-code128.html +++ b/tests/verified-files/081231723897-dynamic-code128.html @@ -1,30 +1,30 @@
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    diff --git a/tests/verified-files/081231723897-ean13-fractional-width.svg b/tests/verified-files/081231723897-ean13-fractional-width.svg index d616490..2ff2967 100644 --- a/tests/verified-files/081231723897-ean13-fractional-width.svg +++ b/tests/verified-files/081231723897-ean13-fractional-width.svg @@ -2,7 +2,7 @@ 0812317238973 - + diff --git a/tests/verified-files/081231723897-ean13-red-background.svg b/tests/verified-files/081231723897-ean13-red-background.svg index 406af57..52bad4d 100644 --- a/tests/verified-files/081231723897-ean13-red-background.svg +++ b/tests/verified-files/081231723897-ean13-red-background.svg @@ -2,8 +2,8 @@ 0812317238973 - - + + diff --git a/tests/verified-files/081231723897-ean13.svg b/tests/verified-files/081231723897-ean13.svg index 96e1619..a396439 100644 --- a/tests/verified-files/081231723897-ean13.svg +++ b/tests/verified-files/081231723897-ean13.svg @@ -2,7 +2,7 @@ 0812317238973 - + diff --git a/tests/verified-files/12345678903-dynamic-imb.html b/tests/verified-files/12345678903-dynamic-imb.html index c06f1fb..7142156 100644 --- a/tests/verified-files/12345678903-dynamic-imb.html +++ b/tests/verified-files/12345678903-dynamic-imb.html @@ -1,67 +1,67 @@
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    diff --git a/tests/verified-files/12345678903-imb.html b/tests/verified-files/12345678903-imb.html index eba3400..7620abc 100644 --- a/tests/verified-files/12345678903-imb.html +++ b/tests/verified-files/12345678903-imb.html @@ -1,67 +1,67 @@
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    -
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    +
     
    diff --git a/tests/verified-files/TypeCodabar-123456789.svg b/tests/verified-files/TypeCodabar-123456789.svg index 0d9606a..0bbe6d1 100644 --- a/tests/verified-files/TypeCodabar-123456789.svg +++ b/tests/verified-files/TypeCodabar-123456789.svg @@ -2,7 +2,7 @@ 123456789 - + diff --git a/tests/verified-files/TypeCode11-123456789.svg b/tests/verified-files/TypeCode11-123456789.svg index f30d919..8c8eb3f 100644 --- a/tests/verified-files/TypeCode11-123456789.svg +++ b/tests/verified-files/TypeCode11-123456789.svg @@ -2,7 +2,7 @@ 123456789 - + diff --git a/tests/verified-files/TypeCode128-081231723897.svg b/tests/verified-files/TypeCode128-081231723897.svg index 64b544f..d7267ab 100644 --- a/tests/verified-files/TypeCode128-081231723897.svg +++ b/tests/verified-files/TypeCode128-081231723897.svg @@ -2,7 +2,7 @@ 081231723897 - + diff --git a/tests/verified-files/TypeCode128-1234567890abcABC-283-33.svg b/tests/verified-files/TypeCode128-1234567890abcABC-283-33.svg index 4a3708a..32b8c1f 100644 --- a/tests/verified-files/TypeCode128-1234567890abcABC-283-33.svg +++ b/tests/verified-files/TypeCode128-1234567890abcABC-283-33.svg @@ -2,7 +2,7 @@ 1234567890abcABC-283*33 - + diff --git a/tests/verified-files/TypeCode128A-1234567890.svg b/tests/verified-files/TypeCode128A-1234567890.svg index 22d2cf4..9964f3d 100644 --- a/tests/verified-files/TypeCode128A-1234567890.svg +++ b/tests/verified-files/TypeCode128A-1234567890.svg @@ -2,7 +2,7 @@ 1234567890 - + diff --git a/tests/verified-files/TypeCode128B-081231723897.svg b/tests/verified-files/TypeCode128B-081231723897.svg index f3ee4e0..99133e9 100644 --- a/tests/verified-files/TypeCode128B-081231723897.svg +++ b/tests/verified-files/TypeCode128B-081231723897.svg @@ -2,7 +2,7 @@ 081231723897 - + diff --git a/tests/verified-files/TypeCode128B-1234567890abcABC-283-33.svg b/tests/verified-files/TypeCode128B-1234567890abcABC-283-33.svg index ec25033..9d79a94 100644 --- a/tests/verified-files/TypeCode128B-1234567890abcABC-283-33.svg +++ b/tests/verified-files/TypeCode128B-1234567890abcABC-283-33.svg @@ -2,7 +2,7 @@ 1234567890abcABC-283*33 - + diff --git a/tests/verified-files/TypeCode39-1234567890ABC.svg b/tests/verified-files/TypeCode39-1234567890ABC.svg index 448792d..a028564 100644 --- a/tests/verified-files/TypeCode39-1234567890ABC.svg +++ b/tests/verified-files/TypeCode39-1234567890ABC.svg @@ -2,7 +2,7 @@ *1234567890ABC* - + diff --git a/tests/verified-files/TypeCode39Checksum-1234567890ABC.svg b/tests/verified-files/TypeCode39Checksum-1234567890ABC.svg index 280551a..00a99a7 100644 --- a/tests/verified-files/TypeCode39Checksum-1234567890ABC.svg +++ b/tests/verified-files/TypeCode39Checksum-1234567890ABC.svg @@ -2,7 +2,7 @@ *1234567890ABCZ* - + diff --git a/tests/verified-files/TypeCode39Extended-1234567890abcABC.svg b/tests/verified-files/TypeCode39Extended-1234567890abcABC.svg index d85cc71..39fc3e3 100644 --- a/tests/verified-files/TypeCode39Extended-1234567890abcABC.svg +++ b/tests/verified-files/TypeCode39Extended-1234567890abcABC.svg @@ -2,7 +2,7 @@ *1234567890+A+B+CABC* - + diff --git a/tests/verified-files/TypeCode39ExtendedChecksum-1234567890abcABC.svg b/tests/verified-files/TypeCode39ExtendedChecksum-1234567890abcABC.svg index a1a87c5..a6880d6 100644 --- a/tests/verified-files/TypeCode39ExtendedChecksum-1234567890abcABC.svg +++ b/tests/verified-files/TypeCode39ExtendedChecksum-1234567890abcABC.svg @@ -2,7 +2,7 @@ *1234567890+A+B+CABCJ* - + diff --git a/tests/verified-files/TypeCode93-1234567890abcABC.svg b/tests/verified-files/TypeCode93-1234567890abcABC.svg index 120c36f..efa0b6e 100644 --- a/tests/verified-files/TypeCode93-1234567890abcABC.svg +++ b/tests/verified-files/TypeCode93-1234567890abcABC.svg @@ -2,7 +2,7 @@ *1234567890dAdBdCABC6-* - + diff --git a/tests/verified-files/TypeEan13-004900000463.svg b/tests/verified-files/TypeEan13-004900000463.svg index 427ff40..5c46159 100644 --- a/tests/verified-files/TypeEan13-004900000463.svg +++ b/tests/verified-files/TypeEan13-004900000463.svg @@ -2,7 +2,7 @@ 0049000004632 - + diff --git a/tests/verified-files/TypeEan13-0049000004632.svg b/tests/verified-files/TypeEan13-0049000004632.svg index 427ff40..5c46159 100644 --- a/tests/verified-files/TypeEan13-0049000004632.svg +++ b/tests/verified-files/TypeEan13-0049000004632.svg @@ -2,7 +2,7 @@ 0049000004632 - + diff --git a/tests/verified-files/TypeEan13-081231723897.svg b/tests/verified-files/TypeEan13-081231723897.svg index 96e1619..a396439 100644 --- a/tests/verified-files/TypeEan13-081231723897.svg +++ b/tests/verified-files/TypeEan13-081231723897.svg @@ -2,7 +2,7 @@ 0812317238973 - + diff --git a/tests/verified-files/TypeEan8-1234568.svg b/tests/verified-files/TypeEan8-1234568.svg index 9c4e210..2dc590c 100644 --- a/tests/verified-files/TypeEan8-1234568.svg +++ b/tests/verified-files/TypeEan8-1234568.svg @@ -2,7 +2,7 @@ 12345687 - + diff --git a/tests/verified-files/TypeITF14-00012345600012.svg b/tests/verified-files/TypeITF14-00012345600012.svg index 25553f0..c2a317b 100644 --- a/tests/verified-files/TypeITF14-00012345600012.svg +++ b/tests/verified-files/TypeITF14-00012345600012.svg @@ -2,7 +2,7 @@ 00012345600012 - + diff --git a/tests/verified-files/TypeITF14-05400141288766.svg b/tests/verified-files/TypeITF14-05400141288766.svg index ece67b7..dd1a0b9 100644 --- a/tests/verified-files/TypeITF14-05400141288766.svg +++ b/tests/verified-files/TypeITF14-05400141288766.svg @@ -2,7 +2,7 @@ 05400141288766 - + diff --git a/tests/verified-files/TypeIntelligentMailBarcode-123456789.svg b/tests/verified-files/TypeIntelligentMailBarcode-123456789.svg index a82dcd9..8637b41 100644 --- a/tests/verified-files/TypeIntelligentMailBarcode-123456789.svg +++ b/tests/verified-files/TypeIntelligentMailBarcode-123456789.svg @@ -2,7 +2,7 @@ 123456789 - + diff --git a/tests/verified-files/TypeInterleaved25-1234567890.svg b/tests/verified-files/TypeInterleaved25-1234567890.svg index 77af1b4..3d0c1ed 100644 --- a/tests/verified-files/TypeInterleaved25-1234567890.svg +++ b/tests/verified-files/TypeInterleaved25-1234567890.svg @@ -2,7 +2,7 @@ AA1234567890ZA - + diff --git a/tests/verified-files/TypeInterleaved25Checksum-1234567890.svg b/tests/verified-files/TypeInterleaved25Checksum-1234567890.svg index 55f32b0..394bf7d 100644 --- a/tests/verified-files/TypeInterleaved25Checksum-1234567890.svg +++ b/tests/verified-files/TypeInterleaved25Checksum-1234567890.svg @@ -2,7 +2,7 @@ AA012345678905ZA - + diff --git a/tests/verified-files/TypeKix-123456789.svg b/tests/verified-files/TypeKix-123456789.svg index 0c05177..953a330 100644 --- a/tests/verified-files/TypeKix-123456789.svg +++ b/tests/verified-files/TypeKix-123456789.svg @@ -2,7 +2,7 @@ 123456789 - + diff --git a/tests/verified-files/TypeMsi-123456789.svg b/tests/verified-files/TypeMsi-123456789.svg index 89c0090..a48095e 100644 --- a/tests/verified-files/TypeMsi-123456789.svg +++ b/tests/verified-files/TypeMsi-123456789.svg @@ -2,7 +2,7 @@ 123456789 - + diff --git a/tests/verified-files/TypeMsiChecksum-123456789.svg b/tests/verified-files/TypeMsiChecksum-123456789.svg index de5eae8..6cd6062 100644 --- a/tests/verified-files/TypeMsiChecksum-123456789.svg +++ b/tests/verified-files/TypeMsiChecksum-123456789.svg @@ -2,7 +2,7 @@ 1234567892 - + diff --git a/tests/verified-files/TypePharmacode-123456789.svg b/tests/verified-files/TypePharmacode-123456789.svg index 46044b0..3dac6de 100644 --- a/tests/verified-files/TypePharmacode-123456789.svg +++ b/tests/verified-files/TypePharmacode-123456789.svg @@ -2,7 +2,7 @@ 123456789 - + diff --git a/tests/verified-files/TypePharmacodeTwoCode-123456789.svg b/tests/verified-files/TypePharmacodeTwoCode-123456789.svg index 39975d4..6a409d3 100644 --- a/tests/verified-files/TypePharmacodeTwoCode-123456789.svg +++ b/tests/verified-files/TypePharmacodeTwoCode-123456789.svg @@ -2,7 +2,7 @@ 123456789 - + diff --git a/tests/verified-files/TypePlanet-123456789.svg b/tests/verified-files/TypePlanet-123456789.svg index 51de3aa..a6a3fe8 100644 --- a/tests/verified-files/TypePlanet-123456789.svg +++ b/tests/verified-files/TypePlanet-123456789.svg @@ -2,7 +2,7 @@ 123456789 - + diff --git a/tests/verified-files/TypePostnet-123456789.svg b/tests/verified-files/TypePostnet-123456789.svg index ca895ec..5cefece 100644 --- a/tests/verified-files/TypePostnet-123456789.svg +++ b/tests/verified-files/TypePostnet-123456789.svg @@ -2,7 +2,7 @@ 123456789 - + diff --git a/tests/verified-files/TypeRms4cc-123456789.svg b/tests/verified-files/TypeRms4cc-123456789.svg index ee91be7..5e7c698 100644 --- a/tests/verified-files/TypeRms4cc-123456789.svg +++ b/tests/verified-files/TypeRms4cc-123456789.svg @@ -2,7 +2,7 @@ 123456789 - + diff --git a/tests/verified-files/TypeStandard2of5-1234567890.svg b/tests/verified-files/TypeStandard2of5-1234567890.svg index 26d48c5..8aee635 100644 --- a/tests/verified-files/TypeStandard2of5-1234567890.svg +++ b/tests/verified-files/TypeStandard2of5-1234567890.svg @@ -2,7 +2,7 @@ 1234567890 - + diff --git a/tests/verified-files/TypeStandard2of5Checksum-1234567890.svg b/tests/verified-files/TypeStandard2of5Checksum-1234567890.svg index dcdc32a..650cc9f 100644 --- a/tests/verified-files/TypeStandard2of5Checksum-1234567890.svg +++ b/tests/verified-files/TypeStandard2of5Checksum-1234567890.svg @@ -2,7 +2,7 @@ 12345678905 - + diff --git a/tests/verified-files/TypeTelepen-1234567890ASCD.svg b/tests/verified-files/TypeTelepen-1234567890ASCD.svg index d44f436..22429b2 100644 --- a/tests/verified-files/TypeTelepen-1234567890ASCD.svg +++ b/tests/verified-files/TypeTelepen-1234567890ASCD.svg @@ -2,7 +2,7 @@ 1234567890ASCD - + diff --git a/tests/verified-files/TypeTelepenNumeric-1234567890.svg b/tests/verified-files/TypeTelepenNumeric-1234567890.svg index 079766b..34c4479 100644 --- a/tests/verified-files/TypeTelepenNumeric-1234567890.svg +++ b/tests/verified-files/TypeTelepenNumeric-1234567890.svg @@ -2,7 +2,7 @@ 1234567890 - + diff --git a/tests/verified-files/TypeUpcA-123456789.svg b/tests/verified-files/TypeUpcA-123456789.svg index d7ed6dc..38b420c 100644 --- a/tests/verified-files/TypeUpcA-123456789.svg +++ b/tests/verified-files/TypeUpcA-123456789.svg @@ -2,7 +2,7 @@ 0001234567895 - + diff --git a/tests/verified-files/TypeUpcE-123456789.svg b/tests/verified-files/TypeUpcE-123456789.svg index 4347e92..4333be7 100644 --- a/tests/verified-files/TypeUpcE-123456789.svg +++ b/tests/verified-files/TypeUpcE-123456789.svg @@ -2,7 +2,7 @@ 012349 - + diff --git a/tests/verified-files/TypeUpcExtension2-22.svg b/tests/verified-files/TypeUpcExtension2-22.svg index d7f285d..1fb84e7 100644 --- a/tests/verified-files/TypeUpcExtension2-22.svg +++ b/tests/verified-files/TypeUpcExtension2-22.svg @@ -2,7 +2,7 @@ 22 - + diff --git a/tests/verified-files/TypeUpcExtension5-1234567890abcABC-283-33.svg b/tests/verified-files/TypeUpcExtension5-1234567890abcABC-283-33.svg index df18eae..a4287a5 100644 --- a/tests/verified-files/TypeUpcExtension5-1234567890abcABC-283-33.svg +++ b/tests/verified-files/TypeUpcExtension5-1234567890abcABC-283-33.svg @@ -2,7 +2,7 @@ 1234567890abcABC-283*33 - + From 1a6071ef5fd74247e6f5a6035e8ab35961430185 Mon Sep 17 00:00:00 2001 From: Casper Bakker Date: Tue, 1 Oct 2024 21:10:45 +0200 Subject: [PATCH 148/155] Add ColorHelper to convert textual colors --- src/BarcodeGeneratorDynamicHTML.php | 3 +- src/BarcodeGeneratorHTML.php | 3 +- src/BarcodeGeneratorJPG.php | 2 +- src/BarcodeGeneratorPNG.php | 2 +- src/BarcodeGeneratorSVG.php | 3 +- src/Exceptions/UnknownColorException.php | 5 +++ src/Helpers/ColorHelper.php | 46 ++++++++++++++++++++++++ tests/HtmlRendererTest.php | 2 +- tests/JpgRendererTest.php | 16 ++++----- tests/PngRendererTest.php | 14 ++++---- tests/SvgRendererTest.php | 2 +- 11 files changed, 76 insertions(+), 22 deletions(-) create mode 100644 src/Exceptions/UnknownColorException.php create mode 100644 src/Helpers/ColorHelper.php diff --git a/src/BarcodeGeneratorDynamicHTML.php b/src/BarcodeGeneratorDynamicHTML.php index c08a4ed..5cebc2b 100644 --- a/src/BarcodeGeneratorDynamicHTML.php +++ b/src/BarcodeGeneratorDynamicHTML.php @@ -3,6 +3,7 @@ namespace Picqer\Barcode; use Picqer\Barcode\Exceptions\UnknownTypeException; +use Picqer\Barcode\Helpers\ColorHelper; class BarcodeGeneratorDynamicHTML extends BarcodeGenerator { @@ -21,7 +22,7 @@ public function getBarcode(string $barcode, $type, string $foregroundColor = 'bl $barcodeData = $this->getBarcodeData($barcode, $type); $renderer = new \Picqer\Barcode\Renderers\DynamicHtmlRenderer(); - $renderer->setForegroundColor($foregroundColor); + $renderer->setForegroundColor(ColorHelper::getArrayFromColorString($foregroundColor)); return $renderer->render($barcodeData); } diff --git a/src/BarcodeGeneratorHTML.php b/src/BarcodeGeneratorHTML.php index bc30f25..01b8b95 100644 --- a/src/BarcodeGeneratorHTML.php +++ b/src/BarcodeGeneratorHTML.php @@ -3,6 +3,7 @@ namespace Picqer\Barcode; use Picqer\Barcode\Exceptions\UnknownTypeException; +use Picqer\Barcode\Helpers\ColorHelper; class BarcodeGeneratorHTML extends BarcodeGenerator { @@ -25,7 +26,7 @@ public function getBarcode(string $barcode, $type, int $widthFactor = 2, int $he $width = round(($barcodeData->getWidth() * $widthFactor), 3); $renderer = new \Picqer\Barcode\Renderers\HtmlRenderer(); - $renderer->setForegroundColor($foregroundColor); + $renderer->setForegroundColor(ColorHelper::getArrayFromColorString($foregroundColor)); return $renderer->render($barcodeData, $width, $height); } diff --git a/src/BarcodeGeneratorJPG.php b/src/BarcodeGeneratorJPG.php index 2e27001..51eaf48 100644 --- a/src/BarcodeGeneratorJPG.php +++ b/src/BarcodeGeneratorJPG.php @@ -34,7 +34,7 @@ public function getBarcode(string $barcode, $type, int $widthFactor = 2, int $he } } - return $renderer->render($barcodeData, $widthFactor, $height); + return $renderer->render($barcodeData, $barcodeData->getWidth() * $widthFactor, $height); } /** diff --git a/src/BarcodeGeneratorPNG.php b/src/BarcodeGeneratorPNG.php index 31edc56..f8f3d0c 100644 --- a/src/BarcodeGeneratorPNG.php +++ b/src/BarcodeGeneratorPNG.php @@ -34,7 +34,7 @@ public function getBarcode(string $barcode, $type, int $widthFactor = 2, int $he } } - return $renderer->render($barcodeData, $widthFactor, $height); + return $renderer->render($barcodeData, $barcodeData->getWidth() * $widthFactor, $height); } /** diff --git a/src/BarcodeGeneratorSVG.php b/src/BarcodeGeneratorSVG.php index ef7d63f..64b725e 100644 --- a/src/BarcodeGeneratorSVG.php +++ b/src/BarcodeGeneratorSVG.php @@ -3,6 +3,7 @@ namespace Picqer\Barcode; use Picqer\Barcode\Exceptions\UnknownTypeException; +use Picqer\Barcode\Helpers\ColorHelper; class BarcodeGeneratorSVG extends BarcodeGenerator { @@ -25,7 +26,7 @@ public function getBarcode(string $barcode, $type, float $widthFactor = 2, float $width = round(($barcodeData->getWidth() * $widthFactor), 3); $renderer = new \Picqer\Barcode\Renderers\SvgRenderer(); - $renderer->setForegroundColor($foregroundColor); + $renderer->setForegroundColor(ColorHelper::getArrayFromColorString($foregroundColor)); return $renderer->render($barcodeData, $width, $height); } diff --git a/src/Exceptions/UnknownColorException.php b/src/Exceptions/UnknownColorException.php new file mode 100644 index 0000000..0401857 --- /dev/null +++ b/src/Exceptions/UnknownColorException.php @@ -0,0 +1,5 @@ +getBarcode('081231723897'); $renderer = new Picqer\Barcode\Renderers\HtmlRenderer(); - $renderer->setBackgroundColor('red'); + $renderer->setBackgroundColor([255, 0, 0]); $generated = $renderer->render($barcode, $barcode->getWidth() * 2); $this->assertStringEqualsFile('tests/verified-files/081231723897-code128-red-background.html', $generated); diff --git a/tests/JpgRendererTest.php b/tests/JpgRendererTest.php index 46aa939..c6ca4c6 100644 --- a/tests/JpgRendererTest.php +++ b/tests/JpgRendererTest.php @@ -10,7 +10,7 @@ public function test_jpg_barcode_generator_can_generate_code_128_barcode() $renderer = new Picqer\Barcode\Renderers\JpgRenderer(); $renderer->useGd(); - $generated = $renderer->render($barcode); + $generated = $renderer->render($barcode, $barcode->getWidth() * 2); $imageInfo = getimagesizefromstring($generated); @@ -27,7 +27,7 @@ public function test_jpg_barcode_generator_can_generate_code_39_barcode() $renderer = new Picqer\Barcode\Renderers\JpgRenderer(); $renderer->useGd(); - $generated = $renderer->render($barcode, 1); + $generated = $renderer->render($barcode, $barcode->getWidth()); $imageInfo = getimagesizefromstring($generated); @@ -43,7 +43,7 @@ public function test_jpg_barcode_generator_can_use_different_height() $renderer = new Picqer\Barcode\Renderers\JpgRenderer(); $renderer->useGd(); - $generated = $renderer->render($barcode, 2, 45); + $generated = $renderer->render($barcode, $barcode->getWidth() * 2, 45); $imageInfo = getimagesizefromstring($generated); @@ -59,7 +59,7 @@ public function test_jpg_barcode_generator_can_use_different_width_factor() $renderer = new Picqer\Barcode\Renderers\JpgRenderer(); $renderer->useGd(); - $generated = $renderer->render($barcode, 5); + $generated = $renderer->render($barcode, $barcode->getWidth() * 5); $imageInfo = getimagesizefromstring($generated); @@ -81,7 +81,7 @@ public function test_jpg_barcode_generator_can_generate_code_128_barcode_imagick $renderer = new Picqer\Barcode\Renderers\JpgRenderer(); $renderer->useImagick(); - $generated = $renderer->render($barcode); + $generated = $renderer->render($barcode, $barcode->getWidth() * 2); $imageInfo = getimagesizefromstring($generated); @@ -101,7 +101,7 @@ public function test_jpg_barcode_generator_can_generate_code_39_barcode_imagick( $renderer = new Picqer\Barcode\Renderers\JpgRenderer(); $renderer->useImagick(); - $generated = $renderer->render($barcode, 1); + $generated = $renderer->render($barcode, $barcode->getWidth()); $imageInfo = getimagesizefromstring($generated); @@ -121,7 +121,7 @@ public function test_jpg_barcode_generator_can_use_different_height_imagick() $renderer = new Picqer\Barcode\Renderers\JpgRenderer(); $renderer->useGd(); - $generated = $renderer->render($barcode, 2, 45); + $generated = $renderer->render($barcode, $barcode->getWidth() * 2, 45); $imageInfo = getimagesizefromstring($generated); @@ -141,7 +141,7 @@ public function test_jpg_barcode_generator_can_use_different_width_factor_imagic $renderer = new Picqer\Barcode\Renderers\JpgRenderer(); $renderer->useGd(); - $generated = $renderer->render($barcode, 5); + $generated = $renderer->render($barcode, $barcode->getWidth() * 5); $imageInfo = getimagesizefromstring($generated); diff --git a/tests/PngRendererTest.php b/tests/PngRendererTest.php index db253ce..fbb3e3c 100644 --- a/tests/PngRendererTest.php +++ b/tests/PngRendererTest.php @@ -22,7 +22,7 @@ public function test_png_barcode_generator_can_generate_code_39_barcode() $renderer = new Picqer\Barcode\Renderers\PngRenderer(); $renderer->useGd(); - $generated = $renderer->render($barcode, 1); + $generated = $renderer->render($barcode, $barcode->getWidth()); $imageInfo = getimagesizefromstring($generated); @@ -38,7 +38,7 @@ public function test_png_barcode_generator_can_use_different_height() $renderer = new Picqer\Barcode\Renderers\PngRenderer(); $renderer->useGd(); - $generated = $renderer->render($barcode, 2, 45); + $generated = $renderer->render($barcode, $barcode->getWidth() * 2, 45); $imageInfo = getimagesizefromstring($generated); @@ -54,7 +54,7 @@ public function test_png_barcode_generator_can_use_different_width_factor() $renderer = new Picqer\Barcode\Renderers\PngRenderer(); $renderer->useGd(); - $generated = $renderer->render($barcode, 5); + $generated = $renderer->render($barcode, $barcode->getWidth() * 5); $imageInfo = getimagesizefromstring($generated); @@ -76,7 +76,7 @@ public function test_png_barcode_generator_can_generate_code_128_barcode_imagick $renderer = new Picqer\Barcode\Renderers\PngRenderer(); $renderer->useImagick(); - $generated = $renderer->render($barcode); + $generated = $renderer->render($barcode, $barcode->getWidth() * 2); $this->assertEquals('PNG', substr($generated, 1, 3)); } @@ -91,7 +91,7 @@ public function test_png_barcode_generator_can_generate_code_39_barcode_imagick( $renderer = new Picqer\Barcode\Renderers\PngRenderer(); $renderer->useImagick(); - $generated = $renderer->render($barcode, 1); + $generated = $renderer->render($barcode, $barcode->getWidth()); $imageInfo = getimagesizefromstring($generated); @@ -111,7 +111,7 @@ public function test_png_barcode_generator_can_use_different_height_imagick() $renderer = new Picqer\Barcode\Renderers\PngRenderer(); $renderer->useGd(); - $generated = $renderer->render($barcode, 2, 45); + $generated = $renderer->render($barcode, $barcode->getWidth() * 2, 45); $imageInfo = getimagesizefromstring($generated); @@ -131,7 +131,7 @@ public function test_png_barcode_generator_can_use_different_width_factor_imagic $renderer = new Picqer\Barcode\Renderers\PngRenderer(); $renderer->useGd(); - $generated = $renderer->render($barcode, 5); + $generated = $renderer->render($barcode, $barcode->getWidth() * 5); $imageInfo = getimagesizefromstring($generated); diff --git a/tests/SvgRendererTest.php b/tests/SvgRendererTest.php index a8d73e6..984d034 100644 --- a/tests/SvgRendererTest.php +++ b/tests/SvgRendererTest.php @@ -62,7 +62,7 @@ public function test_svg_barcode_generator_can_use_background_color() $barcode = (new Picqer\Barcode\Types\TypeEan13())->getBarcode('081231723897'); $renderer = new Picqer\Barcode\Renderers\SvgRenderer(); - $renderer->setBackgroundColor('red'); + $renderer->setBackgroundColor([255, 0, 0]); $generated = $renderer->render($barcode, 190); $this->assertStringEqualsFile('tests/verified-files/081231723897-ean13-red-background.svg', $generated); From 40cfc3f649dbff34c078f05148e4a0704940d3fa Mon Sep 17 00:00:00 2001 From: Casper Bakker Date: Tue, 1 Oct 2024 21:28:12 +0200 Subject: [PATCH 149/155] Fix phpstan --- src/Renderers/PngRenderer.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Renderers/PngRenderer.php b/src/Renderers/PngRenderer.php index 4fd6e68..72c2909 100644 --- a/src/Renderers/PngRenderer.php +++ b/src/Renderers/PngRenderer.php @@ -54,8 +54,8 @@ public function useGd(): self // Example: $width = $barcode->getWidth() * 3 public function render(Barcode $barcode, float $width = 200, float $height = 30): string { - $width = round($width); - $height = round($height); + $width = (int)round($width); + $height = (int)round($height); $widthFactor = $width / $barcode->getWidth(); @@ -80,9 +80,9 @@ public function render(Barcode $barcode, float $width = 200, float $height = 30) // draw a vertical bar if ($this->useImagick) { - $imagickBarsShape->rectangle(round($positionHorizontal), $y, round($positionHorizontal + $barWidth - 1), ($y + $barHeight)); + $imagickBarsShape->rectangle((int)round($positionHorizontal), $y, (int)round($positionHorizontal + $barWidth - 1), ($y + $barHeight)); } else { - \imagefilledrectangle($image, round($positionHorizontal), $y, round($positionHorizontal + $barWidth - 1), ($y + $barHeight), $gdForegroundColor); + \imagefilledrectangle($image, (int)round($positionHorizontal), $y, (int)round($positionHorizontal + $barWidth - 1), ($y + $barHeight), $gdForegroundColor); } } $positionHorizontal += $barWidth; From d06a28f78d8810246795f19068bdc42215f1a6eb Mon Sep 17 00:00:00 2001 From: Casper Bakker Date: Tue, 1 Oct 2024 21:28:35 +0200 Subject: [PATCH 150/155] Add new interface usage to readme --- Readme.md | 35 ++++++++++++++++++++++------------- 1 file changed, 22 insertions(+), 13 deletions(-) diff --git a/Readme.md b/Readme.md index 2a5b960..99652ca 100644 --- a/Readme.md +++ b/Readme.md @@ -18,7 +18,7 @@ Install through [composer](https://getcomposer.org/doc/00-intro.md): composer require picqer/php-barcode-generator ``` -If you want to generate PNG or JPG images, you need the GD library or Imagick installed on your system as well. +If you want to generate PNG or JPG images, you need the GD library or Imagick installed on your system as well. For SVG or HTML renders, there are no dependencies. ## Usage You want a barcode for a specific "type" (for example Code 128 or UPC) in a specific image format (for example PNG or SVG). @@ -54,21 +54,28 @@ $barcode = (new Picqer\Barcode\Types\TypeCode128())->getBarcode('081231723897'); $renderer = new Picqer\Barcode\Renderers\PngRenderer(); $renderer->setForegroundColor($colorRed); -// Save PNG to the filesystem, with widthFactor 3 and height of 50 pixels -file_put_contents('barcode.png', $renderer->render($barcode, 3, 50)); +// Save PNG to the filesystem, with widthFactor 3 (width of the barcode x 3) and height of 50 pixels +file_put_contents('barcode.png', $renderer->render($barcode, $barcode->getWidth() * 3, 50)); ``` ## Image renderers Available image renderers: SVG, PNG, JPG and HTML. -Each renderer has their own options. Only the barcode is required, the rest is optional. Here are all the options for each renderers: +They all conform to the RendererInterface and have the same `render()` method. Some renderers have extra options as well, via set*() methods. + +### Widths +The render() method needs the Barcode object, the width and height. **For JPG/PNG images**, you only get a valid barcode if you give a width that is a factor of the width of the Barcode object. That is why the examples show `$barcode->getWidth() * 2` to make the image 2 times wider in pixels then the width of the barcode data. You *can* give an arbitrary number as width and the image will be scaled as best as possible, but without anti-aliasing, it will not be perfectly valid. + +HTML and SVG renderers can handle any width and height, even floats. + +Here are all the options for each renderer: ### SVG A vector based SVG image. Gives the best quality to print. ```php $renderer = new Picqer\Barcode\Renderers\SvgRenderer(); -$renderer->setForegroundColor('red'); // Give a color for the bars, default is black -$renderer->setBackgroundColor('blue'); // Give a color for the background, default is transparent +$renderer->setForegroundColor([255, 0, 0]); // Give a color red for the bars, default is black. Give it as 3 times 0-255 values for red, green and blue. +$renderer->setBackgroundColor([0, 0, 255]); // Give a color blue for the background, default is transparent. Give it as 3 times 0-255 values for red, green and blue. $renderer->setSvgType($renderer::TYPE_SVG_INLINE); // Changes the output to be used inline inside HTML documents, instead of a standalone SVG image (default) $renderer->setSvgType($renderer::TYPE_SVG_STANDALONE); // If you want to force the default, create a stand alone SVG image @@ -91,8 +98,8 @@ $renderer->render($barcode, 5, 40); // Width factor (how many pixel wide every b Gives HTML to use inline in a full HTML document. ```php $renderer = new Picqer\Barcode\Renderers\HtmlRenderer(); -$renderer->setForegroundColor('red'); // Give a color for the bars, default is black -$renderer->setBackgroundColor('blue'); // Give a color for the background, default is transparent +$renderer->setForegroundColor([255, 0, 0]); // Give a color red for the bars, default is black. Give it as 3 times 0-255 values for red, green and blue. +$renderer->setBackgroundColor([0, 0, 255]); // Give a color blue for the background, default is transparent. Give it as 3 times 0-255 values for red, green and blue. $renderer->render($barcode, 450.20, 75); // Width and height support floats ```` @@ -101,8 +108,8 @@ $renderer->render($barcode, 450.20, 75); // Width and height support floats Give HTML here the barcode is using the full width and height, to put inside a container/div that has a fixed size. ```php $renderer = new Picqer\Barcode\Renderers\DynamicHtmlRenderer(); -$renderer->setForegroundColor('red'); // Give a color for the bars, default is black -$renderer->setBackgroundColor('blue'); // Give a color for the background, default is transparent +$renderer->setForegroundColor([255, 0, 0]); // Give a color red for the bars, default is black. Give it as 3 times 0-255 values for red, green and blue. +$renderer->setBackgroundColor([0, 0, 255]); // Give a color blue for the background, default is transparent. Give it as 3 times 0-255 values for red, green and blue. $renderer->render($barcode); ```` @@ -163,7 +170,7 @@ If you want to use PNG or JPG images, you need to install [Imagick](https://www. ```php $barcode = (new Picqer\Barcode\Types\TypeCode128())->getBarcode('081231723897'); $renderer = new Picqer\Barcode\Renderers\PngRenderer(); -echo ''; +echo ''; ``` ### Save JPG barcode to disk @@ -171,7 +178,7 @@ echo '