Browse Source

Change the QR code generation library

pull/14/head
Miraty 1 month ago
parent
commit
0434534971
  1. 1
      CHANGELOG.md
  2. 4
      README.md
  3. 2
      README_fr.md
  4. 31
      barcode-generator/Generator/CINArgumentException.php
  5. 439
      barcode-generator/Generator/CINBarcode.php
  6. 262
      barcode-generator/Generator/CINBarcode1D.php
  7. 157
      barcode-generator/Generator/CINColor.php
  8. 25
      barcode-generator/Generator/CINDrawException.php
  9. 250
      barcode-generator/Generator/CINDrawing.php
  10. 26
      barcode-generator/Generator/CINFont.php
  11. 211
      barcode-generator/Generator/CINFontFile.php
  12. 155
      barcode-generator/Generator/CINFontPhp.php
  13. 322
      barcode-generator/Generator/CINLabel.php
  14. 29
      barcode-generator/Generator/CINParseException.php
  15. 124
      barcode-generator/Generator/CINcodabar.php
  16. 187
      barcode-generator/Generator/CINcode11.php
  17. 912
      barcode-generator/Generator/CINcode128.php
  18. 195
      barcode-generator/Generator/CINcode39.php
  19. 210
      barcode-generator/Generator/CINcode39extended.php
  20. 303
      barcode-generator/Generator/CINcode93.php
  21. 324
      barcode-generator/Generator/CINean13.php
  22. 246
      barcode-generator/Generator/CINean8.php
  23. 690
      barcode-generator/Generator/CINgs1128.php
  24. 205
      barcode-generator/Generator/CINi25.php
  25. 650
      barcode-generator/Generator/CINintelligentmail.php
  26. 167
      barcode-generator/Generator/CINisbn.php
  27. 186
      barcode-generator/Generator/CINmsi.php
  28. 90
      barcode-generator/Generator/CINothercode.php
  29. 140
      barcode-generator/Generator/CINpostnet.php
  30. 172
      barcode-generator/Generator/CINs25.php
  31. 148
      barcode-generator/Generator/CINupca.php
  32. 338
      barcode-generator/Generator/CINupce.php
  33. 141
      barcode-generator/Generator/CINupcext2.php
  34. 204
      barcode-generator/Generator/CINupcext5.php
  35. 41
      barcode-generator/Generator/Drawer/CINDraw.php
  36. 104
      barcode-generator/Generator/Drawer/CINDrawJPG.php
  37. 205
      barcode-generator/Generator/Drawer/CINDrawPNG.php
  38. 197
      barcode-generator/Generator/JoinDraw.php
  39. 22
      barcode-generator/LICENSE
  40. 136
      barcode-generator/README.md
  41. BIN
      barcode-generator/Resources/data/qrv10_0.dat
  42. BIN
      barcode-generator/Resources/data/qrv10_1.dat
  43. BIN
      barcode-generator/Resources/data/qrv10_2.dat
  44. BIN
      barcode-generator/Resources/data/qrv10_3.dat
  45. BIN
      barcode-generator/Resources/data/qrv11_0.dat
  46. BIN
      barcode-generator/Resources/data/qrv11_1.dat
  47. BIN
      barcode-generator/Resources/data/qrv11_2.dat
  48. BIN
      barcode-generator/Resources/data/qrv11_3.dat
  49. BIN
      barcode-generator/Resources/data/qrv12_0.dat
  50. BIN
      barcode-generator/Resources/data/qrv12_1.dat
  51. BIN
      barcode-generator/Resources/data/qrv12_2.dat
  52. BIN
      barcode-generator/Resources/data/qrv12_3.dat
  53. BIN
      barcode-generator/Resources/data/qrv13_0.dat
  54. BIN
      barcode-generator/Resources/data/qrv13_1.dat
  55. BIN
      barcode-generator/Resources/data/qrv13_2.dat
  56. BIN
      barcode-generator/Resources/data/qrv13_3.dat
  57. BIN
      barcode-generator/Resources/data/qrv14_0.dat
  58. BIN
      barcode-generator/Resources/data/qrv14_1.dat
  59. BIN
      barcode-generator/Resources/data/qrv14_2.dat
  60. BIN
      barcode-generator/Resources/data/qrv14_3.dat
  61. BIN
      barcode-generator/Resources/data/qrv15_0.dat
  62. BIN
      barcode-generator/Resources/data/qrv15_1.dat
  63. BIN
      barcode-generator/Resources/data/qrv15_2.dat
  64. BIN
      barcode-generator/Resources/data/qrv15_3.dat
  65. BIN
      barcode-generator/Resources/data/qrv16_0.dat
  66. BIN
      barcode-generator/Resources/data/qrv16_1.dat
  67. BIN
      barcode-generator/Resources/data/qrv16_2.dat
  68. BIN
      barcode-generator/Resources/data/qrv16_3.dat
  69. BIN
      barcode-generator/Resources/data/qrv17_0.dat
  70. BIN
      barcode-generator/Resources/data/qrv17_1.dat
  71. BIN
      barcode-generator/Resources/data/qrv17_2.dat
  72. BIN
      barcode-generator/Resources/data/qrv17_3.dat
  73. BIN
      barcode-generator/Resources/data/qrv18_0.dat
  74. BIN
      barcode-generator/Resources/data/qrv18_1.dat
  75. BIN
      barcode-generator/Resources/data/qrv18_2.dat
  76. BIN
      barcode-generator/Resources/data/qrv18_3.dat
  77. BIN
      barcode-generator/Resources/data/qrv19_0.dat
  78. BIN
      barcode-generator/Resources/data/qrv19_1.dat
  79. BIN
      barcode-generator/Resources/data/qrv19_2.dat
  80. BIN
      barcode-generator/Resources/data/qrv19_3.dat
  81. BIN
      barcode-generator/Resources/data/qrv1_0.dat
  82. BIN
      barcode-generator/Resources/data/qrv1_1.dat
  83. BIN
      barcode-generator/Resources/data/qrv1_2.dat
  84. BIN
      barcode-generator/Resources/data/qrv1_3.dat
  85. BIN
      barcode-generator/Resources/data/qrv20_0.dat
  86. BIN
      barcode-generator/Resources/data/qrv20_1.dat
  87. BIN
      barcode-generator/Resources/data/qrv20_2.dat
  88. BIN
      barcode-generator/Resources/data/qrv20_3.dat
  89. BIN
      barcode-generator/Resources/data/qrv21_0.dat
  90. BIN
      barcode-generator/Resources/data/qrv21_1.dat
  91. BIN
      barcode-generator/Resources/data/qrv21_2.dat
  92. BIN
      barcode-generator/Resources/data/qrv21_3.dat
  93. BIN
      barcode-generator/Resources/data/qrv22_0.dat
  94. 209
      barcode-generator/Resources/data/qrv22_1.dat
  95. BIN
      barcode-generator/Resources/data/qrv22_2.dat
  96. BIN
      barcode-generator/Resources/data/qrv22_3.dat
  97. BIN
      barcode-generator/Resources/data/qrv23_0.dat
  98. 217
      barcode-generator/Resources/data/qrv23_1.dat
  99. BIN
      barcode-generator/Resources/data/qrv23_2.dat
  100. BIN
      barcode-generator/Resources/data/qrv23_3.dat

1
CHANGELOG.md

@ -13,6 +13,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
### Changed
* Replace GET by POST for QR codes generation (privacy enhancement)
* Change the QR code generation library
* Use less.php instead of lesserphp
* Use the prefers-color-scheme CSS feature to let the client choose their prefered theme (dark/light)

4
README.md

@ -85,8 +85,8 @@ If you want to contact me, for instance to report a bug or ask me a question abo
This source code includes:
* [phpqrcode](https://github.com/t0k4rt/phpqrcode) to generate QR codes
* [less.php](https://github.com/wikimedia/less.php) to compile [Less](http://lesscss.org)
* [CodeItNow Barcode & QrCode Generator](https://github.com/codeitnowin/barcode-generator) to generate QR codes
* [Less.php](https://github.com/wikimedia/less.php) to compile [Less](http://lesscss.org)
## License

2
README_fr.md

@ -85,7 +85,7 @@ Si vous voulez me contacter, par exemple pour signaler un bug ou me poser une qu
Ce code source inclus :
* [phpqrcode](https://github.com/t0k4rt/phpqrcode) pour générer les codes QR
* [CodeItNow Barcode & QrCode Generator](https://github.com/codeitnowin/barcode-generator) pour générer les codes QR
* [less.php](https://github.com/wikimedia/less.php) pour compiler le [Less](http://lesscss.org)
## Licence

31
barcode-generator/Generator/CINArgumentException.php

@ -0,0 +1,31 @@
<?php
/**
*--------------------------------------------------------------------
*
* Argument Exception
*
*--------------------------------------------------------------------
*
* @author Akhtar Khan <er.akhtarkhan@gmail.com>
* @link http://www.codeitnow.in
* @package https://github.com/codeitnowin/barcode-generator
*/
namespace CodeItNow\BarcodeBundle\Generator;
use Exception;
class CINArgumentException extends Exception {
protected $param;
/**
* Constructor with specific message for a parameter.
*
* @param string $message
* @param string $param
*/
public function __construct($message, $param) {
$this->param = $param;
parent::__construct($message, 20000);
}
}
?>

439
barcode-generator/Generator/CINBarcode.php

@ -0,0 +1,439 @@
<?php
/**
*--------------------------------------------------------------------
*
* Base class for Barcode 1D and 2D
*
*--------------------------------------------------------------------
* @author Akhtar Khan <er.akhtarkhan@gmail.com>
* @link http://www.codeitnow.in
* @package https://github.com/codeitnowin/barcode-generator
*/
namespace CodeItNow\BarcodeBundle\Generator;
use CodeItNow\BarcodeBundle\Generator\CINColor;
use CodeItNow\BarcodeBundle\Generator\CINLabel;
use CodeItNow\BarcodeBundle\Generator\CINArgumentException;
use CodeItNow\BarcodeBundle\Generator\CINDrawException;
abstract class CINBarcode {
const COLOR_BG = 0;
const COLOR_FG = 1;
protected $colorFg, $colorBg; // Color Foreground, Barckground
protected $scale; // Scale of the graphic, default: 1
protected $offsetX, $offsetY; // Position where to start the drawing
protected $labels = array(); // Array of CINLabel
protected $pushLabel = array(0, 0); // Push for the label, left and top
/**
* Constructor.
*/
protected function __construct() {
$this->setOffsetX(0);
$this->setOffsetY(0);
$this->setForegroundColor(0x000000);
$this->setBackgroundColor(0xffffff);
$this->setScale(1);
}
/**
* Parses the text before displaying it.
*
* @param mixed $text
*/
public function parse($text) {
}
/**
* Gets the foreground color of the barcode.
*
* @return CINColor
*/
public function getForegroundColor() {
return $this->colorFg;
}
/**
* Sets the foreground color of the barcode. It could be a CINColor
* value or simply a language code (white, black, yellow...) or hex value.
*
* @param mixed $code
*/
public function setForegroundColor($code) {
if ($code instanceof CINColor) {
$this->colorFg = $code;
} else {
$this->colorFg = new CINColor($code);
}
}
/**
* Gets the background color of the barcode.
*
* @return CINColor
*/
public function getBackgroundColor() {
return $this->colorBg;
}
/**
* Sets the background color of the barcode. It could be a CINColor
* value or simply a language code (white, black, yellow...) or hex value.
*
* @param mixed $code
*/
public function setBackgroundColor($code) {
if ($code instanceof CINColor) {
$this->colorBg = $code;
} else {
$this->colorBg = new CINColor($code);
}
foreach ($this->labels as $label) {
$label->setBackgroundColor($this->colorBg);
}
}
/**
* Sets the color.
*
* @param mixed $fg
* @param mixed $bg
*/
public function setColor($fg, $bg) {
$this->setForegroundColor($fg);
$this->setBackgroundColor($bg);
}
/**
* Gets the scale of the barcode.
*
* @return int
*/
public function getScale() {
return $this->scale;
}
/**
* Sets the scale of the barcode in pixel.
* If the scale is lower than 1, an exception is raised.
*
* @param int $scale
*/
public function setScale($scale) {
$scale = intval($scale);
if ($scale <= 0) {
throw new CINArgumentException('The scale must be larger than 0.', 'scale');
}
$this->scale = $scale;
}
/**
* Abstract method that draws the barcode on the resource.
*
* @param resource $im
*/
public abstract function draw($im);
/**
* Returns the maximal size of a barcode.
* [0]->width
* [1]->height
*
* @param int $w
* @param int $h
* @return int[]
*/
public function getDimension($w, $h) {
$labels = $this->getBiggestLabels(false);
$pixelsAround = array(0, 0, 0, 0); // TRBL
if (isset($labels[CINLabel::POSITION_TOP])) {
$dimension = $labels[CINLabel::POSITION_TOP]->getDimension();
$pixelsAround[0] += $dimension[1];
}
if (isset($labels[CINLabel::POSITION_RIGHT])) {
$dimension = $labels[CINLabel::POSITION_RIGHT]->getDimension();
$pixelsAround[1] += $dimension[0];
}
if (isset($labels[CINLabel::POSITION_BOTTOM])) {
$dimension = $labels[CINLabel::POSITION_BOTTOM]->getDimension();
$pixelsAround[2] += $dimension[1];
}
if (isset($labels[CINLabel::POSITION_LEFT])) {
$dimension = $labels[CINLabel::POSITION_LEFT]->getDimension();
$pixelsAround[3] += $dimension[0];
}
$finalW = ($w + $this->offsetX) * $this->scale;
$finalH = ($h + $this->offsetY) * $this->scale;
// This section will check if a top/bottom label is too big for its width and left/right too big for its height
$reversedLabels = $this->getBiggestLabels(true);
foreach ($reversedLabels as $label) {
$dimension = $label->getDimension();
$alignment = $label->getAlignment();
if ($label->getPosition() === CINLabel::POSITION_LEFT || $label->getPosition() === CINLabel::POSITION_RIGHT) {
if ($alignment === CINLabel::ALIGN_TOP) {
$pixelsAround[2] = max($pixelsAround[2], $dimension[1] - $finalH);
} elseif ($alignment === CINLabel::ALIGN_CENTER) {
$temp = ceil(($dimension[1] - $finalH) / 2);
$pixelsAround[0] = max($pixelsAround[0], $temp);
$pixelsAround[2] = max($pixelsAround[2], $temp);
} elseif ($alignment === CINLabel::ALIGN_BOTTOM) {
$pixelsAround[0] = max($pixelsAround[0], $dimension[1] - $finalH);
}
} else {
if ($alignment === CINLabel::ALIGN_LEFT) {
$pixelsAround[1] = max($pixelsAround[1], $dimension[0] - $finalW);
} elseif ($alignment === CINLabel::ALIGN_CENTER) {
$temp = ceil(($dimension[0] - $finalW) / 2);
$pixelsAround[1] = max($pixelsAround[1], $temp);
$pixelsAround[3] = max($pixelsAround[3], $temp);
} elseif ($alignment === CINLabel::ALIGN_RIGHT) {
$pixelsAround[3] = max($pixelsAround[3], $dimension[0] - $finalW);
}
}
}
$this->pushLabel[0] = $pixelsAround[3];
$this->pushLabel[1] = $pixelsAround[0];
$finalW = ($w + $this->offsetX) * $this->scale + $pixelsAround[1] + $pixelsAround[3];
$finalH = ($h + $this->offsetY) * $this->scale + $pixelsAround[0] + $pixelsAround[2];
return array($finalW, $finalH);
}
/**
* Gets the X offset.
*
* @return int
*/
public function getOffsetX() {
return $this->offsetX;
}
/**
* Sets the X offset.
*
* @param int $offsetX
*/
public function setOffsetX($offsetX) {
$offsetX = intval($offsetX);
if ($offsetX < 0) {
throw new CINArgumentException('The offset X must be 0 or larger.', 'offsetX');
}
$this->offsetX = $offsetX;
}
/**
* Gets the Y offset.
*
* @return int
*/
public function getOffsetY() {
return $this->offsetY;
}
/**
* Sets the Y offset.
*
* @param int $offsetY
*/
public function setOffsetY($offsetY) {
$offsetY = intval($offsetY);
if ($offsetY < 0) {
throw new CINArgumentException('The offset Y must be 0 or larger.', 'offsetY');
}
$this->offsetY = $offsetY;
}
/**
* Adds the label to the drawing.
*
* @param CINLabel $label
*/
public function addLabel(CINLabel $label) {
$label->setBackgroundColor($this->colorBg);
$label->setForegroundColor($this->colorFg);
$this->labels[] = $label;
}
/**
* Removes the label from the drawing.
*
* @param CINLabel $label
*/
public function removeLabel(CINLabel $label) {
$remove = -1;
$c = count($this->labels);
for ($i = 0; $i < $c; $i++) {
if ($this->labels[$i] === $label) {
$remove = $i;
break;
}
}
if ($remove > -1) {
array_splice($this->labels, $remove, 1);
}
}
/**
* Clears the labels.
*/
public function clearLabels() {
$this->labels = array();
}
/**
* Draws the text.
* The coordinate passed are the positions of the barcode.
* $x1 and $y1 represent the top left corner.
* $x2 and $y2 represent the bottom right corner.
*
* @param resource $im
* @param int $x1
* @param int $y1
* @param int $x2
* @param int $y2
*/
protected function drawText($im, $x1, $y1, $x2, $y2) {
foreach ($this->labels as $label) {
$label->draw($im,
($x1 + $this->offsetX) * $this->scale + $this->pushLabel[0],
($y1 + $this->offsetY) * $this->scale + $this->pushLabel[1],
($x2 + $this->offsetX) * $this->scale + $this->pushLabel[0],
($y2 + $this->offsetY) * $this->scale + $this->pushLabel[1]);
}
}
/**
* Draws 1 pixel on the resource at a specific position with a determined color.
*
* @param resource $im
* @param int $x
* @param int $y
* @param int $color
*/
protected function drawPixel($im, $x, $y, $color = self::COLOR_FG) {
$xR = ($x + $this->offsetX) * $this->scale + $this->pushLabel[0];
$yR = ($y + $this->offsetY) * $this->scale + $this->pushLabel[1];
// We always draw a rectangle
imagefilledrectangle($im,
$xR,
$yR,
$xR + $this->scale - 1,
$yR + $this->scale - 1,
$this->getColor($im, $color));
}
/**
* Draws an empty rectangle on the resource at a specific position with a determined color.
*
* @param resource $im
* @param int $x1
* @param int $y1
* @param int $x2
* @param int $y2
* @param int $color
*/
protected function drawRectangle($im, $x1, $y1, $x2, $y2, $color = self::COLOR_FG) {
if ($this->scale === 1) {
imagefilledrectangle($im,
($x1 + $this->offsetX) + $this->pushLabel[0],
($y1 + $this->offsetY) + $this->pushLabel[1],
($x2 + $this->offsetX) + $this->pushLabel[0],
($y2 + $this->offsetY) + $this->pushLabel[1],
$this->getColor($im, $color));
} else {
imagefilledrectangle($im, ($x1 + $this->offsetX) * $this->scale + $this->pushLabel[0], ($y1 + $this->offsetY) * $this->scale + $this->pushLabel[1], ($x2 + $this->offsetX) * $this->scale + $this->pushLabel[0] + $this->scale - 1, ($y1 + $this->offsetY) * $this->scale + $this->pushLabel[1] + $this->scale - 1, $this->getColor($im, $color));
imagefilledrectangle($im, ($x1 + $this->offsetX) * $this->scale + $this->pushLabel[0], ($y1 + $this->offsetY) * $this->scale + $this->pushLabel[1], ($x1 + $this->offsetX) * $this->scale + $this->pushLabel[0] + $this->scale - 1, ($y2 + $this->offsetY) * $this->scale + $this->pushLabel[1] + $this->scale - 1, $this->getColor($im, $color));
imagefilledrectangle($im, ($x2 + $this->offsetX) * $this->scale + $this->pushLabel[0], ($y1 + $this->offsetY) * $this->scale + $this->pushLabel[1], ($x2 + $this->offsetX) * $this->scale + $this->pushLabel[0] + $this->scale - 1, ($y2 + $this->offsetY) * $this->scale + $this->pushLabel[1] + $this->scale - 1, $this->getColor($im, $color));
imagefilledrectangle($im, ($x1 + $this->offsetX) * $this->scale + $this->pushLabel[0], ($y2 + $this->offsetY) * $this->scale + $this->pushLabel[1], ($x2 + $this->offsetX) * $this->scale + $this->pushLabel[0] + $this->scale - 1, ($y2 + $this->offsetY) * $this->scale + $this->pushLabel[1] + $this->scale - 1, $this->getColor($im, $color));
}
}
/**
* Draws a filled rectangle on the resource at a specific position with a determined color.
*
* @param resource $im
* @param int $x1
* @param int $y1
* @param int $x2
* @param int $y2
* @param int $color
*/
protected function drawFilledRectangle($im, $x1, $y1, $x2, $y2, $color = self::COLOR_FG) {
if ($x1 > $x2) { // Swap
$x1 ^= $x2 ^= $x1 ^= $x2;
}
if ($y1 > $y2) { // Swap
$y1 ^= $y2 ^= $y1 ^= $y2;
}
imagefilledrectangle($im,
($x1 + $this->offsetX) * $this->scale + $this->pushLabel[0],
($y1 + $this->offsetY) * $this->scale + $this->pushLabel[1],
($x2 + $this->offsetX) * $this->scale + $this->pushLabel[0] + $this->scale - 1,
($y2 + $this->offsetY) * $this->scale + $this->pushLabel[1] + $this->scale - 1,
$this->getColor($im, $color));
}
/**
* Allocates the color based on the integer.
*
* @param resource $im
* @param int $color
* @return resource
*/
protected function getColor($im, $color) {
if ($color === self::COLOR_BG) {
return $this->colorBg->allocate($im);
} else {
return $this->colorFg->allocate($im);
}
}
/**
* Returning the biggest label widths for LEFT/RIGHT and heights for TOP/BOTTOM.
*
* @param bool $reversed
* @return CINLabel[]
*/
private function getBiggestLabels($reversed = false) {
$searchLR = $reversed ? 1 : 0;
$searchTB = $reversed ? 0 : 1;
$labels = array();
foreach ($this->labels as $label) {
$position = $label->getPosition();
if (isset($labels[$position])) {
$savedDimension = $labels[$position]->getDimension();
$dimension = $label->getDimension();
if ($position === CINLabel::POSITION_LEFT || $position === CINLabel::POSITION_RIGHT) {
if ($dimension[$searchLR] > $savedDimension[$searchLR]) {
$labels[$position] = $label;
}
} else {
if ($dimension[$searchTB] > $savedDimension[$searchTB]) {
$labels[$position] = $label;
}
}
} else {
$labels[$position] = $label;
}
}
return $labels;
}
}
?>

262
barcode-generator/Generator/CINBarcode1D.php

@ -0,0 +1,262 @@
<?php
/**
*--------------------------------------------------------------------
*
* Holds all type of barcodes for 1D generation
*
*--------------------------------------------------------------------
* @author Akhtar Khan <er.akhtarkhan@gmail.com>
* @link http://www.codeitnow.in
* @package https://github.com/codeitnowin/barcode-generator
*/
namespace CodeItNow\BarcodeBundle\Generator;
use CodeItNow\BarcodeBundle\Generator\CINArgumentException;
use CodeItNow\BarcodeBundle\Generator\CINBarcode1D;
use CodeItNow\BarcodeBundle\Generator\CINFontPhp;
use CodeItNow\BarcodeBundle\Generator\CINFontFile;
use CodeItNow\BarcodeBundle\Generator\CINLabel;
abstract class CINBarcode1D extends CINBarcode {
const SIZE_SPACING_FONT = 5;
const AUTO_LABEL = '##!!AUTO_LABEL!!##';
protected $thickness; // int
protected $keys, $code; // string[]
protected $positionX; // int
protected $font; // CINFont
protected $text; // string
protected $checksumValue; // int or int[]
protected $displayChecksum; // bool
protected $label; // string
protected $defaultLabel; // CINLabel
/**
* Constructor.
*/
protected function __construct() {
parent::__construct();
$this->setThickness(30);
$this->defaultLabel = new CINLabel();
$this->defaultLabel->setPosition(CINLabel::POSITION_BOTTOM);
$this->setLabel(self::AUTO_LABEL);
$this->setFont(new CINFontPhp(5));
$this->text = '';
$this->checksumValue = false;
$this->positionX = 0;
}
/**
* Gets the thickness.
*
* @return int
*/
public function getThickness() {
return $this->thickness;
}
/**
* Sets the thickness.
*
* @param int $thickness
*/
public function setThickness($thickness) {
$thickness = intval($thickness);
if ($thickness <= 0) {
throw new CINArgumentException('The thickness must be larger than 0.', 'thickness');
}
$this->thickness = $thickness;
}
/**
* Gets the label.
* If the label was set to CINBarcode1D::AUTO_LABEL, the label will display the value from the text parsed.
*
* @return string
*/
public function getLabel() {
$label = $this->label;
if ($this->label === self::AUTO_LABEL) {
$label = $this->text;
if ($this->displayChecksum === true && ($checksum = $this->processChecksum()) !== false) {
$label .= $checksum;
}
}
return $label;
}
/**
* Sets the label.
* You can use CINBarcode::AUTO_LABEL to have the label automatically written based on the parsed text.
*
* @param string $label
*/
public function setLabel($label) {
$this->label = $label;
}
/**
* Gets the font.
*
* @return CINFont
*/
public function getFont() {
return $this->font;
}
/**
* Sets the font.
*
* @param mixed $font CINFont or int
*/
public function setFont($font) {
if (is_int($font)) {
if ($font === 0) {
$font = null;
} else {
$font = new CINFontPhp($font);
}
}
$this->font = $font;
}
/**
* Parses the text before displaying it.
*
* @param mixed $text
*/
public function parse($text) {
$this->text = $text;
$this->checksumValue = false; // Reset checksumValue
$this->validate();
parent::parse($text);
$this->addDefaultLabel();
}
/**
* Gets the checksum of a Barcode.
* If no checksum is available, return FALSE.
*
* @return string
*/
public function getChecksum() {
return $this->processChecksum();
}
/**
* Sets if the checksum is displayed with the label or not.
* The checksum must be activated in some case to make this variable effective.
*
* @param boolean $displayChecksum
*/
public function setDisplayChecksum($displayChecksum) {
$this->displayChecksum = (bool)$displayChecksum;
}
/**
* Adds the default label.
*/
protected function addDefaultLabel() {
$label = $this->getLabel();
$font = $this->font;
if ($label !== null && $label !== '' && $font !== null && $this->defaultLabel !== null) {
$this->defaultLabel->setText($label);
$this->defaultLabel->setFont($font);
$this->addLabel($this->defaultLabel);
}
}
/**
* Validates the input
*/
protected function validate() {
// No validation in the abstract class.
}
/**
* Returns the index in $keys (useful for checksum).
*
* @param mixed $var
* @return mixed
*/
protected function findIndex($var) {
return array_search($var, $this->keys);
}
/**
* Returns the code of the char (useful for drawing bars).
*
* @param mixed $var
* @return string
*/
protected function findCode($var) {
return $this->code[$this->findIndex($var)];
}
/**
* Draws all chars thanks to $code. If $startBar is true, the line begins by a space.
* If $startBar is false, the line begins by a bar.
*
* @param resource $im
* @param string $code
* @param boolean $startBar
*/
protected function drawChar($im, $code, $startBar = true) {
$colors = array(CINBarcode::COLOR_FG, CINBarcode::COLOR_BG);
$currentColor = $startBar ? 0 : 1;
$c = strlen($code);
for ($i = 0; $i < $c; $i++) {
for ($j = 0; $j < intval($code[$i]) + 1; $j++) {
$this->drawSingleBar($im, $colors[$currentColor]);
$this->nextX();
}
$currentColor = ($currentColor + 1) % 2;
}
}
/**
* Draws a Bar of $color depending of the resolution.
*
* @param resource $img
* @param int $color
*/
protected function drawSingleBar($im, $color) {
$this->drawFilledRectangle($im, $this->positionX, 0, $this->positionX, $this->thickness - 1, $color);
}
/**
* Moving the pointer right to write a bar.
*/
protected function nextX() {
$this->positionX++;
}
/**
* Method that saves FALSE into the checksumValue. This means no checksum
* but this method should be overriden when needed.
*/
protected function calculateChecksum() {
$this->checksumValue = false;
}
/**
* Returns FALSE because there is no checksum. This method should be
* overriden to return correctly the checksum in string with checksumValue.
*
* @return string
*/
protected function processChecksum() {
return false;
}
}
?>

157
barcode-generator/Generator/CINColor.php

@ -0,0 +1,157 @@
<?php
/**
*--------------------------------------------------------------------
*
* Holds Color in RGB Format.
*
*--------------------------------------------------------------------
* @author Akhtar Khan <er.akhtarkhan@gmail.com>
* @link http://www.codeitnow.in
* @package https://github.com/codeitnowin/barcode-generator
*/
namespace CodeItNow\BarcodeBundle\Generator;
class CINColor {
protected $r, $g, $b; // int Hexadecimal Value
protected $transparent;
/**
* Save RGB value into the classes.
*
* There are 4 way to associate color with this classes :
* 1. Gives 3 parameters int (R, G, B)
* 2. Gives 1 parameter string hex value (#ff0000) (preceding with #)
* 3. Gives 1 parameter int hex value (0xff0000)
* 4. Gives 1 parameter string color code (white, black, orange...)
*
* @param mixed ...
*/
public function __construct() {
$args = func_get_args();
$c = count($args);
if ($c === 3) {
$this->r = intval($args[0]);
$this->g = intval($args[1]);
$this->b = intval($args[2]);
} elseif ($c === 1) {
if (is_string($args[0]) && strlen($args[0]) === 7 && $args[0][0] === '#') { // Hex Value in String
$this->r = intval(substr($args[0], 1, 2), 16);
$this->g = intval(substr($args[0], 3, 2), 16);
$this->b = intval(substr($args[0], 5, 2), 16);
} else {
if (is_string($args[0])) {
$args[0] = self::getColor($args[0]);
}
$args[0] = intval($args[0]);
$this->r = ($args[0] & 0xff0000) >> 16;
$this->g = ($args[0] & 0x00ff00) >> 8;
$this->b = ($args[0] & 0x0000ff);
}
} else {
$this->r = $this->g = $this->b = 0;
}
}
/**
* Sets the color transparent.
*
* @param bool $transparent
*/
public function setTransparent($transparent) {
$this->transparent = $transparent;
}
/**
* Returns Red Color.
*
* @return int
*/
public function r() {
return $this->r;
}
/**
* Returns Green Color.
*
* @return int
*/
public function g() {
return $this->g;
}
/**
* Returns Blue Color.
*
* @return int
*/
public function b() {
return $this->b;
}
/**
* Returns the int value for PHP color.
*
* @param resource $im
* @return int
*/
public function allocate(&$im) {
$allocated = imagecolorallocate($im, $this->r, $this->g, $this->b);
if ($this->transparent) {
return imagecolortransparent($im, $allocated);
} else {
return $allocated;
}
}
/**
* Returns class of CINColor depending of the string color.
*
* If the color doens't exist, it takes the default one.
*
* @param string $code
* @param string $default
*/
public static function getColor($code, $default = 'white') {
switch(strtolower($code)) {
case '':
case 'white':
return 0xffffff;
case 'black':
return 0x000000;
case 'maroon':
return 0x800000;
case 'red':
return 0xff0000;
case 'orange':
return 0xffa500;
case 'yellow':
return 0xffff00;
case 'olive':
return 0x808000;
case 'purple':
return 0x800080;
case 'fuchsia':
return 0xff00ff;
case 'lime':
return 0x00ff00;
case 'green':
return 0x008000;
case 'navy':
return 0x000080;
case 'blue':
return 0x0000ff;
case 'aqua':
return 0x00ffff;
case 'teal':
return 0x008080;
case 'silver':
return 0xc0c0c0;
case 'gray':
return 0x808080;
default:
return self::getColor($default, 'white');
}
}
}
?>

25
barcode-generator/Generator/CINDrawException.php

@ -0,0 +1,25 @@
<?php
/**
*--------------------------------------------------------------------
*
* Draw Exception
*
*--------------------------------------------------------------------
* @author Akhtar Khan <er.akhtarkhan@gmail.com>
* @link http://www.codeitnow.in
* @package https://github.com/codeitnowin/barcode-generator
*/
namespace CodeItNow\BarcodeBundle\Generator;
use Exception;
class CINDrawException extends Exception {
/**
* Constructor with specific message.
*
* @param string $message
*/
public function __construct($message) {
parent::__construct($message, 30000);
}
}
?>

250
barcode-generator/Generator/CINDrawing.php

@ -0,0 +1,250 @@
<?php
/**
*--------------------------------------------------------------------
*
* Holds the drawing $im
* You can use get_im() to add other kind of form not held into these classes.
*
*--------------------------------------------------------------------
* @author Akhtar Khan <er.akhtarkhan@gmail.com>
* @link http://www.codeitnow.in
* @package https://github.com/codeitnowin/barcode-generator
*/
namespace CodeItNow\BarcodeBundle\Generator;
use CodeItNow\BarcodeBundle\Generator\CINBarcode;
use CodeItNow\BarcodeBundle\Generator\CINColor;
use CodeItNow\BarcodeBundle\Generator\CINDrawException;
use CodeItNow\BarcodeBundle\Generator\Drawer\CINDrawJPG;
use CodeItNow\BarcodeBundle\Generator\Drawer\CINDrawPNG;
class CINDrawing {
const IMG_FORMAT_PNG = 1;
const IMG_FORMAT_JPEG = 2;
const IMG_FORMAT_GIF = 3;
const IMG_FORMAT_WBMP = 4;
private $w, $h; // int
private $color; // CINColor
private $filename; // char *
private $im; // {object}
private $barcode; // CINBarcode
private $dpi; // float
private $rotateDegree; // float
/**
* Constructor.
*
* @param int $w
* @param int $h
* @param string filename
* @param CINColor $color
*/
public function __construct($filename, CINColor $color) {
$this->im = null;
$this->setFilename($filename);
$this->color = $color;
$this->dpi = null;
$this->rotateDegree = 0.0;
}
/**
* Destructor.
*/
public function __destruct() {
$this->destroy();
}
/**
* Gets the filename.
*
* @return string
*/
public function getFilename() {
return $this->filename;
}
/**
* Sets the filename.
*
* @param string $filaneme
*/
public function setFilename($filename) {
$this->filename = $filename;
}
/**
* @return resource.
*/
public function get_im() {
return $this->im;
}
/**
* Sets the image.
*
* @param resource $im
*/
public function set_im($im) {
$this->im = $im;
}
/**
* Gets barcode for drawing.
*
* @return CINBarcode
*/
public function getBarcode() {
return $this->barcode;
}
/**
* Sets barcode for drawing.
*
* @param CINBarcode $barcode
*/
public function setBarcode(CINBarcode $barcode) {
$this->barcode = $barcode;
}
/**
* Gets the DPI for supported filetype.
*
* @return float
*/
public function getDPI() {
return $this->dpi;
}
/**
* Sets the DPI for supported filetype.
*
* @param float $dpi
*/
public function setDPI($dpi) {
$this->dpi = $dpi;
}
/**
* Gets the rotation angle in degree clockwise.
*
* @return float
*/
public function getRotationAngle() {
return $this->rotateDegree;
}
/**
* Sets the rotation angle in degree clockwise.
*
* @param float $degree
*/
public function setRotationAngle($degree) {
$this->rotateDegree = (float)$degree;
}
/**
* Draws the barcode on the image $im.
*/
public function draw() {
$size = $this->barcode->getDimension(0, 0);
$this->w = max(1, $size[0]);
$this->h = max(1, $size[1]);
$this->init();
$this->barcode->draw($this->im);
}
/**
* Saves $im into the file (many format available).
*
* @param int $image_style
* @param int $quality
*/
public function finish($image_style = self::IMG_FORMAT_PNG, $quality = 100) {
$drawer = null;
$im = $this->im;
if ($this->rotateDegree > 0.0) {
if (function_exists('imagerotate')) {
$im = imagerotate($this->im, 360 - $this->rotateDegree, $this->color->allocate($this->im));
} else {
throw new CINDrawException('The method imagerotate doesn\'t exist on your server. Do not use any rotation.');
}
}
if ($image_style === self::IMG_FORMAT_PNG) {
$drawer = new CINDrawPNG($im);
$drawer->setFilename($this->filename);
$drawer->setDPI($this->dpi);
} elseif ($image_style === self::IMG_FORMAT_JPEG) {
$drawer = new CINDrawJPG($im);
$drawer->setFilename($this->filename);
$drawer->setDPI($this->dpi);
$drawer->setQuality($quality);
} elseif ($image_style === self::IMG_FORMAT_GIF) {
// Some PHP versions have a bug if passing 2nd argument as null.
if ($this->filename === null || $this->filename === '') {
imagegif($im);
} else {
imagegif($im, $this->filename);
}
} elseif ($image_style === self::IMG_FORMAT_WBMP) {
imagewbmp($im, $this->filename);
}
if ($drawer !== null) {
$drawer->draw();
}
}
/**
* Writes the Error on the picture.
*
* @param Exception $exception
*/
public function drawException($exception) {
$this->w = 1;
$this->h = 1;
$this->init();
// Is the image big enough?
$w = imagesx($this->im);
$h = imagesy($this->im);
$text = 'Error: ' . $exception->getMessage();
$width = imagefontwidth(2) * strlen($text);
$height = imagefontheight(2);
if ($width > $w || $height > $h) {
$width = max($w, $width);
$height = max($h, $height);
// We change the size of the image
$newimg = imagecreatetruecolor($width, $height);
imagefill($newimg, 0, 0, imagecolorat($this->im, 0, 0));
imagecopy($newimg, $this->im, 0, 0, 0, 0, $w, $h);
$this->im = $newimg;
}
$black = new CINColor('black');
imagestring($this->im, 2, 0, 0, $text, $black->allocate($this->im));
}
/**
* Free the memory of PHP (called also by destructor).
*/
public function destroy() {
@imagedestroy($this->im);
}
/**
* Init Image and color background.
*/
private function init() {
if ($this->im === null) {
$this->im = imagecreatetruecolor($this->w, $this->h)
or die('Can\'t Initialize the GD Libraty');
imagefilledrectangle($this->im, 0, 0, $this->w - 1, $this->h - 1, $this->color->allocate($this->im));
}
}
}
?>

26
barcode-generator/Generator/CINFont.php

@ -0,0 +1,26 @@
<?php
/**
*--------------------------------------------------------------------
*
* Interface for a font.
*
*--------------------------------------------------------------------
* @author Akhtar Khan <er.akhtarkhan@gmail.com>
* @link http://www.codeitnow.in
* @package https://github.com/codeitnowin/barcode-generator
*/
namespace CodeItNow\BarcodeBundle\Generator;
interface CINFont {
public /*internal*/ function getText();
public /*internal*/ function setText($text);
public /*internal*/ function getRotationAngle();
public /*internal*/ function setRotationAngle($rotationDegree);
public /*internal*/ function getBackgroundColor();
public /*internal*/ function setBackgroundColor($backgroundColor);
public /*internal*/ function getForegroundColor();
public /*internal*/ function setForegroundColor($foregroundColor);
public /*internal*/ function getDimension();
public /*internal*/ function draw($im, $x, $y);
}
?>

211
barcode-generator/Generator/CINFontFile.php

@ -0,0 +1,211 @@
<?php
/**
*--------------------------------------------------------------------
*
* Holds font family and size.
*
*--------------------------------------------------------------------
* @author Akhtar Khan <er.akhtarkhan@gmail.com>
* @link http://www.codeitnow.in
* @package https://github.com/codeitnowin/barcode-generator
*/
namespace CodeItNow\BarcodeBundle\Generator;
use CodeItNow\BarcodeBundle\Generator\CINArgumentException;
use CodeItNow\BarcodeBundle\Generator\CINFont;
use CodeItNow\BarcodeBundle\Generator\CINColor;
class CINFontFile implements CINFont {
const PHP_BOX_FIX = 0;
private $path;
private $size;
private $text = '';
private $foregroundColor;
private $rotationAngle;
private $box;
private $boxFix;
/**
* Constructor.
*
* @param string $fontPath path to the file
* @param int $size size in point
*/
public function __construct($fontPath, $size) {
if (!file_exists($fontPath)) {
throw new CINArgumentException('The font path is incorrect.', 'fontPath');
}
$this->path = $fontPath;
$this->size = $size;
$this->foregroundColor = new CINColor('black');
$this->setRotationAngle(0);
$this->setBoxFix(self::PHP_BOX_FIX);
}
/**
* Gets the text associated to the font.
*
* @return string
*/
public function getText() {
return $this->text;
}
/**
* Sets the text associated to the font.
*
* @param string text
*/
public function setText($text) {
$this->text = $text;
$this->box = null;
}
/**
* Gets the rotation in degree.
*
* @return int
*/
public function getRotationAngle() {
return (360 - $this->rotationAngle) % 360;
}
/**
* Sets the rotation in degree.
*
* @param int
*/
public function setRotationAngle($rotationAngle) {
$this->rotationAngle = (int)$rotationAngle;
if ($this->rotationAngle !== 90 && $this->rotationAngle !== 180 && $this->rotationAngle !== 270) {
$this->rotationAngle = 0;
}
$this->rotationAngle = (360 - $this->rotationAngle) % 360;
$this->box = null;
}
/**
* Gets the background color.
*
* @return CINColor
*/
public function getBackgroundColor() {
}
/**
* Sets the background color.
*
* @param CINColor $backgroundColor
*/
public function setBackgroundColor($backgroundColor) {
}
/**
* Gets the foreground color.
*
* @return CINColor
*/
public function getForegroundColor() {
return $this->foregroundColor;
}
/**
* Sets the foreground color.
*
* @param CINColor $foregroundColor
*/
public function setForegroundColor($foregroundColor) {
$this->foregroundColor = $foregroundColor;
}
/**
* Gets the box fix information.
*
* @return int
*/
public function getBoxFix() {
return $this->boxFix;
}
/**
* Sets the box fix information.
*
* @param int $value
*/
public function setBoxFix($value) {
$this->boxFix = intval($value);
}
/**
* Returns the width and height that the text takes to be written.
*
* @return int[]
*/
public function getDimension() {
$w = 0.0;
$h = 0.0;
$box = $this->getBox();
if ($box !== null) {
$minX = min(array($box[0], $box[2], $box[4], $box[6]));
$maxX = max(array($box[0], $box[2], $box[4], $box[6]));
$minY = min(array($box[1], $box[3], $box[5], $box[7]));
$maxY = max(array($box[1], $box[3], $box[5], $box[7]));
$w = $maxX - $minX;
$h = $maxY - $minY;
}
$rotationAngle = $this->getRotationAngle();
if ($rotationAngle === 90 || $rotationAngle === 270) {
return array($h + self::PHP_BOX_FIX, $w);
} else {
return array($w + self::PHP_BOX_FIX, $h);
}
}
/**
* Draws the text on the image at a specific position.
* $x and $y represent the left bottom corner.
*
* @param resource $im
* @param int $x
* @param int $y
*/
public function draw($im, $x, $y) {
$drawingPosition = $this->getDrawingPosition($x, $y);
imagettftext($im, $this->size, $this->rotationAngle, $drawingPosition[0], $drawingPosition[1], $this->foregroundColor->allocate($im), $this->path, $this->text);
}
private function getDrawingPosition($x, $y) {
$dimension = $this->getDimension();
$box = $this->getBox();
$rotationAngle = $this->getRotationAngle();
if ($rotationAngle === 0) {
$y += abs(min($box[5], $box[7]));
} elseif ($rotationAngle === 90) {
$x += abs(min($box[5], $box[7]));
$y += $dimension[1];
} elseif ($rotationAngle === 180) {
$x += $dimension[0];
$y += abs(max($box[1], $box[3]));
} elseif ($rotationAngle === 270) {
$x += abs(max($box[1], $box[3]));
}
return array($x, $y);
}
private function getBox() {
if ($this->box === null) {
$gd = imagecreate(1, 1);
$this->box = imagettftext($gd, $this->size, 0, 0, 0, 0, $this->path, $this->text);
}
return $this->box;
}
}
?>

155
barcode-generator/Generator/CINFontPhp.php

@ -0,0 +1,155 @@
<?php
/**
*--------------------------------------------------------------------
*
* Holds font for PHP.
*
*--------------------------------------------------------------------
* @author Akhtar Khan <er.akhtarkhan@gmail.com>
* @link http://www.codeitnow.in
* @package https://github.com/codeitnowin/barcode-generator
*/
namespace CodeItNow\BarcodeBundle\Generator;
use CodeItNow\BarcodeBundle\Generator\CINFont;
use CodeItNow\BarcodeBundle\Generator\CINColor;
class CINFontPhp implements CINFont {
private $font;
private $text;
private $rotationAngle;
private $backgroundColor;
private $foregroundColor;
/**
* Constructor.
*
* @param int $font
*/
public function __construct($font) {
$this->font = max(0, intval($font));
$this->backgroundColor = new CINColor('white');
$this->foregroundColor = new CINColor('black');
$this->setRotationAngle(0);
}
/**
* Gets the text associated to the font.
*
* @return string
*/
public function getText() {
return $this->text;
}
/**
* Sets the text associated to the font.
*
* @param string text
*/
public function setText($text) {
$this->text = $text;
}
/**
* Gets the rotation in degree.
*
* @return int
*/
public function getRotationAngle() {
return (360 - $this->rotationAngle) % 360;
}
/**
* Sets the rotation in degree.
*
* @param int
*/
public function setRotationAngle($rotationAngle) {
$this->rotationAngle = (int)$rotationAngle;
if ($this->rotationAngle !== 90 && $this->rotationAngle !== 180 && $this->rotationAngle !== 270) {
$this->rotationAngle = 0;
}
$this->rotationAngle = (360 - $this->rotationAngle) % 360;
}
/**
* Gets the background color.
*
* @return CINColor
*/
public function getBackgroundColor() {
return $this->backgroundColor;
}
/**
* Sets the background color.
*
* @param CINColor $backgroundColor
*/
public function setBackgroundColor($backgroundColor) {
$this->backgroundColor = $backgroundColor;
}
/**
* Gets the foreground color.
*
* @return CINColor
*/
public function getForegroundColor() {
return $this->foregroundColor;
}
/**
* Sets the foreground color.
*