Debian Codegraph @

vendor/chillerlan/php-qrcode/src/Common/IMagickLuminanceSource.php

raw
1 <?php
2 /**
3 * Class IMagickLuminanceSource
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 Imagick;
20 use function count;
21
22 /**
23 * This class is used to help decode images from files which arrive as Imagick Resource
24 * It does not support rotation.
25 */
26 final class IMagickLuminanceSource extends LuminanceSourceAbstract{
27
28 private Imagick $imagick;
29
30 /**
31 * IMagickLuminanceSource constructor.
32 */
33 public function __construct(Imagick $imagick, SettingsContainerInterface|QROptions $options = new QROptions){
34 parent::__construct($imagick->getImageWidth(), $imagick->getImageHeight(), $options);
35
36 $this->imagick = $imagick;
37
38 if($this->options->readerGrayscale){
39 $this->imagick->setImageColorspace(Imagick::COLORSPACE_GRAY);
40 }
41
42 if($this->options->readerInvertColors){
43 $this->imagick->negateImage($this->options->readerGrayscale);
44 }
45
46 if($this->options->readerIncreaseContrast){
47 for($i = 0; $i < 10; $i++){
48 $this->imagick->contrastImage(false); // misleading docs
49 }
50 }
51
52 $this->setLuminancePixels();
53 }
54
55 private function setLuminancePixels():void{
56 $pixels = $this->imagick->exportImagePixels(1, 1, $this->width, $this->height, 'RGB', Imagick::PIXEL_CHAR);
57 $count = count($pixels);
58
59 for($i = 0; $i < $count; $i += 3){
60 $this->setLuminancePixel(($pixels[$i] & 0xff), ($pixels[($i + 1)] & 0xff), ($pixels[($i + 2)] & 0xff));
61 }
62 }
63
64 public static function fromFile(string $path, SettingsContainerInterface|QROptions $options = new QROptions):static{
65 return new self(new Imagick(self::checkFile($path)), $options);
66 }
67
68 public static function fromBlob(string $blob, SettingsContainerInterface|QROptions $options = new QROptions):static{
69 $im = new Imagick;
70 $im->readImageBlob($blob);
71
72 return new self($im, $options);
73 }
74
75 }