php-barcode-generator/LICENSE.md000064400000016743150247744220012342 0ustar00 GNU LESSER GENERAL PUBLIC LICENSE Version 3, 29 June 2007 Copyright (C) 2007 Free Software Foundation, Inc. Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. This version of the GNU Lesser General Public License incorporates the terms and conditions of version 3 of the GNU General Public License, supplemented by the additional permissions listed below. 0. Additional Definitions. As used herein, "this License" refers to version 3 of the GNU Lesser General Public License, and the "GNU GPL" refers to version 3 of the GNU General Public License. "The Library" refers to a covered work governed by this License, other than an Application or a Combined Work as defined below. An "Application" is any work that makes use of an interface provided by the Library, but which is not otherwise based on the Library. Defining a subclass of a class defined by the Library is deemed a mode of using an interface provided by the Library. A "Combined Work" is a work produced by combining or linking an Application with the Library. The particular version of the Library with which the Combined Work was made is also called the "Linked Version". The "Minimal Corresponding Source" for a Combined Work means the Corresponding Source for the Combined Work, excluding any source code for portions of the Combined Work that, considered in isolation, are based on the Application, and not on the Linked Version. The "Corresponding Application Code" for a Combined Work means the object code and/or source code for the Application, including any data and utility programs needed for reproducing the Combined Work from the Application, but excluding the System Libraries of the Combined Work. 1. Exception to Section 3 of the GNU GPL. You may convey a covered work under sections 3 and 4 of this License without being bound by section 3 of the GNU GPL. 2. Conveying Modified Versions. If you modify a copy of the Library, and, in your modifications, a facility refers to a function or data to be supplied by an Application that uses the facility (other than as an argument passed when the facility is invoked), then you may convey a copy of the modified version: a) under this License, provided that you make a good faith effort to ensure that, in the event an Application does not supply the function or data, the facility still operates, and performs whatever part of its purpose remains meaningful, or b) under the GNU GPL, with none of the additional permissions of this License applicable to that copy. 3. Object Code Incorporating Material from Library Header Files. The object code form of an Application may incorporate material from a header file that is part of the Library. You may convey such object code under terms of your choice, provided that, if the incorporated material is not limited to numerical parameters, data structure layouts and accessors, or small macros, inline functions and templates (ten or fewer lines in length), you do both of the following: a) Give prominent notice with each copy of the object code that the Library is used in it and that the Library and its use are covered by this License. b) Accompany the object code with a copy of the GNU GPL and this license document. 4. Combined Works. You may convey a Combined Work under terms of your choice that, taken together, effectively do not restrict modification of the portions of the Library contained in the Combined Work and reverse engineering for debugging such modifications, if you also do each of the following: a) Give prominent notice with each copy of the Combined Work that the Library is used in it and that the Library and its use are covered by this License. b) Accompany the Combined Work with a copy of the GNU GPL and this license document. c) For a Combined Work that displays copyright notices during execution, include the copyright notice for the Library among these notices, as well as a reference directing the user to the copies of the GNU GPL and this license document. d) Do one of the following: 0) Convey the Minimal Corresponding Source under the terms of this License, and the Corresponding Application Code in a form suitable for, and under terms that permit, the user to recombine or relink the Application with a modified version of the Linked Version to produce a modified Combined Work, in the manner specified by section 6 of the GNU GPL for conveying Corresponding Source. 1) Use a suitable shared library mechanism for linking with the Library. A suitable mechanism is one that (a) uses at run time a copy of the Library already present on the user's computer system, and (b) will operate properly with a modified version of the Library that is interface-compatible with the Linked Version. e) Provide Installation Information, but only if you would otherwise be required to provide such information under section 6 of the GNU GPL, and only to the extent that such information is necessary to install and execute a modified version of the Combined Work produced by recombining or relinking the Application with a modified version of the Linked Version. (If you use option 4d0, the Installation Information must accompany the Minimal Corresponding Source and Corresponding Application Code. If you use option 4d1, you must provide the Installation Information in the manner specified by section 6 of the GNU GPL for conveying Corresponding Source.) 5. Combined Libraries. You may place library facilities that are a work based on the Library side by side in a single library together with other library facilities that are not Applications and are not covered by this License, and convey such a combined library under terms of your choice, if you do both of the following: a) Accompany the combined library with a copy of the same work based on the Library, uncombined with any other library facilities, conveyed under the terms of this License. b) Give prominent notice with the combined library that part of it is a work based on the Library, and explaining where to find the accompanying uncombined form of the same work. 6. Revised Versions of the GNU Lesser General Public License. The Free Software Foundation may publish revised and/or new versions of the GNU Lesser General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. Each version is given a distinguishing version number. If the Library as you received it specifies that a certain numbered version of the GNU Lesser General Public License "or any later version" applies to it, you have the option of following the terms and conditions either of that published version or of any later version published by the Free Software Foundation. If the Library as you received it does not specify a version number of the GNU Lesser General Public License, you may choose any version of the GNU Lesser General Public License ever published by the Free Software Foundation. If the Library as you received it specifies that a proxy can decide whether future versions of the GNU Lesser General Public License shall apply, that proxy's public statement of acceptance of any version is permanent authorization for you to choose that version for the Library. php-barcode-generator/.gitignore000064400000000103150247744220012705 0ustar00vendor composer.lock composer.phar .phpunit.result.cache .DS_Store php-barcode-generator/examples.md000064400000005543150247744220013072 0ustar00# 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*php-barcode-generator/composer.json000064400000002647150247744220013456 0ustar00{ "name": "picqer/php-barcode-generator", "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": "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" } ], "require": { "php": "^7.3|^8.0", "ext-mbstring": "*" }, "require-dev": { "phpunit/phpunit": "^9.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" } }, "scripts": { "test": "vendor/bin/phpunit" } } php-barcode-generator/CHANGELOG.md000064400000003450150247744220012536 0ustar00# 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. php-barcode-generator/src/Helpers/BinarySequenceConverter.php000064400000001567150247744220020443 0ustar00addBar(new BarcodeBar($barWidth, 1, $drawBar)); $barWidth = 0; } } return $barcode; } } php-barcode-generator/src/BarcodeGeneratorSVG.php000064400000004506150247744220016016 0ustar00getBarcodeData($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; return $svg; } } php-barcode-generator/src/BarcodeGeneratorHTML.php000064400000003633150247744220016123 0ustar00getBarcodeData($barcode, $type); $html = '
' . PHP_EOL; $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 $html .= '
 
' . PHP_EOL; } $positionHorizontal += $barWidth; } $html .= '
' . PHP_EOL; return $html; } } php-barcode-generator/src/Barcode.php000064400000001374150247744220013567 0ustar00barcode = $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; } }php-barcode-generator/src/BarcodeGeneratorJPG.php000064400000000660150247744220015774 0ustar00newImage($width, $height, 'white', 'JPG'); return $image; } protected function generateGdImage($image) { imagejpeg($image); imagedestroy($image); } } php-barcode-generator/src/BarcodeGeneratorDynamicHTML.php000064400000003403150247744220017423 0ustar00getBarcodeData($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; return $html; } } php-barcode-generator/src/Exceptions/InvalidCharacterException.php000064400000000150150247744220021422 0ustar00. // // See LICENSE.TXT file for more information. namespace Picqer\Barcode; use Picqer\Barcode\Exceptions\UnknownTypeException; 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\TypeCode128C; use Picqer\Barcode\Types\TypeCode32; 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; 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\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 { const TYPE_CODE_32 = 'C32'; 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'; 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'; const TYPE_CODE_128_C = 'C128C'; 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'; // 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'; // 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'; const TYPE_PHARMA_CODE_TWO_TRACKS = 'PHARMA2T'; protected function getBarcodeData(string $code, string $type): Barcode { $barcodeDataBuilder = $this->createDataBuilderForType($type); return $barcodeDataBuilder->getBarcodeData($code); } protected function createDataBuilderForType(string $type) { switch (strtoupper($type)) { case self::TYPE_CODE_32: return new TypeCode32(); case self::TYPE_CODE_39: return new TypeCode39(); case self::TYPE_CODE_39_CHECKSUM: return new TypeCode39Checksum(); case self::TYPE_CODE_39E: return new TypeCode39Extended(); case self::TYPE_CODE_39E_CHECKSUM: return new TypeCode39ExtendedChecksum(); case self::TYPE_CODE_93: return new TypeCode93(); case self::TYPE_STANDARD_2_5: return new TypeStandard2of5(); case self::TYPE_STANDARD_2_5_CHECKSUM: return new TypeStandard2of5Checksum(); case self::TYPE_INTERLEAVED_2_5: return new TypeInterleaved25(); case self::TYPE_INTERLEAVED_2_5_CHECKSUM: return new TypeInterleaved25Checksum(); case self::TYPE_CODE_128: return new TypeCode128(); case self::TYPE_CODE_128_A: return new TypeCode128A(); case self::TYPE_CODE_128_B: return new TypeCode128B(); case self::TYPE_CODE_128_C: return new TypeCode128C(); case self::TYPE_EAN_2: return new TypeUpcExtension2(); case self::TYPE_EAN_5: return new TypeUpcExtension5(); case self::TYPE_EAN_8: return new TypeEan8(); case self::TYPE_EAN_13: return new TypeEan13(); case self::TYPE_UPC_A: return new TypeUpcA(); case self::TYPE_UPC_E: return new TypeUpcE(); case self::TYPE_MSI: return new TypeMsi(); case self::TYPE_MSI_CHECKSUM: return new TypeMsiChecksum(); case self::TYPE_POSTNET: return new TypePostnet(); case self::TYPE_PLANET: return new TypePlanet(); case self::TYPE_RMS4CC: return new TypeRms4cc(); case self::TYPE_KIX: return new TypeKix(); case self::TYPE_IMB: return new TypeIntelligentMailBarcode(); case self::TYPE_CODABAR: return new TypeCodabar(); case self::TYPE_CODE_11: return new TypeCode11(); case self::TYPE_PHARMA_CODE: return new TypePharmacode(); case self::TYPE_PHARMA_CODE_TWO_TRACKS: return new TypePharmacodeTwoCode(); } throw new UnknownTypeException(); } } php-barcode-generator/src/Types/TypeCode128B.php000064400000000547150247744220015406 0ustar00 [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 (! $this->kix) { // table for checksum calculation (row,col) $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) { $row += $checktable[$code[$i]][0]; $col += $checktable[$code[$i]][1]; } $row %= 6; $col %= 6; $chk = array_keys($checktable, [$row, $col]); $code .= $chk[0]; ++$len; // start bar $barcode->addBar(new BarcodeBar(1, 2, 1)); $barcode->addBar(new BarcodeBar(1, 2, 0)); } 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; } $barcode->addBar(new BarcodeBar(1, $h, 1, $p)); $barcode->addBar(new BarcodeBar(1, 2, 0)); } } if (! $this->kix) { // stop bar $barcode->addBar(new BarcodeBar(1, 3, 1)); } return $barcode; } } php-barcode-generator/src/Types/TypeMsi.php000064400000000603150247744220014720 0ustar00addBar(new BarcodeBar(1, $h, 1, $p)); if ($i < (strlen($seq) - 1)) { $barcode->addBar(new BarcodeBar(1, 2, 0, 0)); } } return $barcode; } } php-barcode-generator/src/Types/TypeStandard2of5.php000064400000003627150247744220016435 0ustar00checksum) { // add checksum $code .= $this->checksum_s25($code); } $seq = '11011010'; for ($i = 0; $i < strlen($code); ++$i) { $digit = $code[$i]; if (! isset($chr[$digit])) { throw new InvalidCharacterException('Char ' . $digit . ' is unsupported'); } $seq .= $chr[$digit]; } $seq .= '1101011'; return BinarySequenceConverter::convert($code, $seq); } /** * 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; } } php-barcode-generator/src/Types/TypeKix.php000064400000001054150247744220014724 0ustar00length; //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 = [ 'A' => [ // left odd parity '0' => '0001101', '1' => '0011001', '2' => '0010011', '3' => '0111101', '4' => '0100011', '5' => '0110001', '6' => '0101111', '7' => '0111011', '8' => '0110111', '9' => '0001011' ], 'B' => [ // left even parity '0' => '0100111', '1' => '0110011', '2' => '0011011', '3' => '0100001', '4' => '0011101', '5' => '0111001', '6' => '0000101', '7' => '0010001', '8' => '0001001', '9' => '0010111' ] ]; $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 $seq .= $codes[$p[0]][$code[0]]; for ($i = 1; $i < $len; ++$i) { $seq .= '01'; // separator $seq .= $codes[$p[$i]][$code[$i]]; } return BinarySequenceConverter::convert($code, $seq); } } php-barcode-generator/src/Types/TypeCode11.php000064400000005147150247744220015214 0ustar00 '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 { $barcode = new Barcode($code); $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) { $drawBar = true; } else { $drawBar = false; } $barWidth = $seq[$j]; $barcode->addBar(new BarcodeBar($barWidth, 1, $drawBar)); } } return $barcode; } private function getCheckDigitC(string $code): string { $p = 1; $check = 0; for ($i = (strlen($code) - 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 = '-'; } return $check; } private function getCheckDigitK(string $code): string { if (strlen($code) <= 10) { return ''; } $p = 1; $check = 0; for ($i = (strlen($code) - 1); $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 $check; } } php-barcode-generator/src/Types/TypeCode39Checksum.php000064400000000406150247744220016702 0ustar00 [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] ]; } php-barcode-generator/src/Types/TypeUpcE.php000064400000001024150247744220015022 0ustar00getChecksum($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'; $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]; } 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; } 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; } } php-barcode-generator/src/Types/TypeStandard2of5Checksum.php000064400000000447150247744220020115 0ustar00 '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.'); } if ($this->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 . '*'; $barcode = new Barcode($code); for ($i = 0; $i < strlen($code); ++$i) { $char = $code[$i]; if (! isset($this->conversionTable[$char])) { throw new InvalidCharacterException('Char ' . $char . ' is unsupported'); } for ($j = 0; $j < 9; ++$j) { if (($j % 2) == 0) { $drawBar = true; } else { $drawBar = false; } $barWidth = $this->conversionTable[$char][$j]; $barcode->addBar(new BarcodeBar($barWidth, 1, $drawBar)); } // inter character gap $barcode->addBar(new BarcodeBar(1, 1, false)); } return $barcode; } /** * 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 = [ 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 = ''; 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]]; } 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 = [ '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; for ($i = 0; $i < strlen($code); ++$i) { $k = array_keys($chars, $code[$i]); $sum += $k[0]; } $j = ($sum % 43); return $chars[$j]; } } php-barcode-generator/src/Types/TypeCode93.php000064400000017541150247744220015227 0ustar00 '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', // % 97 => '121221', // ($) 98 => '312111', // (%) 99 => '311121', // (/) 100 => '122211', // (+) 42 => '111141', // start-stop ]; public function getBarcodeData(string $code): Barcode { $encode = [ 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) => '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(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) => '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', 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) => '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 = ''; $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 . '*'; $barcode = new Barcode($code); for ($i = 0; $i < strlen($code); ++$i) { $char = ord($code[$i]); if (! isset($this->conversionTable[$char])) { throw new InvalidCharacterException('Char ' . $char . ' is unsupported'); } for ($j = 0; $j < 6; ++$j) { if (($j % 2) == 0) { $drawBar = true; } else { $drawBar = false; } $barWidth = $this->conversionTable[$char][$j]; $barcode->addBar(new BarcodeBar($barWidth, 1, $drawBar)); } } $barcode->addBar(new BarcodeBar(1, 1, true)); return $barcode; } /** * Calculate CODE 93 checksum (modulo 47). * * @param $code (string) code to represent. * @return string checksum code. * @protected */ 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', '-', '.', ' ', '$', '/', '+', '%', '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; } } $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; return $checksum; } } php-barcode-generator/src/Types/TypeUpcA.php000064400000001024150247744220015016 0ustar00checksum) { // 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 return BinarySequenceConverter::convert($code, $seq); } } php-barcode-generator/src/Types/TypeCode32.php000064400000002372150247744220015214 0ustar00 '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); } } php-barcode-generator/src/Types/TypeEan8.php000064400000001024150247744220014761 0ustar00?@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 = [241 => 102, 242 => 97, 243 => 96, 244 => 101]; $fnc_b = [241 => 102, 242 => 97, 243 => 96, 244 => 100]; // array of symbols $code_data = []; // length of the code $len = strlen($code); switch (strtoupper($this->type ?? "")) { case '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': $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': $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: // split code into sequences $sequence = []; // 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])) { $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[] = ['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 $barcode = new Barcode($code); foreach ($code_data as $val) { $seq = $this->conversionTable[$val]; for ($j = 0; $j < 6; ++$j) { if (($j % 2) == 0) { $t = true; // bar } else { $t = false; // space } $w = $seq[$j]; $barcode->addBar(new BarcodeBar($w, 1, $t)); } } return $barcode; } /** * 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 = []; // get A sequences (if any) $numseq = []; 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[] = [ 'B', substr($code, $end_offset, ($offset - $end_offset)), ($offset - $end_offset) ]; } // A sequence $slen = strlen($val[0]); $sequence[] = ['A', substr($code, $offset, $slen), $slen]; $end_offset = $offset + $slen; } if ($end_offset < $len) { $sequence[] = ['B', substr($code, $end_offset), ($len - $end_offset)]; } } else { // only B sequence $sequence[] = ['B', $code, $len]; } return $sequence; } } php-barcode-generator/src/Types/TypePharmacode.php000064400000001324150247744220016234 0ustar00 0) { if (($code % 2) == 0) { $seq .= '11100'; $code -= 2; } else { $seq .= '100'; $code -= 1; } $code /= 2; } $seq = substr($seq, 0, -2); $seq = strrev($seq); return BinarySequenceConverter::convert($code, $seq); } } php-barcode-generator/src/Types/TypeInterleaved25.php000064400000000571150247744220016605 0ustar00
  • 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 implements TypeInterface { public function getBarcodeData(string $code): Barcode { $asc_chr = [ 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 = [ 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 = [ 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 = [ 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'); } $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 = []; $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 = []; $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 $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); 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; } $barcode->addBar(new BarcodeBar(1, $h, true, $p)); if ($i < 64) { $barcode->addBar(new BarcodeBar(1, 2, false, 0)); } } return $barcode; } /** * 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 = []; $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; } } php-barcode-generator/src/Types/TypeCode128A.php000064400000000547150247744220015405 0ustar00 '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 { $barcode = new Barcode($code); $code = 'A' . strtoupper($code) . 'A'; for ($i = 0; $i < strlen($code); ++$i) { if (! isset($this->conversionTable[(string)$code[$i]])) { throw new InvalidCharacterException('Char ' . $code[$i] . ' is unsupported'); } $seq = $this->conversionTable[(string)$code[$i]]; for ($j = 0; $j < 8; ++$j) { if (($j % 2) == 0) { $drawBar = true; } else { $drawBar = false; } $barWidth = $seq[$j]; $barcode->addBar(new BarcodeBar($barWidth, 1, $drawBar)); } } return $barcode; } } php-barcode-generator/src/Types/TypeCode128C.php000064400000000547150247744220015407 0ustar00length; $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) { $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 = [ 'A' => [ // left odd parity '0' => '0001101', '1' => '0011001', '2' => '0010011', '3' => '0111101', '4' => '0100011', '5' => '0110001', '6' => '0101111', '7' => '0111011', '8' => '0110111', '9' => '0001011' ], 'B' => [ // left even parity '0' => '0100111', '1' => '0110011', '2' => '0011011', '3' => '0100001', '4' => '0011101', '5' => '0111001', '6' => '0000101', '7' => '0010001', '8' => '0001001', '9' => '0010111' ], 'C' => [ // right '0' => '1110010', '1' => '1100110', '2' => '1101100', '3' => '1000010', '4' => '1011100', '5' => '1001110', '6' => '1010000', '7' => '1000100', '8' => '1001000', '9' => '1110100' ] ]; $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'], ], ]; $seq = '101'; // left guard bar if ($this->upce) { $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 { $barcode = new Barcode($code); $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 } $barcode->addBar(new BarcodeBar($w, 1, $t)); $w = 0; } } return $barcode; } 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; } } php-barcode-generator/src/Types/TypeInterface.php000064400000000234150247744220016070 0ustar00 [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 { $code = str_replace(['-', ' '], '', $code); $len = strlen($code); $barcode = new Barcode($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 $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); $barcode->addBar(new BarcodeBar(1, $h, 1, $p)); $barcode->addBar(new BarcodeBar(1, 2, 0)); } } // end bar $barcode->addBar(new BarcodeBar(1, 2, 1)); return $barcode; } } php-barcode-generator/src/BarcodeBar.php000064400000001540150247744220014207 0ustar00width = $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; } }php-barcode-generator/src/BarcodeGeneratorPNG.php000064400000007630150247744220016004 0ustar00useImagick = 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; } /** * 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 $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]) { $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); 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)) { $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 && isset($imagickBarsShape)) { $image = $this->createImagickImageObject($width, $height); $image->drawImage($imagickBarsShape); return $image->getImageBlob(); } 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); } } php-barcode-generator/tests/VerifiedBarcodeTest.php000064400000007423150247744220016461 0ustar00 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_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(); foreach ($this::$supportedBarcodes as $barcodeTestSet) { foreach ($barcodeTestSet['barcodes'] as $barcode) { $result = $generator->getBarcode($barcode, $barcodeTestSet['type']); $this->assertStringEqualsFile( 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); } } php-barcode-generator/tests/GeneratorTest.php000064400000005212150247744220015364 0ustar00expectException(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); $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'); } }php-barcode-generator/tests/BarcodePngTest.php000064400000010646150247744220015451 0ustar00useGd(); $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() { $generator = new Picqer\Barcode\BarcodeGeneratorPNG(); $generator->useGd(); $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(); $generator->useGd(); $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(); $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); $this->assertGreaterThan(100, strlen($result)); $this->assertEquals(505, $imageInfo[0]); // Image width $this->assertEquals('image/png', $imageInfo['mime']); } } php-barcode-generator/tests/PharmacodeTest.php000064400000000564150247744220015506 0ustar00expectException(Picqer\Barcode\Exceptions\InvalidLengthException::class); $pharmacode->getBarcodeData('0'); } } php-barcode-generator/tests/TypesTest.php000064400000020671150247744220014550 0ustar00getBarcode('1234567890ABC', $generator::TYPE_CODE_39); $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('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(); $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/C128-1234567890abcABC-283-33.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/C128A-1234567890.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/C128B-1234567890abcABC-283-33.svg', $result); } public function test_generator_can_generate_ean_2_barcode() { $generator = new Picqer\Barcode\BarcodeGeneratorSVG(); $result = $generator->getBarcode('22', $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)); } }php-barcode-generator/tests/BarcodeHtmlTest.php000064400000001410150247744220015616 0ustar00getBarcode('081231723897', $generator::TYPE_CODE_128); $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); } } php-barcode-generator/tests/BarcodeJpgTest.php000064400000011606150247744220015442 0ustar00useGd(); $result = $generator->getBarcode('081231723897', $generator::TYPE_CODE_128); $imageInfo = getimagesizefromstring($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']); } 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); $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']); } 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); $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']); } 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(100, 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(100, 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(100, 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(100, 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); $this->assertGreaterThan(100, strlen($result)); $this->assertEquals(505, $imageInfo[0]); // Image width $this->assertEquals('image/jpeg', $imageInfo['mime']); } } php-barcode-generator/tests/BarcodeSvgTest.php000064400000000643150247744220015460 0ustar00getBarcode('081231723897', $generator::TYPE_EAN_13); $this->assertStringEqualsFile('tests/verified-files/081231723897-ean13.svg', $generated); } } php-barcode-generator/tests/BarcodeDynamicHtmlTest.php000064400000001475150247744220017136 0ustar00getBarcode('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); } } php-barcode-generator/tests/verified-files/081231723897-ean13.svg000064400000003260150247744220017653 0ustar00 0812317238973 php-barcode-generator/tests/verified-files/C39E+-1234567890abcABC.svg000064400000012552150247744220020274 0ustar00 *1234567890+A+B+CABCJ* php-barcode-generator/tests/verified-files/12345678903-dynamic-imb.html000064400000017465150247744220021233 0ustar00
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
    php-barcode-generator/tests/verified-files/C39+-1234567890ABC.svg000064400000007742150247744220017526 0ustar00 *1234567890ABCZ* php-barcode-generator/tests/verified-files/081231723897-code128.html000064400000005724150247744220020265 0ustar00
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
    php-barcode-generator/tests/verified-files/C128-081231723897.svg000064400000003124150247744220017360 0ustar00 081231723897 php-barcode-generator/tests/verified-files/C128A-1234567890.svg000064400000004206150247744220017315 0ustar00 1234567890 php-barcode-generator/tests/verified-files/MSI+-123456789.svg000064400000004420150247744220017220 0ustar00 1234567892 php-barcode-generator/tests/verified-files/UPCA-123456789.svg000064400000003261150247744220017247 0ustar00 0001234567895 php-barcode-generator/tests/verified-files/12345678903-imb.html000064400000015567150247744220017612 0ustar00
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
    php-barcode-generator/tests/verified-files/PHARMA-123456789.svg000064400000002756150247744220017477 0ustar00 0 php-barcode-generator/tests/verified-files/CODE11-123456789.svg000064400000003704150247744220017375 0ustar00 123456789 php-barcode-generator/tests/verified-files/PHARMA2T-123456789.svg000064400000002104150247744220017670 0ustar00 0 php-barcode-generator/tests/verified-files/EAN2-22.svg000064400000001164150247744220016413 0ustar00 22 php-barcode-generator/tests/verified-files/EAN13-0049000004632.svg000064400000003261150247744220017545 0ustar00 0049000004632 php-barcode-generator/tests/verified-files/C128-1234567890abcABC-283-33.svg000064400000006611150247744220020727 0ustar00 1234567890abcABC-283*33 php-barcode-generator/tests/verified-files/C39E-1234567890abcABC.svg000064400000012176150247744220020223 0ustar00 *1234567890+A+B+CABC* php-barcode-generator/tests/verified-files/EAN5-1234567890abcABC-283-33.svg000064400000002047150247744220021001 0ustar00 1234567890abcABC-283*33 php-barcode-generator/tests/verified-files/081231723897-dynamic-code128.html000064400000006412150247744220021702 0ustar00
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
     
    php-barcode-generator/tests/verified-files/POSTNET-123456789.svg000064400000005313150247744220017653 0ustar00 123456789 php-barcode-generator/tests/verified-files/UPCE-123456789.svg000064400000002106150247744220017250 0ustar00 012349 php-barcode-generator/tests/verified-files/EAN13-004900000463.svg000064400000003261150247744220017463 0ustar00 0049000004632 php-barcode-generator/tests/verified-files/C128B-081231723897.svg000064400000004642150247744220017470 0ustar00 081231723897 php-barcode-generator/tests/verified-files/I25+-1234567890.svg000064400000003554150247744220017216 0ustar00 AA012345678905ZA php-barcode-generator/tests/verified-files/I25-1234567890.svg000064400000003177150247744220017144 0ustar00 AA1234567890ZA php-barcode-generator/tests/verified-files/EAN8-1234568.svg000064400000002464150247744220017036 0ustar00 12345687 php-barcode-generator/tests/verified-files/C128B-1234567890abcABC-283-33.svg000064400000007674150247744220021043 0ustar00 1234567890abcABC-283*33 php-barcode-generator/tests/verified-files/EAN13-081231723897.svg000064400000003260150247744220017513 0ustar00 0812317238973 php-barcode-generator/tests/verified-files/MSI-123456789.svg000064400000004123150247744220017145 0ustar00 123456789 php-barcode-generator/tests/verified-files/C39-1234567890ABC.svg000064400000007366150247744220017455 0ustar00 *1234567890ABC* php-barcode-generator/tests/verified-files/PLANET-123456789.svg000064400000005301150247744220017477 0ustar00 123456789 php-barcode-generator/tests/verified-files/KIX-123456789.svg000064400000003717150247744220017160 0ustar00 123456789 php-barcode-generator/tests/verified-files/RMS4CC-123456789.svg000064400000004353150247744220017455 0ustar00 123456789 php-barcode-generator/tests/verified-files/0049000004632-ean13.svg000064400000003261150247744220017705 0ustar00 0049000004632 php-barcode-generator/tests/verified-files/S25+-1234567890.svg000064400000006136150247744220017227 0ustar00 12345678905 php-barcode-generator/tests/verified-files/CODABAR-123456789.svg000064400000004473150247744220017560 0ustar00 123456789 php-barcode-generator/tests/verified-files/C93-1234567890abcABC.svg000064400000007021150247744220020107 0ustar00 *1234567890dAdBdCABC6-* php-barcode-generator/tests/verified-files/S25-1234567890.svg000064400000005562150247744220017156 0ustar00 1234567890 php-barcode-generator/tests/verified-files/IMB-123456789.svg000064400000006456150247744220017137 0ustar00 123456789 php-barcode-generator/Readme.md000064400000011450150247744220012443 0ustar00# 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. 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. ## Installation 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. ## 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); ``` 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 ``` ## 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_32 (italian pharmaceutical code 'MINSAN') - TYPE_CODE_39 - TYPE_CODE_39_CHECKSUM - TYPE_CODE_39E - TYPE_CODE_39E_CHECKSUM - TYPE_CODE_93 - TYPE_STANDARD_2_5 - TYPE_STANDARD_2_5_CHECKSUM - TYPE_INTERLEAVED_2_5 - TYPE_INTERLEAVED_2_5_CHECKSUM - TYPE_CODE_128 - TYPE_CODE_128_A - TYPE_CODE_128_B - TYPE_CODE_128_C - TYPE_EAN_2 - TYPE_EAN_5 - TYPE_EAN_8 - TYPE_EAN_13 - TYPE_UPC_A - TYPE_UPC_E - TYPE_MSI - TYPE_MSI_CHECKSUM - TYPE_POSTNET - TYPE_PLANET - TYPE_RMS4CC - TYPE_KIX - TYPE_IMB - TYPE_CODABAR - TYPE_CODE_11 - TYPE_PHARMA_CODE - TYPE_PHARMA_CODE_TWO_TRACKS [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. ## Examples ### Embedded PNG image in HTML ```php $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)); ``` php-barcode-generator/generate-verified-files.php000064400000003335150247744220016125 0ustar00getBarcode('081231723897', $generatorSVG::TYPE_EAN_13)); $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)); // New style of verified files 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'])); } } php-barcode-generator/.github/FUNDING.yml000064400000000025150247744220014075 0ustar00github: casperbakker php-barcode-generator/.github/workflows/phpunit.yml000064400000001126150247744220016532 0ustar00name: phpunit on: [push, pull_request] jobs: build: runs-on: ubuntu-latest strategy: matrix: php-versions: ['7.3', '7.4', '8.0', '8.1'] steps: - uses: actions/checkout@v2 - name: Setup PHP uses: shivammathur/setup-php@v2 with: php-version: ${{ matrix.php-versions }} extensions: mbstring, gd, bcmath, imagick - name: Validate composer.json run: composer validate - name: Install dependencies run: composer install --prefer-dist --no-progress - name: Run test suite run: composer run-script test php-barcode-generator/generate-examples.php000064400000001563150247744220015047 0ustar00 ./tests/ ./src