| 1 |
<?php |
| 2 |
|
| 3 |
namespace WCPOS\Vendor\Picqer\Barcode; |
| 4 |
|
| 5 |
use Imagick; |
| 6 |
use imagickdraw; |
| 7 |
use imagickpixel; |
| 8 |
use WCPOS\Vendor\Picqer\Barcode\Exceptions\BarcodeException; |
| 9 |
class BarcodeGeneratorPNG extends BarcodeGenerator |
| 10 |
{ |
| 11 |
protected $useImagick = \true; |
| 12 |
/** |
| 13 |
* @throws BarcodeException |
| 14 |
*/ |
| 15 |
public function __construct() |
| 16 |
{ |
| 17 |
// Auto switch between GD and Imagick based on what is installed |
| 18 |
if (\extension_loaded('imagick')) { |
| 19 |
$this->useImagick = \true; |
| 20 |
} elseif (\function_exists('imagecreate')) { |
| 21 |
$this->useImagick = \false; |
| 22 |
} else { |
| 23 |
throw new BarcodeException('Neither gd-lib or imagick are installed!'); |
| 24 |
} |
| 25 |
} |
| 26 |
/** |
| 27 |
* Force the use of Imagick image extension |
| 28 |
*/ |
| 29 |
public function useImagick() |
| 30 |
{ |
| 31 |
$this->useImagick = \true; |
| 32 |
} |
| 33 |
/** |
| 34 |
* Force the use of the GD image library |
| 35 |
*/ |
| 36 |
public function useGd() |
| 37 |
{ |
| 38 |
$this->useImagick = \false; |
| 39 |
} |
| 40 |
/** |
| 41 |
* Return a PNG image representation of barcode (requires GD or Imagick library). |
| 42 |
* |
| 43 |
* @param string $barcode code to print |
| 44 |
* @param BarcodeGenerator::TYPE_* $type (string) type of barcode |
| 45 |
* @param int $widthFactor Width of a single bar element in pixels. |
| 46 |
* @param int $height Height of a single bar element in pixels. |
| 47 |
* @param array $foregroundColor RGB (0-255) foreground color for bar elements (background is transparent). |
| 48 |
* @return string image data or false in case of error. |
| 49 |
*/ |
| 50 |
public function getBarcode(string $barcode, $type, int $widthFactor = 2, int $height = 30, array $foregroundColor = [0, 0, 0]) : string |
| 51 |
{ |
| 52 |
$barcodeData = $this->getBarcodeData($barcode, $type); |
| 53 |
$width = \round($barcodeData->getWidth() * $widthFactor); |
| 54 |
if ($this->useImagick) { |
| 55 |
$imagickBarsShape = new imagickdraw(); |
| 56 |
$imagickBarsShape->setFillColor(new imagickpixel('rgb(' . \implode(',', $foregroundColor) . ')')); |
| 57 |
} else { |
| 58 |
$image = $this->createGdImageObject($width, $height); |
| 59 |
$gdForegroundColor = \imagecolorallocate($image, $foregroundColor[0], $foregroundColor[1], $foregroundColor[2]); |
| 60 |
} |
| 61 |
// print bars |
| 62 |
$positionHorizontal = 0; |
| 63 |
/** @var BarcodeBar $bar */ |
| 64 |
foreach ($barcodeData->getBars() as $bar) { |
| 65 |
$barWidth = \round($bar->getWidth() * $widthFactor, 3); |
| 66 |
if ($bar->isBar() && $barWidth > 0) { |
| 67 |
$y = \round($bar->getPositionVertical() * $height / $barcodeData->getHeight(), 3); |
| 68 |
$barHeight = \round($bar->getHeight() * $height / $barcodeData->getHeight(), 3); |
| 69 |
// draw a vertical bar |
| 70 |
if ($this->useImagick) { |
| 71 |
$imagickBarsShape->rectangle($positionHorizontal, $y, $positionHorizontal + $barWidth - 1, $y + $barHeight); |
| 72 |
} else { |
| 73 |
\imagefilledrectangle($image, $positionHorizontal, $y, $positionHorizontal + $barWidth - 1, $y + $barHeight, $gdForegroundColor); |
| 74 |
} |
| 75 |
} |
| 76 |
$positionHorizontal += $barWidth; |
| 77 |
} |
| 78 |
if ($this->useImagick) { |
| 79 |
$image = $this->createImagickImageObject($width, $height); |
| 80 |
$image->drawImage($imagickBarsShape); |
| 81 |
return $image->getImageBlob(); |
| 82 |
} |
| 83 |
\ob_start(); |
| 84 |
$this->generateGdImage($image); |
| 85 |
return \ob_get_clean(); |
| 86 |
} |
| 87 |
protected function createGdImageObject(int $width, int $height) |
| 88 |
{ |
| 89 |
$image = \imagecreate($width, $height); |
| 90 |
$colorBackground = \imagecolorallocate($image, 255, 255, 255); |
| 91 |
\imagecolortransparent($image, $colorBackground); |
| 92 |
return $image; |
| 93 |
} |
| 94 |
protected function createImagickImageObject(int $width, int $height) : Imagick |
| 95 |
{ |
| 96 |
$image = new Imagick(); |
| 97 |
$image->newImage($width, $height, 'none', 'PNG'); |
| 98 |
return $image; |
| 99 |
} |
| 100 |
protected function generateGdImage($image) |
| 101 |
{ |
| 102 |
\imagepng($image); |
| 103 |
\imagedestroy($image); |
| 104 |
} |
| 105 |
} |
| 106 |
|