| 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 |
declare(strict_types=1); |
| 14 |
|
| 15 |
namespace chillerlan\QRCode\Common; |
| 16 |
|
| 17 |
use chillerlan\QRCode\QROptions; |
| 18 |
use chillerlan\Settings\SettingsContainerInterface; |
| 19 |
use GdImage; |
| 20 |
use function file_get_contents, imagecolorat, imagecolorsforindex, |
| 21 |
imagecreatefromstring, imagefilter, imagesx, imagesy; |
| 22 |
use const IMG_FILTER_BRIGHTNESS, IMG_FILTER_CONTRAST, IMG_FILTER_GRAYSCALE, IMG_FILTER_NEGATE; |
| 23 |
|
| 24 |
/** |
| 25 |
* This class is used to help decode images from files which arrive as GD Resource |
| 26 |
* It does not support rotation. |
| 27 |
*/ |
| 28 |
final class GDLuminanceSource extends LuminanceSourceAbstract{ |
| 29 |
|
| 30 |
private GdImage $gdImage; |
| 31 |
|
| 32 |
/** |
| 33 |
* GDLuminanceSource constructor. |
| 34 |
* |
| 35 |
* @throws \chillerlan\QRCode\Decoder\QRCodeDecoderException |
| 36 |
*/ |
| 37 |
public function __construct(GdImage $gdImage, SettingsContainerInterface|QROptions $options = new QROptions){ |
| 38 |
parent::__construct(imagesx($gdImage), imagesy($gdImage), $options); |
| 39 |
|
| 40 |
$this->gdImage = $gdImage; |
| 41 |
|
| 42 |
if($this->options->readerGrayscale){ |
| 43 |
imagefilter($this->gdImage, IMG_FILTER_GRAYSCALE); |
| 44 |
} |
| 45 |
|
| 46 |
if($this->options->readerInvertColors){ |
| 47 |
imagefilter($this->gdImage, IMG_FILTER_NEGATE); |
| 48 |
} |
| 49 |
|
| 50 |
if($this->options->readerIncreaseContrast){ |
| 51 |
imagefilter($this->gdImage, IMG_FILTER_BRIGHTNESS, -100); |
| 52 |
imagefilter($this->gdImage, IMG_FILTER_CONTRAST, -100); |
| 53 |
} |
| 54 |
|
| 55 |
$this->setLuminancePixels(); |
| 56 |
} |
| 57 |
|
| 58 |
private function setLuminancePixels():void{ |
| 59 |
|
| 60 |
for($j = 0; $j < $this->height; $j++){ |
| 61 |
for($i = 0; $i < $this->width; $i++){ |
| 62 |
$argb = imagecolorat($this->gdImage, $i, $j); |
| 63 |
$pixel = imagecolorsforindex($this->gdImage, $argb); |
| 64 |
|
| 65 |
$this->setLuminancePixel($pixel['red'], $pixel['green'], $pixel['blue']); |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
} |
| 70 |
|
| 71 |
public static function fromFile(string $path, SettingsContainerInterface|QROptions $options = new QROptions):static{ |
| 72 |
return new self(imagecreatefromstring(file_get_contents(self::checkFile($path))), $options); |
| 73 |
} |
| 74 |
|
| 75 |
public static function fromBlob(string $blob, SettingsContainerInterface|QROptions $options = new QROptions):static{ |
| 76 |
return new self(imagecreatefromstring($blob), $options); |
| 77 |
} |
| 78 |
|
| 79 |
} |