diff --git a/Utils/Barcode/C128Abstract.php b/Utils/Barcode/C128Abstract.php new file mode 100644 index 000000000..58b238c68 --- /dev/null +++ b/Utils/Barcode/C128Abstract.php @@ -0,0 +1,112 @@ +content = $content; + $this->setSize($size); + $this->setOrientation($orientation); + } + + public function setOrientation(int $orientation) + { + if (!OrientationType::isValidValue($orientation)) { + throw new InvalidEnumValue($orientation); + } + + $this->orientation = $orientation; + } + + public function setContent(string $content) + { + $this->content = $content; + } + + public function setSize(int $size) + { + if ($size < 0) { + throw new \OutOfBoundsException($size); + } + + $this->size = $size; + } + + protected function generateCodeString() + { + $keys = array_keys(self::$CODEARRAY); + $values = array_flip($keys); + $codeString = ''; + $length = strlen($this->content); + $checksum = self::$CHECKSUM; + + for ($pos = 1; $pos <= $length; $pos += 2) { + $activeKey = substr($this->content, ($pos - 1), 1); + $codeString .= self::$CODEARRAY[$activeKey]; + $checksum = ($checksum + ($values[$activeKey] * $pos)); + } + + $codeString .= self::$CODEARRAY[$keys[($checksum - (intval($checksum / 103) * 103))]]; + $codeString = self::$CODE_START . $codeString . self::$CODE_END; + + return $codeString; + } + + public function get() + { + $codeString = self::$CODE_START . $this->generateCodeString() . self::$CODE_END; + + return $this->createImage($codeString, 20); + } + + protected function createImage(string $codeString, int $codeLength = 20) + { + for ($i = 1; $i <= strlen($codeString); $i++) { + $codeLength = $codeLength + (int) (substr($codeString, ($i - 1), 1)); + } + + if (strtolower($this->orientation) === OrientationType::HORIZONTAL) { + $imgWidth = $codeLength; + $imgHeight = $this->size; + } else { + $imgWidth = $this->size; + $imgHeight = $codeLength; + } + + $image = imagecreate($imgWidth, $imgHeight); + $black = imagecolorallocate($image, 0, 0, 0); + $white = imagecolorallocate($image, 255, 255, 255); + $location = 0; + $length = strlen($codeString); + imagefill($image, 0, 0, $white); + + for ($position = 1; $position <= $length; $position++) { + $cur_size = $location + (int) (substr($codeString, ($position - 1), 1)); + + if (strtolower($this->orientation) === OrientationType::HORIZONTAL) { + imagefilledrectangle($image, $location, 0, $cur_size, $imgHeight, ($position % 2 == 0 ? $white : $black)); + } else { + imagefilledrectangle($image, 0, $location, $imgWidth, $cur_size, ($position % 2 == 0 ? $white : $black)); + } + + $location = $cur_size; + } + + return $image; + } +} diff --git a/Utils/Barcode/C128a.php b/Utils/Barcode/C128a.php index 3721d7d34..4ec82cd7a 100644 --- a/Utils/Barcode/C128a.php +++ b/Utils/Barcode/C128a.php @@ -1,103 +1,44 @@ '212222', '!' => '222122', '"' => '222221', '#' => '121223', '$' => '121322', '%' => '131222', + '&' => '122213', '\'' => '122312', '(' => '132212', ')' => '221213', '*' => '221312', '+' => '231212', + ',' => '112232', '-' => '122132', '.' => '122231', '/' => '113222', '0' => '123122', '1' => '123221', + '2' => '223211', '3' => '221132', '4' => '221231', '5' => '213212', '6' => '223112', '7' => '312131', + '8' => '311222', '9' => '321122', ':' => '321221', ';' => '312212', '<' => '322112', '=' => '322211', + '>' => '212123', '?' => '212321', '@' => '232121', 'A' => '111323', 'B' => '131123', 'C' => '131321', + 'D' => '112313', 'E' => '132113', 'F' => '132311', 'G' => '211313', 'H' => '231113', 'I' => '231311', + 'J' => '112133', 'K' => '112331', 'L' => '132131', 'M' => '113123', 'N' => '113321', 'O' => '133121', + 'P' => '313121', 'Q' => '211331', 'R' => '231131', 'S' => '213113', 'T' => '213311', 'U' => '213131', + 'V' => '311123', 'W' => '311321', 'X' => '331121', 'Y' => '312113', 'Z' => '312311', '[' => '332111', + '\\' => '314111', ']' => '221411', '^' => '431111', '_' => '111224', 'NUL' => '111422', 'SOH' => '121124', + 'STX' => '121421', 'ETX' => '141122', 'EOT' => '141221', 'ENQ' => '112214', 'ACK' => '112412', + 'BEL' => '122114', 'BS' => '122411', 'HT' => '142112', 'LF' => '142211', 'VT' => '241211', 'FF' => '221114', + 'CR' => '413111', 'SO' => '241112', 'SI' => '134111', 'DLE' => '111242', 'DC1' => '121142', + 'DC2' => '121241', 'DC3' => '114212', 'DC4' => '124112', 'NAK' => '124211', 'SYN' => '411212', + 'ETB' => '421112', 'CAN' => '421211', 'EM' => '212141', 'SUB' => '214121', 'ESC' => '412121', + 'FS' => '111143', 'GS' => '111341', 'RS' => '131141', 'US' => '114113', 'FNC 3' => '114311', + 'FNC 2' => '411113', 'SHIFT' => '411311', 'CODE C' => '113141', 'CODE B' => '114131', 'FNC 4' => '311141', + 'FNC 1' => '411131', 'Start A' => '211412', 'Start B' => '211214', 'Start C' => '211232', + 'Stop' => '2331112', + ]; + + protected static $CODE_START = '211412'; + protected static $CODE_END = '2331112'; public function __construct(string $content = '', int $size = 20, int $orientation = 0) { - $this->content = $content; - $this->size = $size; - $this->orientation = $orientation; - } - - public function setOrientation(int $orientation) - { - $this->orientation = $orientation; + parent::__construct(strtoupper($content), $size, $orientation); } public function setContent(string $content) { - $this->content = $content; - } - - public function setSize(int $size) - { - $this->size = $size; - } - - public function get() - { - $codeString = ''; - $chksum = 104; - - /* Order is critical for checksum */ - $codeArray = [ - ' ' => '212222', '!' => '222122', '\'' => '222221', '#' => '121223', '$' => '121322', '%' => '131222', - '&' => '122213', '\'' => '122312', '(' => '132212', ')' => '221213', '*' => '221312', '+' => '231212', - ',' => '112232', '-' => '122132', '.' => '122231', '/' => '113222', '0' => '123122', '1' => '123221', - '2' => '223211', '3' => '221132', '4' => '221231', '5' => '213212', '6' => '223112', '7' => '312131', - '8' => '311222', '9' => '321122', ':' => '321221', ';' => '312212', '<' => '322112', '=' => '322211', - '>' => '212123', '?' => '212321', '@' => '232121', 'A' => '111323', 'B' => '131123', 'C' => '131321', - 'D' => '112313', 'E' => '132113', 'F' => '132311', 'G' => '211313', 'H' => '231113', 'I' => '231311', - 'J' => '112133', 'K' => '112331', 'L' => '132131', 'M' => '113123', 'N' => '113321', 'O' => '133121', - 'P' => '313121', 'Q' => '211331', 'R' => '231131', 'S' => '213113', 'T' => '213311', 'U' => '213131', - 'V' => '311123', 'W' => '311321', 'X' => '331121', 'Y' => '312113', 'Z' => '312311', '[' => '332111', - '\\' => '314111', ']' => '221411', '^' => '431111', '_' => '111224', '\`' => '111422', 'a' => '121124', - 'b' => '121421', 'c' => '141122', 'd' => '141221', 'e' => '112214', 'f' => '112412', 'g' => '122114', - 'h' => '122411', 'i' => '142112', 'j' => '142211', 'k' => '241211', 'l' => '221114', 'm' => '413111', - 'n' => '241112', 'o' => '134111', 'p' => '111242', 'q' => '121142', 'r' => '121241', 's' => '114212', - 't' => '124112', 'u' => '124211', 'v' => '411212', 'w' => '421112', 'x' => '421211', 'y' => '212141', - 'z' => '214121', '{' => '412121', '|' => '111143', '}' => '111341', '~' => '131141', 'DEL' => '114113', - 'FNC 3' => '114311', 'FNC 2' => '411113', 'SHIFT' => '411311', 'CODE C' => '113141', 'FNC 4' => '114131', - 'CODE A' => '311141', 'FNC 1' => '411131', 'Start A' => '211412', 'Start B' => '211214', - 'Start C' => '211232', 'Stop' => '2331112', - ]; - - $codeKeys = array_keys($codeArray); - $codeValues = array_flip($codeKeys); - - for ($X = 1; $X <= strlen($this->content); $X++) { - $activeKey = substr($this->content, ($X - 1), 1); - $codeString .= $codeArray[$activeKey]; - $chksum = ($chksum + ($codeValues[$activeKey] * $X)); - } - $codeString .= $codeArray[$codeKeys[($chksum - (intval($chksum / 103) * 103))]]; - $codeString = '211214' . $codeString . '2331112'; - $codeLength = 20; - - for ($i = 1; $i <= strlen($codeString); $i++) { - $codeLength = $codeLength + (integer) (substr($codeString, ($i - 1), 1)); - } - - if (strtolower($this->orientation) == 'horizontal') { - $imgWidth = $codeLength; - $imgHeight = $this->size; - } else { - $imgWidth = $this->size; - $imgHeight = $codeLength; - } - - $image = imagecreate($imgWidth, $imgHeight); - $black = imagecolorallocate($image, 0, 0, 0); - $white = imagecolorallocate($image, 255, 255, 255); - $location = 0; - imagefill($image, 0, 0, $white); - - for ($position = 1; $position <= strlen($codeString); $position++) { - $cur_size = $location + (substr($codeString, ($position - 1), 1)); - - if (strtolower($this->orientation) == 'horizontal') { - imagefilledrectangle($image, $location, 0, $cur_size, $imgHeight, ($position % 2 == 0 ? $white : $black)); - } else { - imagefilledrectangle($image, 0, $location, $imgWidth, $cur_size, ($position % 2 == 0 ? $white : $black)); - } - - $location = $cur_size; - } - - return $image; + parent::setContent(strtoupper($content)); } } diff --git a/Utils/Barcode/C128b.php b/Utils/Barcode/C128b.php index 3721d7d34..3ec1fc68c 100644 --- a/Utils/Barcode/C128b.php +++ b/Utils/Barcode/C128b.php @@ -1,103 +1,33 @@ content = $content; - $this->size = $size; - $this->orientation = $orientation; - } + protected static $CODEARRAY = [ + ' ' => '212222', '!' => '222122', '"' => '222221', '#' => '121223', '$' => '121322', '%' => '131222', + '&' => '122213', '\'' => '122312', '(' => '132212', ')' => '221213', '*' => '221312', '+' => '231212', + ',' => '112232', '-' => '122132', '.' => '122231', '/' => '113222', '0' => '123122', '1' => '123221', + '2' => '223211', '3' => '221132', '4' => '221231', '5' => '213212', '6' => '223112', '7' => '312131', + '8' => '311222', '9' => '321122', ':' => '321221', ';' => '312212', '<' => '322112', '=' => '322211', + '>' => '212123', '?' => '212321', '@' => '232121', 'A' => '111323', 'B' => '131123', 'C' => '131321', + 'D' => '112313', 'E' => '132113', 'F' => '132311', 'G' => '211313', 'H' => '231113', 'I' => '231311', + 'J' => '112133', 'K' => '112331', 'L' => '132131', 'M' => '113123', 'N' => '113321', 'O' => '133121', + 'P' => '313121', 'Q' => '211331', 'R' => '231131', 'S' => '213113', 'T' => '213311', 'U' => '213131', + 'V' => '311123', 'W' => '311321', 'X' => '331121', 'Y' => '312113', 'Z' => '312311', '[' => '332111', + '\\' => '314111', ']' => '221411', '^' => '431111', '_' => '111224', '`' => '111422', 'a' => '121124', + 'b' => '121421', 'c' => '141122', 'd' => '141221', 'e' => '112214', 'f' => '112412', 'g' => '122114', + 'h' => '122411', 'i' => '142112', 'j' => '142211', 'k' => '241211', 'l' => '221114', 'm' => '413111', + 'n' => '241112', 'o' => '134111', 'p' => '111242', 'q' => '121142', 'r' => '121241', 's' => '114212', + 't' => '124112', 'u' => '124211', 'v' => '411212', 'w' => '421112', 'x' => '421211', 'y' => '212141', + 'z' => '214121', '{' => '412121', '|' => '111143', '}' => '111341', '~' => '131141', 'DEL' => '114113', + 'FNC 3' => '114311', 'FNC 2' => '411113', 'SHIFT' => '411311', 'CODE C' => '113141', 'FNC 4' => '114131', + 'CODE A' => '311141', 'FNC 1' => '411131', 'Start A' => '211412', 'Start B' => '211214', + 'Start C' => '211232', 'Stop' => '2331112', + ]; - public function setOrientation(int $orientation) - { - $this->orientation = $orientation; - } - - public function setContent(string $content) - { - $this->content = $content; - } - - public function setSize(int $size) - { - $this->size = $size; - } - - public function get() - { - $codeString = ''; - $chksum = 104; - - /* Order is critical for checksum */ - $codeArray = [ - ' ' => '212222', '!' => '222122', '\'' => '222221', '#' => '121223', '$' => '121322', '%' => '131222', - '&' => '122213', '\'' => '122312', '(' => '132212', ')' => '221213', '*' => '221312', '+' => '231212', - ',' => '112232', '-' => '122132', '.' => '122231', '/' => '113222', '0' => '123122', '1' => '123221', - '2' => '223211', '3' => '221132', '4' => '221231', '5' => '213212', '6' => '223112', '7' => '312131', - '8' => '311222', '9' => '321122', ':' => '321221', ';' => '312212', '<' => '322112', '=' => '322211', - '>' => '212123', '?' => '212321', '@' => '232121', 'A' => '111323', 'B' => '131123', 'C' => '131321', - 'D' => '112313', 'E' => '132113', 'F' => '132311', 'G' => '211313', 'H' => '231113', 'I' => '231311', - 'J' => '112133', 'K' => '112331', 'L' => '132131', 'M' => '113123', 'N' => '113321', 'O' => '133121', - 'P' => '313121', 'Q' => '211331', 'R' => '231131', 'S' => '213113', 'T' => '213311', 'U' => '213131', - 'V' => '311123', 'W' => '311321', 'X' => '331121', 'Y' => '312113', 'Z' => '312311', '[' => '332111', - '\\' => '314111', ']' => '221411', '^' => '431111', '_' => '111224', '\`' => '111422', 'a' => '121124', - 'b' => '121421', 'c' => '141122', 'd' => '141221', 'e' => '112214', 'f' => '112412', 'g' => '122114', - 'h' => '122411', 'i' => '142112', 'j' => '142211', 'k' => '241211', 'l' => '221114', 'm' => '413111', - 'n' => '241112', 'o' => '134111', 'p' => '111242', 'q' => '121142', 'r' => '121241', 's' => '114212', - 't' => '124112', 'u' => '124211', 'v' => '411212', 'w' => '421112', 'x' => '421211', 'y' => '212141', - 'z' => '214121', '{' => '412121', '|' => '111143', '}' => '111341', '~' => '131141', 'DEL' => '114113', - 'FNC 3' => '114311', 'FNC 2' => '411113', 'SHIFT' => '411311', 'CODE C' => '113141', 'FNC 4' => '114131', - 'CODE A' => '311141', 'FNC 1' => '411131', 'Start A' => '211412', 'Start B' => '211214', - 'Start C' => '211232', 'Stop' => '2331112', - ]; - - $codeKeys = array_keys($codeArray); - $codeValues = array_flip($codeKeys); - - for ($X = 1; $X <= strlen($this->content); $X++) { - $activeKey = substr($this->content, ($X - 1), 1); - $codeString .= $codeArray[$activeKey]; - $chksum = ($chksum + ($codeValues[$activeKey] * $X)); - } - $codeString .= $codeArray[$codeKeys[($chksum - (intval($chksum / 103) * 103))]]; - $codeString = '211214' . $codeString . '2331112'; - $codeLength = 20; - - for ($i = 1; $i <= strlen($codeString); $i++) { - $codeLength = $codeLength + (integer) (substr($codeString, ($i - 1), 1)); - } - - if (strtolower($this->orientation) == 'horizontal') { - $imgWidth = $codeLength; - $imgHeight = $this->size; - } else { - $imgWidth = $this->size; - $imgHeight = $codeLength; - } - - $image = imagecreate($imgWidth, $imgHeight); - $black = imagecolorallocate($image, 0, 0, 0); - $white = imagecolorallocate($image, 255, 255, 255); - $location = 0; - imagefill($image, 0, 0, $white); - - for ($position = 1; $position <= strlen($codeString); $position++) { - $cur_size = $location + (substr($codeString, ($position - 1), 1)); - - if (strtolower($this->orientation) == 'horizontal') { - imagefilledrectangle($image, $location, 0, $cur_size, $imgHeight, ($position % 2 == 0 ? $white : $black)); - } else { - imagefilledrectangle($image, 0, $location, $imgWidth, $cur_size, ($position % 2 == 0 ? $white : $black)); - } - - $location = $cur_size; - } - - return $image; - } + protected static $CODE_START = '211214'; + protected static $CODE_END = '2331112'; } diff --git a/Utils/Barcode/C128c.php b/Utils/Barcode/C128c.php new file mode 100644 index 000000000..1668671e3 --- /dev/null +++ b/Utils/Barcode/C128c.php @@ -0,0 +1,62 @@ + '212222', '01' => '222122', '02' => '222221', '03' => '121223', '04' => '121322', '05' => '131222', + '06' => '122213', '07' => '122312', '08' => '132212', '09' => '221213', '10' => '221312', '11' => '231212', + '12' => '112232', '13' => '122132', '14' => '122231', '15' => '113222', '16' => '123122', '17' => '123221', + '18' => '223211', '19' => '221132', '20' => '221231', '21' => '213212', '22' => '223112', '23' => '312131', + '24' => '311222', '25' => '321122', '26' => '321221', '27' => '312212', '28' => '322112', '29' => '322211', + '30' => '212123', '31' => '212321', '32' => '232121', '33' => '111323', '34' => '131123', '35' => '131321', + '36' => '112313', '37' => '132113', '38' => '132311', '39' => '211313', '40' => '231113', '41' => '231311', + '42' => '112133', '43' => '112331', '44' => '132131', '45' => '113123', '46' => '113321', '47' => '133121', + '48' => '313121', '49' => '211331', '50' => '231131', '51' => '213113', '52' => '213311', '53' => '213131', + '54' => '311123', '55' => '311321', '56' => '331121', '57' => '312113', '58' => '312311', '59' => '332111', + '60' => '314111', '61' => '221411', '62' => '431111', '63' => '111224', '64' => '111422', '65' => '121124', + '66' => '121421', '67' => '141122', '68' => '141221', '69' => '112214', '70' => '112412', '71' => '122114', + '72' => '122411', '73' => '142112', '74' => '142211', '75' => '241211', '76' => '221114', '77' => '413111', + '78' => '241112', '79' => '134111', '80' => '111242', '81' => '121142', '82' => '121241', '83' => '114212', + '84' => '124112', '85' => '124211', '86' => '411212', '87' => '421112', '88' => '421211', '89' => '212141', + '90' => '214121', '91' => '412121', '92' => '111143', '93' => '111341', '94' => '131141', '95' => '114113', + '96' => '114311', '97' => '411113', '98' => '411311', '99' => '113141', 'CODE B' => '114131', + 'CODE A' => '311141', 'FNC 1' => '411131', 'Start A' => '211412', 'Start B' => '211214', 'Start C' => '211232', + 'Stop' => '2331112', + ]; + + protected static $CODE_START = '211232'; + + protected static $CODE_END = '2331112'; + + protected function generateCodeString() + { + $keys = array_keys(self::$CODEARRAY); + $values = array_flip($keys); + $codeString = ''; + $length = strlen($this->content); + $checksum = self::$CHECKSUM; + + for ($pos = 1; $pos <= $length; $pos += 2) { + if ($pos + 1 <= $length) { + $activeKey = substr($this->content, ($pos - 1), 2); + } else { + $activeKey = substr($this->content, ($pos - 1), 1) . '0'; + } + + $codeString .= self::$CODEARRAY[$activeKey]; + $checksum = ($checksum + ($values[$activeKey] * $pos)); + } + + $codeString .= self::$CODEARRAY[$keys[($checksum - (intval($checksum / 103) * 103))]]; + + return $codeString; + } +} diff --git a/Utils/Barcode/C25.php b/Utils/Barcode/C25.php index 3721d7d34..a07627e71 100644 --- a/Utils/Barcode/C25.php +++ b/Utils/Barcode/C25.php @@ -1,103 +1,62 @@ content = $content; - $this->size = $size; - $this->orientation = $orientation; - } + if(!ctype_digit($content)) { + throw new \InvalidArgumentException($content); + } - public function setOrientation(int $orientation) - { - $this->orientation = $orientation; + parent::__construct($content, $size, $orientation); } public function setContent(string $content) { - $this->content = $content; + if(!ctype_digit($content)) { + throw new \InvalidArgumentException($content); + } + + parent::setContent($content); } - public function setSize(int $size) + protected function generateCodeString() { - $this->size = $size; - } + $codeString = ''; + $length = strlen($this->content); + $arrayLength = count(self::$CODEARRAY); - public function get() - { - $codeString = ''; - $chksum = 104; - - /* Order is critical for checksum */ - $codeArray = [ - ' ' => '212222', '!' => '222122', '\'' => '222221', '#' => '121223', '$' => '121322', '%' => '131222', - '&' => '122213', '\'' => '122312', '(' => '132212', ')' => '221213', '*' => '221312', '+' => '231212', - ',' => '112232', '-' => '122132', '.' => '122231', '/' => '113222', '0' => '123122', '1' => '123221', - '2' => '223211', '3' => '221132', '4' => '221231', '5' => '213212', '6' => '223112', '7' => '312131', - '8' => '311222', '9' => '321122', ':' => '321221', ';' => '312212', '<' => '322112', '=' => '322211', - '>' => '212123', '?' => '212321', '@' => '232121', 'A' => '111323', 'B' => '131123', 'C' => '131321', - 'D' => '112313', 'E' => '132113', 'F' => '132311', 'G' => '211313', 'H' => '231113', 'I' => '231311', - 'J' => '112133', 'K' => '112331', 'L' => '132131', 'M' => '113123', 'N' => '113321', 'O' => '133121', - 'P' => '313121', 'Q' => '211331', 'R' => '231131', 'S' => '213113', 'T' => '213311', 'U' => '213131', - 'V' => '311123', 'W' => '311321', 'X' => '331121', 'Y' => '312113', 'Z' => '312311', '[' => '332111', - '\\' => '314111', ']' => '221411', '^' => '431111', '_' => '111224', '\`' => '111422', 'a' => '121124', - 'b' => '121421', 'c' => '141122', 'd' => '141221', 'e' => '112214', 'f' => '112412', 'g' => '122114', - 'h' => '122411', 'i' => '142112', 'j' => '142211', 'k' => '241211', 'l' => '221114', 'm' => '413111', - 'n' => '241112', 'o' => '134111', 'p' => '111242', 'q' => '121142', 'r' => '121241', 's' => '114212', - 't' => '124112', 'u' => '124211', 'v' => '411212', 'w' => '421112', 'x' => '421211', 'y' => '212141', - 'z' => '214121', '{' => '412121', '|' => '111143', '}' => '111341', '~' => '131141', 'DEL' => '114113', - 'FNC 3' => '114311', 'FNC 2' => '411113', 'SHIFT' => '411311', 'CODE C' => '113141', 'FNC 4' => '114131', - 'CODE A' => '311141', 'FNC 1' => '411131', 'Start A' => '211412', 'Start B' => '211214', - 'Start C' => '211232', 'Stop' => '2331112', - ]; - - $codeKeys = array_keys($codeArray); - $codeValues = array_flip($codeKeys); - - for ($X = 1; $X <= strlen($this->content); $X++) { - $activeKey = substr($this->content, ($X - 1), 1); - $codeString .= $codeArray[$activeKey]; - $chksum = ($chksum + ($codeValues[$activeKey] * $X)); - } - $codeString .= $codeArray[$codeKeys[($chksum - (intval($chksum / 103) * 103))]]; - $codeString = '211214' . $codeString . '2331112'; - $codeLength = 20; - - for ($i = 1; $i <= strlen($codeString); $i++) { - $codeLength = $codeLength + (integer) (substr($codeString, ($i - 1), 1)); - } - - if (strtolower($this->orientation) == 'horizontal') { - $imgWidth = $codeLength; - $imgHeight = $this->size; - } else { - $imgWidth = $this->size; - $imgHeight = $codeLength; - } - - $image = imagecreate($imgWidth, $imgHeight); - $black = imagecolorallocate($image, 0, 0, 0); - $white = imagecolorallocate($image, 255, 255, 255); - $location = 0; - imagefill($image, 0, 0, $white); - - for ($position = 1; $position <= strlen($codeString); $position++) { - $cur_size = $location + (substr($codeString, ($position - 1), 1)); - - if (strtolower($this->orientation) == 'horizontal') { - imagefilledrectangle($image, $location, 0, $cur_size, $imgHeight, ($position % 2 == 0 ? $white : $black)); - } else { - imagefilledrectangle($image, 0, $location, $imgWidth, $cur_size, ($position % 2 == 0 ? $white : $black)); + for ($posX = 1; $posX <= $length; $posX++) { + for ($posY = 0; $posY < $arrayLength; $posY++) { + if (substr($this->content, ($posX - 1), 1) == self::$CODEARRAY[$posY]) { + $temp[$posX] = self::$CODEARRAY2[$posY]; + } } - - $location = $cur_size; } - return $image; + for ($posX = 1; $posX <= $length; $posX += 2) { + if (isset($temp[$posX]) && isset($temp[($posX + 1)])) { + $temp1 = explode('-', $temp[$posX]); + $temp2 = explode('-', $temp[($posX + 1)]); + + for ($posY = 0; $posY < count($temp1); $posY++) { + $codeString .= $temp1[$posY] . $temp2[$posY]; + } + } + } + + return $codeString; } } diff --git a/Utils/Barcode/C39.php b/Utils/Barcode/C39.php index 3721d7d34..97049d3ea 100644 --- a/Utils/Barcode/C39.php +++ b/Utils/Barcode/C39.php @@ -1,103 +1,45 @@ '111221211', '1' => '211211112', '2' => '112211112', '3' => '212211111', '4' => '111221112', + '5' => '211221111', '6' => '112221111', '7' => '111211212', '8' => '211211211', '9' => '112211211', + 'A' => '211112112', 'B' => '112112112', 'C' => '212112111', 'D' => '111122112', 'E' => '211122111', + 'F' => '112122111', 'G' => '111112212', 'H' => '211112211', 'I' => '112112211', 'J' => '111122211', + 'K' => '211111122', 'L' => '112111122', 'M' => '212111121', 'N' => '111121122', 'O' => '211121121', + 'P' => '112121121', 'Q' => '111111222', 'R' => '211111221', 'S' => '112111221', 'T' => '111121221', + 'U' => '221111112', 'V' => '122111112', 'W' => '222111111', 'X' => '121121112', 'Y' => '221121111', + 'Z' => '122121111', '-' => '121111212', '.' => '221111211', ' ' => '122111211', '$' => '121212111', + '/' => '121211121', '+' => '121112121', '%' => '111212121', '*' => '121121211', + ]; + + + protected static $CODE_START = '1211212111'; + + protected static $CODE_END = '121121211'; public function __construct(string $content = '', int $size = 20, int $orientation = 0) { - $this->content = $content; - $this->size = $size; - $this->orientation = $orientation; - } - - public function setOrientation(int $orientation) - { - $this->orientation = $orientation; + parent::__construct(strtoupper($content), $size, $orientation); } public function setContent(string $content) { - $this->content = $content; + parent::setContent(strtoupper($content)); } - public function setSize(int $size) - { - $this->size = $size; - } - - public function get() + protected function generateCodeString() { $codeString = ''; - $chksum = 104; + $length = strlen($this->content); - /* Order is critical for checksum */ - $codeArray = [ - ' ' => '212222', '!' => '222122', '\'' => '222221', '#' => '121223', '$' => '121322', '%' => '131222', - '&' => '122213', '\'' => '122312', '(' => '132212', ')' => '221213', '*' => '221312', '+' => '231212', - ',' => '112232', '-' => '122132', '.' => '122231', '/' => '113222', '0' => '123122', '1' => '123221', - '2' => '223211', '3' => '221132', '4' => '221231', '5' => '213212', '6' => '223112', '7' => '312131', - '8' => '311222', '9' => '321122', ':' => '321221', ';' => '312212', '<' => '322112', '=' => '322211', - '>' => '212123', '?' => '212321', '@' => '232121', 'A' => '111323', 'B' => '131123', 'C' => '131321', - 'D' => '112313', 'E' => '132113', 'F' => '132311', 'G' => '211313', 'H' => '231113', 'I' => '231311', - 'J' => '112133', 'K' => '112331', 'L' => '132131', 'M' => '113123', 'N' => '113321', 'O' => '133121', - 'P' => '313121', 'Q' => '211331', 'R' => '231131', 'S' => '213113', 'T' => '213311', 'U' => '213131', - 'V' => '311123', 'W' => '311321', 'X' => '331121', 'Y' => '312113', 'Z' => '312311', '[' => '332111', - '\\' => '314111', ']' => '221411', '^' => '431111', '_' => '111224', '\`' => '111422', 'a' => '121124', - 'b' => '121421', 'c' => '141122', 'd' => '141221', 'e' => '112214', 'f' => '112412', 'g' => '122114', - 'h' => '122411', 'i' => '142112', 'j' => '142211', 'k' => '241211', 'l' => '221114', 'm' => '413111', - 'n' => '241112', 'o' => '134111', 'p' => '111242', 'q' => '121142', 'r' => '121241', 's' => '114212', - 't' => '124112', 'u' => '124211', 'v' => '411212', 'w' => '421112', 'x' => '421211', 'y' => '212141', - 'z' => '214121', '{' => '412121', '|' => '111143', '}' => '111341', '~' => '131141', 'DEL' => '114113', - 'FNC 3' => '114311', 'FNC 2' => '411113', 'SHIFT' => '411311', 'CODE C' => '113141', 'FNC 4' => '114131', - 'CODE A' => '311141', 'FNC 1' => '411131', 'Start A' => '211412', 'Start B' => '211214', - 'Start C' => '211232', 'Stop' => '2331112', - ]; - - $codeKeys = array_keys($codeArray); - $codeValues = array_flip($codeKeys); - - for ($X = 1; $X <= strlen($this->content); $X++) { - $activeKey = substr($this->content, ($X - 1), 1); - $codeString .= $codeArray[$activeKey]; - $chksum = ($chksum + ($codeValues[$activeKey] * $X)); - } - $codeString .= $codeArray[$codeKeys[($chksum - (intval($chksum / 103) * 103))]]; - $codeString = '211214' . $codeString . '2331112'; - $codeLength = 20; - - for ($i = 1; $i <= strlen($codeString); $i++) { - $codeLength = $codeLength + (integer) (substr($codeString, ($i - 1), 1)); + for ($X = 1; $X <= $length; $X++) { + $codeString .= self::$CODEARRAY[substr($this->content, ($X - 1), 1)] . '1'; } - if (strtolower($this->orientation) == 'horizontal') { - $imgWidth = $codeLength; - $imgHeight = $this->size; - } else { - $imgWidth = $this->size; - $imgHeight = $codeLength; - } - - $image = imagecreate($imgWidth, $imgHeight); - $black = imagecolorallocate($image, 0, 0, 0); - $white = imagecolorallocate($image, 255, 255, 255); - $location = 0; - imagefill($image, 0, 0, $white); - - for ($position = 1; $position <= strlen($codeString); $position++) { - $cur_size = $location + (substr($codeString, ($position - 1), 1)); - - if (strtolower($this->orientation) == 'horizontal') { - imagefilledrectangle($image, $location, 0, $cur_size, $imgHeight, ($position % 2 == 0 ? $white : $black)); - } else { - imagefilledrectangle($image, 0, $location, $imgWidth, $cur_size, ($position % 2 == 0 ? $white : $black)); - } - - $location = $cur_size; - } - - return $image; + return $codeString; } } diff --git a/Utils/Barcode/Codebar.php b/Utils/Barcode/Codebar.php index 3721d7d34..87d04c9d8 100644 --- a/Utils/Barcode/Codebar.php +++ b/Utils/Barcode/Codebar.php @@ -1,103 +1,41 @@ content = $content; - $this->size = $size; - $this->orientation = $orientation; - } - - public function setOrientation(int $orientation) - { - $this->orientation = $orientation; + parent::__construct(strtoupper($content), $size, $orientation); } public function setContent(string $content) { - $this->content = $content; + parent::setContent(strtoupper($content)); } - public function setSize(int $size) - { - $this->size = $size; - } - - public function get() + protected function generateCodeString() { $codeString = ''; - $chksum = 104; + $length = strlen($this->content); - /* Order is critical for checksum */ - $codeArray = [ - ' ' => '212222', '!' => '222122', '\'' => '222221', '#' => '121223', '$' => '121322', '%' => '131222', - '&' => '122213', '\'' => '122312', '(' => '132212', ')' => '221213', '*' => '221312', '+' => '231212', - ',' => '112232', '-' => '122132', '.' => '122231', '/' => '113222', '0' => '123122', '1' => '123221', - '2' => '223211', '3' => '221132', '4' => '221231', '5' => '213212', '6' => '223112', '7' => '312131', - '8' => '311222', '9' => '321122', ':' => '321221', ';' => '312212', '<' => '322112', '=' => '322211', - '>' => '212123', '?' => '212321', '@' => '232121', 'A' => '111323', 'B' => '131123', 'C' => '131321', - 'D' => '112313', 'E' => '132113', 'F' => '132311', 'G' => '211313', 'H' => '231113', 'I' => '231311', - 'J' => '112133', 'K' => '112331', 'L' => '132131', 'M' => '113123', 'N' => '113321', 'O' => '133121', - 'P' => '313121', 'Q' => '211331', 'R' => '231131', 'S' => '213113', 'T' => '213311', 'U' => '213131', - 'V' => '311123', 'W' => '311321', 'X' => '331121', 'Y' => '312113', 'Z' => '312311', '[' => '332111', - '\\' => '314111', ']' => '221411', '^' => '431111', '_' => '111224', '\`' => '111422', 'a' => '121124', - 'b' => '121421', 'c' => '141122', 'd' => '141221', 'e' => '112214', 'f' => '112412', 'g' => '122114', - 'h' => '122411', 'i' => '142112', 'j' => '142211', 'k' => '241211', 'l' => '221114', 'm' => '413111', - 'n' => '241112', 'o' => '134111', 'p' => '111242', 'q' => '121142', 'r' => '121241', 's' => '114212', - 't' => '124112', 'u' => '124211', 'v' => '411212', 'w' => '421112', 'x' => '421211', 'y' => '212141', - 'z' => '214121', '{' => '412121', '|' => '111143', '}' => '111341', '~' => '131141', 'DEL' => '114113', - 'FNC 3' => '114311', 'FNC 2' => '411113', 'SHIFT' => '411311', 'CODE C' => '113141', 'FNC 4' => '114131', - 'CODE A' => '311141', 'FNC 1' => '411131', 'Start A' => '211412', 'Start B' => '211214', - 'Start C' => '211232', 'Stop' => '2331112', - ]; - - $codeKeys = array_keys($codeArray); - $codeValues = array_flip($codeKeys); - - for ($X = 1; $X <= strlen($this->content); $X++) { - $activeKey = substr($this->content, ($X - 1), 1); - $codeString .= $codeArray[$activeKey]; - $chksum = ($chksum + ($codeValues[$activeKey] * $X)); - } - $codeString .= $codeArray[$codeKeys[($chksum - (intval($chksum / 103) * 103))]]; - $codeString = '211214' . $codeString . '2331112'; - $codeLength = 20; - - for ($i = 1; $i <= strlen($codeString); $i++) { - $codeLength = $codeLength + (integer) (substr($codeString, ($i - 1), 1)); - } - - if (strtolower($this->orientation) == 'horizontal') { - $imgWidth = $codeLength; - $imgHeight = $this->size; - } else { - $imgWidth = $this->size; - $imgHeight = $codeLength; - } - - $image = imagecreate($imgWidth, $imgHeight); - $black = imagecolorallocate($image, 0, 0, 0); - $white = imagecolorallocate($image, 255, 255, 255); - $location = 0; - imagefill($image, 0, 0, $white); - - for ($position = 1; $position <= strlen($codeString); $position++) { - $cur_size = $location + (substr($codeString, ($position - 1), 1)); - - if (strtolower($this->orientation) == 'horizontal') { - imagefilledrectangle($image, $location, 0, $cur_size, $imgHeight, ($position % 2 == 0 ? $white : $black)); - } else { - imagefilledrectangle($image, 0, $location, $imgWidth, $cur_size, ($position % 2 == 0 ? $white : $black)); + for ($posX = 1; $posX <= $length; $posX++) { + for ($posY = 0; $posY < count(self::$CODEARRAY); $posY++) { + if (substr($this->content, ($posX - 1), 1) == self::$CODEARRAY[$posY]) + $codeString .= self::$CODEARRAY2[$posY] . '1'; } - - $location = $cur_size; } - return $image; + return $codeString; } } diff --git a/Utils/Barcode/OrientationType.php b/Utils/Barcode/OrientationType.php index e69de29bb..818ec831f 100644 --- a/Utils/Barcode/OrientationType.php +++ b/Utils/Barcode/OrientationType.php @@ -0,0 +1,35 @@ + + * @author Dennis Eichhorn + * @copyright 2013 Dennis Eichhorn + * @license OMS License 1.0 + * @version 1.0.0 + * @link http://orange-management.com + */ +namespace phpOMS\Utils\Barcode; + +use phpOMS\Datatypes\Enum; + +/** + * Account type enum. + * + * @category Framework + * @package phpOMS\DataStorage\Database + * @author OMS Development Team + * @author Dennis Eichhorn + * @license OMS License 1.0 + * @link http://orange-management.com + * @since 1.0.0 + */ +abstract class OrientationType extends Enum +{ + const HORIZONTAL = 0; + const VERTICAL = 1; +}