| 1 |
<?php |
| 2 |
/** |
| 3 |
* Class QRInterventionImage |
| 4 |
* |
| 5 |
* @created 21.01.2024 |
| 6 |
* @author smiley <[email protected]> |
| 7 |
* @copyright 2024 smiley |
| 8 |
* @license MIT |
| 9 |
*/ |
| 10 |
declare(strict_types=1); |
| 11 |
|
| 12 |
namespace chillerlan\QRCode\Output; |
| 13 |
|
| 14 |
use chillerlan\QRCode\Data\QRMatrix; |
| 15 |
use chillerlan\QRCode\QROptions; |
| 16 |
use chillerlan\Settings\SettingsContainerInterface; |
| 17 |
use Intervention\Image\Drivers\Gd\Driver as GdDriver; |
| 18 |
use Intervention\Image\Drivers\Imagick\Driver as ImagickDriver; |
| 19 |
use Intervention\Image\Geometry\Factories\CircleFactory; |
| 20 |
use Intervention\Image\Geometry\Factories\RectangleFactory; |
| 21 |
use Intervention\Image\ImageManager; |
| 22 |
use Intervention\Image\Interfaces\DriverInterface; |
| 23 |
use Intervention\Image\Interfaces\ImageInterface; |
| 24 |
use Intervention\Image\Interfaces\ImageManagerInterface; |
| 25 |
use UnhandledMatchError; |
| 26 |
use function class_exists; |
| 27 |
use function extension_loaded; |
| 28 |
use function intdiv; |
| 29 |
|
| 30 |
/** |
| 31 |
* intervention/image (GD/ImageMagick) output |
| 32 |
* |
| 33 |
* note: this output class works very slow compared to the native GD/Imagick output classes for obvious reasons. |
| 34 |
* use only if you must. |
| 35 |
* |
| 36 |
* @see https://github.com/Intervention/image |
| 37 |
* @see https://image.intervention.io/ |
| 38 |
*/ |
| 39 |
class QRInterventionImage extends QROutputAbstract{ |
| 40 |
use CssColorModuleValueTrait; |
| 41 |
|
| 42 |
protected DriverInterface $driver; |
| 43 |
protected ImageManagerInterface $manager; |
| 44 |
protected ImageInterface $image; |
| 45 |
|
| 46 |
/** |
| 47 |
* QRInterventionImage constructor. |
| 48 |
* |
| 49 |
* @throws \chillerlan\QRCode\Output\QRCodeOutputException |
| 50 |
*/ |
| 51 |
public function __construct(SettingsContainerInterface|QROptions|iterable $options, QRMatrix $matrix){ |
| 52 |
|
| 53 |
if(!class_exists(ImageManager::class)){ |
| 54 |
// @codeCoverageIgnoreStart |
| 55 |
throw new QRCodeOutputException( |
| 56 |
'The QRInterventionImage output requires Intervention/image (https://github.com/Intervention/image)'. |
| 57 |
' as dependency but the class "\\Intervention\\Image\\ImageManager" could not be found.', |
| 58 |
); |
| 59 |
// @codeCoverageIgnoreEnd |
| 60 |
} |
| 61 |
|
| 62 |
try{ |
| 63 |
$this->driver = match(true){ |
| 64 |
extension_loaded('gd') => new GdDriver, |
| 65 |
extension_loaded('imagick') => new ImagickDriver, |
| 66 |
}; |
| 67 |
|
| 68 |
$this->setDriver($this->driver); |
| 69 |
} |
| 70 |
catch(UnhandledMatchError){ |
| 71 |
throw new QRCodeOutputException('no image processing extension loaded (gd, imagick)'); // @codeCoverageIgnore |
| 72 |
} |
| 73 |
|
| 74 |
parent::__construct($options, $matrix); |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Sets a DriverInterface |
| 79 |
*/ |
| 80 |
public function setDriver(DriverInterface $driver):static{ |
| 81 |
$this->manager = new ImageManager($driver); |
| 82 |
|
| 83 |
return $this; |
| 84 |
} |
| 85 |
|
| 86 |
public function dump(string|null $file = null):string|ImageInterface{ |
| 87 |
[$width, $height] = $this->getOutputDimensions(); |
| 88 |
|
| 89 |
$this->image = $this->manager->create($width, $height); |
| 90 |
|
| 91 |
$this->image->fill($this->getDefaultModuleValue(false)); |
| 92 |
|
| 93 |
if($this->options->imageTransparent && $this::moduleValueIsValid($this->options->transparencyColor)){ |
| 94 |
$this->manager |
| 95 |
->driver() |
| 96 |
->config() |
| 97 |
->setOptions(blendingColor: $this->prepareModuleValue($this->options->transparencyColor)) |
| 98 |
; |
| 99 |
} |
| 100 |
|
| 101 |
if($this::moduleValueIsValid($this->options->bgColor)){ |
| 102 |
$this->image->fill($this->prepareModuleValue($this->options->bgColor)); |
| 103 |
} |
| 104 |
|
| 105 |
foreach($this->matrix->getMatrix() as $y => $row){ |
| 106 |
foreach($row as $x => $M_TYPE){ |
| 107 |
$this->module($x, $y, $M_TYPE); |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
if($this->options->returnResource){ |
| 112 |
return $this->image; |
| 113 |
} |
| 114 |
|
| 115 |
$image = $this->image->toPng(); |
| 116 |
$imageData = $image->toString(); |
| 117 |
|
| 118 |
$this->saveToFile($imageData, $file); |
| 119 |
|
| 120 |
if($this->options->outputBase64){ |
| 121 |
return $image->toDataUri(); |
| 122 |
} |
| 123 |
|
| 124 |
return $imageData; |
| 125 |
} |
| 126 |
|
| 127 |
/** |
| 128 |
* draws a single pixel at the given position |
| 129 |
*/ |
| 130 |
protected function module(int $x, int $y, int $M_TYPE):void{ |
| 131 |
|
| 132 |
if(!$this->drawLightModules && !$this->matrix->isDark($M_TYPE)){ |
| 133 |
return; |
| 134 |
} |
| 135 |
|
| 136 |
$color = $this->getModuleValue($M_TYPE); |
| 137 |
|
| 138 |
if($this->drawCircularModules && !$this->matrix->checkTypeIn($x, $y, $this->keepAsSquare)){ |
| 139 |
|
| 140 |
$this->image->drawCircle( |
| 141 |
(($x * $this->scale) + intdiv($this->scale, 2)), |
| 142 |
(($y * $this->scale) + intdiv($this->scale, 2)), |
| 143 |
function(CircleFactory $circle) use ($color):void{ |
| 144 |
$circle->radius((int)($this->circleRadius * $this->scale)); |
| 145 |
$circle->background($color); |
| 146 |
}, |
| 147 |
); |
| 148 |
|
| 149 |
return; |
| 150 |
} |
| 151 |
|
| 152 |
$this->image->drawRectangle( |
| 153 |
($x * $this->scale), |
| 154 |
($y * $this->scale), |
| 155 |
function(RectangleFactory $rectangle) use ($color):void{ |
| 156 |
$rectangle->size($this->scale, $this->scale); |
| 157 |
$rectangle->background($color); |
| 158 |
}, |
| 159 |
); |
| 160 |
} |
| 161 |
|
| 162 |
} |