| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Class GDLuminanceSource |
| 5 |
* |
| 6 |
* @created 17.01.2021 |
| 7 |
* @author Ashot Khanamiryan |
| 8 |
* @author Smiley <smiley@chillerlan.net> |
| 9 |
* @copyright 2021 Smiley |
| 10 |
* @license MIT |
| 11 |
* |
| 12 |
* @noinspection PhpComposerExtensionStubsInspection |
| 13 |
*/ |
| 14 |
namespace WCPOS\Vendor\chillerlan\QRCode\Common; |
| 15 |
|
| 16 |
use WCPOS\Vendor\chillerlan\QRCode\Decoder\QRCodeDecoderException; |
| 17 |
use WCPOS\Vendor\chillerlan\Settings\SettingsContainerInterface; |
| 18 |
use function file_get_contents, get_resource_type, imagecolorat, imagecolorsforindex, imagecreatefromstring, imagefilter, imagesx, imagesy, is_resource; |
| 19 |
use const IMG_FILTER_BRIGHTNESS, IMG_FILTER_CONTRAST, IMG_FILTER_GRAYSCALE, IMG_FILTER_NEGATE, PHP_MAJOR_VERSION; |
| 20 |
/** |
| 21 |
* This class is used to help decode images from files which arrive as GD Resource |
| 22 |
* It does not support rotation. |
| 23 |
*/ |
| 24 |
class GDLuminanceSource extends LuminanceSourceAbstract |
| 25 |
{ |
| 26 |
/** |
| 27 |
* @var resource|\GdImage |
| 28 |
*/ |
| 29 |
protected $gdImage; |
| 30 |
/** |
| 31 |
* GDLuminanceSource constructor. |
| 32 |
* |
| 33 |
* @param resource|\GdImage $gdImage |
| 34 |
* @param \chillerlan\Settings\SettingsContainerInterface|null $options |
| 35 |
* |
| 36 |
* @throws \chillerlan\QRCode\Decoder\QRCodeDecoderException |
| 37 |
*/ |
| 38 |
public function __construct($gdImage, ?SettingsContainerInterface $options = null) |
| 39 |
{ |
| 40 |
/** @noinspection PhpFullyQualifiedNameUsageInspection */ |
| 41 |
if (PHP_MAJOR_VERSION >= 8 && !$gdImage instanceof \GdImage || PHP_MAJOR_VERSION < 8 && (!is_resource($gdImage) || get_resource_type($gdImage) !== 'gd')) { |
| 42 |
throw new QRCodeDecoderException('Invalid GD image source.'); |
| 43 |
// @codeCoverageIgnore |
| 44 |
} |
| 45 |
parent::__construct(imagesx($gdImage), imagesy($gdImage), $options); |
| 46 |
$this->gdImage = $gdImage; |
| 47 |
if ($this->options->readerGrayscale) { |
| 48 |
imagefilter($this->gdImage, IMG_FILTER_GRAYSCALE); |
| 49 |
} |
| 50 |
if ($this->options->readerInvertColors) { |
| 51 |
imagefilter($this->gdImage, IMG_FILTER_NEGATE); |
| 52 |
} |
| 53 |
if ($this->options->readerIncreaseContrast) { |
| 54 |
imagefilter($this->gdImage, IMG_FILTER_BRIGHTNESS, -100); |
| 55 |
imagefilter($this->gdImage, IMG_FILTER_CONTRAST, -100); |
| 56 |
} |
| 57 |
$this->setLuminancePixels(); |
| 58 |
} |
| 59 |
/** |
| 60 |
* |
| 61 |
*/ |
| 62 |
protected function setLuminancePixels() : void |
| 63 |
{ |
| 64 |
for ($j = 0; $j < $this->height; $j++) { |
| 65 |
for ($i = 0; $i < $this->width; $i++) { |
| 66 |
$argb = imagecolorat($this->gdImage, $i, $j); |
| 67 |
$pixel = imagecolorsforindex($this->gdImage, $argb); |
| 68 |
$this->setLuminancePixel($pixel['red'], $pixel['green'], $pixel['blue']); |
| 69 |
} |
| 70 |
} |
| 71 |
} |
| 72 |
/** @inheritDoc */ |
| 73 |
public static function fromFile(string $path, ?SettingsContainerInterface $options = null) : self |
| 74 |
{ |
| 75 |
return new self(imagecreatefromstring(file_get_contents(self::checkFile($path))), $options); |
| 76 |
} |
| 77 |
/** @inheritDoc */ |
| 78 |
public static function fromBlob(string $blob, ?SettingsContainerInterface $options = null) : self |
| 79 |
{ |
| 80 |
return new self(imagecreatefromstring($blob), $options); |
| 81 |
} |
| 82 |
} |
| 83 |
|