qrcode.chapril.org/barcode-generator/Generator/CINDrawing.php
2021-11-01 18:08:42 +01:00

251 lines
6.7 KiB
PHP

<?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));
}
}
}
?>