From eb71dbc3009ed949c24340e0139a23d0b3b441b0 Mon Sep 17 00:00:00 2001 From: FrancescoUK Date: Sun, 13 Sep 2015 21:50:07 +0100 Subject: [PATCH] Fix EAN 13 typos + set pix per bar Add EAN 8 support --- application/libraries/Barcode_lib.php | 42 ++-- application/views/barcodes/Ean13.php | 7 +- application/views/barcodes/Ean8.php | 348 ++++++++++++++++++++++++++ 3 files changed, 377 insertions(+), 20 deletions(-) create mode 100644 application/views/barcodes/Ean8.php diff --git a/application/libraries/Barcode_lib.php b/application/libraries/Barcode_lib.php index 76aa9d8cc..725f2e534 100644 --- a/application/libraries/Barcode_lib.php +++ b/application/libraries/Barcode_lib.php @@ -5,11 +5,12 @@ require APPPATH.'/views/barcodes/BarcodeBase.php'; require APPPATH.'/views/barcodes/Code39.php'; require APPPATH.'/views/barcodes/Code128.php'; require APPPATH.'/views/barcodes/Ean13.php'; +require APPPATH.'/views/barcodes/Ean8.php'; class Barcode_lib { var $CI; - var $supported_barcodes = array(1 => 'Code 39', 2 => 'Code 128', 3 => 'EAN13'); + var $supported_barcodes = array(1 => 'Code 39', 2 => 'Code 128', 3 => 'EAN 8', 4 => 'EAN 13'); function __construct() { @@ -35,6 +36,7 @@ class Barcode_lib $data['barcode_num_in_row'] = $this->CI->Appconfig->get('barcode_num_in_row'); $data['barcode_page_width'] = $this->CI->Appconfig->get('barcode_page_width'); $data['barcode_page_cellspacing'] = $this->CI->Appconfig->get('barcode_page_cellspacing'); + return $data; } @@ -42,25 +44,32 @@ class Barcode_lib { try { - if ($barcode_config['barcode_type'] == '1') + switch($barcode_config['barcode_type']) { - $barcode = new emberlabs\Barcode\Code39(); - } - else if ($barcode_config['barcode_type'] == '2') - { - $barcode = new emberlabs\Barcode\Code128(); - } - else - { - $barcode = new emberlabs\Barcode\Ean13(); + case '1': + $barcode = new emberlabs\Barcode\Code39(); + break; + + case '2': + $barcode = new emberlabs\Barcode\Code128(); + break; + + case '3': + $barcode = new emberlabs\Barcode\Ean8(); + break; + + case '4': + default: + $barcode = new emberlabs\Barcode\Ean13(); + break; } $barcode->setData($barcode_content); $barcode->setQuality($barcode_config['barcode_quality']); $barcode->setDimensions($barcode_config['barcode_width'], $barcode_config['barcode_height']); $barcode->draw(); + return $barcode->base64(); - return ""; } catch(Exception $e) { @@ -72,13 +81,14 @@ class Barcode_lib { $display_table = ""; - $display_table .= ""; - $barcode_content=$this->CI->Appconfig->get('barcode_content') === "id" ? $item['item_id'] : $item['item_number']; + $display_table .= ""; + $barcode_content = $this->CI->Appconfig->get('barcode_content') === "id" ? $item['item_id'] : $item['item_number']; $barcode = $this->generate_barcode($barcode_content,$barcode_config); $display_table .= ""; - $display_table .= ""; - $display_table .= ""; + $display_table .= ""; + $display_table .= ""; $display_table .= "
". $this->manage_display_layout($barcode_config['barcode_first_row'], $item, $barcode_config)."
" . $this->manage_display_layout($barcode_config['barcode_first_row'], $item, $barcode_config) . "
". $this->manage_display_layout($barcode_config['barcode_second_row'], $item, $barcode_config)."
". $this->manage_display_layout($barcode_config['barcode_third_row'], $item, $barcode_config)."
" . $this->manage_display_layout($barcode_config['barcode_second_row'], $item, $barcode_config) . "
" . $this->manage_display_layout($barcode_config['barcode_third_row'], $item, $barcode_config) . "
"; + return $display_table; } diff --git a/application/views/barcodes/Ean13.php b/application/views/barcodes/Ean13.php index 3d9133e68..cb5f82903 100644 --- a/application/views/barcodes/Ean13.php +++ b/application/views/barcodes/Ean13.php @@ -166,11 +166,10 @@ class Ean13 extends BarcodeBase */ public function draw() { - $code = generateEAN($this->data); + $code = $this->generateEAN($this->data); - // Calc scaling // Bars is in reference to a single, 1-level bar - $pxPerBar = 5; + $pxPerBar = 2.5; // Calculate the barcode width $barcodewidth = (strlen($code)) * (7 * $pxPerBar) @@ -306,7 +305,7 @@ class Ean13 extends BarcodeBase ); } - $xpos += $this->pxPerBar; + $xpos += $pxPerBar; } } diff --git a/application/views/barcodes/Ean8.php b/application/views/barcodes/Ean8.php new file mode 100644 index 000000000..0989b51a5 --- /dev/null +++ b/application/views/barcodes/Ean8.php @@ -0,0 +1,348 @@ + + * @copyright 2005 The PHP Group + * @license http://www.php.net/license/3_0.txt PHP License 3.0 + * @link http://pear.php.net/package/Image_Barcode2 + */ + +namespace emberlabs\Barcode; + +/** + * emberlabs Barcode Creator - Ean8 + * Generate Ean8 Barcodes + * + * + * @license http://opensource.org/licenses/mit-license.php The MIT License + * @link https://github.com/samt/barcode + */ +class Ean8 extends BarcodeBase +{ + /* + * @var data - to be set + */ + private $data = ''; + + /* + * Coding map + * @var array + */ + private $_codingmap = array( + '0' => array( + 'A' => array(0,0,0,1,1,0,1), + 'C' => array(1,1,1,0,0,1,0) + ), + '1' => array( + 'A' => array(0,0,1,1,0,0,1), + 'C' => array(1,1,0,0,1,1,0) + ), + '2' => array( + 'A' => array(0,0,1,0,0,1,1), + 'C' => array(1,1,0,1,1,0,0) + ), + '3' => array( + 'A' => array(0,1,1,1,1,0,1), + 'C' => array(1,0,0,0,0,1,0) + ), + '4' => array( + 'A' => array(0,1,0,0,0,1,1), + 'C' => array(1,0,1,1,1,0,0) + ), + '5' => array( + 'A' => array(0,1,1,0,0,0,1), + 'C' => array(1,0,0,1,1,1,0) + ), + '6' => array( + 'A' => array(0,1,0,1,1,1,1), + 'C' => array(1,0,1,0,0,0,0) + ), + '7' => array( + 'A' => array(0,1,1,1,0,1,1), + 'C' => array(1,0,0,0,1,0,0) + ), + '8' => array( + 'A' => array(0,1,1,0,1,1,1), + 'C' => array(1,0,0,1,0,0,0) + ), + '9' => array( + 'A' => array(0,0,0,1,0,1,1), + 'C' => array(1,1,1,0,1,0,0) + ) + ); + + /* + * Calculate EAN8 or EAN13 automatically + * set $len = 8 for EAN8, $len = 13 for EAN13 + * + * @param number is the internal code you want to have EANed. The prefix, zero-padding and checksum are added by the function. + * @return string with complete EAN13 code + */ + private function generateEAN($number, $len = 8) + { + $code = null; + + if($number > -1) + { + $data_len = $len - 1; + $code = $number; + + //Padding + $code = str_pad($code, $data_len, '0', STR_PAD_LEFT); + $code_len = strlen($code); + + // calculate check digit + $sum_a = 0; + for ($i = 1; $i < $data_len; $i += 2) + { + $sum_a += $code{$i}; + } + + if ($len > 12) + { + $sum_a *= 3; + } + + $sum_b = 0; + for ($i = 0; $i < $data_len; $i += 2) + { + $sum_b += ($code{$i}); + } + + if ($len < 13) + { + $sum_b *= 3; + } + + $r = ($sum_a + $sum_b) % 10; + + if($r > 0) + { + $r = (10 - $r); + } + + if ($code_len == $data_len) + { + // add check digit + $code .= $r; + } + elseif ($r !== intval($code{$data_len})) + { + // wrong checkdigit + $code = null; + } + } + + return $code; + } + + /* + * Set the data + * + * @param mixed data - (int or string) Data to be encoded + * @return instance of \emberlabs\Barcode\BarcodeInterface + * @return throws \OverflowException + */ + public function setData($data) + { + $this->data = $data; + } + + /* + * Draw the image + * + * @return void + */ + public function draw() + { + $code = $this->generateEAN($this->data); + + // Bars is in reference to a single, 1-level bar + $pxPerBar = 2.5; + + // Calculate the barcode width + $barcodewidth = (strlen($code)) * (7 * $pxPerBar) + + 3 * $pxPerBar // left + + 5 * $pxPerBar // center + + 3 * $pxPerBar // right + ; + + $this->x = ($this->x == 0) ? $barcodewidth : $this->x; + + $this->img = @imagecreate($this->x, $this->y); + + if (!$this->img) + { + throw new \RuntimeException("Ean13: Image failed to initialize"); + } + + $white = imagecolorallocate($this->img, 255, 255, 255); + $black = imagecolorallocate($this->img, 0, 0, 0); + + // Fill image with white color + imagefill($this->img, 0, 0, $white); + + // get the first digit which is the key for creating the first 6 bars + $key = substr($code, 0, 1); + + // Initiate x position + $xpos = 0; + + // Draws the left guard pattern (bar-space-bar) + // bar + imagefilledrectangle( + $this->img, + $xpos, + 0, + $xpos + $pxPerBar - 1, + $this->y, + $black + ); + + $xpos += $pxPerBar; + + // space + $xpos += $pxPerBar; + + // bar + imagefilledrectangle( + $this->img, + $xpos, + 0, + $xpos + $pxPerBar - 1, + $this->y, + $black + ); + + $xpos += $pxPerBar; + + for ($idx = 0; $idx < 4; $idx ++) + { + $value = substr($code, $idx, 1); + + foreach ($this->_codingmap[$value]['A'] as $bar) + { + if ($bar) + { + imagefilledrectangle( + $this->img, + $xpos, + 0, + $xpos + $pxPerBar - 1, + $this->y, + $black + ); + } + + $xpos += $pxPerBar; + } + } + + // Draws the center pattern (space-bar-space-bar-space) + // space + $xpos += $pxPerBar; + + // bar + imagefilledrectangle( + $this->img, + $xpos, + 0, + $xpos + $pxPerBar - 1, + $this->y, + $black + ); + + $xpos += $pxPerBar; + + // space + $xpos += $pxPerBar; + + // bar + imagefilledrectangle( + $this->img, + $xpos, + 0, + $xpos + $pxPerBar - 1, + $this->y, + $black + ); + + $xpos += $pxPerBar; + + // space + $xpos += $pxPerBar; + + // Draw right $code contents + for ($idx = 4; $idx < 8; $idx ++) + { + $value = substr($code, $idx, 1); + + foreach ($this->_codingmap[$value]['C'] as $bar) + { + if ($bar) + { + imagefilledrectangle( + $this->img, + $xpos, + 0, + $xpos + $pxPerBar - 1, + $this->y, + $black + ); + } + + $xpos += $pxPerBar; + } + } + + // Draws the right guard pattern (bar-space-bar) + // bar + imagefilledrectangle( + $this->img, + $xpos, + 0, + $xpos + $pxPerBar - 1, + $this->y, + $black + ); + + $xpos += $pxPerBar; + + // space + $xpos += $pxPerBar; + + // bar + imagefilledrectangle( + $this->img, + $xpos, + 0, + $xpos + $pxPerBar - 1, + $this->y, + $black + ); + } +} +?> \ No newline at end of file