Completed
Push — master ( 102ef4...1ff5c6 )
by smiley
02:47
created

QRImage::__construct()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 8
nc 4
nop 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\QRCode;
16
17
/**
18
 *
19
 */
20
class QRImage extends QROutputAbstract{
21
22
	/**
23
	 * @var string
24
	 */
25
	protected $optionsInterface = QRImageOptions::class;
26
27
	/**
28
	 * @var array
29
	 */
30
	protected $types = [
31
		QRCode::OUTPUT_IMAGE_GIF,
32
		QRCode::OUTPUT_IMAGE_JPG,
33
		QRCode::OUTPUT_IMAGE_PNG,
34
	];
35
36
	/**
37
	 * @return string
38
	 */
39
	public function dump(){
40
		// clamp input (@todo: determine sane values!)
41
		$this->options->pixelSize = max(1, min(25, (int)$this->options->pixelSize));
42
		$this->options->marginSize = max(0, min(25, (int)$this->options->marginSize));
43
44
		foreach(['fgRed', 'fgGreen', 'fgBlue', 'bgRed', 'bgGreen', 'bgBlue'] as $val){
45
			$this->options->{$val} = max(0, min(255, (int)$this->options->{$val}));
46
		}
47
48
		$length     = $this->pixelCount * $this->options->pixelSize + $this->options->marginSize * 2;
49
		$image      = imagecreatetruecolor($length, $length);
50
		$foreground = imagecolorallocate($image, $this->options->fgRed, $this->options->fgGreen, $this->options->fgBlue);
51
		$background = imagecolorallocate($image, $this->options->bgRed, $this->options->bgGreen, $this->options->bgBlue);
52
53
		if((bool)$this->options->transparent && $this->options->type !== QRCode::OUTPUT_IMAGE_JPG){
54
			imagecolortransparent($image, $background);
55
		}
56
57
		imagefilledrectangle($image, 0, 0, $length, $length, $background);
58
59
		foreach($this->matrix as $r => $row){
60
			foreach($row as $c => $pixel){
61
				if($pixel){
62
					imagefilledrectangle($image,
63
						$this->options->marginSize +  $c      * $this->options->pixelSize,
64
						$this->options->marginSize +  $r      * $this->options->pixelSize,
65
						$this->options->marginSize + ($c + 1) * $this->options->pixelSize - 1,
66
						$this->options->marginSize + ($r + 1) * $this->options->pixelSize - 1,
67
						$foreground);
68
				}
69
			}
70
		}
71
72
		ob_start();
73
74
		switch($this->options->type){
75 View Code Duplication
			case QRCode::OUTPUT_IMAGE_JPG:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
76
				imagejpeg(
77
					$image,
78
					$this->options->cachefile,
79
					in_array($this->options->jpegQuality, range(0, 100), true)
80
						? $this->options->jpegQuality
81
						: 85
82
				);
83
				break;
84
			case QRCode::OUTPUT_IMAGE_GIF: /** Actually, it's pronounced "DJIFF". *hides* */
1 ignored issue
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
85
				imagegif(
86
					$image,
87
					$this->options->cachefile
88
				);
89
				break;
90
			case QRCode::OUTPUT_IMAGE_PNG:
91 View Code Duplication
			default:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
92
				imagepng(
93
					$image,
94
					$this->options->cachefile,
95
					in_array($this->options->pngCompression, range(-1, 9), true)
96
						? $this->options->pngCompression
97
						: -1
98
				);
99
		}
100
101
		$imageData = ob_get_contents();
102
		imagedestroy($image);
103
		ob_end_clean();
104
105
		if((bool)$this->options->base64){
106
			$imageData = 'data:image/'.$this->options->type.';base64,'.base64_encode($imageData);
107
		}
108
109
		return $imageData;
110
	}
111
112
}
113