323 changed files with 34198 additions and 3656 deletions
@ -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); |
|||
} |
|||
} |
|||
?> |
@ -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; |
|||
} |
|||
} |
|||
?> |
@ -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; |
|||
} |
|||
} |
|||
?> |
@ -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'); |
|||
} |
|||
} |
|||
} |
|||
?> |
@ -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); |
|||
} |
|||
} |
|||
?> |
@ -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)); |
|||
} |
|||
} |
|||
} |
|||
?> |
@ -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); |
|||
} |
|||
?> |
@ -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; |
|||
} |
|||
} |
|||
?> |
@ -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. |
|||
* |
|||
* @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)); |
|||
} |
|||
} |
|||
} |
|||
?> |
@ -0,0 +1,322 @@ |
|||
<?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; |
|||
} |
|||
|
|||
/** |
|||
* Sets the text alignment for drawing. |
|||
* |
|||
* @param int $alignment |
|||
*/ |
|||
public function setAlignment($alignment) { |
|||
$alignment = intval($alignment); |
|||
if ($alignment !== self::ALIGN_LEFT && $alignment !== self::ALIGN_TOP && $alignment !== self::ALIGN_CENTER && $alignment !== self::ALIGN_RIGHT && $alignment !== self::ALIGN_BOTTOM) { |
|||
throw new CINArgumentException('The text alignment must be one of a valid constant.', 'alignment'); |
|||
} |
|||
|
|||
$this->alignment = $alignment; |
|||
} |
|||
|
|||
/** |
|||
* Gets the offset. |
|||
* |
|||
* @return int |
|||
*/ |
|||
public function getOffset() { |
|||
return $this->offset; |
|||
} |
|||
|
|||
/** |
|||
* Sets the offset. |
|||
* |
|||
* @param int $offset |
|||
*/ |
|||
public function setOffset($offset) { |
|||
$this->offset = intval($offset); |
|||
} |
|||
|
|||
/** |
|||
* Gets the spacing. |
|||
* |
|||
* @return int |
|||
*/ |
|||
public function getSpacing() { |
|||
return $this->spacing; |
|||
} |
|||
|
|||
/** |
|||
* Sets the spacing. |
|||
* |
|||
* @param int $spacing |
|||
*/ |
|||
public function setSpacing($spacing) { |
|||
$this->spacing = max(0, intval($spacing)); |
|||
} |
|||
|
|||
/** |
|||
* Gets the rotation angle in degree. |
|||
* |
|||
* @return int |
|||
*/ |
|||
public function getRotationAngle() { |
|||
return $this->font->getRotationAngle(); |
|||
} |
|||
|
|||
/** |
|||
* Sets the rotation angle in degree. |
|||
* |
|||
* @param int $rotationAngle |
|||
*/ |
|||
public function setRotationAngle($rotationAngle) { |
|||
$this->rotationAngle = intval($rotationAngle); |
|||
$this->font->setRotationAngle($this->rotationAngle); |
|||
} |
|||
|
|||
/** |
|||
* Gets the background color in case of rotation. |
|||
* |
|||
* @return CINColor |
|||
*/ |
|||
public function getBackgroundColor() { |
|||
return $this->backgroundColor; |
|||
} |
|||
|
|||
/** |
|||
* Sets the background color in case of rotation. |
|||
* |
|||
* @param CINColor $backgroundColor |
|||
*/ |
|||
public /*internal*/ function setBackgroundColor($backgroundColor) { |
|||
$this->backgroundColor = $backgroundColor; |
|||
$this->font->setBackgroundColor($this->backgroundColor); |
|||
} |
|||
|
|||
/** |
|||
* Gets the foreground color. |
|||
* |
|||
* @return CINColor |
|||
*/ |
|||
public function getForegroundColor() { |
|||
return $this->font->getForegroundColor(); |
|||
} |
|||
|
|||
/** |
|||
* Sets the foreground color. |
|||
* |
|||
* @param CINColor $foregroundColor |
|||
*/ |
|||
public function setForegroundColor($foregroundColor) { |
|||
$this->foregroundColor = $foregroundColor; |
|||
$this->font->setForegroundColor($this->foregroundColor); |
|||
} |
|||
|
|||
/** |
|||
* Gets the dimension taken by the label, including the spacing and offset. |
|||
* [0]: width |
|||
* [1]: height |
|||
* |
|||
* @return int[] |
|||
*/ |
|||
public function getDimension() { |
|||
$w = 0; |
|||
$h = 0; |
|||
|
|||
$dimension = $this->font->getDimension(); |
|||
$w = $dimension[0]; |
|||
$h = $dimension[1]; |
|||
|
|||
if ($this->position === self::POSITION_TOP || $this->position === self::POSITION_BOTTOM) { |
|||
$h += $this->spacing; |
|||
$w += max(0, $this->offset); |
|||
} else { |
|||
$w += $this->spacing; |
|||
$h += max(0, $this->offset); |
|||
} |
|||
|
|||
return array($w, $h); |
|||
} |
|||
|
|||
/** |
|||
* 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 |
|||