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