Browse Source

Use composer and change QR code generation library

main
Miraty 4 weeks ago
parent
commit
147a25c93f
  1. 7
      .gitignore
  2. 24
      README.md
  3. 79
      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. 136
      barcode-generator/README.md
  40. BIN
      barcode-generator/Resources/data/qrv10_0.dat
  41. BIN
      barcode-generator/Resources/data/qrv10_1.dat
  42. BIN
      barcode-generator/Resources/data/qrv10_2.dat
  43. BIN
      barcode-generator/Resources/data/qrv10_3.dat
  44. BIN
      barcode-generator/Resources/data/qrv11_0.dat
  45. BIN
      barcode-generator/Resources/data/qrv11_1.dat
  46. BIN
      barcode-generator/Resources/data/qrv11_2.dat
  47. BIN
      barcode-generator/Resources/data/qrv11_3.dat
  48. BIN
      barcode-generator/Resources/data/qrv12_0.dat
  49. BIN
      barcode-generator/Resources/data/qrv12_1.dat
  50. BIN
      barcode-generator/Resources/data/qrv12_2.dat
  51. BIN
      barcode-generator/Resources/data/qrv12_3.dat
  52. BIN
      barcode-generator/Resources/data/qrv13_0.dat
  53. BIN
      barcode-generator/Resources/data/qrv13_1.dat
  54. BIN
      barcode-generator/Resources/data/qrv13_2.dat
  55. BIN
      barcode-generator/Resources/data/qrv13_3.dat
  56. BIN
      barcode-generator/Resources/data/qrv14_0.dat
  57. BIN
      barcode-generator/Resources/data/qrv14_1.dat
  58. BIN
      barcode-generator/Resources/data/qrv14_2.dat
  59. BIN
      barcode-generator/Resources/data/qrv14_3.dat
  60. BIN
      barcode-generator/Resources/data/qrv15_0.dat
  61. BIN
      barcode-generator/Resources/data/qrv15_1.dat
  62. BIN
      barcode-generator/Resources/data/qrv15_2.dat
  63. BIN
      barcode-generator/Resources/data/qrv15_3.dat
  64. BIN
      barcode-generator/Resources/data/qrv16_0.dat
  65. BIN
      barcode-generator/Resources/data/qrv16_1.dat
  66. BIN
      barcode-generator/Resources/data/qrv16_2.dat
  67. BIN
      barcode-generator/Resources/data/qrv16_3.dat
  68. BIN
      barcode-generator/Resources/data/qrv17_0.dat
  69. BIN
      barcode-generator/Resources/data/qrv17_1.dat
  70. BIN
      barcode-generator/Resources/data/qrv17_2.dat
  71. BIN
      barcode-generator/Resources/data/qrv17_3.dat
  72. BIN
      barcode-generator/Resources/data/qrv18_0.dat
  73. BIN
      barcode-generator/Resources/data/qrv18_1.dat
  74. BIN
      barcode-generator/Resources/data/qrv18_2.dat
  75. BIN
      barcode-generator/Resources/data/qrv18_3.dat
  76. BIN
      barcode-generator/Resources/data/qrv19_0.dat
  77. BIN
      barcode-generator/Resources/data/qrv19_1.dat
  78. BIN
      barcode-generator/Resources/data/qrv19_2.dat
  79. BIN
      barcode-generator/Resources/data/qrv19_3.dat
  80. BIN
      barcode-generator/Resources/data/qrv1_0.dat
  81. BIN
      barcode-generator/Resources/data/qrv1_1.dat
  82. BIN
      barcode-generator/Resources/data/qrv1_2.dat
  83. BIN
      barcode-generator/Resources/data/qrv1_3.dat
  84. BIN
      barcode-generator/Resources/data/qrv20_0.dat
  85. BIN
      barcode-generator/Resources/data/qrv20_1.dat
  86. BIN
      barcode-generator/Resources/data/qrv20_2.dat
  87. BIN
      barcode-generator/Resources/data/qrv20_3.dat
  88. BIN
      barcode-generator/Resources/data/qrv21_0.dat
  89. BIN
      barcode-generator/Resources/data/qrv21_1.dat
  90. BIN
      barcode-generator/Resources/data/qrv21_2.dat
  91. BIN
      barcode-generator/Resources/data/qrv21_3.dat
  92. BIN
      barcode-generator/Resources/data/qrv22_0.dat
  93. 209
      barcode-generator/Resources/data/qrv22_1.dat
  94. BIN
      barcode-generator/Resources/data/qrv22_2.dat
  95. BIN
      barcode-generator/Resources/data/qrv22_3.dat
  96. BIN
      barcode-generator/Resources/data/qrv23_0.dat
  97. 217
      barcode-generator/Resources/data/qrv23_1.dat
  98. BIN
      barcode-generator/Resources/data/qrv23_2.dat
  99. BIN
      barcode-generator/Resources/data/qrv23_3.dat
  100. BIN
      barcode-generator/Resources/data/qrv24_0.dat

7
.gitignore

@ -1,3 +1,4 @@
.directory
css/*
!css/.gitkeep
/.directory
/css/*
!/css/.gitkeep
/composer.lock

24
README.md

@ -1,6 +1,4 @@
# ![](themes/libreqr/icons/32.png) LibreQR
[Lire ceci en français](README_fr.md)
# LibreQR
A PHP Web interface for generating QR codes.
@ -14,19 +12,10 @@ A LibreQR instance is available at <https://qr.antopie.org>.
Just place this source code in a Web server with PHP.
`wget https://code.antopie.org/miraty/libreqr/archive/1.3.0.zip`
GD extension is required.
`apt install php7.3-gd`
PHP extensions GD and iconv are required.
LibreQR need writing rights on the `css/` directory.
```
chown -R www-data:www-data /var/www/libreqr/css
chmod -R 600 /var/www/libreqr/css
```
#### Security hardening
Your HTTP server can reply the following headers:
@ -61,16 +50,9 @@ If you want to report a bug, you can open an issue at <https://code.antopie.org/
If you want to contact me, for instance to report a bug or ask me a question about installing or using LibreQR, you can get my contact details on <https://miraty.antopie.org>.
## Libraries
This source code includes:
* [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
[AGPLv3+](https://code.antopie.org/miraty/libreqr/src/branch/main/LICENSE)
LibreQR is published under [AGPLv3+](https://code.antopie.org/miraty/libreqr/src/branch/main/LICENSE). Librairies located in the `vendor` subdirectory use their own licenses.
LibreQR is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

79
README_fr.md

@ -1,79 +0,0 @@
# ![](themes/libreqr/icons/32.png) LibreQR
[Read this in english](README.md)
Une interface Web en PHP pour générer des codes QR.
## Démo
Une instance de LibreQR est disponible sur <https://qr.antopie.org>.
## Installation
### Générique
Placez simplement ce code source dans un serveur Web avec PHP.
`wget https://code.antopie.org/miraty/libreqr/archive/1.3.0.zip`
L'extension GD est requise.
`apt install php7.3-gd`
LibreQR a besoin des permissions d'écriture dans le dossier `css/`.
```
chown -R www-data:www-data /var/www/libreqr/css
chmod -R 600 /var/www/libreqr/css
```
#### Durcir la sécurité
Votre serveur HTTP peut répondre avec les entêtes suivantes :
```
Content-Security-Policy: default-src 'none'; img-src 'self' data:; style-src 'self'; frame-ancestors 'none'; form-action 'self';
Referrer-Policy: no-referrer
```
### YunoHost
Il y a [un paquet](https://code.antopie.org/miraty/qr_ynh/) pour [YunoHost](https://yunohost.org/).
Pour des raisons historiques, LibreQR est techniquement nommée `qr` dans YunoHost.
Vous pouvez l'installer depuis l'interface Web d'administration ou avec cette commande :
```
sudo yunohost app install qr
```
## Themes
Les thèmes sont situés dans `themes/`, le thème par défaut est dans `themes/libreqr`.
Vous pouvez personnaliser l'apparence de votre instance LibreQR en changeant les couleurs dans `theme.php`, le logo dans `logo.less` ou les icônes dans `icons/<size>.png` (ensuite indiquer leur taille dans `theme.php`).
## Contribuer
Si vous souhaitez rapporter un bug, vous pouvez ouvrir un ticket sur <https://code.antopie.org/miraty/libreqr/issues> après vous être créé un compte (méthode préférée) ou me contacter d'une autre manière.
## Contact
Si vous voulez me contacter, par exemple pour signaler un bug ou me poser une question sur l'installation ou l'utilisation de LibreQR, vous trouverez des moyens de me contacter sur <https://miraty.antopie.org>.
## Bibliothèques tierces
Ce code source inclus :
* [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
[AGPLv3+](https://code.antopie.org/miraty/libreqr/src/branch/main/LICENSE)
LibreQR est un logiciel libre ; vous pouvez le diffuser et le modifier suivant les termes de la GNU Affero General Public License telle que publiée par la Free Software Foundation ; soit la version 3 de cette licence, soit (à votre convenance) une version ultérieure.
LibreQR est diffusé dans l’espoir qu’il sera utile, mais SANS AUCUNE GARANTIE ; sans même une garantie implicite de COMMERCIALISATION ou d’ADÉQUATION À UN USAGE PARTICULIER. Voyez la GNU Affero General Public License pour plus de détails.
Vous devriez avoir reçu une copie de la GNU Affero General Public License avec ce code. Sinon, consultez <https://www.gnu.org/licenses/>

31
barcode-generator/Generator/CINArgumentException.php

@ -1,31 +0,0 @@
<?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

@ -1,439 +0,0 @@
<?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

@ -1,262 +0,0 @@
<?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

@ -1,157 +0,0 @@
<?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

@ -1,25 +0,0 @@
<?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

@ -1,250 +0,0 @@
<?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

@ -1,26 +0,0 @@
<?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

@ -1,211 +0,0 @@
<?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

@ -1,155 +0,0 @@
<?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.
*
* @param CINColor $foregroundColor
*/
public function setForegroundColor($foregroundColor) {
$this->foregroundColor = $foregroundColor;
}
/**
* Returns the width and height that the text takes to be written.
*
* @return int[]
*/
public function getDimension() {
$w = imagefontwidth($this->font) * strlen($this->text);
$h = imagefontheight($this->font);
$rotationAngle = $this->getRotationAngle();
if ($rotationAngle === 90 || $rotationAngle === 270) {
return array($h, $w);
} else {
return array($w, $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) {
if ($this->getRotationAngle() !== 0) {
if (!function_exists('imagerotate')) {
throw new CINDrawException('The method imagerotate doesn\'t exist on your server. Do not use any rotation.');
}
$w = imagefontwidth($this->font) * strlen($this->text);
$h = imagefontheight($this->font);
$gd = imagecreatetruecolor($w, $h);
imagefilledrectangle($gd, 0, 0, $w - 1, $h - 1, $this->backgroundColor->allocate($gd));
imagestring($gd, $this->font, 0, 0, $this->text, $this->foregroundColor->allocate($gd));
$gd = imagerotate($gd, $this->rotationAngle, 0);
imagecopy($im, $gd, $x, $y, 0, 0, imagesx($gd), imagesy($gd));
} else {
imagestring($im, $this->font, $x, $y, $this->text, $this->foregroundColor->allocate($im));
}
}
}
?>

322
barcode-generator/Generator/CINLabel.php

@ -1,322 +0,0 @@
<?php
/**
*--------------------------------------------------------------------
*
* Class for Label
*
*--------------------------------------------------------------------
* @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\CINFontPhp;
use CodeItNow\BarcodeBundle\Generator\CINFontFile;
class CINLabel {
const POSITION_TOP = 0;
const POSITION_RIGHT = 1;
const POSITION_BOTTOM = 2;
const POSITION_LEFT = 3;
const ALIGN_LEFT = 0;
const ALIGN_TOP = 0;
const ALIGN_CENTER = 1;
const ALIGN_RIGHT = 2;
const ALIGN_BOTTOM = 2;
private $font;
private $text;
private $position;
private $alignment;
private $offset;
private $spacing;
private $rotationAngle;
private $backgroundColor;
private $foregroundColor;
/**
* Constructor.
*
* @param string $text
* @param CINFont $font
* @param int $position
* @param int $alignment
*/
public function __construct($text = '', $font = null, $position = self::POSITION_BOTTOM, $alignment = self::ALIGN_CENTER) {
$font = $font === null ? new CINFontPhp(5) : $font;
$this->setFont($font);
$this->setText($text);
$this->setPosition($position);
$this->setAlignment($alignment);
$this->setSpacing(4);
$this->setOffset(0);
$this->setRotationAngle(0);
$this->setBackgroundColor(new CINColor('white'));
$this->setForegroundColor(new CINColor('black'));
}
/**
* Gets the text.
*
* @return string
*/
public function getText() {
return $this->font->getText();
}
/**
* Sets the text.
*
* @param string $text
*/
public function setText($text) {
$this->text = $text;
$this->font->setText($this->text);
}
/**
* Gets the font.
*
* @return CINFont
*/
public function getFont() {
return $this->font;
}
/**
* Sets the font.
*
* @param CINFont $font
*/
public function setFont($font) {
if ($font === null) {
throw new CINArgumentException('Font cannot be null.', 'font');
}
$this->font = clone $font;
$this->font->setText($this->text);
$this->font->setRotationAngle($this->rotationAngle);
$this->font->setBackgroundColor($this->backgroundColor);
$this->font->setForegroundColor($this->foregroundColor);
}
/**
* Gets the text position for drawing.
*
* @return int
*/
public function getPosition() {
return $this->position;
}
/**
* Sets the text position for drawing.
*
* @param int $position
*/
public function setPosition($position) {
$position = intval($position);
if ($position !== self::POSITION_TOP && $position !== self::POSITION_RIGHT && $position !== self::POSITION_BOTTOM && $position !== self::POSITION_LEFT) {
throw new CINArgumentException('The text position must be one of a valid constant.', 'position');
}
$this->position = $position;
}
/**
* Gets the text alignment for drawing.
*
* @return int
*/
public function getAlignment() {
return $this->alignment;
}