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: |
|
|
|
|
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* */ |
|
|
|
|
93
|
|
|
imagegif( |
94
|
|
|
$image, |
95
|
|
|
$this->options->cachefile |
96
|
|
|
); |
97
|
|
|
break; |
98
|
|
|
case QRCode::OUTPUT_IMAGE_PNG: |
99
|
|
View Code Duplication |
default: |
|
|
|
|
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
|
|
|
if(false === @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)){ |
170
|
|
|
throw new QRCodeOutputException('Could not write to cache file.'); |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
if((bool)$this->options->base64){ |
175
|
|
|
$imageData = 'data:image/svg+xml;base64,'.base64_encode($imageData); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
return $imageData; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
} |
182
|
|
|
|
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.