Passed
Push — v5 ( 93618e...84eb31 )
by smiley
01:52
created

GDLuminanceSource   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 13
c 1
b 0
f 0
dl 0
loc 43
rs 10
wmc 9

2 Methods

Rating   Name   Duplication   Size   Complexity  
A setLuminancePixels() 0 7 3
A __construct() 0 18 6
1
<?php
2
/**
3
 * Class GDLuminanceSource
4
 *
5
 * @created      17.01.2021
6
 * @author       ZXing Authors
7
 * @author       Smiley <[email protected]>
8
 * @copyright    2021 Smiley
9
 * @license      Apache-2.0
10
 *
11
 *  @noinspection PhpComposerExtensionStubsInspection
12
 */
13
14
namespace chillerlan\QRCode\Decoder;
15
16
use InvalidArgumentException;
17
use function get_resource_type, imagecolorat, imagecolorsforindex, imagesx, imagesy, is_resource;
18
use const PHP_MAJOR_VERSION;
19
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
final class GDLuminanceSource extends LuminanceSource{
25
26
	/**
27
	 * @var resource|\GdImage
28
	 * @phan-suppress PhanUndeclaredTypeProperty
29
	 */
30
	private $gdImage;
31
32
	/**
33
	 * GDLuminanceSource constructor.
34
	 *
35
	 * @param resource|\GdImage $gdImage
36
	 * @phan-suppress PhanUndeclaredTypeParameter
37
	 *
38
	 * @throws \InvalidArgumentException
39
	 */
40
	public function __construct($gdImage){
41
42
		/**
43
		 * @noinspection PhpElementIsNotAvailableInCurrentPhpVersionInspection
44
		 * @phan-suppress PhanUndeclaredClassInstanceof
45
		 */
46
		if(
47
			(PHP_MAJOR_VERSION >= 8 && !$gdImage instanceof \GdImage)
48
			|| (PHP_MAJOR_VERSION < 8 && (!is_resource($gdImage) || get_resource_type($gdImage) !== 'gd'))
49
		){
50
			throw new InvalidArgumentException('Invalid GD image source.');
51
		}
52
53
		parent::__construct(imagesx($gdImage), imagesy($gdImage));
54
55
		$this->gdImage = $gdImage;
56
57
		$this->setLuminancePixels();
58
	}
59
60
	private function setLuminancePixels():void{
61
		for($j = 0; $j < $this->height; $j++){
62
			for($i = 0; $i < $this->width; $i++){
63
				$argb  = imagecolorat($this->gdImage, $i, $j);
64
				$pixel = imagecolorsforindex($this->gdImage, $argb);
65
66
				$this->setLuminancePixel($pixel['red'], $pixel['green'], $pixel['blue']);
67
			}
68
		}
69
	}
70
71
}
72