|
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
|
|
|
/** |
|
18
|
|
|
* Converts the matrix into markup types: HTML, SVG, ... |
|
19
|
|
|
*/ |
|
20
|
|
|
class QRMarkup extends QROutputAbstract{ |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @return string |
|
|
|
|
|
|
24
|
|
|
*/ |
|
25
|
|
View Code Duplication |
public function dump(){ |
|
|
|
|
|
|
26
|
|
|
|
|
27
|
|
|
if($this->options->cachefile !== null && !is_writable(dirname($this->options->cachefile))){ |
|
28
|
|
|
throw new QRCodeOutputException('Could not write data to cache file: '.$this->options->cachefile); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
$data = $this->options->outputType === QRCode::OUTPUT_MARKUP_HTML |
|
32
|
|
|
? $this->toHTML() |
|
33
|
|
|
: $this->toSVG(); |
|
34
|
|
|
|
|
35
|
|
|
if($this->options->cachefile !== null){ |
|
36
|
|
|
$this->saveToFile($data); |
|
|
|
|
|
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
return $data; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @return string|bool |
|
44
|
|
|
*/ |
|
45
|
|
|
protected function toHTML(){ |
|
46
|
|
|
$html = ''; |
|
47
|
|
|
|
|
48
|
|
|
foreach($this->matrix->matrix() as $row){ |
|
49
|
|
|
$html .= '<div>'; |
|
50
|
|
|
|
|
51
|
|
|
foreach($row as $pixel){ |
|
52
|
|
|
$html .= '<span style="background: '.($this->options->moduleValues[$pixel] ?: 'lightgrey').';"></span>'; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
$html .= '</div>'.$this->options->eol; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
if($this->options->cachefile){ |
|
59
|
|
|
return '<!DOCTYPE html><head><meta charset="UTF-8"></head><body>'.$this->options->eol.$html.'</body>'; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
return $html; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @link https://github.com/codemasher/php-qrcode/pull/5 |
|
67
|
|
|
* |
|
68
|
|
|
* @return string|bool |
|
69
|
|
|
*/ |
|
70
|
|
|
protected function toSVG(){ |
|
71
|
|
|
$length = ($this->moduleCount + ($this->options->addQuietzone ? 8 : 0)) * $this->options->scale; |
|
72
|
|
|
|
|
73
|
|
|
// svg header |
|
74
|
|
|
$svg = '<svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="'.$length.'" height="'.$length.'" viewBox="0 0 '.$length.' '.$length.'">'.$this->options->eol. |
|
75
|
|
|
'<defs><style>rect{shape-rendering:crispEdges}</style></defs>'.$this->options->eol; |
|
76
|
|
|
|
|
77
|
|
|
// @todo: optimize -> see https://github.com/alexeyten/qr-image/blob/master/lib/vector.js |
|
78
|
|
|
foreach($this->options->moduleValues as $key => $value){ |
|
79
|
|
|
|
|
80
|
|
|
// fallback |
|
81
|
|
|
if(is_bool($value)){ |
|
82
|
|
|
$value = $value ? '#000' : '#fff'; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
// svg body |
|
86
|
|
|
foreach($this->matrix->matrix() as $y => $row){ |
|
87
|
|
|
//we'll combine active blocks within a single row as a lightweight compression technique |
|
88
|
|
|
$from = -1; |
|
89
|
|
|
$count = 0; |
|
90
|
|
|
|
|
91
|
|
|
foreach($row as $x => $pixel){ |
|
92
|
|
|
if($pixel === $key){ |
|
93
|
|
|
$count++; |
|
94
|
|
|
|
|
95
|
|
|
if($from < 0){ |
|
96
|
|
|
$from = $x; |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
elseif($from >= 0){ |
|
100
|
|
|
$svg .= '<rect x="'.($from * $this->options->scale).'" y="'.($y * $this->options->scale) |
|
101
|
|
|
.'" width="'.($this->options->scale * $count).'" height="'.$this->options->scale.'" fill="'.$value.'"' |
|
102
|
|
|
.(trim($this->options->cssClass) !== '' ? ' class="'.$this->options->cssClass.'"' :'').' />' |
|
103
|
|
|
.$this->options->eol; |
|
104
|
|
|
|
|
105
|
|
|
// reset count |
|
106
|
|
|
$from = -1; |
|
107
|
|
|
$count = 0; |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
// close off the row, if applicable |
|
112
|
|
|
if($from >= 0){ |
|
113
|
|
|
$svg .= '<rect x="'.($from * $this->options->scale).'" y="'.($y * $this->options->scale) |
|
114
|
|
|
.'" width="'.($this->options->scale * $count).'" height="'.$this->options->scale.'" class="'.$this->options->cssClass.'" fill="'.$value.'" />'.$this->options->eol; |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
// close svg |
|
120
|
|
|
$svg .= '</svg>'.$this->options->eol; |
|
121
|
|
|
|
|
122
|
|
|
// if saving to file, append the correct headers |
|
123
|
|
|
if($this->options->cachefile){ |
|
124
|
|
|
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; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
return $svg; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
} |
|
131
|
|
|
|
This check compares the return type specified in the
@returnannotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.