From 8f2740d3496fc5ddf5f1180f4564e90fd8c98d78 Mon Sep 17 00:00:00 2001 From: FrancescoUK Date: Mon, 14 Sep 2015 23:19:33 +0100 Subject: [PATCH] Centred barcode image Moved $data to base class and added getData Fixed indentation --- application/libraries/Barcode_lib.php | 233 ++++++------ application/models/item.php | 4 +- application/views/barcode_sheet.php | 5 +- application/views/barcodes/BarcodeBase.php | 17 + application/views/barcodes/Code128.php | 13 +- application/views/barcodes/Code39.php | 33 +- application/views/barcodes/Ean13.php | 401 ++++++++++----------- application/views/barcodes/Ean8.php | 371 ++++++++++--------- 8 files changed, 540 insertions(+), 537 deletions(-) diff --git a/application/libraries/Barcode_lib.php b/application/libraries/Barcode_lib.php index 725f2e534..13c8ec883 100644 --- a/application/libraries/Barcode_lib.php +++ b/application/libraries/Barcode_lib.php @@ -9,43 +9,43 @@ require APPPATH.'/views/barcodes/Ean8.php'; class Barcode_lib { - var $CI; - var $supported_barcodes = array(1 => 'Code 39', 2 => 'Code 128', 3 => 'EAN 8', 4 => 'EAN 13'); - - function __construct() - { - $this->CI =& get_instance(); - } - - function get_list_barcodes() - { - return $this->supported_barcodes; - } - - function get_barcode_config() - { - $data['barcode_type'] = $this->CI->Appconfig->get('barcode_type'); - $data['barcode_font'] = $this->CI->Appconfig->get('barcode_font'); - $data['barcode_font_size'] = $this->CI->Appconfig->get('barcode_font_size'); - $data['barcode_height'] = $this->CI->Appconfig->get('barcode_height'); - $data['barcode_width'] = $this->CI->Appconfig->get('barcode_width'); - $data['barcode_quality'] = $this->CI->Appconfig->get('barcode_quality'); - $data['barcode_first_row'] = $this->CI->Appconfig->get('barcode_first_row'); - $data['barcode_second_row'] = $this->CI->Appconfig->get('barcode_second_row'); - $data['barcode_third_row'] = $this->CI->Appconfig->get('barcode_third_row'); - $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'); + private $CI = null; + private $supported_barcodes = array(1 => 'Code 39', 2 => 'Code 128', 3 => 'EAN 8', 4 => 'EAN 13'); + + function __construct() + { + $this->CI =& get_instance(); + } + + public function get_list_barcodes() + { + return $this->supported_barcodes; + } + + public function get_barcode_config() + { + $data['barcode_type'] = $this->CI->Appconfig->get('barcode_type'); + $data['barcode_font'] = $this->CI->Appconfig->get('barcode_font'); + $data['barcode_font_size'] = $this->CI->Appconfig->get('barcode_font_size'); + $data['barcode_height'] = $this->CI->Appconfig->get('barcode_height'); + $data['barcode_width'] = $this->CI->Appconfig->get('barcode_width'); + $data['barcode_quality'] = $this->CI->Appconfig->get('barcode_quality'); + $data['barcode_first_row'] = $this->CI->Appconfig->get('barcode_first_row'); + $data['barcode_second_row'] = $this->CI->Appconfig->get('barcode_second_row'); + $data['barcode_third_row'] = $this->CI->Appconfig->get('barcode_third_row'); + $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; - } - - function generate_barcode($barcode_content, $barcode_config) - { - try - { - switch($barcode_config['barcode_type']) - { + return $data; + } + + public function generate_barcode($barcode_content, $barcode_config) + { + try + { + switch($barcode_config['barcode_type']) + { case '1': $barcode = new emberlabs\Barcode\Code39(); break; @@ -62,87 +62,92 @@ class Barcode_lib 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(); + $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(); - } - catch(Exception $e) - { - echo 'Caught exception: ', $e->getMessage(), "\n"; - } - } - - function create_display_barcode($item, $barcode_config) - { - - $display_table = ""; - $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 .= "
" . $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) . "
"; + return $barcode->base64(); + } + catch(Exception $e) + { + echo 'Caught exception: ', $e->getMessage(), "\n"; + } + } + + public function create_display_barcode($item, $barcode_config) + { + $display_table = ""; + $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 .= "
" . $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) . "
"; - return $display_table; - } - - private function manage_display_layout($layout_type, $item, $barcode_config) - { - $result = ''; - - if($layout_type == 'name') - { - $result = $this->CI->lang->line('items_name') . " " . $item['name']; - } - else if($layout_type == 'category' && isset($item['category'])) - { - $result = $this->CI->lang->line('items_category') . " " . $item['category']; - } - else if($layout_type == 'cost_price' && isset($item['cost_price'])) - { - $result = $this->CI->lang->line('items_cost_price') . " " . to_currency($item['cost_price']); - } - else if($layout_type == 'unit_price' && isset($item['unit_price'])) - { - $result = $this->CI->lang->line('items_unit_price') . " " . to_currency($item['unit_price']); - } - else if($layout_type == 'company_name') - { - $result = $this->CI->Appconfig->get('company'); - } - else if($layout_type == 'item_code') - { - $result = $this->CI->Appconfig->get('barcode_content') !== "id" && isset($item['item_number']) ? $item['item_number'] : $item['item_id']; - } - return $result; - } - - function listfonts($folder) - { - $array = array(); - if (($handle = opendir($folder)) !== false) { - while (($file = readdir($handle)) !== false) { - if(substr($file, -4, 4) === '.ttf') { - $array[$file] = $file; - } - } - } - closedir($handle); - - array_unshift($array, 'No Label'); - - return $array; - } + return $display_table; + } + + private function manage_display_layout($layout_type, $item, $barcode_config) + { + $result = ''; + + if($layout_type == 'name') + { + $result = $this->CI->lang->line('items_name') . " " . $item['name']; + } + else if($layout_type == 'category' && isset($item['category'])) + { + $result = $this->CI->lang->line('items_category') . " " . $item['category']; + } + else if($layout_type == 'cost_price' && isset($item['cost_price'])) + { + $result = $this->CI->lang->line('items_cost_price') . " " . to_currency($item['cost_price']); + } + else if($layout_type == 'unit_price' && isset($item['unit_price'])) + { + $result = $this->CI->lang->line('items_unit_price') . " " . to_currency($item['unit_price']); + } + else if($layout_type == 'company_name') + { + $result = $this->CI->Appconfig->get('company'); + } + else if($layout_type == 'item_code') + { + $result = $this->CI->Appconfig->get('barcode_content') !== "id" && isset($item['item_number']) ? $item['item_number'] : $item['item_id']; + } - function get_font_name($font_file_name) - { - return substr($font_file_name, 0, -4); - } + return $result; + } + + public function listfonts($folder) + { + $array = array(); + + if (($handle = opendir($folder)) !== false) + { + while (($file = readdir($handle)) !== false) + { + if(substr($file, -4, 4) === '.ttf') + { + $array[$file] = $file; + } + } + } + + closedir($handle); + + array_unshift($array, 'No Label'); + + return $array; + } + + public function get_font_name($font_file_name) + { + return substr($font_file_name, 0, -4); + } } ?> \ No newline at end of file diff --git a/application/models/item.php b/application/models/item.php index 0ffdb8c04..f8d979782 100644 --- a/application/models/item.php +++ b/application/models/item.php @@ -143,7 +143,7 @@ class Item extends CI_Model $this->db->from('items'); $this->db->join('suppliers', 'suppliers.person_id = items.supplier_id', 'left'); $this->db->where('item_number',$item_number); - $this->db->where('items.deleted',0); // Parq 131226 + $this->db->where('items.deleted',0); // Parq 131226 $query = $this->db->get(); @@ -162,7 +162,7 @@ class Item extends CI_Model { $this->db->from('items'); $this->db->join('suppliers', 'suppliers.person_id = items.supplier_id', 'left'); - $this->db->where_in('item_id',$item_ids); + $this->db->where_in('item_id', $item_ids); $this->db->order_by('item_id', 'asc'); return $this->db->get(); } diff --git a/application/views/barcode_sheet.php b/application/views/barcode_sheet.php index 4f8fc0a1d..a5d199a3f 100644 --- a/application/views/barcode_sheet.php +++ b/application/views/barcode_sheet.php @@ -22,12 +22,11 @@ $count = 0; foreach($items as $item) { - - if ($count % $barcode_config['barcode_num_in_row'] ==0 and $count!=0) + if ($count % $barcode_config['barcode_num_in_row'] == 0 and $count != 0) { echo ''; } - echo "". $this->barcode_lib->create_display_barcode($item,$barcode_config).""; + echo "" . $this->barcode_lib->create_display_barcode($item, $barcode_config) . ""; $count++; } ?> diff --git a/application/views/barcodes/BarcodeBase.php b/application/views/barcodes/BarcodeBase.php index 4a6453152..a9859a4dd 100644 --- a/application/views/barcodes/BarcodeBase.php +++ b/application/views/barcodes/BarcodeBase.php @@ -27,6 +27,11 @@ abstract class BarcodeBase */ protected $img = null; + /* + * @var data - to be set + */ + protected $data = ''; + /* * @var int x (width) */ @@ -58,6 +63,18 @@ abstract class BarcodeBase */ abstract public function setData($data); + /* + * Get the data + * + * @param mixed data - (int or string) Data to be encoded + * @return instance of \emberlabs\Barcode\BarcodeInterface + * @return throws \OverflowException + */ + public function getData() + { + return $this->data; + } + /* * (Abstract) Draw the image * diff --git a/application/views/barcodes/Code128.php b/application/views/barcodes/Code128.php index b84193771..896df8624 100644 --- a/application/views/barcodes/Code128.php +++ b/application/views/barcodes/Code128.php @@ -21,11 +21,6 @@ namespace emberlabs\Barcode; */ class Code128 extends BarcodeBase { - /* - * @var data - to be set - */ - private $data = ''; - /* * Sub Type encoding * @var int (should be a class constant) @@ -252,7 +247,7 @@ class Code128 extends BarcodeBase // Calc scaling // Bars is in reference to a single, 1-level bar $numBarsRequired = ($this->type != self::TYPE_C) ? (sizeof($charAry) * 11) + 35 : ((sizeof($charAry)/2) * 11) + 35; - $this->x = ($this->x == 0) ? $numBarsRequired : $this->x; + $this->x = ($this->x == 0) ? $numBarsRequired : $this->x; $pxPerBar = (int) ($this->x / $numBarsRequired); $currentX = ($this->x - ($numBarsRequired * $pxPerBar)) / 2; @@ -292,6 +287,12 @@ class Code128 extends BarcodeBase $checkSumCollector = $this->getKey($this->getStartChar()); $this->img = @imagecreate($this->x, $this->y); + + if (!$this->img) + { + throw new \RuntimeException("Code128: Image failed to initialize"); + } + $white = imagecolorallocate($this->img, 255, 255, 255); $black = imagecolorallocate($this->img, 0, 0, 0); diff --git a/application/views/barcodes/Code39.php b/application/views/barcodes/Code39.php index 7038bc9a4..69aeb29bc 100644 --- a/application/views/barcodes/Code39.php +++ b/application/views/barcodes/Code39.php @@ -21,12 +21,6 @@ namespace emberlabs\Barcode; */ class Code39 extends BarcodeBase { - /* - * Data to be set - * @var data - */ - private $data = ''; - /* * Binary map * @var array binMap @@ -95,8 +89,7 @@ class Code39 extends BarcodeBase */ public function setData($data) { - // I know, lots of junk. - $this->data = '*' . strtoupper(ltrim(rtrim(trim($data), '*'), '*')) . '*'; + $this->data = $data; } /* @@ -118,24 +111,19 @@ class Code39 extends BarcodeBase */ public function draw() { - $this->img = @imagecreate($this->x, $this->y); - - if (!$this->img) - { - throw new \RuntimeException("Code39: Image failed to initialize"); - } - + // I know, lots of junk. + $data = '*' . strtoupper(ltrim(rtrim(trim($this->data), '*'), '*')) . '*'; + // Length of data X [ 6 narrow bars + 3 wide bars + A single Quiet stop ] - a single quiet stop - $pxPerChar = (strlen($this->data) * ((6 * self::NARROW_BAR) + (3 * self::WIDE_BAR) + self::QUIET_BAR)) - self::QUIET_BAR; + $pxPerChar = (strlen($data) * ((6 * self::NARROW_BAR) + (3 * self::WIDE_BAR) + self::QUIET_BAR)) - self::QUIET_BAR; $widthQuotient = $this->x / $pxPerChar; // Lengths per type $narrowBar = (int) (self::NARROW_BAR * $widthQuotient); $wideBar = (int) (self::WIDE_BAR * $widthQuotient); $quietBar = (int) (self::QUIET_BAR * $widthQuotient); - - $imageWidth = (strlen($this->data) * ((6 * $narrowBar) + (3 * $wideBar) + $quietBar)) - $quietBar; + $imageWidth = (strlen($data) * ((6 * $narrowBar) + (3 * $wideBar) + $quietBar)) - $quietBar; // Do we have degenerate rectangles? if ($narrowBar < 1 || $wideBar < 1 || $quietBar < 1 || $narrowBar == $quietBar || $narrowBar == $wideBar || $wideBar == $quietBar) @@ -144,8 +132,15 @@ class Code39 extends BarcodeBase } $currentBarX = (int)(($this->x - $imageWidth) / 2); - $charAry = str_split($this->data); + $charAry = str_split($data); + $this->img = @imagecreate($this->x, $this->y); + + if (!$this->img) + { + throw new \RuntimeException("Code39: Image failed to initialize"); + } + // Grab our colors $white = imagecolorallocate($this->img, 255, 255, 255); $black = imagecolorallocate($this->img, 0, 0, 0); diff --git a/application/views/barcodes/Ean13.php b/application/views/barcodes/Ean13.php index cb5f82903..90fc5438d 100644 --- a/application/views/barcodes/Ean13.php +++ b/application/views/barcodes/Ean13.php @@ -1,10 +1,10 @@ array( - 'A' => array(0,0,0,1,1,0,1), - 'B' => array(0,1,0,0,1,1,1), - 'C' => array(1,1,1,0,0,1,0) - ), - '1' => array( - 'A' => array(0,0,1,1,0,0,1), - 'B' => array(0,1,1,0,0,1,1), - 'C' => array(1,1,0,0,1,1,0) - ), - '2' => array( - 'A' => array(0,0,1,0,0,1,1), - 'B' => array(0,0,1,1,0,1,1), - 'C' => array(1,1,0,1,1,0,0) - ), - '3' => array( - 'A' => array(0,1,1,1,1,0,1), - 'B' => array(0,1,0,0,0,0,1), - 'C' => array(1,0,0,0,0,1,0) - ), - '4' => array( - 'A' => array(0,1,0,0,0,1,1), - 'B' => array(0,0,1,1,1,0,1), - 'C' => array(1,0,1,1,1,0,0) - ), - '5' => array( - 'A' => array(0,1,1,0,0,0,1), - 'B' => array(0,1,1,1,0,0,1), - 'C' => array(1,0,0,1,1,1,0) - ), - '6' => array( - 'A' => array(0,1,0,1,1,1,1), - 'B' => array(0,0,0,0,1,0,1), - 'C' => array(1,0,1,0,0,0,0) - ), - '7' => array( - 'A' => array(0,1,1,1,0,1,1), - 'B' => array(0,0,1,0,0,0,1), - 'C' => array(1,0,0,0,1,0,0) - ), - '8' => array( - 'A' => array(0,1,1,0,1,1,1), - 'B' => array(0,0,0,1,0,0,1), - 'C' => array(1,0,0,1,0,0,0) - ), - '9' => array( - 'A' => array(0,0,0,1,0,1,1), - 'B' => array(0,0,1,0,1,1,1), - 'C' => array(1,1,1,0,1,0,0) - ) - ); + private $_codingmap = array( + '0' => array( + 'A' => array(0,0,0,1,1,0,1), + 'B' => array(0,1,0,0,1,1,1), + 'C' => array(1,1,1,0,0,1,0) + ), + '1' => array( + 'A' => array(0,0,1,1,0,0,1), + 'B' => array(0,1,1,0,0,1,1), + 'C' => array(1,1,0,0,1,1,0) + ), + '2' => array( + 'A' => array(0,0,1,0,0,1,1), + 'B' => array(0,0,1,1,0,1,1), + 'C' => array(1,1,0,1,1,0,0) + ), + '3' => array( + 'A' => array(0,1,1,1,1,0,1), + 'B' => array(0,1,0,0,0,0,1), + 'C' => array(1,0,0,0,0,1,0) + ), + '4' => array( + 'A' => array(0,1,0,0,0,1,1), + 'B' => array(0,0,1,1,1,0,1), + 'C' => array(1,0,1,1,1,0,0) + ), + '5' => array( + 'A' => array(0,1,1,0,0,0,1), + 'B' => array(0,1,1,1,0,0,1), + 'C' => array(1,0,0,1,1,1,0) + ), + '6' => array( + 'A' => array(0,1,0,1,1,1,1), + 'B' => array(0,0,0,0,1,0,1), + 'C' => array(1,0,1,0,0,0,0) + ), + '7' => array( + 'A' => array(0,1,1,1,0,1,1), + 'B' => array(0,0,1,0,0,0,1), + 'C' => array(1,0,0,0,1,0,0) + ), + '8' => array( + 'A' => array(0,1,1,0,1,1,1), + 'B' => array(0,0,0,1,0,0,1), + 'C' => array(1,0,0,1,0,0,0) + ), + '9' => array( + 'A' => array(0,0,0,1,0,1,1), + 'B' => array(0,0,1,0,1,1,1), + 'C' => array(1,1,1,0,1,0,0) + ) + ); - /* - * Coding map left - * @var array - */ - private $_codingmapleft = array( - '0' => array('A','A','A','A','A','A'), - '1' => array('A','A','B','A','B','B'), - '2' => array('A','A','B','B','A','B'), - '3' => array('A','A','B','B','B','A'), - '4' => array('A','B','A','A','B','B'), - '5' => array('A','B','B','A','A','B'), - '6' => array('A','B','B','B','A','A'), - '7' => array('A','B','A','B','A','B'), - '8' => array('A','B','A','B','B','A'), - '9' => array('A','B','B','A','B','A') - ); + /* + * Coding map left + * @var array + */ + private $_codingmapleft = array( + '0' => array('A','A','A','A','A','A'), + '1' => array('A','A','B','A','B','B'), + '2' => array('A','A','B','B','A','B'), + '3' => array('A','A','B','B','B','A'), + '4' => array('A','B','A','A','B','B'), + '5' => array('A','B','B','A','A','B'), + '6' => array('A','B','B','B','A','A'), + '7' => array('A','B','A','B','A','B'), + '8' => array('A','B','A','B','B','A'), + '9' => array('A','B','B','A','B','A') + ); /* * Generate EAN13 code out of a provided number @@ -130,21 +125,21 @@ class Ean13 extends BarcodeBase */ private function generateEAN($number) { - $code = '200' . str_pad($number, 9, '0'); + $this->data = '200' . str_pad($number, 9, '0'); $weightflag = true; $sum = 0; // Weight for a digit in the checksum is 3, 1, 3.. starting from the last digit. // loop backwards to make the loop length-agnostic. The same basic functionality // will work for codes of different lengths. - for ($i = strlen($code) - 1; $i >= 0; $i--) + for ($i = strlen($this->data) - 1; $i >= 0; $i--) { - $sum += (int)$code[$i] * ($weightflag?3:1); + $sum += (int)$this->data[$i] * ($weightflag?3:1); $weightflag = !$weightflag; } - $code .= (10 - ($sum % 10)) % 10; + $this->data .= (10 - ($sum % 10)) % 10; - return $code; + return $this->data; } /* @@ -156,7 +151,7 @@ class Ean13 extends BarcodeBase */ public function setData($data) { - $this->data = $data; + $this->data = $this->generateEAN($data); } /* @@ -166,17 +161,15 @@ class Ean13 extends BarcodeBase */ public function draw() { - $code = $this->generateEAN($this->data); - // Bars is in reference to a single, 1-level bar - $pxPerBar = 2.5; + $pxPerBar = 2; - // Calculate the barcode width - $barcodewidth = (strlen($code)) * (7 * $pxPerBar) - + 3 * $pxPerBar // left - + 5 * $pxPerBar // center - + 3 * $pxPerBar // right - ; + // Calculate the barcode width + $barcodewidth = (strlen($this->data)) * (7 * $pxPerBar) + + 3 * $pxPerBar // left + + 5 * $pxPerBar // center + + 3 * $pxPerBar // right + ; $this->x = ($this->x == 0) ? $barcodewidth : $this->x; @@ -190,110 +183,53 @@ class Ean13 extends BarcodeBase $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); + // 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); + // get the first digit which is the key for creating the first 6 bars + $key = substr($this->data, 0, 1); - // Initiate x position - $xpos = 0; + // Initiate x position centering the bar + $xpos = ($this->x - $barcodewidth) / 2; - // Draws the left guard pattern (bar-space-bar) - // bar - imagefilledrectangle( - $this->img, - $xpos, - 0, - $xpos + $pxPerBar - 1, - $this->y, - $black - ); + // Draws the left guard pattern (bar-space-bar) + // bar + imagefilledrectangle( + $this->img, + $xpos, + 0, + $xpos + $pxPerBar - 1, + $this->y, + $black + ); - $xpos += $pxPerBar; + $xpos += $pxPerBar; - // space - $xpos += $pxPerBar; + // space + $xpos += $pxPerBar; - // bar - imagefilledrectangle( - $this->img, - $xpos, - 0, - $xpos + $pxPerBar - 1, - $this->y, - $black - ); - - $xpos += $pxPerBar; - - // Draw left $code contents - $set_array = $this->_codingmapleft[$key]; - - for ($idx = 1; $idx < 7; $idx ++) - { - $value = substr($code, $idx, 1); - - foreach ($this->_codingmap[$value][$set_array[$idx - 1]] 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, + // bar + imagefilledrectangle( + $this->img, + $xpos, + 0, $xpos + $pxPerBar - 1, $this->y, - $black - ); + $black + ); - $xpos += $pxPerBar; + $xpos += $pxPerBar; - // space - $xpos += $pxPerBar; + // Draw left $this->data contents + $set_array = $this->_codingmapleft[$key]; - // bar - imagefilledrectangle( - $this->img, - $xpos, - 0, - $xpos + $pxPerBar - 1, - $this->y, - $black - ); - - $xpos += $pxPerBar; - - // space - $xpos += $pxPerBar; - - // Draw right $code contents - for ($idx = 7; $idx < 13; $idx ++) + for ($idx = 1; $idx < 7; $idx ++) { - $value = substr($code, $idx, 1); + $value = substr($this->data, $idx, 1); - foreach ($this->_codingmap[$value]['C'] as $bar) + foreach ($this->_codingmap[$value][$set_array[$idx - 1]] as $bar) { - if ($bar) + if ($bar) { imagefilledrectangle( $this->img, @@ -302,38 +238,95 @@ class Ean13 extends BarcodeBase $xpos + $pxPerBar - 1, $this->y, $black - ); - } + ); + } - $xpos += $pxPerBar; - } - } + $xpos += $pxPerBar; + } + } - // Draws the right guard pattern (bar-space-bar) - // bar - imagefilledrectangle( - $this->img, + // 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; + $xpos += $pxPerBar; - // space - $xpos += $pxPerBar; + // space + $xpos += $pxPerBar; - // bar - imagefilledrectangle( - $this->img, + // bar + imagefilledrectangle( + $this->img, $xpos, 0, $xpos + $pxPerBar - 1, $this->y, $black - ); + ); + + $xpos += $pxPerBar; + + // space + $xpos += $pxPerBar; + + // Draw right $this->data contents + for ($idx = 7; $idx < 13; $idx ++) + { + $value = substr($this->data, $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 diff --git a/application/views/barcodes/Ean8.php b/application/views/barcodes/Ean8.php index 0989b51a5..2e98da107 100644 --- a/application/views/barcodes/Ean8.php +++ b/application/views/barcodes/Ean8.php @@ -1,10 +1,10 @@ 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) - ) - ); + 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 + * @return string with complete EAN code */ private function generateEAN($number, $len = 8) { - $code = null; + $this->data = null; if($number > -1) { $data_len = $len - 1; - $code = $number; + $this->data = $number; //Padding - $code = str_pad($code, $data_len, '0', STR_PAD_LEFT); - $code_len = strlen($code); + $this->data = str_pad($this->data, $data_len, '0', STR_PAD_LEFT); + $this->data_len = strlen($this->data); // calculate check digit $sum_a = 0; for ($i = 1; $i < $data_len; $i += 2) { - $sum_a += $code{$i}; + $sum_a += $this->data{$i}; } if ($len > 12) { - $sum_a *= 3; + $sum_a *= 3; } $sum_b = 0; for ($i = 0; $i < $data_len; $i += 2) { - $sum_b += ($code{$i}); + $sum_b += ($this->data{$i}); } if ($len < 13) { - $sum_b *= 3; + $sum_b *= 3; } $r = ($sum_a + $sum_b) % 10; if($r > 0) { - $r = (10 - $r); + $r = (10 - $r); } - if ($code_len == $data_len) + if ($this->data_len == $data_len) { - // add check digit - $code .= $r; + // add check digit + $this->data .= $r; } - elseif ($r !== intval($code{$data_len})) + elseif ($r !== intval($this->data{$data_len})) { - // wrong checkdigit - $code = null; + // wrong checkdigit + $this->data = null; } } - return $code; + return $this->data; } /* @@ -168,7 +163,7 @@ class Ean8 extends BarcodeBase */ public function setData($data) { - $this->data = $data; + $this->data = $this->generateEAN($data); } /* @@ -178,17 +173,15 @@ class Ean8 extends BarcodeBase */ public function draw() { - $code = $this->generateEAN($this->data); - // Bars is in reference to a single, 1-level bar - $pxPerBar = 2.5; + $pxPerBar = 2; - // Calculate the barcode width - $barcodewidth = (strlen($code)) * (7 * $pxPerBar) - + 3 * $pxPerBar // left - + 5 * $pxPerBar // center - + 3 * $pxPerBar // right - ; + // Calculate the barcode width + $barcodewidth = (strlen($this->data)) * (7 * $pxPerBar) + + 3 * $pxPerBar // left + + 5 * $pxPerBar // center + + 3 * $pxPerBar // right + ; $this->x = ($this->x == 0) ? $barcodewidth : $this->x; @@ -196,113 +189,56 @@ class Ean8 extends BarcodeBase if (!$this->img) { - throw new \RuntimeException("Ean13: Image failed to initialize"); + throw new \RuntimeException("Ean8: 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); + // 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); + // get the first digit which is the key for creating the first 6 bars + $key = substr($this->data, 0, 1); - // Initiate x position - $xpos = 0; + // Initiate x position centering the bar + $xpos = ($this->x - $barcodewidth) / 2; - // Draws the left guard pattern (bar-space-bar) - // bar - imagefilledrectangle( - $this->img, - $xpos, - 0, - $xpos + $pxPerBar - 1, - $this->y, - $black - ); + // Draws the left guard pattern (bar-space-bar) + // bar + imagefilledrectangle( + $this->img, + $xpos, + 0, + $xpos + $pxPerBar - 1, + $this->y, + $black + ); - $xpos += $pxPerBar; + $xpos += $pxPerBar; - // space - $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, + // bar + imagefilledrectangle( + $this->img, + $xpos, + 0, $xpos + $pxPerBar - 1, $this->y, - $black - ); + $black + ); - $xpos += $pxPerBar; + $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 ++) + for ($idx = 0; $idx < 4; $idx ++) { - $value = substr($code, $idx, 1); + $value = substr($this->data, $idx, 1); - foreach ($this->_codingmap[$value]['C'] as $bar) + foreach ($this->_codingmap[$value]['A'] as $bar) { - if ($bar) + if ($bar) { imagefilledrectangle( $this->img, @@ -311,38 +247,95 @@ class Ean8 extends BarcodeBase $xpos + $pxPerBar - 1, $this->y, $black - ); - } + ); + } - $xpos += $pxPerBar; - } - } + $xpos += $pxPerBar; + } + } - // Draws the right guard pattern (bar-space-bar) - // bar - imagefilledrectangle( - $this->img, + // 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; + $xpos += $pxPerBar; - // space - $xpos += $pxPerBar; + // space + $xpos += $pxPerBar; - // bar - imagefilledrectangle( - $this->img, + // bar + imagefilledrectangle( + $this->img, $xpos, 0, $xpos + $pxPerBar - 1, $this->y, $black - ); + ); + + $xpos += $pxPerBar; + + // space + $xpos += $pxPerBar; + + // Draw right $this->data contents + for ($idx = 4; $idx < 8; $idx ++) + { + $value = substr($this->data, $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