Completed
Pull Request — master (#5)
by
unknown
04:28
created

QRImage::toSVG()   C

Complexity

Conditions 10
Paths 88

Size

Total Lines 55
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 55
rs 6.8372
c 0
b 0
f 0
cc 10
eloc 30
nc 88
nop 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 \chillerlan\QRCode\Output\QRImageOptions $outputOptions
24
	 */
25
	protected $options;
26
27
	/**
28
	 * @var \chillerlan\QRCode\Output\QRImageOptions $outputOptions
29
	 */
30
	public function __construct(QRImageOptions $outputOptions = null){
31
		$this->options = $outputOptions;
32
33
		if(!$this->options instanceof QRImageOptions){
34
			$this->options = new QRImageOptions;
35
		}
36
37
		// clamp input (determine sane values!)
38
		$this->options->pixelSize = max(1, min(25, (int)$this->options->pixelSize));
39
		$this->options->marginSize = max(0, min(25, (int)$this->options->marginSize));
40
41
		foreach(['fgRed', 'fgGreen', 'fgBlue', 'bgRed', 'bgGreen', 'bgBlue'] as $val){
42
			$this->options->{$val} = max(0, min(255, (int)$this->options->{$val}));
43
		}
44
45
	}
46
47
	/**
48
	 * @return string
49
	 */
50
	public function dump(){
51
		// svg doesn't need all this GD business
52
		if($this->options->type === QRCode::OUTPUT_IMAGE_SVG){
53
			return $this->toSVG();
54
		}
55
56
		$length     = $this->pixelCount * $this->options->pixelSize + $this->options->marginSize * 2;
57
		$image      = imagecreatetruecolor($length, $length);
58
		$foreground = imagecolorallocate($image, $this->options->fgRed, $this->options->fgGreen, $this->options->fgBlue);
59
		$background = imagecolorallocate($image, $this->options->bgRed, $this->options->bgGreen, $this->options->bgBlue);
60
61
		if((bool)$this->options->transparent && $this->options->type !== QRCode::OUTPUT_IMAGE_JPG){
62
			imagecolortransparent($image, $background);
63
		}
64
65
		imagefilledrectangle($image, 0, 0, $length, $length, $background);
66
67
		foreach($this->matrix as $r => $row){
68
			foreach($row as $c => $pixel){
69
				if($pixel){
70
					imagefilledrectangle($image,
71
						$this->options->marginSize +  $c      * $this->options->pixelSize,
72
						$this->options->marginSize +  $r      * $this->options->pixelSize,
73
						$this->options->marginSize + ($c + 1) * $this->options->pixelSize - 1,
74
						$this->options->marginSize + ($r + 1) * $this->options->pixelSize - 1,
75
						$foreground);
76
				}
77
			}
78
		}
79
80
		ob_start();
81
82
		switch($this->options->type){
83 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...
84
				imagejpeg(
85
					$image,
86
					$this->options->cachefile,
87
					in_array($this->options->jpegQuality, range(0, 100), true)
88
						? $this->options->jpegQuality
89
						: 85
90
				);
91
				break;
92
			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...
93
				imagegif(
94
					$image,
95
					$this->options->cachefile
96
				);
97
				break;
98
			case QRCode::OUTPUT_IMAGE_PNG:
99 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...
100
				imagepng(
101
					$image,
102
					$this->options->cachefile,
103
					in_array($this->options->pngCompression, range(-1, 9), true)
104
						? $this->options->pngCompression
105
						: -1
106
				);
107
		}
108
109
		$imageData = ob_get_contents();
110
		imagedestroy($image);
111
		ob_end_clean();
112
113
		if((bool)$this->options->base64){
114
			$imageData = 'data:image/'.$this->options->type.';base64,'.base64_encode($imageData);
115
		}
116
117
		return $imageData;
118
	}
119
120
	/**
121
	 * @return string
122
	 */
123
	protected function toSVG(){
124
		$length     = $this->pixelCount * $this->options->pixelSize + $this->options->marginSize * 2;
125
		$class      = 'f' . hash('crc32', microtime(true));
126
		$foreground = 'rgb(' . $this->options->fgRed . ',' . $this->options->fgGreen . ',' . $this->options->fgBlue . ')';
127
		$background = (bool)$this->options->transparent
128
			? 'transparent'
129
			: 'rgb(' . $this->options->bgRed . ',' . $this->options->bgGreen . ',' . $this->options->bgBlue . ')';
130
131
		ob_start();
132
133
		// svg header
134
		echo '<svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="' . $length . '" height="' . $length . '" viewBox="0 0 ' . $length . ' ' . $length . '" style="background-color:' . $background . '"><defs><style>.' . $class . '{fill:' . $foreground . '} rect{shape-rendering:crispEdges}</style></defs>';
135
136
		// svg body
137
		foreach($this->matrix AS $r=>$row){
138
			//we'll combine active blocks within a single row as a lightweight compression technique
139
			$from = -1;
140
			$count = 0;
141
142
			foreach($row AS $c=>$pixel){
143
				if($pixel){
144
					$count++;
145
					if($from < 0)
146
						$from = $c;
147
				}
148
				else if($from >= 0){
149
					echo '<rect x="' . ($from * $this->options->pixelSize + $this->options->marginSize) . '" y="' . ($r * $this->options->pixelSize + $this->options->marginSize) . '" width="' . ($this->options->pixelSize * $count) . '" height="' . $this->options->pixelSize . '" class="' . $class . '" />';
150
151
					// reset count
152
					$from = -1;
153
					$count = 0;
154
				}
155
			}
156
157
			// close off the row, if applicable
158
			if($from >= 0){
159
				echo '<rect x="' . ($from * $this->options->pixelSize + $this->options->marginSize) . '" y="' . ($r * $this->options->pixelSize + $this->options->marginSize) . '" width="' . ($this->options->pixelSize * $count) . '" height="' . $this->options->pixelSize . '" class="' . $class . '" />';
160
			}
161
		}
162
163
		// close svg
164
		echo '</svg>';
165
		$imageData = ob_get_clean();
166
167
		// if saving to file, append the correct headers
168
		if($this->options->cachefile){
169
			@file_put_contents($this->options->cachefile, '<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">' . "\n" . $imageData);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
170
		}
171
172
		if((bool)$this->options->base64){
173
			$imageData = 'data:image/svg+xml;base64,'.base64_encode($imageData);
174
		}
175
176
		return $imageData;
177
	}
178
179
}
180