Passed
Push — master ( 88f800...5334bf )
by smiley
02:00
created

QRImage::dump()   C

Complexity

Conditions 7
Paths 24

Size

Total Lines 38
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 7
eloc 23
nc 24
nop 0
dl 0
loc 38
rs 6.7272
c 1
b 0
f 1
1
<?php
2
/**
3
 * Class QRImage
4
 *
5
 * @filesource   QRImage.php
6
 * @created      05.12.2015
7
 * @package      chillerlan\QRCode\Output
8
 * @author       Smiley <[email protected]>
9
 * @copyright    2015 Smiley
10
 * @license      MIT
11
 */
12
13
namespace chillerlan\QRCode\Output;
14
15
use chillerlan\QRCode\Data\QRMatrix;
16
use chillerlan\QRCode\QRCode;
17
18
/**
19
 * Converts the matrix into images, raw or base64 output
20
 */
21
class QRImage extends QROutputAbstract{
22
23
	protected $moduleValues = [
24
		// light
25
		QRMatrix::M_DATA            => [255, 255, 255],
26
		QRMatrix::M_FINDER          => [255, 255, 255],
27
		QRMatrix::M_SEPARATOR       => [255, 255, 255],
28
		QRMatrix::M_ALIGNMENT       => [255, 255, 255],
29
		QRMatrix::M_TIMING          => [255, 255, 255],
30
		QRMatrix::M_FORMAT          => [255, 255, 255],
31
		QRMatrix::M_VERSION         => [255, 255, 255],
32
		QRMatrix::M_QUIETZONE       => [255, 255, 255],
33
		QRMatrix::M_TEST            => [255, 255, 255],
34
		// dark
35
		QRMatrix::M_DARKMODULE << 8 => [0, 0, 0],
36
		QRMatrix::M_DATA << 8       => [0, 0, 0],
37
		QRMatrix::M_FINDER << 8     => [0, 0, 0],
38
		QRMatrix::M_ALIGNMENT << 8  => [0, 0, 0],
39
		QRMatrix::M_TIMING << 8     => [0, 0, 0],
40
		QRMatrix::M_FORMAT << 8     => [0, 0, 0],
41
		QRMatrix::M_VERSION << 8    => [0, 0, 0],
42
		QRMatrix::M_TEST << 8       => [0, 0, 0],
43
	];
44
45
	/**
46
	 * @return string
47
	 */
48
	public function dump():string{
49
		$scale        = $this->options->scale;
50
		$length       = $this->moduleCount * $scale;
51
		$image        = imagecreatetruecolor($length, $length);
52
		$background   = imagecolorallocate($image, ...$this->options->imageTransparencyBG);
53
		$moduleValues = is_array($this->options->moduleValues[QRMatrix::M_DATA])
54
			? $this->options->moduleValues // @codeCoverageIgnore
55
			: $this->moduleValues;
56
57
		if((bool)$this->options->imageTransparent && in_array($this->options->outputType, [QRCode::OUTPUT_IMAGE_PNG, QRCode::OUTPUT_IMAGE_GIF,], true)){
58
			imagecolortransparent($image, $background);
59
		}
60
61
		imagefilledrectangle($image, 0, 0, $length, $length, $background);
62
63
		foreach($this->matrix->matrix() as $y => $row){
64
			foreach($row as $x => $pixel){
65
				$color = imagecolorallocate($image, ...$moduleValues[$pixel]);
66
67
				imagefilledrectangle($image, $x * $scale, $y * $scale, ($x + 1) * $scale - 1, ($y + 1) * $scale - 1, $color);
68
			}
69
		}
70
71
		ob_start();
72
73
		call_user_func_array([$this, $this->options->outputType ?? QRCode::OUTPUT_IMAGE_PNG], [&$image]);
74
75
		$imageData = ob_get_contents();
76
		imagedestroy($image);
77
78
		ob_end_clean();
79
80
		if((bool)$this->options->imageBase64){
81
			$imageData = 'data:image/'.$this->options->outputType.';base64,'.base64_encode($imageData);
82
		}
83
84
		return $imageData;
85
	}
86
87
	/**
88
	 * @param $image
89
	 */
90 View Code Duplication
	protected function png(&$image){
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
91
		imagepng(
92
			$image,
93
			$this->options->cachefile,
94
			in_array($this->options->pngCompression, range(-1, 9), true)
95
				? $this->options->pngCompression
96
				: -1
97
		);
98
99
	}
100
101
	/**
102
	 * Jiff - like... JitHub!
103
	 *
104
	 * @param $image
105
	 */
106
	protected function gif(&$image){
107
		imagegif($image, $this->options->cachefile);
108
	}
109
110
	/**
111
	 * @param $image
112
	 */
113 View Code Duplication
	protected function jpg(&$image){
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
114
		imagejpeg(
115
			$image,
116
			$this->options->cachefile,
117
			in_array($this->options->jpegQuality, range(0, 100), true)
118
				? $this->options->jpegQuality
119
				: 85
120
		);
121
	}
122
123
}
124