1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Class QROutputAbstract |
4
|
|
|
* |
5
|
|
|
* @filesource QROutputAbstract.php |
6
|
|
|
* @created 09.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\{Data\QRMatrix, QRCode}; |
16
|
|
|
use chillerlan\Settings\SettingsContainerInterface; |
17
|
|
|
use function base64_encode, call_user_func_array, dirname, file_put_contents, get_called_class, in_array, is_writable, sprintf; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* common output abstract |
21
|
|
|
*/ |
22
|
|
|
abstract class QROutputAbstract implements QROutputInterface{ |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* the current size of the QR matrix |
26
|
|
|
* |
27
|
|
|
* @see \chillerlan\QRCode\Data\QRMatrix::size() |
28
|
|
|
*/ |
29
|
|
|
protected int $moduleCount; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* the current output mode |
33
|
|
|
* |
34
|
|
|
* @see \chillerlan\QRCode\QROptions::$outputType |
35
|
|
|
*/ |
36
|
|
|
protected string $outputMode; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* the default output mode of the current output module |
40
|
|
|
*/ |
41
|
|
|
protected string $defaultMode; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* the current scaling for a QR pixel |
45
|
|
|
* |
46
|
|
|
* @see \chillerlan\QRCode\QROptions::$scale |
47
|
|
|
*/ |
48
|
|
|
protected int $scale; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* the side length of the QR image (modules * scale) |
52
|
|
|
*/ |
53
|
|
|
protected int $length; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* an (optional) array of color values for the several QR matrix parts |
57
|
|
|
*/ |
58
|
|
|
protected array $moduleValues; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* the (filled) data matrix object |
62
|
|
|
*/ |
63
|
|
|
protected QRMatrix $matrix; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @var \chillerlan\Settings\SettingsContainerInterface|\chillerlan\QRCode\QROptions |
67
|
|
|
*/ |
68
|
|
|
protected SettingsContainerInterface $options; |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* QROutputAbstract constructor. |
72
|
|
|
*/ |
73
|
|
|
public function __construct(SettingsContainerInterface $options, QRMatrix $matrix){ |
74
|
|
|
$this->options = $options; |
75
|
|
|
$this->matrix = $matrix; |
76
|
|
|
$this->moduleCount = $this->matrix->size(); |
77
|
|
|
$this->scale = $this->options->scale; |
78
|
|
|
$this->length = $this->moduleCount * $this->scale; |
79
|
|
|
|
80
|
|
|
$class = get_called_class(); |
81
|
|
|
|
82
|
|
|
if(isset(QRCode::OUTPUT_MODES[$class]) && in_array($this->options->outputType, QRCode::OUTPUT_MODES[$class])){ |
83
|
|
|
$this->outputMode = $this->options->outputType; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
$this->setModuleValues(); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Sets the initial module values (clean-up & defaults) |
91
|
|
|
*/ |
92
|
|
|
abstract protected function setModuleValues():void; |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Returns a base64 data URI for the given string and mime type |
96
|
|
|
*/ |
97
|
|
|
protected function base64encode(string $data, string $mime):string{ |
98
|
|
|
return sprintf('data:%s;base64,%s', $mime, base64_encode($data)); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* saves the qr data to a file |
103
|
|
|
* |
104
|
|
|
* @see file_put_contents() |
105
|
|
|
* @see \chillerlan\QRCode\QROptions::cachefile |
106
|
|
|
* |
107
|
|
|
* @throws \chillerlan\QRCode\Output\QRCodeOutputException |
108
|
|
|
*/ |
109
|
|
|
protected function saveToFile(string $data, string $file):bool{ |
110
|
|
|
|
111
|
|
|
if(!is_writable(dirname($file))){ |
112
|
|
|
throw new QRCodeOutputException(sprintf('Could not write data to cache file: %s', $file)); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
return (bool)file_put_contents($file, $data); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @inheritDoc |
120
|
|
|
*/ |
121
|
|
|
public function dump(string $file = null){ |
122
|
|
|
$file ??= $this->options->cachefile; |
123
|
|
|
|
124
|
|
|
// call the built-in output method with the optional file path as parameter |
125
|
|
|
// to make the called method aware if a cache file was given |
126
|
|
|
$data = call_user_func_array([$this, $this->outputMode ?? $this->defaultMode], [$file]); |
127
|
|
|
|
128
|
|
|
if($file !== null){ |
129
|
|
|
$this->saveToFile($data, $file); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
return $data; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
} |
136
|
|
|
|