|
| 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 | + |
| 11 | +namespace chillerlan\QRCode\Output; |
| 12 | + |
| 13 | +use chillerlan\QRCode\Data\QRMatrix; |
| 14 | +use chillerlan\QRCode\QROptions; |
| 15 | +use chillerlan\Settings\SettingsContainerInterface; |
| 16 | +use Intervention\Image\Drivers\Gd\Driver as GdDriver; |
| 17 | +use Intervention\Image\Drivers\Imagick\Driver as ImagickDriver; |
| 18 | +use Intervention\Image\Geometry\Factories\CircleFactory; |
| 19 | +use Intervention\Image\Geometry\Factories\RectangleFactory; |
| 20 | +use Intervention\Image\ImageManager; |
| 21 | +use Intervention\Image\Interfaces\DriverInterface; |
| 22 | +use Intervention\Image\Interfaces\ImageInterface; |
| 23 | +use Intervention\Image\Interfaces\ImageManagerInterface; |
| 24 | +use UnhandledMatchError; |
| 25 | +use function class_exists; |
| 26 | +use function extension_loaded; |
| 27 | +use function intdiv; |
| 28 | + |
| 29 | +/** |
| 30 | + * intervention/image (GD/ImageMagick) output |
| 31 | + * |
| 32 | + * note: this output class works very slow compared to the native GD/Imagick output classes for obvious reasons. |
| 33 | + * use only if you must. |
| 34 | + * |
| 35 | + * @see https://github.com/Intervention/image |
| 36 | + * @see https://image.intervention.io/ |
| 37 | + */ |
| 38 | +class QRInterventionImage extends QROutputAbstract{ |
| 39 | + use CssColorModuleValueTrait; |
| 40 | + |
| 41 | + protected DriverInterface $driver; |
| 42 | + protected ImageManagerInterface $manager; |
| 43 | + protected ImageInterface $image; |
| 44 | + |
| 45 | + /** |
| 46 | + * QRInterventionImage constructor. |
| 47 | + * |
| 48 | + * @throws \chillerlan\QRCode\Output\QRCodeOutputException |
| 49 | + */ |
| 50 | + public function __construct(SettingsContainerInterface|QROptions $options, QRMatrix $matrix){ |
| 51 | + |
| 52 | + if(!class_exists(ImageManager::class)){ |
| 53 | + // @codeCoverageIgnoreStart |
| 54 | + throw new QRCodeOutputException( |
| 55 | + 'The QRInterventionImage output requires Intervention/image (https://github.com/Intervention/image)'. |
| 56 | + ' as dependency but the class "\\Intervention\\Image\\ImageManager" could not be found.', |
| 57 | + ); |
| 58 | + // @codeCoverageIgnoreEnd |
| 59 | + } |
| 60 | + |
| 61 | + try{ |
| 62 | + |
| 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 | + /** |
| 87 | + * @inheritDoc |
| 88 | + */ |
| 89 | + public function dump(string $file = null):string|ImageInterface{ |
| 90 | + [$width, $height] = $this->getOutputDimensions(); |
| 91 | + |
| 92 | + $this->image = $this->manager->create($width, $height); |
| 93 | + |
| 94 | + $this->image->fill($this->getDefaultModuleValue(false)); |
| 95 | + |
| 96 | + if($this->options->imageTransparent && $this::moduleValueIsValid($this->options->transparencyColor)){ |
| 97 | + $this->image->setBlendingColor($this->prepareModuleValue($this->options->transparencyColor)); |
| 98 | + } |
| 99 | + |
| 100 | + if($this::moduleValueIsValid($this->options->bgColor)){ |
| 101 | + $this->image->fill($this->prepareModuleValue($this->options->bgColor)); |
| 102 | + } |
| 103 | + |
| 104 | + foreach($this->matrix->getMatrix() as $y => $row){ |
| 105 | + foreach($row as $x => $M_TYPE){ |
| 106 | + $this->module($x, $y, $M_TYPE); |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + if($this->options->returnResource){ |
| 111 | + return $this->image; |
| 112 | + } |
| 113 | + |
| 114 | + $image = $this->image->toPng(); |
| 115 | + $imageData = $image->toString(); |
| 116 | + |
| 117 | + $this->saveToFile($imageData, $file); |
| 118 | + |
| 119 | + if($this->options->outputBase64){ |
| 120 | + return $image->toDataUri(); |
| 121 | + } |
| 122 | + |
| 123 | + return $imageData; |
| 124 | + } |
| 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 | +} |
0 commit comments