mirror of
https://github.com/opensourcepos/opensourcepos.git
synced 2026-02-18 14:48:42 -05:00
Barcode Changes
- Strip out old code - Added missing variable declaration
This commit is contained in:
@@ -506,9 +506,6 @@ class Items extends Secure_Controller
|
||||
{
|
||||
if(isset($item['item_number']) && empty($item['item_number']) && $this->config['barcode_generate_if_empty'])
|
||||
{
|
||||
$barcode_instance = Barcode_lib::barcode_instance($item, $data['barcode_config']);
|
||||
$item['item_number'] = $barcode_instance->getData();
|
||||
|
||||
if(isset($item['item_id']))
|
||||
{
|
||||
$save_item = ['item_number' => $item['item_number']];
|
||||
|
||||
@@ -100,84 +100,6 @@ class Barcode_lib
|
||||
: $item['item_id'];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $item
|
||||
* @param array $barcode_config
|
||||
* @return object
|
||||
*/
|
||||
public static function barcode_instance(array $item, array $barcode_config): object
|
||||
{
|
||||
$barcode_instance = Barcode_lib::get_barcode_instance($barcode_config['barcode_type']);
|
||||
$is_valid = empty($item['item_number'])
|
||||
&& $barcode_config['barcode_generate_if_empty']
|
||||
|| $barcode_instance->validate($item['item_number']);
|
||||
|
||||
// if barcode validation does not succeed,
|
||||
if(!$is_valid)
|
||||
{
|
||||
$barcode_instance = Barcode_lib::get_barcode_instance();
|
||||
}
|
||||
|
||||
$seed = Barcode_lib::barcode_seed($item, $barcode_instance, $barcode_config);
|
||||
$barcode_instance->setData($seed);
|
||||
|
||||
return $barcode_instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $barcode_type
|
||||
* @return object
|
||||
*/
|
||||
private static function get_barcode_instance(string $barcode_type = 'Code128'): object
|
||||
{
|
||||
switch($barcode_type)
|
||||
{
|
||||
case 'Code39':
|
||||
return new Code39();
|
||||
|
||||
case 'Code128':
|
||||
default:
|
||||
return new Code128();
|
||||
|
||||
case 'Ean8':
|
||||
return new Ean8();
|
||||
|
||||
case 'Ean13':
|
||||
return new Ean13();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $item
|
||||
* @param object $barcode_instance
|
||||
* @param array $barcode_config
|
||||
* @return
|
||||
*/
|
||||
private static function barcode_seed(array $item, object $barcode_instance, array $barcode_config)
|
||||
{
|
||||
$seed = $barcode_config['barcode_content'] !== "id" && !empty($item['item_number'])
|
||||
? $item['item_number']
|
||||
: $item['item_id'];
|
||||
|
||||
if($barcode_config['barcode_content'] !== "id" && !empty($item['item_number'])) //TODO: === ?
|
||||
{
|
||||
$seed = $item['item_number'];
|
||||
}
|
||||
else
|
||||
{
|
||||
if($barcode_config['barcode_generate_if_empty'])
|
||||
{
|
||||
// generate barcode with the correct instance
|
||||
$seed = $barcode_instance->generate($seed);
|
||||
}
|
||||
else
|
||||
{
|
||||
$seed = $item['item_id'];
|
||||
}
|
||||
}
|
||||
return $seed;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $item
|
||||
* @param array $barcode_config
|
||||
|
||||
@@ -1,217 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
*
|
||||
* @package Barcode Creator
|
||||
* @copyright (c) 2011 emberlabs.org
|
||||
* @license http://opensource.org/licenses/mit-license.php The MIT License
|
||||
* @link https://github.com/samt/barcode
|
||||
*
|
||||
* Minimum Requirement: PHP 5.3.0
|
||||
*/
|
||||
|
||||
namespace App\Libraries\Barcodes;
|
||||
|
||||
/**
|
||||
* emberlabs Barcode Creator - Barcode Base
|
||||
* Abstract Base
|
||||
*
|
||||
*
|
||||
* @license http://opensource.org/licenses/mit-license.php The MIT License
|
||||
* @link https://github.com/samt/barcode
|
||||
*/
|
||||
abstract class BarcodeBase
|
||||
{
|
||||
/*
|
||||
* GD Resource
|
||||
* @var resource
|
||||
*/
|
||||
protected $img = null;
|
||||
|
||||
/*
|
||||
* @var data - to be set
|
||||
*/
|
||||
protected $data = '';
|
||||
|
||||
/*
|
||||
* @var int x (width)
|
||||
*/
|
||||
protected $x = 0;
|
||||
|
||||
/*
|
||||
* @var int y (height)
|
||||
*/
|
||||
protected $y = 0;
|
||||
|
||||
/*
|
||||
* Print Human Text?
|
||||
* @var bool
|
||||
*/
|
||||
protected $humanText = true;
|
||||
|
||||
/*
|
||||
* Quality
|
||||
* @var int
|
||||
*/
|
||||
protected $jpgQuality = 85;
|
||||
|
||||
/**
|
||||
* (Abstract) Set the data
|
||||
*
|
||||
* @param $data - (int or string) Data to be encoded
|
||||
* @return void
|
||||
* @throws OverflowException
|
||||
*/
|
||||
abstract public function setData($data): void;
|
||||
|
||||
/**
|
||||
* Get the data
|
||||
*
|
||||
* @return BarcodeInterface
|
||||
*/
|
||||
public function getData()
|
||||
{
|
||||
return $this->data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate the given barcode.
|
||||
*
|
||||
* @param $barcode The barcode to validate
|
||||
* @return bool true if it complies with the barcode formatting
|
||||
*/
|
||||
public function validate($barcode)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate a barcode for this implementation using the given seed.
|
||||
* Default implementation returns just the seed
|
||||
* @param $number The seed to generate a barcode for
|
||||
* @return string|null The generated barcode
|
||||
*/
|
||||
public function generate($number)
|
||||
{
|
||||
return $number;
|
||||
}
|
||||
|
||||
/**
|
||||
* (Abstract) Draw the image
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
abstract public function draw();
|
||||
|
||||
/**
|
||||
* Set the Dimensions
|
||||
*
|
||||
* @param int $x
|
||||
* @param int $y
|
||||
* @return BarcodeBase
|
||||
*/
|
||||
public function setDimensions($x, $y)
|
||||
{
|
||||
$this->x = (int) $x;
|
||||
$this->y = (int) $y;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set Quality
|
||||
*
|
||||
* @param int $q - jpeg quality
|
||||
* @return BarcodeBase
|
||||
*/
|
||||
public function setQuality($q)
|
||||
{
|
||||
$this->jpgQuality = (int) $q;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Display human readable text below the code
|
||||
*
|
||||
* @param boolean $enable - Enable the human readable text
|
||||
* @return BarcodeBase
|
||||
*/
|
||||
public function enableHumanText($enable = true)
|
||||
{
|
||||
$this->humanText = (boolean) $enable;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Output Image to the buffer
|
||||
*
|
||||
* @param $type
|
||||
* @return void
|
||||
*/
|
||||
public function output($type = 'png'): void
|
||||
{
|
||||
switch($type)
|
||||
{
|
||||
case 'jpg':
|
||||
case 'jpeg':
|
||||
imagejpeg($this->img, null, $this->jpgQuality);
|
||||
break;
|
||||
|
||||
case 'gif':
|
||||
imagegif($this->img);
|
||||
break;
|
||||
|
||||
case 'png':
|
||||
default:
|
||||
imagepng($this->img);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Save Image
|
||||
*
|
||||
* @param string $filename - File to write to (needs to have .png, .gif, or .jpg extension)
|
||||
* @return void
|
||||
* @throws RuntimeException - If the file could not be written or some other I/O error.
|
||||
*/
|
||||
public function save($filename): void
|
||||
{
|
||||
$type = strtolower(substr(strrchr($filename, '.'), 1));
|
||||
|
||||
switch($type)
|
||||
{
|
||||
case 'jpg':
|
||||
case 'jpeg':
|
||||
imagejpeg($this->img, $filename, $this->jpgQuality);
|
||||
break;
|
||||
|
||||
case 'gif':
|
||||
imagegif($this->img, $filename);
|
||||
break;
|
||||
|
||||
case 'png':
|
||||
imagepng($this->img, $filename);
|
||||
break;
|
||||
|
||||
default:
|
||||
throw new \RuntimeException("Could not determine file type.");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Base64 Encoded
|
||||
* For ouput in-page
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function base64(): string
|
||||
{
|
||||
ob_start();
|
||||
$this->output();
|
||||
|
||||
return base64_encode(ob_get_clean());
|
||||
}
|
||||
}
|
||||
?>
|
||||
@@ -1,328 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
*
|
||||
* @package Barcode Creator
|
||||
* @copyright (c) 2011 emberlabs.org
|
||||
* @license http://opensource.org/licenses/mit-license.php The MIT License
|
||||
* @link https://github.com/samt/barcode
|
||||
*
|
||||
* Minimum Requirement: PHP 5.3.0
|
||||
*/
|
||||
|
||||
namespace App\Libraries\Barcodes;
|
||||
|
||||
/**
|
||||
* emberlabs Barcode Creator - Code128
|
||||
* Generate Code128 Barcodes
|
||||
*
|
||||
*
|
||||
* @license http://opensource.org/licenses/mit-license.php The MIT License
|
||||
* @link https://github.com/samt/barcode
|
||||
*/
|
||||
class Code128 extends BarcodeBase
|
||||
{
|
||||
/*
|
||||
* Sub Type encoding
|
||||
* @var int (should be a class constant)
|
||||
*/
|
||||
private $type = self::TYPE_AUTO;
|
||||
|
||||
/*
|
||||
* This map maps the bar code to the common index. We use the built-in
|
||||
* index that PHP gives us to produce the common index.
|
||||
* @var static array
|
||||
*/
|
||||
private static $barMap = array(
|
||||
11011001100, 11001101100, 11001100110, 10010011000, 10010001100, // 4 (end)
|
||||
10001001100, 10011001000, 10011000100, 10001100100, 11001001000, // 9
|
||||
11001000100, 11000100100, 10110011100, 10011011100, 10011001110, // 14
|
||||
10111001100, 10011101100, 10011100110, 11001110010, 11001011100, // 19
|
||||
11001001110, 11011100100, 11001110100, 11101101110, 11101001100, // 24
|
||||
11100101100, 11100100110, 11101100100, 11100110100, 11100110010, // 29
|
||||
11011011000, 11011000110, 11000110110, 10100011000, 10001011000, // 34
|
||||
10001000110, 10110001000, 10001101000, 10001100010, 11010001000, // 39
|
||||
11000101000, 11000100010, 10110111000, 10110001110, 10001101110, // 44
|
||||
10111011000, 10111000110, 10001110110, 11101110110, 11010001110, // 49
|
||||
11000101110, 11011101000, 11011100010, 11011101110, 11101011000, // 54
|
||||
11101000110, 11100010110, 11101101000, 11101100010, 11100011010, // 59
|
||||
11101111010, 11001000010, 11110001010, 10100110000, 10100001100, // 64
|
||||
10010110000, 10010000110, 10000101100, 10000100110, 10110010000, // 69
|
||||
10110000100, 10011010000, 10011000010, 10000110100, 10000110010, // 74
|
||||
11000010010, 11001010000, 11110111010, 11000010100, 10001111010, // 79
|
||||
10100111100, 10010111100, 10010011110, 10111100100, 10011110100, // 84
|
||||
10011110010, 11110100100, 11110010100, 11110010010, 11011011110, // 89
|
||||
11011110110, 11110110110, 10101111000, 10100011110, 10001011110, // 94
|
||||
10111101000, 10111100010, 11110101000, 11110100010, 10111011110, // 99
|
||||
10111101110, 11101011110, 11110101110, 11010000100, 11010010000, // 104
|
||||
11010011100, 1100011101011 // 106 (last char, also one bit longer)
|
||||
);
|
||||
|
||||
/*
|
||||
* This map takes the charset from subtype A and PHP will index the array
|
||||
* natively to the matching code from the barMap.
|
||||
* @var static array
|
||||
*/
|
||||
private static $mapA = array(
|
||||
' ', '!', '"', '#', '$', '%', '&', "'", '(', ')', // 9 (end)
|
||||
'*', '+', ',', '-', '.', '/', '0', '1', '2', '3', // 19
|
||||
'4', '5', '6', '7', '8', '9', ':', ';', '<', '=', // 29
|
||||
'>', '?', '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G', // 39
|
||||
'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', // 49
|
||||
'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '[', // 59
|
||||
'\\', ']', '^', '_', // 63 (We're going into the weird bytes next)
|
||||
|
||||
// Hex is a little more concise in this context
|
||||
"\x00", "\x01", "\x02", "\x03", "\x04", "\x05", // 69
|
||||
"\x06", "\x07", "\x08", "\x09", "\x0A", "\x0B", // 75
|
||||
"\x0C", "\x0D", "\x0E", "\x0F", "\x10", "\x11", // 81
|
||||
"\x12", "\x13", "\x14", "\x15", "\x16", "\x17", // 87
|
||||
"\x18", "\x19", "\x1A", "\x1B", "\x1C", "\x1D", // 93
|
||||
"\x1E", "\x1F", // 95
|
||||
|
||||
// Now for system codes
|
||||
'FNC_3', 'FNC_2', 'SHIFT_B', 'CODE_C', 'CODE_B', // 100
|
||||
'FNC_4', 'FNC_1', 'START_A', 'START_B', 'START_C', // 105
|
||||
'STOP', // 106
|
||||
);
|
||||
|
||||
/*
|
||||
* Same idea from MapA applied here to B.
|
||||
* @var static array
|
||||
*/
|
||||
private static $mapB = array(
|
||||
' ', '!', '"', '#', '$', '%', '&', "'", '(', ')', // 9 (end)
|
||||
'*', '+', ',', '-', '.', '/', '0', '1', '2', '3', // 19
|
||||
'4', '5', '6', '7', '8', '9', ':', ';', '<', '=', // 29
|
||||
'>', '?', '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G', // 39
|
||||
'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', // 49
|
||||
'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '[', // 59
|
||||
'\\', ']', '^', '_', '`', 'a', 'b', 'c', 'd', 'e', // 69
|
||||
'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', // 79
|
||||
'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', // 89
|
||||
'z', '{', '|', '}', '~', "\x7F", // 95
|
||||
|
||||
// Now for system codes
|
||||
'FNC_3', 'FNC_2', 'SHIFT_A', 'CODE_C', 'FNC_4', // 100
|
||||
'CODE_A', 'FNC_1', 'START_A', 'START_B', 'START_C', // 105
|
||||
'STOP', // 106
|
||||
);
|
||||
|
||||
/*
|
||||
* Map C works a little different. The index is the value when the mapping
|
||||
* occors.
|
||||
* @var static array
|
||||
*/
|
||||
private static $mapC = array(
|
||||
100 =>
|
||||
'CODE_B', 'CODE_A', 'FNC_1', 'START_A', 'START_B',
|
||||
'START_C', 'STOP', // 106
|
||||
);
|
||||
|
||||
/*
|
||||
* Subtypes
|
||||
*/
|
||||
public const TYPE_AUTO = 0; // Automatically detect the best code
|
||||
public const TYPE_A = 1; // ASCII 00-95 (0-9, A-Z, Control codes, and some special chars)
|
||||
public const TYPE_B = 2; // ASCII 32-127 (0-9, A-Z, a-z, special chars)
|
||||
public const TYPE_C = 3; // Numbers 00-99 (two digits per code)
|
||||
|
||||
/**
|
||||
* Set the data
|
||||
*
|
||||
* @param $data - (int or string) Data to be encoded
|
||||
* @return void
|
||||
* @throws OverflowException
|
||||
*/
|
||||
public function setData($data): void
|
||||
{
|
||||
$this->data = $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the subtype
|
||||
* Defaults to Autodetect
|
||||
*
|
||||
* @param int $type - Const flag for the type
|
||||
* @return void
|
||||
*/
|
||||
public function setSubType($type)
|
||||
{
|
||||
$this->type = ($type < 1 || $type > 3) ? self::TYPE_AUTO : (int) $type;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get they key (value of the character)
|
||||
*
|
||||
* @param $char
|
||||
* @return false|int|string pattern
|
||||
*/
|
||||
private function getKey($char)
|
||||
{
|
||||
switch ($this->type)
|
||||
{
|
||||
case self::TYPE_A:
|
||||
return array_search($char, self::$mapA);
|
||||
|
||||
case self::TYPE_B:
|
||||
return array_search($char, self::$mapB);
|
||||
|
||||
case self::TYPE_C:
|
||||
$charInt = (int) $char;
|
||||
if (strlen($char) == 2 && $charInt <= 99 && $charInt >= 0)
|
||||
{
|
||||
return $charInt;
|
||||
}
|
||||
|
||||
return array_search($char, self::$mapC);
|
||||
|
||||
default:
|
||||
$this->resolveSubtype();
|
||||
return $this->getKey($char);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the bar
|
||||
*
|
||||
* @param $char
|
||||
* @return int - pattern
|
||||
*/
|
||||
private function getBar($char)
|
||||
{
|
||||
$key = $this->getKey($char);
|
||||
|
||||
return self::$barMap[($key !== false) ? $key : 0];
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolve subtype
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private function resolveSubtype() //TODO: Do some better charset checking and enforcement
|
||||
{
|
||||
if ($this->type == self::TYPE_AUTO)
|
||||
{
|
||||
// If it is purely numeric, this is easy
|
||||
if (is_numeric($this->data))
|
||||
{
|
||||
$this->type = self::TYPE_C;
|
||||
}
|
||||
// Are there only capitals?
|
||||
elseif(strtoupper($this->data) == $this->data)
|
||||
{
|
||||
$this->type = self::TYPE_A;
|
||||
}
|
||||
else
|
||||
{
|
||||
$this->type = self::TYPE_B;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the name of a start char from the current subtype
|
||||
* @return string
|
||||
*/
|
||||
private function getStartChar(): string
|
||||
{
|
||||
$this->resolveSubtype();
|
||||
|
||||
switch($this->type)
|
||||
{
|
||||
default:
|
||||
case self::TYPE_A: return 'START_A';
|
||||
case self::TYPE_B: return 'START_B';
|
||||
case self::TYPE_C: return 'START_C';
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Draw the image
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function draw(): void
|
||||
{
|
||||
$this->resolveSubtype();
|
||||
$charAry = str_split($this->data);
|
||||
|
||||
// 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;
|
||||
$pxPerBar = (int) ($this->x / $numBarsRequired);
|
||||
$currentX = ($this->x - ($numBarsRequired * $pxPerBar)) / 2;
|
||||
|
||||
if ($pxPerBar < 1)
|
||||
{
|
||||
throw new \LogicException("Not enough space on this barcode for this message, increase the width of the barcode");
|
||||
}
|
||||
|
||||
if ($this->type == self::TYPE_C)
|
||||
{
|
||||
if (sizeof($charAry) % 2)
|
||||
{
|
||||
array_unshift($charAry, '0');
|
||||
}
|
||||
|
||||
$pairs = '';
|
||||
$newAry = array();
|
||||
foreach($charAry as $k => $char)
|
||||
{
|
||||
if (($k % 2) == 0 && $k != 0)
|
||||
{
|
||||
$newAry[] = $pairs;
|
||||
$pairs = '';
|
||||
}
|
||||
|
||||
$pairs .= $char;
|
||||
}
|
||||
|
||||
$newAry[] = $pairs;
|
||||
$charAry = $newAry;
|
||||
}
|
||||
|
||||
// Add the start
|
||||
array_unshift($charAry, $this->getStartChar());
|
||||
|
||||
// Checksum collector
|
||||
$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);
|
||||
|
||||
// Print the code
|
||||
foreach($charAry as $k => $char)
|
||||
{
|
||||
$code = $this->getBar($char);
|
||||
$checkSumCollector += $this->getKey($char) * $k; // $k will be 0 for our first
|
||||
|
||||
foreach(str_split((string) $code) as $bit)
|
||||
{
|
||||
imagefilledrectangle($this->img, $currentX, 0, ($currentX + $pxPerBar), ($this->y - 1), (($bit == '1') ? $black : $white));
|
||||
$currentX += $pxPerBar;
|
||||
}
|
||||
}
|
||||
|
||||
$ending[] = self::$barMap[$checkSumCollector % 103];
|
||||
$ending[] = self::$barMap[106]; // STOP.
|
||||
|
||||
foreach($ending as $code)
|
||||
{
|
||||
foreach(str_split((string) $code) as $bit)
|
||||
{
|
||||
imagefilledrectangle($this->img, $currentX, 0, ($currentX + $pxPerBar), ($this->y - 1), (($bit == '1') ? $black : $white));
|
||||
$currentX += $pxPerBar;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
?>
|
||||
@@ -1,190 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
*
|
||||
* @package Barcode Creator
|
||||
* @copyright (c) 2011 emberlabs.org
|
||||
* @license http://opensource.org/licenses/mit-license.php The MIT License
|
||||
* @link https://github.com/samt/barcode
|
||||
*
|
||||
* Minimum Requirement: PHP 5.3.0
|
||||
*/
|
||||
|
||||
namespace App\Libraries\Barcodes;
|
||||
|
||||
use OverflowException;
|
||||
|
||||
/**
|
||||
* emberlabs Barcode Creator - Code39
|
||||
* Generate Code39 Barcodes
|
||||
*
|
||||
*
|
||||
* @license http://opensource.org/licenses/mit-license.php The MIT License
|
||||
* @link https://github.com/samt/barcode
|
||||
*/
|
||||
class Code39 extends BarcodeBase
|
||||
{
|
||||
/**
|
||||
* Binary map
|
||||
* @var array binMap
|
||||
*/
|
||||
private static $binMap = array(
|
||||
' ' => '011000100',
|
||||
'$' => '010101000',
|
||||
'%' => '000101010',
|
||||
'*' => '010010100', // Start/stop marker
|
||||
'+' => '010001010',
|
||||
'|' => '010000101',
|
||||
'.' => '110000100',
|
||||
'/' => '010100010',
|
||||
'-' => '010000101',
|
||||
'0' => '000110100',
|
||||
'1' => '100100001',
|
||||
'2' => '001100001',
|
||||
'3' => '101100000',
|
||||
'4' => '000110001',
|
||||
'5' => '100110000',
|
||||
'6' => '001110000',
|
||||
'7' => '000100101',
|
||||
'8' => '100100100',
|
||||
'9' => '001100100',
|
||||
'A' => '100001001',
|
||||
'B' => '001001001',
|
||||
'C' => '101001000',
|
||||
'D' => '000011001',
|
||||
'E' => '100011000',
|
||||
'F' => '001011000',
|
||||
'G' => '000001101',
|
||||
'H' => '100001100',
|
||||
'I' => '001001100',
|
||||
'J' => '000011100',
|
||||
'K' => '100000011',
|
||||
'L' => '001000011',
|
||||
'M' => '101000010',
|
||||
'N' => '000010011',
|
||||
'O' => '100010010',
|
||||
'P' => '001010010',
|
||||
'Q' => '000000111',
|
||||
'R' => '100000110',
|
||||
'S' => '001000110',
|
||||
'T' => '000010110',
|
||||
'U' => '110000001',
|
||||
'V' => '011000001',
|
||||
'W' => '111000000',
|
||||
'X' => '010010001',
|
||||
'Y' => '110010000',
|
||||
'Z' => '011010000',
|
||||
);
|
||||
|
||||
/**
|
||||
* const bar proportions
|
||||
*/
|
||||
public const NARROW_BAR = 20;
|
||||
public const WIDE_BAR = 55;
|
||||
public const QUIET_BAR = 35;
|
||||
|
||||
/**
|
||||
* Set the data
|
||||
*
|
||||
* @param $data - (int or string) Data to be encoded
|
||||
* @return void
|
||||
* @throws OverflowException
|
||||
*/
|
||||
public function setData($data): void
|
||||
{
|
||||
$this->data = $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a binary map value
|
||||
*/
|
||||
/**
|
||||
* @param $char
|
||||
* @return string
|
||||
*/
|
||||
private function getMap($char)
|
||||
{
|
||||
return self::$binMap[$char] ?: self::$this->binMap[' '];
|
||||
}
|
||||
|
||||
/**
|
||||
* Draw the image
|
||||
*
|
||||
* Based on the implentation PHP Barcode Image Generator v1.0
|
||||
* by Charles J. Scheffold - cs@sid6581.net
|
||||
* It was released into the Public Domain by its creator.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function draw(): void
|
||||
{
|
||||
// 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($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($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)
|
||||
{
|
||||
throw new OverflowException("You need to specify a bigger width to properly display this barcode");
|
||||
}
|
||||
|
||||
$currentBarX = (int)(($this->x - $imageWidth) / 2);
|
||||
$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);
|
||||
$color = $black;
|
||||
|
||||
foreach($charAry as $_k => $char)
|
||||
{
|
||||
$code = str_split($this->getMap($char));
|
||||
$color = $black;
|
||||
|
||||
foreach($code as $k => $bit)
|
||||
{
|
||||
// Narrow bar
|
||||
if ($bit == '0')
|
||||
{
|
||||
imagefilledrectangle($this->img, $currentBarX, 0, ($currentBarX + $narrowBar), ($this->y - 1), $color);
|
||||
$currentBarX += $narrowBar;
|
||||
}
|
||||
// Wide Bar
|
||||
elseif($bit == '1')
|
||||
{
|
||||
imagefilledrectangle($this->img, $currentBarX, 0, ($currentBarX + $wideBar), ($this->y - 1), $color);
|
||||
$currentBarX += $wideBar;
|
||||
}
|
||||
|
||||
$color = ($color == $black) ? $white : $black;
|
||||
}
|
||||
|
||||
// Skip the spacer on the last run
|
||||
if ($_k == (sizeof($charAry) - 1))
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
// Draw spacer
|
||||
imagefilledrectangle($this->img, $currentBarX, 0, ($currentBarX + $quietBar), ($this->y - 1), $white);
|
||||
$currentBarX += $quietBar;
|
||||
}
|
||||
}
|
||||
}
|
||||
?>
|
||||
@@ -1,385 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
*
|
||||
* @package Barcode Creator
|
||||
* @copyright (c) 2011 emberlabs.org
|
||||
* @license http://opensource.org/licenses/mit-license.php The MIT License
|
||||
* @link https://github.com/samt/barcode
|
||||
*
|
||||
* Minimum Requirement: PHP 5.3.0
|
||||
*/
|
||||
|
||||
/**
|
||||
* Image_Barcode2_Driver_Ean13 class
|
||||
*
|
||||
* Renders EAN 13 barcodes
|
||||
*
|
||||
* PHP versions 5
|
||||
*
|
||||
* LICENSE: This source file is subject to version 3.0 of the PHP license
|
||||
* that is available through the world-wide-web at the following URI:
|
||||
* http://www.php.net/license/3_0.txt. If you did not receive a copy of
|
||||
* the PHP License and are unable to obtain it through the web, please
|
||||
* send a note to license@php.net so we can mail you a copy immediately.
|
||||
*
|
||||
* @category Image
|
||||
* @package Image_Barcode2
|
||||
* @author Didier Fournout <didier.fournout@nyc.fr>
|
||||
* @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 App\Libraries\Barcodes;
|
||||
|
||||
/**
|
||||
* emberlabs Barcode Creator - Ean13
|
||||
* Generate Ean13 Barcodes
|
||||
*
|
||||
*
|
||||
* @license http://opensource.org/licenses/mit-license.php The MIT License
|
||||
* @link https://github.com/samt/barcode
|
||||
*/
|
||||
class Ean13 extends BarcodeBase
|
||||
{
|
||||
/*
|
||||
* Coding map
|
||||
* @var array
|
||||
*/
|
||||
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')
|
||||
);
|
||||
|
||||
/**
|
||||
* Set the data
|
||||
*
|
||||
* @param $data - (int or string) Data to be encoded
|
||||
* @return void
|
||||
*/
|
||||
public function setData($data): void
|
||||
{
|
||||
$this->data = $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate EAN13 code out of a provided number
|
||||
* Code taken from http://stackoverflow.com/questions/19890144/generate-valid-ean13-in-php (unknown copyright / license claims)
|
||||
*
|
||||
* @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
|
||||
*/
|
||||
public function generate($number): string
|
||||
{
|
||||
$number = '200' . str_pad($number, 9, '0', STR_PAD_LEFT);
|
||||
$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($number) - 1; $i >= 0; --$i)
|
||||
{
|
||||
$sum += (int)$number[$i] * ($weightflag?3:1);
|
||||
$weightflag = !$weightflag;
|
||||
}
|
||||
$number .= (10 - ($sum % 10)) % 10;
|
||||
|
||||
return $number;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $barcode
|
||||
* @return bool
|
||||
*/
|
||||
public function validate($barcode): bool
|
||||
{
|
||||
// check to see if barcode is 13 digits long
|
||||
if (!preg_match("/^[0-9]{13}$/", $barcode)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$digits = $barcode;
|
||||
|
||||
// 1. Add the values of the digits in the
|
||||
// even-numbered positions: 2, 4, 6, etc.
|
||||
$even_sum = $digits[1] + $digits[3] + $digits[5] +
|
||||
$digits[7] + $digits[9] + $digits[11];
|
||||
|
||||
// 2. Multiply this result by 3.
|
||||
$even_sum_three = $even_sum * 3;
|
||||
|
||||
// 3. Add the values of the digits in the
|
||||
// odd-numbered positions: 1, 3, 5, etc.
|
||||
$odd_sum = $digits[0] + $digits[2] + $digits[4] +
|
||||
$digits[6] + $digits[8] + $digits[10];
|
||||
|
||||
// 4. Sum the results of steps 2 and 3.
|
||||
$total_sum = $even_sum_three + $odd_sum;
|
||||
|
||||
// 5. The check character is the smallest number which,
|
||||
// when added to the result in step 4, produces a multiple of 10.
|
||||
$next_ten = (ceil($total_sum / 10)) * 10;
|
||||
$check_digit = $next_ten - $total_sum;
|
||||
|
||||
// if the check digit and the last digit of the
|
||||
// barcode are OK return true;
|
||||
if ($check_digit == $digits[12]) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Draw the image
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
/**
|
||||
* (Abstract) Draw the image
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function draw()
|
||||
{
|
||||
// Bars is in reference to a single, 1-level bar
|
||||
$pxPerBar = 2;
|
||||
|
||||
// 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;
|
||||
|
||||
$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($this->data, 0, 1);
|
||||
|
||||
// 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
|
||||
);
|
||||
|
||||
$xpos += $pxPerBar;
|
||||
|
||||
// space
|
||||
$xpos += $pxPerBar;
|
||||
|
||||
// bar
|
||||
imagefilledrectangle(
|
||||
$this->img,
|
||||
$xpos,
|
||||
0,
|
||||
$xpos + $pxPerBar - 1,
|
||||
$this->y,
|
||||
$black
|
||||
);
|
||||
|
||||
$xpos += $pxPerBar;
|
||||
|
||||
// Draw left $this->data contents
|
||||
$set_array = $this->_codingmapleft[$key];
|
||||
|
||||
for ($idx = 1; $idx < 7; ++$idx)
|
||||
{
|
||||
$value = substr($this->data, $idx, 1);
|
||||
|
||||
if(isset($this->_codingmap[$value][$set_array[$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;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new \Exception("Invalid index in _codingmap array. Value: $value, Set: {$set_array[$idx - 1]}");
|
||||
}
|
||||
}
|
||||
|
||||
// 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 $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
|
||||
);
|
||||
}
|
||||
}
|
||||
?>
|
||||
@@ -1,374 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
*
|
||||
* @package Barcode Creator
|
||||
* @copyright (c) 2011 emberlabs.org
|
||||
* @license http://opensource.org/licenses/mit-license.php The MIT License
|
||||
* @link https://github.com/samt/barcode
|
||||
*
|
||||
* Minimum Requirement: PHP 5.3.0
|
||||
*/
|
||||
|
||||
/**
|
||||
* Image_Barcode2_Driver_Ean8 class
|
||||
*
|
||||
* Renders EAN 8 barcodes
|
||||
*
|
||||
* PHP versions 5
|
||||
*
|
||||
* LICENSE: This source file is subject to version 3.0 of the PHP license
|
||||
* that is available through the world-wide-web at the following URI:
|
||||
* http://www.php.net/license/3_0.txt. If you did not receive a copy of
|
||||
* the PHP License and are unable to obtain it through the web, please
|
||||
* send a note to license@php.net so we can mail you a copy immediately.
|
||||
*
|
||||
* @category Image
|
||||
* @package Image_Barcode2
|
||||
* @author Didier Fournout <didier.fournout@nyc.fr>
|
||||
* @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 App\Libraries\Barcodes;
|
||||
|
||||
/**
|
||||
* 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
|
||||
{
|
||||
/*
|
||||
* 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 The internal code you want to have EANed. The prefix, zero-padding and checksum are added by the function.
|
||||
* @param $len
|
||||
* @return string|null Complete EAN code
|
||||
*/
|
||||
public function generate($number, $len = 8)
|
||||
{
|
||||
$barcode = $number;
|
||||
|
||||
if($number > -1)
|
||||
{
|
||||
$data_len = $len - 1;
|
||||
|
||||
//Padding
|
||||
$barcode = str_pad($barcode, $data_len, '0', STR_PAD_LEFT);
|
||||
$barcode_len = strlen($barcode);
|
||||
|
||||
// calculate check digit
|
||||
$sum_a = 0;
|
||||
for ($i = 1; $i < $data_len; $i += 2)
|
||||
{
|
||||
$sum_a += $barcode[$i];
|
||||
}
|
||||
|
||||
if ($len > 12)
|
||||
{
|
||||
$sum_a *= 3;
|
||||
}
|
||||
|
||||
$sum_b = 0;
|
||||
for ($i = 0; $i < $data_len; $i += 2)
|
||||
{
|
||||
$sum_b += ($barcode[$i]);
|
||||
}
|
||||
|
||||
if ($len < 13)
|
||||
{
|
||||
$sum_b *= 3;
|
||||
}
|
||||
|
||||
$r = ($sum_a + $sum_b) % 10;
|
||||
|
||||
if($r > 0)
|
||||
{
|
||||
$r = (10 - $r);
|
||||
}
|
||||
|
||||
if (strlen($barcode) == $data_len)
|
||||
{
|
||||
// add check digit
|
||||
$barcode .= $r;
|
||||
}
|
||||
elseif ($r !== intval($barcode[$data_len]))
|
||||
{
|
||||
// wrong check digit
|
||||
$barcode = null;
|
||||
}
|
||||
}
|
||||
|
||||
return $barcode;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $barcode
|
||||
* @return bool
|
||||
*/
|
||||
public function validate($barcode): bool
|
||||
{
|
||||
$ean = str_replace(array("-","/"," ","\t","\n"), "", $barcode); // make a clean ean
|
||||
$len = strlen($ean);
|
||||
|
||||
if( !is_numeric($ean) || strlen($barcode) != 8 )
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
$weights = array(3,1,3,1,3,1,3); // weights
|
||||
$chk = $ean[7]; // 8. digit
|
||||
|
||||
$i = 0;
|
||||
$sum = 0;
|
||||
|
||||
// sum or weight * digit
|
||||
foreach($weights as $num) {
|
||||
|
||||
$sum += $num * $ean[$i];
|
||||
++$i;
|
||||
}
|
||||
|
||||
if( (($sum + $chk) % 10) == 0 )
|
||||
{
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the data
|
||||
*
|
||||
* @param $data - (int or string) Data to be encoded
|
||||
* @return void
|
||||
*/
|
||||
public function setData($data): void
|
||||
{
|
||||
$this->data = $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Draw the image
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function draw(): void
|
||||
{
|
||||
// Bars is in reference to a single, 1-level bar
|
||||
$pxPerBar = 2;
|
||||
|
||||
// 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;
|
||||
|
||||
$this->img = @imagecreate($this->x, $this->y);
|
||||
|
||||
if (!$this->img)
|
||||
{
|
||||
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);
|
||||
|
||||
// get the first digit which is the key for creating the first 6 bars
|
||||
$key = substr($this->data, 0, 1);
|
||||
|
||||
// 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
|
||||
);
|
||||
|
||||
$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($this->data, $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 $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
|
||||
);
|
||||
}
|
||||
}
|
||||
?>
|
||||
@@ -19,7 +19,7 @@ body {
|
||||
}
|
||||
h1 {
|
||||
font-weight: lighter;
|
||||
letter-spacing: 0.8;
|
||||
letter-spacing: 0.8rem;
|
||||
font-size: 3rem;
|
||||
color: var(--dark-text-color);
|
||||
margin: 0;
|
||||
|
||||
@@ -1,3 +1,8 @@
|
||||
<?php
|
||||
/**
|
||||
* @var string $controller_name
|
||||
*/
|
||||
?>
|
||||
<?= view('partial/header') ?>
|
||||
|
||||
<script type="text/javascript">
|
||||
|
||||
Reference in New Issue
Block a user