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
|
|
|
protected $defaultMode = QRCode::OUTPUT_MARKUP_SVG; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @return string|bool |
26
|
|
|
*/ |
27
|
|
|
protected function html(){ |
28
|
|
|
$html = ''; |
29
|
|
|
|
30
|
|
|
foreach($this->matrix->matrix() as $row){ |
31
|
|
|
$html .= '<div>'; |
32
|
|
|
|
33
|
|
|
foreach($row as $pixel){ |
34
|
|
|
$html .= '<span style="background: '.($this->options->moduleValues[$pixel] ?: 'lightgrey').';"></span>'; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
$html .= '</div>'.$this->options->eol; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
if($this->options->cachefile){ |
41
|
|
|
return '<!DOCTYPE html><head><meta charset="UTF-8"></head><body>'.$this->options->eol.$html.'</body>'; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
return $html; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @link https://github.com/codemasher/php-qrcode/pull/5 |
49
|
|
|
* |
50
|
|
|
* @return string|bool |
51
|
|
|
*/ |
52
|
|
|
protected function svg(){ |
53
|
|
|
$scale = $this->options->scale; |
54
|
|
|
$length = $this->moduleCount * $scale; |
55
|
|
|
$matrix = $this->matrix->matrix(); |
56
|
|
|
|
57
|
|
|
$svg = '<svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="'.$length.'px" height="'.$length.'px">' |
58
|
|
|
.$this->options->eol |
59
|
|
|
.'<defs>'.$this->options->svgDefs.'</defs>' |
60
|
|
|
.$this->options->eol; |
61
|
|
|
|
62
|
|
|
foreach($this->options->moduleValues as $M_TYPE => $value){ |
63
|
|
|
|
64
|
|
|
// fallback |
65
|
|
|
if(is_bool($value)){ |
66
|
|
|
$value = $value ? '#000' : '#fff'; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
$path = ''; |
70
|
|
|
|
71
|
|
|
foreach($matrix as $y => $row){ |
72
|
|
|
//we'll combine active blocks within a single row as a lightweight compression technique |
73
|
|
|
$start = null; |
74
|
|
|
$count = 0; |
75
|
|
|
|
76
|
|
|
foreach($row as $x => $module){ |
77
|
|
|
|
78
|
|
|
if($module === $M_TYPE){ |
79
|
|
|
$count++; |
80
|
|
|
|
81
|
|
|
if($start === null){ |
82
|
|
|
$start = $x * $scale; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
if($row[$x + 1] ?? false){ |
86
|
|
|
continue; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
if($count > 0){ |
91
|
|
|
$len = $count * $scale; |
92
|
|
|
$path .= 'M' .$start. ' ' .($y * $scale). ' h'.$len.' v'.$scale.' h-'.$len.'Z '; |
93
|
|
|
|
94
|
|
|
// reset count |
95
|
|
|
$count = 0; |
96
|
|
|
$start = null; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
if(!empty($path)){ |
104
|
|
|
$svg .= '<path class="qr-'.$M_TYPE.' '.$this->options->cssClass.'" stroke="transparent" fill="'.$value.'" fill-opacity="'.$this->options->svgOpacity.'" d="'.$path.'" />'; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
// close svg |
110
|
|
|
$svg .= '</svg>'.$this->options->eol; |
111
|
|
|
|
112
|
|
|
// if saving to file, append the correct headers |
113
|
|
|
if($this->options->cachefile){ |
114
|
|
|
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; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
return $svg; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
} |
121
|
|
|
|