1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Class QRMarkup |
4
|
|
|
* |
5
|
|
|
* @filesource QRMarkup.php |
6
|
|
|
* @created 17.12.2016 |
7
|
|
|
* @package chillerlan\QRCode\Output |
8
|
|
|
* @author Smiley <[email protected]> |
9
|
|
|
* @copyright 2016 Smiley |
10
|
|
|
* @license MIT |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace chillerlan\QRCode\Output; |
14
|
|
|
|
15
|
|
|
use chillerlan\QRCode\QRCode; |
16
|
|
|
|
17
|
|
|
use function is_string, sprintf, strip_tags, trim; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Converts the matrix into markup types: HTML, SVG, ... |
21
|
|
|
*/ |
22
|
|
|
class QRMarkup extends QROutputAbstract{ |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
protected $defaultMode = QRCode::OUTPUT_MARKUP_SVG; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @see \sprintf() |
31
|
|
|
* |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
protected $svgHeader = '<svg xmlns="http://www.w3.org/2000/svg" version="1.1" class="qr-svg %1$s" style="width: 100%%; height: auto;" viewBox="0 0 %2$d %2$d">'; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @inheritDoc |
38
|
|
|
*/ |
39
|
|
View Code Duplication |
protected function setModuleValues():void{ |
|
|
|
|
40
|
|
|
|
41
|
|
|
foreach($this::DEFAULT_MODULE_VALUES as $M_TYPE => $defaultValue){ |
42
|
|
|
$v = $this->options->moduleValues[$M_TYPE] ?? null; |
43
|
|
|
|
44
|
|
|
if(!is_string($v)){ |
45
|
|
|
$this->moduleValues[$M_TYPE] = $defaultValue |
46
|
|
|
? $this->options->markupDark |
47
|
|
|
: $this->options->markupLight; |
48
|
|
|
} |
49
|
|
|
else{ |
50
|
|
|
$this->moduleValues[$M_TYPE] = trim(strip_tags($v), '\'"'); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @return string |
59
|
|
|
*/ |
60
|
|
|
protected function html():string{ |
61
|
|
|
$html = '<div class="'.$this->options->cssClass.'">'.$this->options->eol; |
62
|
|
|
|
63
|
|
|
foreach($this->matrix->matrix() as $row){ |
64
|
|
|
$html .= '<div>'; |
65
|
|
|
|
66
|
|
|
foreach($row as $M_TYPE){ |
67
|
|
|
$html .= '<span style="background: '.$this->moduleValues[$M_TYPE].';"></span>'; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
$html .= '</div>'.$this->options->eol; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
$html .= '</div>'.$this->options->eol; |
74
|
|
|
|
75
|
|
|
if($this->options->cachefile){ |
76
|
|
|
return '<!DOCTYPE html><head><meta charset="UTF-8"></head><body>'.$this->options->eol.$html.'</body>'; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
return $html; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @link https://github.com/codemasher/php-qrcode/pull/5 |
84
|
|
|
* |
85
|
|
|
* @return string |
86
|
|
|
*/ |
87
|
|
|
protected function svg():string{ |
88
|
|
|
$matrix = $this->matrix->matrix(); |
89
|
|
|
|
90
|
|
|
$svg = sprintf($this->svgHeader, $this->options->cssClass, $this->options->svgViewBoxSize ?? $this->moduleCount) |
91
|
|
|
.$this->options->eol |
92
|
|
|
.'<defs>'.$this->options->svgDefs.'</defs>' |
93
|
|
|
.$this->options->eol; |
94
|
|
|
|
95
|
|
|
foreach($this->moduleValues as $M_TYPE => $value){ |
96
|
|
|
$path = ''; |
97
|
|
|
|
98
|
|
|
foreach($matrix as $y => $row){ |
99
|
|
|
//we'll combine active blocks within a single row as a lightweight compression technique |
100
|
|
|
$start = null; |
101
|
|
|
$count = 0; |
102
|
|
|
|
103
|
|
|
foreach($row as $x => $module){ |
104
|
|
|
|
105
|
|
|
if($module === $M_TYPE){ |
106
|
|
|
$count++; |
107
|
|
|
|
108
|
|
|
if($start === null){ |
109
|
|
|
$start = $x; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
if(isset($row[$x + 1])){ |
113
|
|
|
continue; |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
if($count > 0){ |
118
|
|
|
$len = $count; |
119
|
|
|
$path .= sprintf('M%s %s h%s v1 h-%sZ ', $start, $y, $len, $len); |
120
|
|
|
|
121
|
|
|
// reset count |
122
|
|
|
$count = 0; |
123
|
|
|
$start = null; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
if(!empty($path)){ |
131
|
|
|
$svg .= sprintf('<path class="qr-%s %s" stroke="transparent" fill="%s" fill-opacity="%s" d="%s" />', $M_TYPE, $this->options->cssClass, $value, $this->options->svgOpacity, $path); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
// close svg |
137
|
|
|
$svg .= '</svg>'.$this->options->eol; |
138
|
|
|
|
139
|
|
|
// if saving to file, append the correct headers |
140
|
|
|
if($this->options->cachefile){ |
141
|
|
|
return '<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">'.$this->options->eol.$svg; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
if($this->options->imageBase64){ |
145
|
|
|
$svg = sprintf('data:image/svg+xml;base64,%s', base64_encode($svg)); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
return $svg; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
} |
152
|
|
|
|
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.