/barcodes.php
PHP | 2709 lines | 2132 code | 56 blank | 521 comment | 298 complexity | 663df485ef7d628a29cdaca2c4bc7185 MD5 | raw file
Large files files are truncated, but you can click here to view the full file
- <?php
- //============================================================+
- // File name : barcodes.php
- // @author DNS
- class DNS2DBarcode {
- /**
- * Array representation of barcode.
- * @protected
- */
- protected $barcode_array = false;
- /**
- *path to save png in getBarcodePNGPath
- * @var <type>
- */
- public $save_path;
- /**
- * Return an array representations of barcode.
- * @return array
- */
- public function getBarcodeArray() {
- return $this->barcode_array;
- }
- /**
- * <li>$arrcode['code'] code to be printed on text label</li>
- * <li>$arrcode['num_rows'] required number of rows</li>
- * <li>$arrcode['num_cols'] required number of columns</li>
- * <li>$arrcode['bcode'][$r][$c] value of the cell is $r row and $c column (0 = transparent, 1 = black)</li></ul>
- * @param $code (string) code to print
- * @param $type (string) type of barcode: <ul><li>DATAMATRIX : Datamatrix (ISO/IEC 16022)</li><li>PDF417 : PDF417 (ISO/IEC 15438:2006)</li><li>PDF417,a,e,t,s,f,o0,o1,o2,o3,o4,o5,o6 : PDF417 with parameters: a = aspect ratio (width/height); e = error correction level (0-8); t = total number of macro segments; s = macro segment index (0-99998); f = file ID; o0 = File Name (text); o1 = Segment Count (numeric); o2 = Time Stamp (numeric); o3 = Sender (text); o4 = Addressee (text); o5 = File Size (numeric); o6 = Checksum (numeric). NOTES: Parameters t, s and f are required for a Macro Control Block, all other parametrs are optional. To use a comma character ',' on text options, replace it with the character 255: "\xff".</li><li>QRCODE : QRcode Low error correction</li><li>QRCODE,L : QRcode Low error correction</li><li>QRCODE,M : QRcode Medium error correction</li><li>QRCODE,Q : QRcode Better error correction</li><li>QRCODE,H : QR-CODE Best error correction</li><li>RAW: raw mode - comma-separad list of array rows</li><li>RAW2: raw mode - array rows are surrounded by square parenthesis.</li><li>TEST : Test matrix</li></ul>
- * Send barcode as SVG image object to the standard output.
- * @param $w (int) Width of a single rectangle element in user units.
- * @param $h (int) Height of a single rectangle element in user units.
- * @param $color (string) Foreground color (in SVG format) for bar elements (background is transparent).
- * @public
- */
- public function getBarcodeSVG($code, $type, $w=3, $h=3, $color='black') {
- //set barcode code and type
- $this->setBarcode($code, $type);
- // send headers
- $code = $this->getBarcodeSVGcode($w, $h, $color);
- header('Content-Type: application/svg+xml');
- header('Cache-Control: public, must-revalidate, max-age=0'); // HTTP/1.1
- header('Pragma: public');
- header('Expires: Sat, 26 Jul 1997 05:00:00 GMT'); // Date in the past
- header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
- header('Content-Disposition: inline; filename="' . md5($code) . '.svg";');
- //header('Content-Length: '.strlen($code));
- echo $code;
- }
- /**
- * Return a SVG string representation of barcode.
- * <li>$arrcode['code'] code to be printed on text label</li>
- * <li>$arrcode['num_rows'] required number of rows</li>
- * <li>$arrcode['num_cols'] required number of columns</li>
- * <li>$arrcode['bcode'][$r][$c] value of the cell is $r row and $c column (0 = transparent, 1 = black)</li></ul>
- * @param $code (string) code to print
- * @param $type (string) type of barcode: <ul><li>DATAMATRIX : Datamatrix (ISO/IEC 16022)</li><li>PDF417 : PDF417 (ISO/IEC 15438:2006)</li><li>PDF417,a,e,t,s,f,o0,o1,o2,o3,o4,o5,o6 : PDF417 with parameters: a = aspect ratio (width/height); e = error correction level (0-8); t = total number of macro segments; s = macro segment index (0-99998); f = file ID; o0 = File Name (text); o1 = Segment Count (numeric); o2 = Time Stamp (numeric); o3 = Sender (text); o4 = Addressee (text); o5 = File Size (numeric); o6 = Checksum (numeric). NOTES: Parameters t, s and f are required for a Macro Control Block, all other parametrs are optional. To use a comma character ',' on text options, replace it with the character 255: "\xff".</li><li>QRCODE : QRcode Low error correction</li><li>QRCODE,L : QRcode Low error correction</li><li>QRCODE,M : QRcode Medium error correction</li><li>QRCODE,Q : QRcode Better error correction</li><li>QRCODE,H : QR-CODE Best error correction</li><li>RAW: raw mode - comma-separad list of array rows</li><li>RAW2: raw mode - array rows are surrounded by square parenthesis.</li><li>TEST : Test matrix</li></ul>
- * @param $w (int) Width of a single rectangle element in user units.
- * @param $h (int) Height of a single rectangle element in user units.
- * @param $color (string) Foreground color (in SVG format) for bar elements (background is transparent).
- * @return string SVG code.
- * @public
- */
- public function getBarcodeSVGcode($code, $type, $w=3, $h=3, $color='black') {
- //set barcode code and type
- $this->setBarcode($code, $type);
- // replace table for special characters
- $repstr = array("\0" => '', '&' => '&', '<' => '<', '>' => '>');
- $svg = '<' . '?' . 'xml version="1.0" standalone="no"' . '?' . '>' . "\n";
- $svg .= '<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">' . "\n";
- $svg .= '<svg width="' . round(($this->barcode_array['num_cols'] * $w), 3) . '" height="' . round(($this->barcode_array['num_rows'] * $h), 3) . '" version="1.1" xmlns="http://www.w3.org/2000/svg">' . "\n";
- $svg .= "\t" . '<desc>' . strtr($this->barcode_array['code'], $repstr) . '</desc>' . "\n";
- $svg .= "\t" . '<g id="elements" fill="' . $color . '" stroke="none">' . "\n";
- // print barcode elements
- $y = 0;
- // for each row
- for ($r = 0; $r < $this->barcode_array['num_rows']; ++$r) {
- $x = 0;
- // for each column
- for ($c = 0; $c < $this->barcode_array['num_cols']; ++$c) {
- if ($this->barcode_array['bcode'][$r][$c] == 1) {
- // draw a single barcode cell
- $svg .= "\t\t" . '<rect x="' . $x . '" y="' . $y . '" width="' . $w . '" height="' . $h . '" />' . "\n";
- }
- $x += $w;
- }
- $y += $h;
- }
- $svg .= "\t" . '</g>' . "\n";
- $svg .= '</svg>' . "\n";
- return $svg;
- }
- /**
- * Return an HTML representation of barcode.
- * <li>$arrcode['code'] code to be printed on text label</li>
- * <li>$arrcode['num_rows'] required number of rows</li>
- * <li>$arrcode['num_cols'] required number of columns</li>
- * <li>$arrcode['bcode'][$r][$c] value of the cell is $r row and $c column (0 = transparent, 1 = black)</li></ul>
- * @param $code (string) code to print
- * @param $type (string) type of barcode: <ul><li>DATAMATRIX : Datamatrix (ISO/IEC 16022)</li><li>PDF417 : PDF417 (ISO/IEC 15438:2006)</li><li>PDF417,a,e,t,s,f,o0,o1,o2,o3,o4,o5,o6 : PDF417 with parameters: a = aspect ratio (width/height); e = error correction level (0-8); t = total number of macro segments; s = macro segment index (0-99998); f = file ID; o0 = File Name (text); o1 = Segment Count (numeric); o2 = Time Stamp (numeric); o3 = Sender (text); o4 = Addressee (text); o5 = File Size (numeric); o6 = Checksum (numeric). NOTES: Parameters t, s and f are required for a Macro Control Block, all other parametrs are optional. To use a comma character ',' on text options, replace it with the character 255: "\xff".</li><li>QRCODE : QRcode Low error correction</li><li>QRCODE,L : QRcode Low error correction</li><li>QRCODE,M : QRcode Medium error correction</li><li>QRCODE,Q : QRcode Better error correction</li><li>QRCODE,H : QR-CODE Best error correction</li><li>RAW: raw mode - comma-separad list of array rows</li><li>RAW2: raw mode - array rows are surrounded by square parenthesis.</li><li>TEST : Test matrix</li></ul>
- * @param $w (int) Width of a single rectangle element in pixels.
- * @param $h (int) Height of a single rectangle element in pixels.
- * @param $color (string) Foreground color for bar elements (background is transparent).
- * @return string HTML code.
- * @public
- */
- public function getBarcodeHTML($code, $type, $w=10, $h=10, $color='black') {
- //set barcode code and type
- $this->setBarcode($code, $type);
- $html = '<div style="font-size:0;position:relative;width:' . ($w * $this->barcode_array['num_cols']) . 'px;height:' . ($h * $this->barcode_array['num_rows']) . 'px;">' . "\n";
- // print barcode elements
- $y = 0;
- // for each row
- for ($r = 0; $r < $this->barcode_array['num_rows']; ++$r) {
- $x = 0;
- // for each column
- for ($c = 0; $c < $this->barcode_array['num_cols']; ++$c) {
- if ($this->barcode_array['bcode'][$r][$c] == 1) {
- // draw a single barcode cell
- $html .= '<div style="background-color:' . $color . ';width:' . $w . 'px;height:' . $h . 'px;position:absolute;left:' . $x . 'px;top:' . $y . 'px;"> </div>' . "\n";
- }
- $x += $w;
- }
- $y += $h;
- }
- $html .= '</div>' . "\n";
- return $html;
- }
- /**
- * Return a PNG image representation of barcode (requires GD or Imagick library).
- * <li>$arrcode['code'] code to be printed on text label</li>
- * <li>$arrcode['num_rows'] required number of rows</li>
- * <li>$arrcode['num_cols'] required number of columns</li>
- * <li>$arrcode['bcode'][$r][$c] value of the cell is $r row and $c column (0 = transparent, 1 = black)</li></ul>
- * @param $code (string) code to print
- * @param $type (string) type of barcode: <ul><li>DATAMATRIX : Datamatrix (ISO/IEC 16022)</li><li>PDF417 : PDF417 (ISO/IEC 15438:2006)</li><li>PDF417,a,e,t,s,f,o0,o1,o2,o3,o4,o5,o6 : PDF417 with parameters: a = aspect ratio (width/height); e = error correction level (0-8); t = total number of macro segments; s = macro segment index (0-99998); f = file ID; o0 = File Name (text); o1 = Segment Count (numeric); o2 = Time Stamp (numeric); o3 = Sender (text); o4 = Addressee (text); o5 = File Size (numeric); o6 = Checksum (numeric). NOTES: Parameters t, s and f are required for a Macro Control Block, all other parametrs are optional. To use a comma character ',' on text options, replace it with the character 255: "\xff".</li><li>QRCODE : QRcode Low error correction</li><li>QRCODE,L : QRcode Low error correction</li><li>QRCODE,M : QRcode Medium error correction</li><li>QRCODE,Q : QRcode Better error correction</li><li>QRCODE,H : QR-CODE Best error correction</li><li>RAW: raw mode - comma-separad list of array rows</li><li>RAW2: raw mode - array rows are surrounded by square parenthesis.</li><li>TEST : Test matrix</li></ul>
- * @param $w (int) Width of a single rectangle element in pixels.
- * @param $h (int) Height of a single rectangle element in pixels.
- * @param $color (array) RGB (0-255) foreground color for bar elements (background is transparent).
- * @return image or false in case of error.
- * @public
- */
- public function getBarcodePNG($code, $type, $w=3, $h=3, $color=array(0, 0, 0)) {
- //set barcode code and type
- $this->setBarcode($code, $type);
- // calculate image size
- $width = ($this->barcode_array['num_cols'] * $w);
- $height = ($this->barcode_array['num_rows'] * $h);
- if (function_exists('imagecreate')) {
- // GD library
- $imagick = false;
- $png = imagecreate($width, $height);
- $bgcol = imagecolorallocate($png, 255, 255, 255);
- imagecolortransparent($png, $bgcol);
- $fgcol = imagecolorallocate($png, $color[0], $color[1], $color[2]);
- } elseif (extension_loaded('imagick')) {
- $imagick = true;
- $bgcol = new imagickpixel('rgb(255,255,255');
- $fgcol = new imagickpixel('rgb(' . $color[0] . ',' . $color[1] . ',' . $color[2] . ')');
- $png = new Imagick();
- $png->newImage($width, $height, 'none', 'png');
- $bar = new imagickdraw();
- $bar->setfillcolor($fgcol);
- } else {
- return false;
- }
- // print barcode elements
- $y = 0;
- // for each row
- for ($r = 0; $r < $this->barcode_array['num_rows']; ++$r) {
- $x = 0;
- // for each column
- for ($c = 0; $c < $this->barcode_array['num_cols']; ++$c) {
- if ($this->barcode_array['bcode'][$r][$c] == 1) {
- // draw a single barcode cell
- if ($imagick) {
- $bar->rectangle($x, $y, ($x + $w), ($y + $h));
- } else {
- imagefilledrectangle($png, $x, $y, ($x + $w), ($y + $h), $fgcol);
- }
- }
- $x += $w;
- }
- $y += $h;
- }
- // send headers
- header('Content-Type: image/png');
- header('Cache-Control: public, must-revalidate, max-age=0'); // HTTP/1.1
- header('Pragma: public');
- header('Expires: Sat, 26 Jul 1997 05:00:00 GMT'); // Date in the past
- header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
- if ($imagick) {
- $png->drawimage($bar);
- echo $png;
- } else {
- imagepng($png);
- imagedestroy($png);
- }
- }
- /**
- * Return a .png file path which create in server
- * <li>$arrcode['code'] code to be printed on text label</li>
- * <li>$arrcode['num_rows'] required number of rows</li>
- * <li>$arrcode['num_cols'] required number of columns</li>
- * <li>$arrcode['bcode'][$r][$c] value of the cell is $r row and $c column (0 = transparent, 1 = black)</li></ul>
- * @param $code (string) code to print
- * @param $type (string) type of barcode: <ul><li>DATAMATRIX : Datamatrix (ISO/IEC 16022)</li><li>PDF417 : PDF417 (ISO/IEC 15438:2006)</li><li>PDF417,a,e,t,s,f,o0,o1,o2,o3,o4,o5,o6 : PDF417 with parameters: a = aspect ratio (width/height); e = error correction level (0-8); t = total number of macro segments; s = macro segment index (0-99998); f = file ID; o0 = File Name (text); o1 = Segment Count (numeric); o2 = Time Stamp (numeric); o3 = Sender (text); o4 = Addressee (text); o5 = File Size (numeric); o6 = Checksum (numeric). NOTES: Parameters t, s and f are required for a Macro Control Block, all other parametrs are optional. To use a comma character ',' on text options, replace it with the character 255: "\xff".</li><li>QRCODE : QRcode Low error correction</li><li>QRCODE,L : QRcode Low error correction</li><li>QRCODE,M : QRcode Medium error correction</li><li>QRCODE,Q : QRcode Better error correction</li><li>QRCODE,H : QR-CODE Best error correction</li><li>RAW: raw mode - comma-separad list of array rows</li><li>RAW2: raw mode - array rows are surrounded by square parenthesis.</li><li>TEST : Test matrix</li></ul>
- * @param $w (int) Width of a single rectangle element in pixels.
- * @param $h (int) Height of a single rectangle element in pixels.
- * @param $color (array) RGB (0-255) foreground color for bar elements (background is transparent).
- * @return path of image whice created
- * @public
- */
- public function getBarcodePNGPath($code, $type, $w=3, $h=3, $color=array(0, 0, 0)) {
- //set barcode code and type
- $this->setBarcode($code, $type);
- // calculate image size
- $width = ($this->barcode_array['num_cols'] * $w);
- $height = ($this->barcode_array['num_rows'] * $h);
- if (function_exists('imagecreate')) {
- // GD library
- $imagick = false;
- $png = imagecreate($width, $height);
- $bgcol = imagecolorallocate($png, 255, 255, 255);
- imagecolortransparent($png, $bgcol);
- $fgcol = imagecolorallocate($png, $color[0], $color[1], $color[2]);
- } elseif (extension_loaded('imagick')) {
- $imagick = true;
- $bgcol = new imagickpixel('rgb(255,255,255');
- $fgcol = new imagickpixel('rgb(' . $color[0] . ',' . $color[1] . ',' . $color[2] . ')');
- $png = new Imagick();
- $png->newImage($width, $height, 'none', 'png');
- $bar = new imagickdraw();
- $bar->setfillcolor($fgcol);
- } else {
- return false;
- }
- // print barcode elements
- $y = 0;
- // for each row
- for ($r = 0; $r < $this->barcode_array['num_rows']; ++$r) {
- $x = 0;
- // for each column
- for ($c = 0; $c < $this->barcode_array['num_cols']; ++$c) {
- if ($this->barcode_array['bcode'][$r][$c] == 1) {
- // draw a single barcode cell
- if ($imagick) {
- $bar->rectangle($x, $y, ($x + $w), ($y + $h));
- } else {
- imagefilledrectangle($png, $x, $y, ($x + $w), ($y + $h), $fgcol);
- }
- }
- $x += $w;
- }
- $y += $h;
- }
- $save_file = $this->checkfile($this->save_path.$code . ".png");
- if ($imagick) {
- $png->drawimage($bar);
- //echo $png;
- }
- if (ImagePng($png, $save_file)) {
- imagedestroy($png);
- return $save_file;
- } else {
- imagedestroy($png);
- return $code;
- }
- }
- /**
- * Set the barcode.
- * @param $code (string) code to print
- * @param $type (string) type of barcode: <ul><li>DATAMATRIX : Datamatrix (ISO/IEC 16022)</li><li>PDF417 : PDF417 (ISO/IEC 15438:2006)</li><li>PDF417,a,e,t,s,f,o0,o1,o2,o3,o4,o5,o6 : PDF417 with parameters: a = aspect ratio (width/height); e = error correction level (0-8); t = total number of macro segments; s = macro segment index (0-99998); f = file ID; o0 = File Name (text); o1 = Segment Count (numeric); o2 = Time Stamp (numeric); o3 = Sender (text); o4 = Addressee (text); o5 = File Size (numeric); o6 = Checksum (numeric). NOTES: Parameters t, s and f are required for a Macro Control Block, all other parametrs are optional. To use a comma character ',' on text options, replace it with the character 255: "\xff".</li><li>QRCODE : QRcode Low error correction</li><li>QRCODE,L : QRcode Low error correction</li><li>QRCODE,M : QRcode Medium error correction</li><li>QRCODE,Q : QRcode Better error correction</li><li>QRCODE,H : QR-CODE Best error correction</li><li>RAW: raw mode - comma-separad list of array rows</li><li>RAW2: raw mode - array rows are surrounded by square parenthesis.</li><li>TEST : Test matrix</li></ul>
- * @return array
- */
- public function setBarcode($code, $type) {
- $mode = explode(',', $type);
- $qrtype = strtoupper($mode[0]);
- switch ($qrtype) {
- case 'DATAMATRIX': { // DATAMATRIX (ISO/IEC 16022)
- require_once(dirname(__FILE__) . '/datamatrix.php');
- $qrcode = new Datamatrix($code);
- $this->barcode_array = $qrcode->getBarcodeArray();
- $this->barcode_array['code'] = $code;
- break;
- }
- case 'PDF417': { // PDF417 (ISO/IEC 15438:2006)
- require_once(dirname(__FILE__) . '/pdf417.php');
- if (!isset($mode[1]) OR ($mode[1] === '')) {
- $aspectratio = 2; // default aspect ratio (width / height)
- } else {
- $aspectratio = floatval($mode[1]);
- }
- if (!isset($mode[2]) OR ($mode[2] === '')) {
- $ecl = -1; // default error correction level (auto)
- } else {
- $ecl = intval($mode[2]);
- }
- // set macro block
- $macro = array();
- if (isset($mode[3]) AND ($mode[3] !== '') AND isset($mode[4]) AND ($mode[4] !== '') AND isset($mode[5]) AND ($mode[5] !== '')) {
- $macro['segment_total'] = intval($mode[3]);
- $macro['segment_index'] = intval($mode[4]);
- $macro['file_id'] = strtr($mode[5], "\xff", ',');
- for ($i = 0; $i < 7; ++$i) {
- $o = $i + 6;
- if (isset($mode[$o]) AND ($mode[$o] !== '')) {
- // add option
- $macro['option_' . $i] = strtr($mode[$o], "\xff", ',');
- }
- }
- }
- $qrcode = new PDF417($code, $ecl, $aspectratio, $macro);
- $this->barcode_array = $qrcode->getBarcodeArray();
- $this->barcode_array['code'] = $code;
- break;
- }
- case 'QRCODE': { // QR-CODE
- require_once(dirname(__FILE__) . '/qrcode.php');
- if (!isset($mode[1]) OR (!in_array($mode[1], array('L', 'M', 'Q', 'H')))) {
- $mode[1] = 'L'; // Ddefault: Low error correction
- }
- $qrcode = new QRcode($code, strtoupper($mode[1]));
- $this->barcode_array = $qrcode->getBarcodeArray();
- $this->barcode_array['code'] = $code;
- break;
- }
- case 'RAW':
- case 'RAW2': { // RAW MODE
- // remove spaces
- $code = preg_replace('/[\s]*/si', '', $code);
- if (strlen($code) < 3) {
- break;
- }
- if ($qrtype == 'RAW') {
- // comma-separated rows
- $rows = explode(',', $code);
- } else { // RAW2
- // rows enclosed in square parentheses
- $code = substr($code, 1, -1);
- $rows = explode('][', $code);
- }
- $this->barcode_array['num_rows'] = count($rows);
- $this->barcode_array['num_cols'] = strlen($rows[0]);
- $this->barcode_array['bcode'] = array();
- foreach ($rows as $r) {
- $this->barcode_array['bcode'][] = str_split($r, 1);
- }
- $this->barcode_array['code'] = $code;
- break;
- }
- default: {
- $this->barcode_array = false;
- }
- }
- }
- public function checkfile($path) {
- if (file_exists($path)) {
- $base_name = pathinfo($path, PATHINFO_BASENAME);
- return $this->checkfile(str_replace($base_name, rand(0, 9999) . $base_name, $path));
- } else {
- return $path;
- }
- }
- }
- // end of class
- //============================================================+
- class DNS1DBarcode {
- /**
- * Array representation of barcode.
- * @protected
- */
- protected $barcode_array;
- /**
- *path to save png in getBarcodePNGPath
- * @var <type>
- */
- public $save_path;
- /**
- * This is the class constructor.
- * Return an array representations for common 1D barcodes:<ul>
- * <li>$arrcode['code'] code to be printed on text label</li>
- * <li>$arrcode['maxh'] max barcode height</li>
- * <li>$arrcode['maxw'] max barcode width</li>
- * <li>$arrcode['bcode'][$k] single bar or space in $k position</li>
- * <li>$arrcode['bcode'][$k]['t'] bar type: true = bar, false = space.</li>
- * <li>$arrcode['bcode'][$k]['w'] bar width in units.</li>
- * <li>$arrcode['bcode'][$k]['h'] bar height in units.</li>
- * <li>$arrcode['bcode'][$k]['p'] bar top position (0 = top, 1 = middle)</li></ul>
- * @param $code (string) code to print
- * @param $type (string) type of barcode: <ul><li>C39 : CODE 39 - ANSI MH10.8M-1983 - USD-3 - 3 of 9.</li><li>C39+ : CODE 39 with checksum</li><li>C39E : CODE 39 EXTENDED</li><li>C39E+ : CODE 39 EXTENDED + CHECKSUM</li><li>C93 : CODE 93 - USS-93</li><li>S25 : Standard 2 of 5</li><li>S25+ : Standard 2 of 5 + CHECKSUM</li><li>I25 : Interleaved 2 of 5</li><li>I25+ : Interleaved 2 of 5 + CHECKSUM</li><li>C128 : CODE 128</li><li>C128A : CODE 128 A</li><li>C128B : CODE 128 B</li><li>C128C : CODE 128 C</li><li>EAN2 : 2-Digits UPC-Based Extention</li><li>EAN5 : 5-Digits UPC-Based Extention</li><li>EAN8 : EAN 8</li><li>EAN13 : EAN 13</li><li>UPCA : UPC-A</li><li>UPCE : UPC-E</li><li>MSI : MSI (Variation of Plessey code)</li><li>MSI+ : MSI + CHECKSUM (modulo 11)</li><li>POSTNET : POSTNET</li><li>PLANET : PLANET</li><li>RMS4CC : RMS4CC (Royal Mail 4-state Customer Code) - CBC (Customer Bar Code)</li><li>KIX : KIX (Klant index - Customer index)</li><li>IMB: Intelligent Mail Barcode - Onecode - USPS-B-3200</li><li>CODABAR : CODABAR</li><li>CODE11 : CODE 11</li><li>PHARMA : PHARMACODE</li><li>PHARMA2T : PHARMACODE TWO-TRACKS</li></ul>
- * @public
- */
-
- /**
- * Return an array representations of barcode.
- * @return array
- * @public
- */
- public function getBarcodeArray() {
- return $this->barcode_array;
- }
- /**
- * Send barcode as SVG image object to the standard output.
- * @param $code (string) code to print
- * @param $type (string) type of barcode: <ul><li>C39 : CODE 39 - ANSI MH10.8M-1983 - USD-3 - 3 of 9.</li><li>C39+ : CODE 39 with checksum</li><li>C39E : CODE 39 EXTENDED</li><li>C39E+ : CODE 39 EXTENDED + CHECKSUM</li><li>C93 : CODE 93 - USS-93</li><li>S25 : Standard 2 of 5</li><li>S25+ : Standard 2 of 5 + CHECKSUM</li><li>I25 : Interleaved 2 of 5</li><li>I25+ : Interleaved 2 of 5 + CHECKSUM</li><li>C128 : CODE 128</li><li>C128A : CODE 128 A</li><li>C128B : CODE 128 B</li><li>C128C : CODE 128 C</li><li>EAN2 : 2-Digits UPC-Based Extention</li><li>EAN5 : 5-Digits UPC-Based Extention</li><li>EAN8 : EAN 8</li><li>EAN13 : EAN 13</li><li>UPCA : UPC-A</li><li>UPCE : UPC-E</li><li>MSI : MSI (Variation of Plessey code)</li><li>MSI+ : MSI + CHECKSUM (modulo 11)</li><li>POSTNET : POSTNET</li><li>PLANET : PLANET</li><li>RMS4CC : RMS4CC (Royal Mail 4-state Customer Code) - CBC (Customer Bar Code)</li><li>KIX : KIX (Klant index - Customer index)</li><li>IMB: Intelligent Mail Barcode - Onecode - USPS-B-3200</li><li>CODABAR : CODABAR</li><li>CODE11 : CODE 11</li><li>PHARMA : PHARMACODE</li><li>PHARMA2T : PHARMACODE TWO-TRACKS</li></ul>
- * @param $w (int) Minimum width of a single bar in user units.
- * @param $h (int) Height of barcode in user units.
- * @param $color (string) Foreground color (in SVG format) for bar elements (background is transparent).
- * @public
- */
- public function getBarcodeSVG($code, $type,$w=2, $h=30, $color='black') {
- $this->setBarcode($code, $type);
- // send headers
- $code = $this->getBarcodeSVGcode($w, $h, $color);
- header('Content-Type: application/svg+xml');
- header('Cache-Control: public, must-revalidate, max-age=0'); // HTTP/1.1
- header('Pragma: public');
- header('Expires: Sat, 26 Jul 1997 05:00:00 GMT'); // Date in the past
- header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
- header('Content-Disposition: inline; filename="'.md5($code).'.svg";');
- //header('Content-Length: '.strlen($code));
- echo $code;
- }
- /**
- * Return a SVG string representation of barcode.
- * @param $code (string) code to print
- * @param $type (string) type of barcode: <ul><li>C39 : CODE 39 - ANSI MH10.8M-1983 - USD-3 - 3 of 9.</li><li>C39+ : CODE 39 with checksum</li><li>C39E : CODE 39 EXTENDED</li><li>C39E+ : CODE 39 EXTENDED + CHECKSUM</li><li>C93 : CODE 93 - USS-93</li><li>S25 : Standard 2 of 5</li><li>S25+ : Standard 2 of 5 + CHECKSUM</li><li>I25 : Interleaved 2 of 5</li><li>I25+ : Interleaved 2 of 5 + CHECKSUM</li><li>C128 : CODE 128</li><li>C128A : CODE 128 A</li><li>C128B : CODE 128 B</li><li>C128C : CODE 128 C</li><li>EAN2 : 2-Digits UPC-Based Extention</li><li>EAN5 : 5-Digits UPC-Based Extention</li><li>EAN8 : EAN 8</li><li>EAN13 : EAN 13</li><li>UPCA : UPC-A</li><li>UPCE : UPC-E</li><li>MSI : MSI (Variation of Plessey code)</li><li>MSI+ : MSI + CHECKSUM (modulo 11)</li><li>POSTNET : POSTNET</li><li>PLANET : PLANET</li><li>RMS4CC : RMS4CC (Royal Mail 4-state Customer Code) - CBC (Customer Bar Code)</li><li>KIX : KIX (Klant index - Customer index)</li><li>IMB: Intelligent Mail Barcode - Onecode - USPS-B-3200</li><li>CODABAR : CODABAR</li><li>CODE11 : CODE 11</li><li>PHARMA : PHARMACODE</li><li>PHARMA2T : PHARMACODE TWO-TRACKS</li></ul>
- * @param $w (int) Minimum width of a single bar in user units.
- * @param $h (int) Height of barcode in user units.
- * @param $color (string) Foreground color (in SVG format) for bar elements (background is transparent).
- * @return string SVG code.
- * @public
- */
- public function getBarcodeSVGcode($code, $type,$w=2, $h=30, $color='black') {
- $this->setBarcode($code, $type);
- // replace table for special characters
- $repstr = array("\0" => '', '&' => '&', '<' => '<', '>' => '>');
- $svg = '<'.'?'.'xml version="1.0" standalone="no"'.'?'.'>'."\n";
- $svg .= '<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">'."\n";
- $svg .= '<svg width="'.round(($this->barcode_array['maxw'] * $w), 3).'" height="'.$h.'" version="1.1" xmlns="http://www.w3.org/2000/svg">'."\n";
- $svg .= "\t".'<desc>'.strtr($this->barcode_array['code'], $repstr).'</desc>'."\n";
- $svg .= "\t".'<g id="bars" fill="'.$color.'" stroke="none">'."\n";
- // print bars
- $x = 0;
- foreach ($this->barcode_array['bcode'] as $k => $v) {
- $bw = round(($v['w'] * $w), 3);
- $bh = round(($v['h'] * $h / $this->barcode_array['maxh']), 3);
- if ($v['t']) {
- $y = round(($v['p'] * $h / $this->barcode_array['maxh']), 3);
- // draw a vertical bar
- $svg .= "\t\t".'<rect x="'.$x.'" y="'.$y.'" width="'.$bw.'" height="'.$bh.'" />'."\n";
- }
- $x += $bw;
- }
- $svg .= "\t".'</g>'."\n";
- $svg .= '</svg>'."\n";
- return $svg;
- }
- /**
- * Return an HTML representation of barcode.
- * @param $code (string) code to print
- * @param $type (string) type of barcode: <ul><li>C39 : CODE 39 - ANSI MH10.8M-1983 - USD-3 - 3 of 9.</li><li>C39+ : CODE 39 with checksum</li><li>C39E : CODE 39 EXTENDED</li><li>C39E+ : CODE 39 EXTENDED + CHECKSUM</li><li>C93 : CODE 93 - USS-93</li><li>S25 : Standard 2 of 5</li><li>S25+ : Standard 2 of 5 + CHECKSUM</li><li>I25 : Interleaved 2 of 5</li><li>I25+ : Interleaved 2 of 5 + CHECKSUM</li><li>C128 : CODE 128</li><li>C128A : CODE 128 A</li><li>C128B : CODE 128 B</li><li>C128C : CODE 128 C</li><li>EAN2 : 2-Digits UPC-Based Extention</li><li>EAN5 : 5-Digits UPC-Based Extention</li><li>EAN8 : EAN 8</li><li>EAN13 : EAN 13</li><li>UPCA : UPC-A</li><li>UPCE : UPC-E</li><li>MSI : MSI (Variation of Plessey code)</li><li>MSI+ : MSI + CHECKSUM (modulo 11)</li><li>POSTNET : POSTNET</li><li>PLANET : PLANET</li><li>RMS4CC : RMS4CC (Royal Mail 4-state Customer Code) - CBC (Customer Bar Code)</li><li>KIX : KIX (Klant index - Customer index)</li><li>IMB: Intelligent Mail Barcode - Onecode - USPS-B-3200</li><li>CODABAR : CODABAR</li><li>CODE11 : CODE 11</li><li>PHARMA : PHARMACODE</li><li>PHARMA2T : PHARMACODE TWO-TRACKS</li></ul>
- * @param $w (int) Width of a single bar element in pixels.
- * @param $h (int) Height of a single bar element in pixels.
- * @param $color (string) Foreground color for bar elements (background is transparent).
- * @return string HTML code.
- * @public
- */
- public function getBarcodeHTML($code, $type,$w=2, $h=30, $color='black') {
- $this->setBarcode($code, $type);
- $html = '<div style="font-size:0;position:relative;">'."\n";
- $html = '<div style="font-size:0;position:relative;width:'.($this->barcode_array['maxw'] * $w).'px;height:'.($h).'px;">'."\n";
- // print bars
- $x = 0;
- foreach ($this->barcode_array['bcode'] as $k => $v) {
- $bw = round(($v['w'] * $w), 3);
- $bh = round(($v['h'] * $h / $this->barcode_array['maxh']), 3);
- if ($v['t']) {
- $y = round(($v['p'] * $h / $this->barcode_array['maxh']), 3);
- // draw a vertical bar
- $html .= '<div style="background-color:'.$color.';width:'.$bw.'px;height:'.$bh.'px;position:absolute;left:'.$x.'px;top:'.$y.'px;"> </div>'."\n";
- }
- $x += $bw;
- }
- $html .= '</div>'."\n";
- return $html;
- }
- /**
- * Return a PNG image representation of barcode (requires GD or Imagick library).
- * @param $code (string) code to print
- * @param $type (string) type of barcode: <ul><li>C39 : CODE 39 - ANSI MH10.8M-1983 - USD-3 - 3 of 9.</li><li>C39+ : CODE 39 with checksum</li><li>C39E : CODE 39 EXTENDED</li><li>C39E+ : CODE 39 EXTENDED + CHECKSUM</li><li>C93 : CODE 93 - USS-93</li><li>S25 : Standard 2 of 5</li><li>S25+ : Standard 2 of 5 + CHECKSUM</li><li>I25 : Interleaved 2 of 5</li><li>I25+ : Interleaved 2 of 5 + CHECKSUM</li><li>C128 : CODE 128</li><li>C128A : CODE 128 A</li><li>C128B : CODE 128 B</li><li>C128C : CODE 128 C</li><li>EAN2 : 2-Digits UPC-Based Extention</li><li>EAN5 : 5-Digits UPC-Based Extention</li><li>EAN8 : EAN 8</li><li>EAN13 : EAN 13</li><li>UPCA : UPC-A</li><li>UPCE : UPC-E</li><li>MSI : MSI (Variation of Plessey code)</li><li>MSI+ : MSI + CHECKSUM (modulo 11)</li><li>POSTNET : POSTNET</li><li>PLANET : PLANET</li><li>RMS4CC : RMS4CC (Royal Mail 4-state Customer Code) - CBC (Customer Bar Code)</li><li>KIX : KIX (Klant index - Customer index)</li><li>IMB: Intelligent Mail Barcode - Onecode - USPS-B-3200</li><li>CODABAR : CODABAR</li><li>CODE11 : CODE 11</li><li>PHARMA : PHARMACODE</li><li>PHARMA2T : PHARMACODE TWO-TRACKS</li></ul>
- * @param $w (int) Width of a single bar element in pixels.
- * @param $h (int) Height of a single bar element in pixels.
- * @param $color (array) RGB (0-255) foreground color for bar elements (background is transparent).
- * @return image or false in case of error.
- * @public
- */
- public function getBarcodePNG($code, $type,$w=2, $h=30, $color=array(0,0,0)) {
- $this->setBarcode($code, $type);
- // calculate image size
- $width = ($this->barcode_array['maxw'] * $w);
- $height = $h;
- if (function_exists('imagecreate')) {
- // GD library
- $imagick = false;
- $png = imagecreate($width, $height);
- $bgcol = imagecolorallocate($png, 255, 255, 255);
- imagecolortransparent($png, $bgcol);
- $fgcol = imagecolorallocate($png, $color[0], $color[1], $color[2]);
- } elseif (extension_loaded('imagick')) {
- $imagick = true;
- $bgcol = new imagickpixel('rgb(255,255,255');
- $fgcol = new imagickpixel('rgb('.$color[0].','.$color[1].','.$color[2].')');
- $png = new Imagick();
- $png->newImage($width, $height, 'none', 'png');
- $bar = new imagickdraw();
- $bar->setfillcolor($fgcol);
- } else {
- return false;
- }
- // print bars
- $x = 0;
- foreach ($this->barcode_array['bcode'] as $k => $v) {
- $bw = round(($v['w'] * $w), 3);
- $bh = round(($v['h'] * $h / $this->barcode_array['maxh']), 3);
- if ($v['t']) {
- $y = round(($v['p'] * $h / $this->barcode_array['maxh']), 3);
- // draw a vertical bar
- if ($imagick) {
- $bar->rectangle($x, $y, ($x + $bw), ($y + $bh));
- } else {
- imagefilledrectangle($png, $x, $y, ($x + $bw), ($y + $bh), $fgcol);
- }
- }
- $x += $bw;
- }
- // send headers
- header('Content-Type: image/png');
- header('Cache-Control: public, must-revalidate, max-age=0'); // HTTP/1.1
- header('Pragma: public');
- header('Expires: Sat, 26 Jul 1997 05:00:00 GMT'); // Date in the past
- header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
- if ($imagick) {
- $png->drawimage($bar);
- echo $png;
- } else {
- imagepng($png);
- imagedestroy($png);
- }
- }
- /**
- * Return a .png file path which create in server
- * @param $code (string) code to print
- * @param $type (string) type of barcode: <ul><li>C39 : CODE 39 - ANSI MH10.8M-1983 - USD-3 - 3 of 9.</li><li>C39+ : CODE 39 with checksum</li><li>C39E : CODE 39 EXTENDED</li><li>C39E+ : CODE 39 EXTENDED + CHECKSUM</li><li>C93 : CODE 93 - USS-93</li><li>S25 : Standard 2 of 5</li><li>S25+ : Standard 2 of 5 + CHECKSUM</li><li>I25 : Interleaved 2 of 5</li><li>I25+ : Interleaved 2 of 5 + CHECKSUM</li><li>C128 : CODE 128</li><li>C128A : CODE 128 A</li><li>C128B : CODE 128 B</li><li>C128C : CODE 128 C</li><li>EAN2 : 2-Digits UPC-Based Extention</li><li>EAN5 : 5-Digits UPC-Based Extention</li><li>EAN8 : EAN 8</li><li>EAN13 : EAN 13</li><li>UPCA : UPC-A</li><li>UPCE : UPC-E</li><li>MSI : MSI (Variation of Plessey code)</li><li>MSI+ : MSI + CHECKSUM (modulo 11)</li><li>POSTNET : POSTNET</li><li>PLANET : PLANET</li><li>RMS4CC : RMS4CC (Royal Mail 4-state Customer Code) - CBC (Customer Bar Code)</li><li>KIX : KIX (Klant index - Customer index)</li><li>IMB: Intelligent Mail Barcode - Onecode - USPS-B-3200</li><li>CODABAR : CODABAR</li><li>CODE11 : CODE 11</li><li>PHARMA : PHARMACODE</li><li>PHARMA2T : PHARMACODE TWO-TRACKS</li></ul>
- * @param $w (int) Width of a single bar element in pixels.
- * @param $h (int) Height of a single bar element in pixels.
- * @param $color (array) RGB (0-255) foreground color for bar elements (background is transparent).
- * @return image or false in case of error.
- * @public
- */
- public function getBarcodePNGPath($code, $type,$w=2, $h=30, $color=array(0,0,0)) {
- $this->setBarcode($code, $type);
- // calculate image size
- $width = ($this->barcode_array['maxw'] * $w);
- $height = $h;
- if (function_exists('imagecreate')) {
- // GD library
- $imagick = false;
- $png = imagecreate($width, $height);
- $bgcol = imagecolorallocate($png, 255, 255, 255);
- imagecolortransparent($png, $bgcol);
- $fgcol = imagecolorallocate($png, $color[0], $color[1], $color[2]);
- } elseif (extension_loaded('imagick')) {
- $imagick = true;
- $bgcol = new imagickpixel('rgb(255,255,255');
- $fgcol = new imagickpixel('rgb('.$color[0].','.$color[1].','.$color[2].')');
- $png = new Imagick();
- $png->newImage($width, $height, 'none', 'png');
- $bar = new imagickdraw();
- $bar->setfillcolor($fgcol);
- } else {
- return false;
- }
- // print bars
- $x = 0;
- foreach ($this->barcode_array['bcode'] as $k => $v) {
- $bw = round(($v['w'] * $w), 3);
- $bh = round(($v['h'] * $h / $this->barcode_array['maxh']), 3);
- if ($v['t']) {
- $y = round(($v['p'] * $h / $this->barcode_array['maxh']), 3);
- // draw a vertical bar
- if ($imagick) {
- $bar->rectangle($x, $y, ($x + $bw), ($y + $bh));
- } else {
- imagefilledrectangle($png, $x, $y, ($x + $bw), ($y + $bh), $fgcol);
- }
- }
- $x += $bw;
- }
- $save_file = $this->checkfile($this->save_path.$code . ".png");
- if ($imagick) {
- $png->drawimage($bar);
- //echo $png;
- }
- if (ImagePng($png, $save_file)) {
- imagedestroy($png);
- return $save_file;
- } else {
- imagedestroy($png);
- return $code;
- }
- }
- /**
- * Set the barcode.
- * @param $code (string) code to print
- * @param $type (string) type of barcode: <ul><li>C39 : CODE 39 - ANSI MH10.8M-1983 - USD-3 - 3 of 9.</li><li>C39+ : CODE 39 with checksum</li><li>C39E : CODE 39 EXTENDED</li><li>C39E+ : CODE 39 EXTENDED + CHECKSUM</li><li>C93 : CODE 93 - USS-93</li><li>S25 : Standard 2 of 5</li><li>S25+ : Standard 2 of 5 + CHECKSUM</li><li>I25 : Interleaved 2 of 5</li><li>I25+ : Interleaved 2 of 5 + CHECKSUM</li><li>C128 : CODE 128</li><li>C128A : CODE 128 A</li><li>C128B : CODE 128 B</li><li>C128C : CODE 128 C</li><li>EAN2 : 2-Digits UPC-Based Extention</li><li>EAN5 : 5-Digits UPC-Based Extention</li><li>EAN8 : EAN 8</li><li>EAN13 : EAN 13</li><li>UPCA : UPC-A</li><li>UPCE : UPC-E</li><li>MSI : MSI (Variation of Plessey code)</li><li>MSI+ : MSI + CHECKSUM (modulo 11)</li><li>POSTNET : POSTNET</li><li>PLANET : PLANET</li><li>RMS4CC : RMS4CC (Royal Mail 4-state Customer Code) - CBC (Customer Bar Code)</li><li>KIX : KIX (Klant index - Customer index)</li><li>IMB: Intelligent Mail Barcode - Onecode - USPS-B-3200</li><li>CODABAR : CODABAR</li><li>CODE11 : CODE 11</li><li>PHARMA : PHARMACODE</li><li>PHARMA2T : PHARMACODE TWO-TRACKS</li></ul>
- * @return array barcode array
- * @public
- */
- public function setBarcode($code, $type) {
- switch (strtoupper($type)) {
- case 'C39': { // CODE 39 - ANSI MH10.8M-1983 - USD-3 - 3 of 9.
- $arrcode = $this->barcode_code39($code, false, false);
- break;
- }
- case 'C39+': { // CODE 39 with checksum
- $arrcode = $this->barcode_code39($code, false, true);
- break;
- }
- case 'C39E': { // CODE 39 EXTENDED
- $arrcode = $this->barcode_code39($code, true, false);
- break;
- }
- case 'C39E+': { // CODE 39 EXTENDED + CHECKSUM
- $arrcode = $this->barcode_code39($code, true, true);
- break;
- }
- case 'C93': { // CODE 93 - USS-93
- $arrcode = $this->barcode_code93($code);
- break;
- }
- case 'S25': { // Standard 2 of 5
- $arrcode = $this->barcode_s25($code, false);
- break;
- }
- case 'S25+': { // Standard 2 of 5 + CHECKSUM
- $arrcode = $this->barcode_s25($code, true);
- break;
- }
- case 'I25': { // Interleaved 2 of 5
- $arrcode = $this->barcode_i25($code, false);
- break;
- }
- case 'I25+': { // Interleaved 2 of 5 + CHECKSUM
- $arrcode = $this->barcode_i25($code, true);
- break;
- }
- case 'C128': { // CODE 128
- $arrcode = $this->barcode_c128($code, '');
- break;
- }
- case 'C128A': { // CODE 128 A
- $arrcode = $this->barcode_c128($code, 'A');
- break;
- }
- case 'C128B': { // CODE 128 B
- $arrcode = $this->barcode_c128($code, 'B');
- break;
- }
- case 'C128C': { // CODE 128 C
- $arrcode = $this->barcode_c128($code, 'C');
- break;
- }
- case 'EAN2': { // 2-Digits UPC-Based Extention
- $arrcode = $this->barcode_eanext($code, 2);
- break;
- }
- case 'EAN5': { // 5-Digits UPC-Based Extention
- $arrcode = $this->barcode_eanext($code, 5);
- break;
- }
- case 'EAN8': { // EAN 8
- $arrcode = $this->barcode_eanupc($code, 8);
- break;
- }
- case 'EAN13': { // EAN 13
- $arrcode = $this->barcode_eanupc($code, 13);
- break;
- }
- case 'UPCA': { // UPC-A
- $arrcode = $this->barcode_eanupc($code, 12);
- break;
- }
- case 'UPCE': { // UPC-E
- $arrcode = $this->barcode_eanupc($code, 6);
- break;
- }
- case 'MSI': { // MSI (Variation of Plessey code)
- $arrcode = $this->barcode_msi($code, false);
- break;
- }
- case 'MSI+': { // MSI + CHECKSUM (modulo 11)
- $arrcode = $this->barcode_msi($code, true);
- break;
- }
- case 'POSTNET': { // POSTNET
- $arrcode = $this->barcode_postnet($code, false);
- break;
- }
- case 'PLANET': { // PLANET
- $arrcode = $this->barcode_postnet($code, true);
- break;
- }
- case 'RMS4CC': { // RMS4CC (Royal Mail 4-state Customer Code) - CBC (Customer Bar Code)
- $arrcode = $this->barcode_rms4cc($code, false);
- break;
- }
- case 'KIX': { // KIX (Klant index - Customer index)
- $arrcode = $this->barcode_rms4cc($code, true);
- break;
- }
- case 'IMB': { // IMB - Intelligent Mail Barcode - Onecode - USPS-B-3200
- $arrcode = $this->barcode_imb($code);
- break;
- }
- case 'CODABAR': { // CODABAR
- $arrcode = $this->barcode_codabar($code);
- break;
- }
- case 'CODE11': { // CODE 11
- $arrcode = $this->barcode_code11($code);
- break;
- }
- case 'PHARMA': { // PHARMACODE
- $arrcode = $this->barcode_pharmacode($code);
- break;
- }
- case 'PHARMA2T': { // PHARMACODE TWO-TRACKS
- $arrcode = $this->barcode_pharmacode2t($code);
- break;
- }
- default: {
- $this->barcode_array = false;
- $arrcode = false;
- break;
- }
- }
- $this->barcode_array = $arrcode;
- }
- /**
- * CODE 39 - ANSI MH10.8M-1983 - USD-3 - 3 of 9.
- * General-purpose code in very wide use world-wide
- * @param $code (string) code to represent.
- * @param $extended (boolean) if true uses the extended mode.
- * @param $checksum (boolean) if true add a checksum to the code.
- * @return array barcode representation.
- * @protected
- */
- protected function barcode_code39($code, $extended=false, $checksum=false) {
- $chr['0'] = '111331311';
- $chr['1'] = '311311113';
- $chr['2'] = '113311113';
- $chr['3'] = '313311111';
- $chr['4'] = '111331113';
- $chr['5'] = '311331111';
- $chr['6'] = '113331111';
- $chr['7'] = '111311313';
- $chr['8'] = '311311311';
- $chr['9'] = '113311311';
- $chr['A'] = '311113113';
- $chr['B'] = '113113113';
- $chr['C'] = '313113111';
- $chr['D'] = '111133113';
- $chr['E'] = '311133111';
- $chr['F'] = '113133111';
- $chr['G'] = '111113313';
- $chr['H'] = '311113311';
- $chr['I'] = '113113311';
- $chr['J'] = '111133311';
- $chr['K'] = '311111133';
- $chr['L'] = '113111133';
- $chr['M'] = '313111131';
- $chr['N'] = '111131133';
- $chr['O'] = '311131131';
- $chr['P'] = '113131131';
- $chr['Q'] = '111111333';
- $chr['R'] = '311111331';
- $chr['S'] = '113111331';
- $chr['T'] = '111131331';
- $chr['U'] = '331111113';
- $chr['V'] = '133111113';
- $chr['W'] = '333111111';
- $chr['X'] = '131131113';
- $chr['Y'] = '331131111';
- $chr['Z'] = '133131111';
- $chr['-'] = '131111313';
- $chr['.'] = '331111311';
- $chr[' '] = '133111311';
- $chr['$'] = '131313111';
- $chr['/'] = '131311131';
- $chr['+'] = '131113131';
- $chr['%'] = '111313131';
- $chr['*'] = '131131311';
- $code = strtoupper($code);
- if ($extended) {
- // extended mode
- $code = $this->encode_code39_ext($code);
- }
- if ($code === false) {
- return false;
- }
- if ($checksum) {
- // checksum
- $code .= $this->checksum_code39($code);
- }
- // add start and stop codes
- $code = '*'.$code.'*';
- $bararray = array('code' => $code, 'maxw' => 0, 'maxh' => 1, 'bcode' => array());
- $k = 0;
- $clen = strlen($code);
- for ($i = 0; $i < $clen; ++$i) {
- $char = $code{$i};
- if(!isset($chr[$char])) {
- // invalid character
- return false;
- }
- for ($j = 0; $j < 9; ++$j) {
- if (($j % 2) == 0) {
- $t = true; // bar
- } else {
- $t = false; // space
- }
- $w = $chr[$char]{$j};
- $bararray['bcode'][$k] = array('t' => $t, 'w' => $w, 'h' => 1, 'p' => 0);
- $bararray['maxw'] += $w;
- ++$k;
- }
- // intercharacter gap
- $bararray['bcode'][$k] = array('t' => false, 'w' => 1, 'h' => 1, 'p' => 0);
- $bararray['maxw'] += 1;
- ++$k;
- }
- return $bararray;
- }
- /**
- * Encode a string to be used for CODE 39 Extended mode.
- * @param $code (string) code to represent.
- * @return encoded string.
- * @protected
- */
- protected function encode_code39_ext($code) {
- $encode = array(
- chr(0) => '%U', chr(1) => '$A', chr(2) => '$B', chr(3) => '$C',
- chr(4) => '$D', chr(5) => '$E', chr(6) => '$F', chr(7) => '$G',
- chr(8) => '$H', chr(9) => '$I', chr(10) => '$J', chr(11) => '£K',
- chr(12) => '$L', chr(13) => '$M', chr(14) => '$N', chr(15) => '$O',
- chr(16) => '$P', chr(17) => '$Q', chr(18) => '$R', chr(19) => '$S',
- chr(20) => '$T', chr(21) => '$U', chr(22) => '$V', chr(23) => '$W',
- chr(24) => '$X', chr(25) => '$Y', chr(26) => '$Z', chr(27) => '%A',
- chr(28) => '%B', chr(29) => '%C', chr(30) => '%D', chr(31) => '%E',
- chr(32) => ' ', chr(33) => '/A', chr(34) => '/B', chr(35) => '/C',
- chr(36) => '/D', chr(37) => '/E', chr(38) => '/F', chr(39) => '/G',
- chr(40) => '/H', chr(41) => '/I', chr(42) => '/J', chr(43) => '/K',
- chr(44) => '/L', chr(45) => '-', chr(46) => '.', chr(47) => '/O',
- chr(48) => '0', chr(49) => '1', chr(50) => '2', chr(51) => '3',
- chr(52) => '4', chr(53) => '5', chr(54) => '6', chr(55) => '7',
- chr(56) => '8', chr(57) => '9', chr(58) => '/Z', chr(59) => '%F',
- chr(60) => '%G', chr(61) => '%H', chr(62) => '%I', chr(63) => '%J',
- chr(64) => '%V', chr(65) => 'A', chr(66) => 'B', chr(67) => 'C',
- chr(68) => 'D', chr(69) => 'E', chr(70) => 'F', chr(71) => 'G',
- chr(72) => 'H', chr(73) => 'I', chr(74) => 'J', chr(75) => 'K',
- chr(76) => 'L', chr(77) => 'M', chr(78) => 'N', chr(79) => 'O',
- chr(80) => 'P', chr(81) => 'Q', chr(82) => 'R', chr(83) => 'S',
- chr(84) => 'T', chr(85) => 'U', chr(86) => 'V', chr(87) => 'W',
- chr(88) => 'X', chr(89) => 'Y', chr(90) => 'Z', chr(91) => '%K',
- chr(92) => '%L', chr(93) => '%M', chr(94) => '%N', chr(95) => '%O',
- chr(96) => '%W', chr(97) => '+A', chr(98) => '+B', chr(99) => '+C',
- chr(100) => '+D', chr(101) => '+E', chr(102) => '+F', chr(103) => '+G',
- chr(104) => '+H', chr(105) => '+I', chr(106) => '+J', chr(107) => '+K',
- chr(108) => '+L', chr(109) => '+M', chr(110) => '+N', chr(111) => '+O',
- chr(112) => '+P', chr(113) => '+Q', chr(114) => '+R', chr(115) => '+S',
- chr(116) => '+T', chr(117) => '+U', chr(118) => '+V', chr(119) => '+W',
- chr(120) => '+X', chr(121) => '+Y', chr(122) => '+Z', chr(123) => '%P',
- chr(124) => '%Q', chr(125) => '%R', chr(126) => '%S', chr(127) => '%T');
- $code_ext = '';
- $clen = strlen($code);
- for ($i = 0 ; $i < $clen; ++$i) {
- if (ord($code{$i}) > 127) {
- return false;
- }
- $code_ext .= $encode[$code{$i}];
- }
- return $code_ext;
- }
- /**
- * Calculate CODE 39 checksum (modulo 43).
- * @param $code (string) code to represent.
- * @return char checksum.
- * @protected
- */
- protected function checksum_code39($code) {
- $chars = array(
- '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
- 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K',
- 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V',
- 'W', 'X', 'Y', 'Z', '-', '.', ' ', '$', '/', '+', '%');
- $sum = 0;
- $clen = strlen($code);
- for ($i = 0 ; $i < $clen; ++$i) {
- $k = array_keys($chars, $code{$i});
- $sum += $k[0];
- }
- $j = ($sum % 43);
- return $chars[$j];
- }
- /**
- * CODE 93 - USS-93
- * Compact code similar …
Large files files are truncated, but you can click here to view the full file