Post by qstrecsteve » Thu May 19, 2022 5:14 pm

hello,

please forgive me if this has already been posted, I'm sure I posted this yesterday but cant find it in my posts.

I'm after a modification for opencart product page where you set up products, for our point of sale system we can print off labels that will scan with a barcode scanner but in order for it to work the products require a EAN number (UK based so EAN 13 is required)

any new products come with them so that's not a problem but we keep a lot of used stock that is pre barcode and it requires a barcode to be generated.

does anyone know of a module that could enable a button by the EAN field t say generate a barcode and then we can print it off.

Attachments

Untitled.png

area circled - Untitled.png (163.37 KiB) Viewed 17058 times


Newbie

Posts

Joined
Sun Oct 31, 2021 9:57 pm

Post by by mona » Fri May 20, 2022 11:59 pm

You can use the following class (save it as barcodeEan13.php) and font file to generate ean-13 barcodes.

Code: Select all

<?php
class BarcodeEan13 {
   public $font;
   public $number;
   public $scale;
   private $_key;
   private $_bars;
   private $_image;
   private $_width;
   private $_height;

   public static $PARITY_KEY = array(
      0 => "000000", 1 => "001011", 2 => "001101", 3 => "001110",
      4 => "010011", 5 => "011001", 6 => "011100", 7 => "010101",
      8 => "010110", 9 => "011010"
   );

   public static $LEFT_PARITY = array(
       0 => array(
          0 => "0001101", 1 => "0011001", 2 => "0010011", 3 => "0111101",
          4 => "0100011", 5 => "0110001", 6 => "0101111", 7 => "0111011",
          8 => "0110111", 9 => "0001011"
      ),
      1 => array ( 
          0 => "0100111", 1 => "0110011", 2 => "0011011", 3 => "0100001", 
          4 => "0011101", 5 => "0111001", 6 => "0000101", 7 => "0010001", 
          8 => "0001001", 9 => "0010111"
       )
   );

   public static $RIGHT_PARITY = array(
      0 => "1110010", 1 => "1100110", 2 => "1101100", 3 => "1000010", 
      4 => "1011100", 5 => "1001110", 6 => "1010000", 7 => "1000100", 
      8 => "1001000", 9 => "1110100"
   );

   public static $GUARD = array(
      'start' => "101", 'middle' => "01010", 'end' => "101"
   );

   public static function checksum (string $ean) {
      $even=true; $esum=0; $osum=0;
      for ($i = strlen($ean)-1; $i >= 0; $i--) {
         if ($even) $esum+=$ean[$i]; else $osum+=$ean[$i];
            $even=!$even;
      }
      return (10-((3*$esum+$osum)%10))%10;
   }

   public function __construct (string $number, $scale, $fontpath=null) {
      $this->_key = self::$PARITY_KEY[substr($number,0,1)];
      if (!$fontpath)
          $this->font = dirname(__FILE__) . "/" . "FreeSansBold.ttf";
      else
          $this->font = $fontpath;
      if ($scale < 2)
          $this->scale = 2;
      else if ($scale > 12)
          $this->scale = 12;
      else
          $this->scale = $scale;
      $len = strlen($number);
      if ($len != 13 && $len != 12)
          trigger_error('Barcode expects 12 or 13 digit number', E_USER_ERROR);
      $this->number = $number;
      if ($len === 12)
          $this->number .= self::checksum($number);
      $this->_bars = $this->_encode();
      $this->_createImage();
      $this->_drawBars();
      $this->_drawText();
   }

   public function __destruct()  {
      imagedestroy($this->_image);
   }

   protected function _encode()  {
      $barcode[] = self::$GUARD['start'];
      for($i=1;$i<=strlen($this->number)-1;$i++) {
         if($i < 7)
            $barcode[] = self::$LEFT_PARITY[$this->_key[$i-1]][substr($this->number, $i, 1)];
         else
            $barcode[] = self::$RIGHT_PARITY[substr($this->number, $i, 1)];
         if($i == 6)
            $barcode[] = self::$GUARD['middle'];
      }
      $barcode[] = self::$GUARD['end'];
      return $barcode;
   }

   protected function _createImage()  {
      $this->_height = $this->scale * 60;
      $this->_width  = 1.8 * $this->_height;
      $this->_image = imagecreate($this->_width, $this->_height);
      ImageColorAllocate($this->_image, 0xFF, 0xFF, 0xFF);
   }

   protected function _drawBars()  {
      $bar_color=ImageColorAllocate($this->_image, 0x00, 0x00, 0x00);
      $MAX   = $this->_height*0.025;
      $FLOOR = $this->_height*0.825;
      $WIDTH = $this->scale;
      $x = ($this->_height * 0.2) - $WIDTH;

      foreach($this->_bars as $bar) {
         $tall = 0;
         if(strlen($bar)==3 || strlen($bar)==5)
            $tall = ($this->_height * 0.15);
         for($i = 1; $i <= strlen($bar); $i++) {
            if(substr($bar, $i-1, 1)==='1')
                imagefilledrectangle($this->_image, $x, $MAX, $x + $WIDTH, 
                    $FLOOR + $tall, $bar_color);
            $x += $WIDTH;
         }
      }
   }

   protected function _drawText()  {
      $x = $this->_width*0.05;
      $y = $this->_height*0.96;
      $text_color=ImageColorAllocate($this->_image, 0x00, 0x00, 0x00);
      $fontsize = $this->scale*7;
      $kerning = $fontsize*1;
      for($i=0;$i<strlen($this->number);$i++) {
         imagettftext($this->_image, $fontsize, 0, $x, $y, $text_color, $this->font, $this->number[$i]);
         if($i==0 || $i==6)
            $x += $kerning*0.5;
         $x += $kerning;
      }
   }

    public function &image()  {
      return $this->_image;
   }

   public function display()  {
      header("Content-Type: image/png; name=\"barcode.png\"");
      imagepng($this->_image);
   }

   public function save($path = 'barcode.png') {
      $dir = dirname($path);
      if (!file_exists($dir)) {
           mkdir($dir, 0644, true);
      }
      imagepng($this->_image, $path);
   }
}

For generation of the barcode file you can use this in your controller:

Code: Select all

// EAN 13 BARCODE generation
$barcode_file = 'path to your barcode file/'.$product_info['model'].'/ean13-'.$product_info['model'].'.png'; // use the product id or model or both in the filename
if (!is_file($barcode_file)) {
	// barcode file doesn't exist
	if (!is_dir('path to your barcode file/'.$product_info['model'])) {
		// directory doesn't exist, make directory
		mkdir('path to your barcode file/'.$product_info['model']);
	}
	// include the class
	include('path to your class file/barcodeEan13.php');
	// create the ean13 number
	$modnr = preg_replace("/[^0-9]/", "", $product_info['model'] );
	$number = '020'.$product_info['product_id'].'0'.$modnr.'00000';
	$number = substr($number,0,13);
	// create instance, parameters: (number, scale 2-12, font file)
	$barcodeEan13 = new BarcodeEan13($number, 4, "path to your font file/FreeSansBold.ttf");
	$barcodeEan13->save($barcode_file);
}
You can not add a tiff file on here so you will need to create one

Attachments

ean13-BR-7730.png

ean13-BR-7730.png (1.6 KiB) Viewed 17023 times


DISCLAIMER:
You should not modify core files .. if you would like to donate a cup of coffee I will write it in a modification for you.


https://www.youtube.com/watch?v=zXIxDoCRc84


User avatar
Expert Member

Posts

Joined
Mon Jun 10, 2019 9:31 am

Post by shivankagrawal » Mon Aug 01, 2022 3:25 pm

Hi @qstrecsteve,
Greetings, if you want I can create a custom extension for you. Or you can email me at: info@elderberrytech.com to discuss further.

Thanks,
Shivank Agrawal
https://elderberrytech.com/

Extensions Developed for Opencart | Portfolio Opencart | info@elderberrytech.com


Active Member

Posts

Joined
Mon Jul 18, 2016 7:05 pm
Who is online

Users browsing this forum: matteovisotto and 44 guests