.
//
// See LICENSE.TXT file for more information.
// -------------------------------------------------------------------
//
// Description : PHP class to creates array representations for
// 2D barcodes to be used with TCPDF.
//
//============================================================+
/**
* @file
* PHP class to creates array representations for 2D barcodes to be used with TCPDF.
* @package com.tecnick.tcpdf
* @author Nicola Asuni
* @version 1.0.015
*/
/**
* @class TCPDF2DBarcode
* PHP class to creates array representations for 2D barcodes to be used with TCPDF (http://www.tcpdf.org).
* @package com.tecnick.tcpdf
* @version 1.0.015
* @author Nicola Asuni
*/
use Milon\Barcode\QRcode;
use Milon\Barcode\Datamatrix;
use Milon\Barcode\PDF417;
use Illuminate\Support\Str;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
* Description of DNS2D
*
* @author dinesh
*/
class DNS2D {
/**
* Array representation of barcode.
* @protected
*/
protected $barcode_array = false;
/**
* path to save png in getBarcodePNGPath
* @var
*/
protected $store_path;
/**
* Return a SVG string representation of barcode.
*
$arrcode['code'] code to be printed on text label
*
$arrcode['num_rows'] required number of rows
*
$arrcode['num_cols'] required number of columns
*
$arrcode['bcode'][$r][$c] value of the cell is $r row and $c column (0 = transparent, 1 = black)
* @param $code (string) code to print
* @param $type (string) type of barcode:
DATAMATRIX : Datamatrix (ISO/IEC 16022)
PDF417 : PDF417 (ISO/IEC 15438:2006)
PDF417,a,e,t,s,f,o0,o1,o2,o3,o4,o5,o6 : PDF417 with parameters: a = aspect ratio (width/height); e = error correction level (0-8); t = total number of macro segments; s = macro segment index (0-99998); f = file ID; o0 = File Name (text); o1 = Segment Count (numeric); o2 = Time Stamp (numeric); o3 = Sender (text); o4 = Addressee (text); o5 = File Size (numeric); o6 = Checksum (numeric). NOTES: Parameters t, s and f are required for a Macro Control Block, all other parametrs are optional. To use a comma character ',' on text options, replace it with the character 255: "\xff".
QRCODE : QRcode Low error correction
QRCODE,L : QRcode Low error correction
QRCODE,M : QRcode Medium error correction
QRCODE,Q : QRcode Better error correction
QRCODE,H : QR-CODE Best error correction
RAW: raw mode - comma-separad list of array rows
RAW2: raw mode - array rows are surrounded by square parenthesis.
TEST : Test matrix
* @param $w (int) Width of a single rectangle element in user units.
* @param $h (int) Height of a single rectangle element in user units.
* @param $color (string) Foreground color (in SVG format) for bar elements (background is transparent).
* @return string SVG code.
* @protected
*/
public function getBarcodeSVG($code, $type, $w = 3, $h = 3, $color = 'black') {
if (!$this->store_path) {
$this->setStorPath(app('config')->get("barcode.store_path"));
}
//set barcode code and type
$this->setBarcode($code, $type);
// replace table for special characters
$repstr = array("\0" => '', '&' => '&', '<' => '<', '>' => '>');
$svg = '<' . '?' . 'xml version="1.0" standalone="no"' . '?' . '>' . "\n";
$svg .= '' . "\n";
$svg .= '' . "\n";
return $svg;
}
/**
* Return an HTML representation of barcode.
*
$arrcode['code'] code to be printed on text label
*
$arrcode['num_rows'] required number of rows
*
$arrcode['num_cols'] required number of columns
*
$arrcode['bcode'][$r][$c] value of the cell is $r row and $c column (0 = transparent, 1 = black)
* @param $code (string) code to print
* @param $type (string) type of barcode:
DATAMATRIX : Datamatrix (ISO/IEC 16022)
PDF417 : PDF417 (ISO/IEC 15438:2006)
PDF417,a,e,t,s,f,o0,o1,o2,o3,o4,o5,o6 : PDF417 with parameters: a = aspect ratio (width/height); e = error correction level (0-8); t = total number of macro segments; s = macro segment index (0-99998); f = file ID; o0 = File Name (text); o1 = Segment Count (numeric); o2 = Time Stamp (numeric); o3 = Sender (text); o4 = Addressee (text); o5 = File Size (numeric); o6 = Checksum (numeric). NOTES: Parameters t, s and f are required for a Macro Control Block, all other parametrs are optional. To use a comma character ',' on text options, replace it with the character 255: "\xff".
QRCODE : QRcode Low error correction
QRCODE,L : QRcode Low error correction
QRCODE,M : QRcode Medium error correction
QRCODE,Q : QRcode Better error correction
QRCODE,H : QR-CODE Best error correction
RAW: raw mode - comma-separad list of array rows
RAW2: raw mode - array rows are surrounded by square parenthesis.
TEST : Test matrix
* @param $w (int) Width of a single rectangle element in pixels.
* @param $h (int) Height of a single rectangle element in pixels.
* @param $color (string) Foreground color for bar elements (background is transparent).
* @return string HTML code.
* @protected
*/
public function getBarcodeHTML($code, $type, $w = 10, $h = 10, $color = 'black') {
if (!$this->store_path) {
$this->setStorPath(app('config')->get("barcode.store_path"));
}
//set barcode code and type
$this->setBarcode($code, $type);
$html = '
' . "\n";
// print barcode elements
$y = 0;
// for each row
for ($r = 0; $r < $this->barcode_array['num_rows']; ++$r) {
$x = 0;
// for each column
for ($c = 0; $c < $this->barcode_array['num_cols']; ++$c) {
if ($this->barcode_array['bcode'][$r][$c] == 1) {
// draw a single barcode cell
$html .= '
' . "\n";
}
$x += $w;
}
$y += $h;
}
$html .= '
' . "\n";
return $html;
}
/**
* Return a PNG image representation of barcode (requires GD or Imagick library).
*
$arrcode['code'] code to be printed on text label
*
$arrcode['num_rows'] required number of rows
*
$arrcode['num_cols'] required number of columns
*
$arrcode['bcode'][$r][$c] value of the cell is $r row and $c column (0 = transparent, 1 = black)
* @param $code (string) code to print
* @param $type (string) type of barcode:
DATAMATRIX : Datamatrix (ISO/IEC 16022)
PDF417 : PDF417 (ISO/IEC 15438:2006)
PDF417,a,e,t,s,f,o0,o1,o2,o3,o4,o5,o6 : PDF417 with parameters: a = aspect ratio (width/height); e = error correction level (0-8); t = total number of macro segments; s = macro segment index (0-99998); f = file ID; o0 = File Name (text); o1 = Segment Count (numeric); o2 = Time Stamp (numeric); o3 = Sender (text); o4 = Addressee (text); o5 = File Size (numeric); o6 = Checksum (numeric). NOTES: Parameters t, s and f are required for a Macro Control Block, all other parametrs are optional. To use a comma character ',' on text options, replace it with the character 255: "\xff".
QRCODE : QRcode Low error correction
QRCODE,L : QRcode Low error correction
QRCODE,M : QRcode Medium error correction
QRCODE,Q : QRcode Better error correction
QRCODE,H : QR-CODE Best error correction
RAW: raw mode - comma-separad list of array rows
RAW2: raw mode - array rows are surrounded by square parenthesis.
TEST : Test matrix
* @param $w (int) Width of a single rectangle element in pixels.
* @param $h (int) Height of a single rectangle element in pixels.
* @param $color (array) RGB (0-255) foreground color for bar elements (background is transparent).
* @return path or false in case of error.
* @protected
*/
public function getBarcodePNG($code, $type, $w = 3, $h = 3, $color = array(0, 0, 0)) {
if (!$this->store_path) {
$this->setStorPath(app('config')->get("barcode.store_path"));
}
//set barcode code and type
$this->setBarcode($code, $type);
// calculate image size
$width = ($this->barcode_array['num_cols'] * $w);
$height = ($this->barcode_array['num_rows'] * $h);
if (function_exists('imagecreate')) {
// GD library
$imagick = false;
$png = imagecreate($width, $height);
$bgcol = imagecolorallocate($png, 255, 255, 255);
imagecolortransparent($png, $bgcol);
$fgcol = imagecolorallocate($png, $color[0], $color[1], $color[2]);
} elseif (extension_loaded('imagick')) {
$imagick = true;
$bgcol = new \imagickpixel('rgb(255,255,255');
$fgcol = new \imagickpixel('rgb(' . $color[0] . ',' . $color[1] . ',' . $color[2] . ')');
$png = new \Imagick();
$png->newImage($width, $height, 'none', 'png');
$bar = new \imagickdraw();
$bar->setfillcolor($fgcol);
} else {
return false;
}
// print barcode elements
$y = 0;
// for each row
for ($r = 0; $r < $this->barcode_array['num_rows']; ++$r) {
$x = 0;
// for each column
for ($c = 0; $c < $this->barcode_array['num_cols']; ++$c) {
if ($this->barcode_array['bcode'][$r][$c] == 1) {
// draw a single barcode cell
if ($imagick) {
$bar->rectangle($x, $y, ($x + $w), ($y + $h));
} else {
imagefilledrectangle($png, $x, $y, ($x + $w), ($y + $h), $fgcol);
}
}
$x += $w;
}
$y += $h;
}
ob_start();
// get image out put
if ($imagick) {
$png->drawimage($bar);
echo $png;
} else {
imagepng($png);
imagedestroy($png);
}
$image = ob_get_clean();
$image = base64_encode($image);
//$image = 'data:image/png;base64,' . base64_encode($image);
return $image;
}
/**
* Return a .png file path which create in server
* @param $code (string) code to print
* @param $type (string) type of barcode:
RMS4CC : RMS4CC (Royal Mail 4-state Customer Code) - CBC (Customer Bar Code)
KIX : KIX (Klant index - Customer index)
IMB: Intelligent Mail Barcode - Onecode - USPS-B-3200
CODABAR : CODABAR
CODE11 : CODE 11
PHARMA : PHARMACODE
PHARMA2T : PHARMACODE TWO-TRACKS
* @param $w (int) Width of a single bar element in pixels.
* @param $h (int) Height of a single bar element in pixels.
* @param $color (array) RGB (0-255) foreground color for bar elements (background is transparent).
* @return url or false in case of error.
* @protected
*/
protected function getBarcodePNGUri($code, $type, $w = 3, $h = 3, $color = array(0, 0, 0)) {
$path = $this->getBarcodePNGPath($code, $type, $w, $h, $color);
// Replace backslash (Windows) with forward slashes, to make it compatible with url().
return url(str_replace('\\', '/', $path));
}
/**
* Return a .png file path which create in server
*
$arrcode['code'] code to be printed on text label
*
$arrcode['num_rows'] required number of rows
*
$arrcode['num_cols'] required number of columns
*
$arrcode['bcode'][$r][$c] value of the cell is $r row and $c column (0 = transparent, 1 = black)
* @param $code (string) code to print
* @param $type (string) type of barcode:
DATAMATRIX : Datamatrix (ISO/IEC 16022)
PDF417 : PDF417 (ISO/IEC 15438:2006)
PDF417,a,e,t,s,f,o0,o1,o2,o3,o4,o5,o6 : PDF417 with parameters: a = aspect ratio (width/height); e = error correction level (0-8); t = total number of macro segments; s = macro segment index (0-99998); f = file ID; o0 = File Name (text); o1 = Segment Count (numeric); o2 = Time Stamp (numeric); o3 = Sender (text); o4 = Addressee (text); o5 = File Size (numeric); o6 = Checksum (numeric). NOTES: Parameters t, s and f are required for a Macro Control Block, all other parametrs are optional. To use a comma character ',' on text options, replace it with the character 255: "\xff".
QRCODE : QRcode Low error correction
QRCODE,L : QRcode Low error correction
QRCODE,M : QRcode Medium error correction
QRCODE,Q : QRcode Better error correction
QRCODE,H : QR-CODE Best error correction
RAW: raw mode - comma-separad list of array rows
RAW2: raw mode - array rows are surrounded by square parenthesis.
TEST : Test matrix
* @param $w (int) Width of a single rectangle element in pixels.
* @param $h (int) Height of a single rectangle element in pixels.
* @param $color (array) RGB (0-255) foreground color for bar elements (background is transparent).
* @return path of image whice created
* @protected
*/
protected function getBarcodePNGPath($code, $type, $w = 3, $h = 3, $color = array(0, 0, 0)) {
if (!$this->store_path) {
$this->setStorPath(app('config')->get("barcode.store_path"));
}
//set barcode code and type
$this->setBarcode($code, $type);
// calculate image size
$width = ($this->barcode_array['num_cols'] * $w);
$height = ($this->barcode_array['num_rows'] * $h);
if (function_exists('imagecreate')) {
// GD library
$imagick = false;
$png = imagecreate($width, $height);
$bgcol = imagecolorallocate($png, 255, 255, 255);
imagecolortransparent($png, $bgcol);
$fgcol = imagecolorallocate($png, $color[0], $color[1], $color[2]);
} elseif (extension_loaded('imagick')) {
$imagick = true;
$bgcol = new imagickpixel('rgb(255,255,255');
$fgcol = new imagickpixel('rgb(' . $color[0] . ',' . $color[1] . ',' . $color[2] . ')');
$png = new Imagick();
$png->newImage($width, $height, 'none', 'png');
$bar = new imagickdraw();
$bar->setfillcolor($fgcol);
} else {
return false;
}
// print barcode elements
$y = 0;
// for each row
for ($r = 0; $r < $this->barcode_array['num_rows']; ++$r) {
$x = 0;
// for each column
for ($c = 0; $c < $this->barcode_array['num_cols']; ++$c) {
if ($this->barcode_array['bcode'][$r][$c] == 1) {
// draw a single barcode cell
if ($imagick) {
$bar->rectangle($x, $y, ($x + $w), ($y + $h));
} else {
imagefilledrectangle($png, $x, $y, ($x + $w), ($y + $h), $fgcol);
}
}
$x += $w;
}
$y += $h;
}
$file_name= Str::slug($code.$type);
$save_file = $this->checkfile($this->store_path . $file_name . ".png");
if ($imagick) {
$png->drawimage($bar);
//echo $png;
}
if (ImagePng($png, $save_file)) {
imagedestroy($png);
return str_replace(public_path(), '', $save_file);
} else {
imagedestroy($png);
return $code;
}
}
/**
* Set the barcode.
* @param $code (string) code to print
* @param $type (string) type of barcode:
DATAMATRIX : Datamatrix (ISO/IEC 16022)
PDF417 : PDF417 (ISO/IEC 15438:2006)
PDF417,a,e,t,s,f,o0,o1,o2,o3,o4,o5,o6 : PDF417 with parameters: a = aspect ratio (width/height); e = error correction level (0-8); t = total number of macro segments; s = macro segment index (0-99998); f = file ID; o0 = File Name (text); o1 = Segment Count (numeric); o2 = Time Stamp (numeric); o3 = Sender (text); o4 = Addressee (text); o5 = File Size (numeric); o6 = Checksum (numeric). NOTES: Parameters t, s and f are required for a Macro Control Block, all other parametrs are optional. To use a comma character ',' on text options, replace it with the character 255: "\xff".
QRCODE : QRcode Low error correction
QRCODE,L : QRcode Low error correction
QRCODE,M : QRcode Medium error correction
QRCODE,Q : QRcode Better error correction
QRCODE,H : QR-CODE Best error correction
RAW: raw mode - comma-separad list of array rows
RAW2: raw mode - array rows are surrounded by square parenthesis.
TEST : Test matrix
* @return array
*/
protected function setBarcode($code, $type) {
$mode = explode(',', $type);
$qrtype = strtoupper($mode[0]);
switch ($qrtype) {
case 'DATAMATRIX': { // DATAMATRIX (ISO/IEC 16022)
$barcode = new Datamatrix($code);
$this->barcode_array = $barcode->getBarcodeArray();
$this->barcode_array['code'] = $code;
break;
}
case 'PDF417': { // PDF417 (ISO/IEC 15438:2006)
if (!isset($mode[1]) OR ($mode[1] === '')) {
$aspectratio = 2; // default aspect ratio (width / height)
} else {
$aspectratio = floatval($mode[1]);
}
if (!isset($mode[2]) OR ($mode[2] === '')) {
$ecl = -1; // default error correction level (auto)
} else {
$ecl = intval($mode[2]);
}
// set macro block
$macro = array();
if (isset($mode[3]) AND ($mode[3] !== '') AND isset($mode[4]) AND ($mode[4] !== '') AND isset($mode[5]) AND ($mode[5] !== '')) {
$macro['segment_total'] = intval($mode[3]);
$macro['segment_index'] = intval($mode[4]);
$macro['file_id'] = strtr($mode[5], "\xff", ',');
for ($i = 0; $i < 7; ++$i) {
$o = $i + 6;
if (isset($mode[$o]) AND ($mode[$o] !== '')) {
// add option
$macro['option_' . $i] = strtr($mode[$o], "\xff", ',');
}
}
}
$barcode = new PDF417($code, $ecl, $aspectratio, $macro);
$this->barcode_array = $barcode->getBarcodeArray();
$this->barcode_array['code'] = $code;
break;
}
case 'QRCODE': { // QR-CODE
if (!isset($mode[1]) OR (!in_array($mode[1], array('L', 'M', 'Q', 'H')))) {
$mode[1] = 'L'; // Ddefault: Low error correction
}
$barcode = new QRcode($code, strtoupper($mode[1]));
$this->barcode_array = $barcode->getBarcodeArray();
$this->barcode_array['code'] = $code;
break;
}
default: {
$this->barcode_array = false;
}
}
}
/**
*
* @param type $path
* @return type
*/
protected function checkfile($path) {
if (file_exists($path)) {
unlink($path);
}
return $path;
}
protected function setStorPath($path) {
$this->store_path = $path;
return $this;
}
/**
* Handle dynamic method calls.
*
* @param string $method
* @param array $parameters
* @return mixed
*/
public function __call($method, $parameters)
{
return $this->$method(...$parameters);
}
/**
* Handle dynamic static method calls.
*
* @param string $method
* @param array $parameters
* @return mixed
*/
public static function __callStatic($method, $parameters)
{
return (new static)->$method(...$parameters);
}
}