|
2 | 2 |
|
3 | 3 | namespace SimpleSoftwareIO\QrCode; |
4 | 4 |
|
5 | | -use BaconQrCode; |
6 | 5 | use BaconQrCode\Common\ErrorCorrectionLevel; |
7 | 6 | use BaconQrCode\Encoder\Encoder; |
8 | | -use BaconQrCode\Renderer\Color\Rgb; |
9 | | -use BaconQrCode\Renderer\Image\Eps; |
10 | | -use BaconQrCode\Renderer\Image\Png; |
11 | | -use BaconQrCode\Renderer\Image\RendererInterface; |
12 | | -use BaconQrCode\Renderer\Image\Svg; |
| 7 | +use BaconQrCode\Renderer\Eye\EyeInterface; |
| 8 | +use BaconQrCode\Renderer\Eye\ModuleEye; |
| 9 | +use BaconQrCode\Renderer\Eye\SimpleCircleEye; |
| 10 | +use BaconQrCode\Renderer\Eye\SquareEye; |
| 11 | +use BaconQrCode\Renderer\Image\SvgImageBackEnd; |
| 12 | +use BaconQrCode\Renderer\ImageRenderer; |
| 13 | +use BaconQrCode\Renderer\Module\DotsModule; |
| 14 | +use BaconQrCode\Renderer\Module\ModuleInterface; |
| 15 | +use BaconQrCode\Renderer\Module\RoundnessModule; |
| 16 | +use BaconQrCode\Renderer\Module\SquareModule; |
| 17 | +use BaconQrCode\Renderer\RendererInterface; |
| 18 | +use BaconQrCode\Renderer\RendererStyle\RendererStyle; |
13 | 19 | use BaconQrCode\Writer; |
| 20 | +use InvalidArgumentException; |
14 | 21 |
|
15 | 22 | class Generator |
16 | 23 | { |
17 | | - /** |
18 | | - * Holds the BaconQrCode Writer Object. |
19 | | - * |
20 | | - * @var \BaconQrCode\Writer |
21 | | - */ |
22 | | - protected $writer; |
23 | | - |
24 | | - /** |
25 | | - * Holds the QrCode error correction levels. This is stored by using the BaconQrCode ErrorCorrectionLevel class constants. |
26 | | - * |
27 | | - * @var \BaconQrCode\Common\ErrorCorrectionLevel |
28 | | - */ |
29 | | - protected $errorCorrection = ErrorCorrectionLevel::L; |
30 | | - |
31 | | - /** |
32 | | - * Holds the Encoder mode to encode a QrCode. |
33 | | - * |
34 | | - * @var string |
35 | | - */ |
| 24 | + protected $pixels = 100; |
| 25 | + |
| 26 | + protected $margin = 0; |
| 27 | + |
| 28 | + protected $errorCorrection = null; |
| 29 | + |
36 | 30 | protected $encoding = Encoder::DEFAULT_BYTE_MODE_ECODING; |
37 | 31 |
|
38 | | - /** |
39 | | - * Holds an image string that will be merged with the QrCode. |
40 | | - * |
41 | | - * @var null|string |
42 | | - */ |
43 | | - protected $imageMerge = null; |
44 | | - |
45 | | - /** |
46 | | - * The percentage that a merged image should take over the source image. |
47 | | - * |
48 | | - * @var float |
49 | | - */ |
50 | | - protected $imagePercentage = .2; |
51 | | - |
52 | | - /** |
53 | | - * BaconQrCodeGenerator constructor. |
54 | | - * |
55 | | - * @param Writer|null $writer |
56 | | - * @param RendererInterface|null $format |
57 | | - */ |
58 | | - public function __construct(Writer $writer = null, RendererInterface $format = null) |
59 | | - { |
60 | | - $format = $format ?: new Svg(); |
61 | | - $this->writer = $writer ?: new Writer($format); |
62 | | - } |
| 32 | + protected $style = 'square'; |
63 | 33 |
|
64 | | - /** |
65 | | - * Generates a QrCode. |
66 | | - * |
67 | | - * @param string $text The text to be converted into a QrCode |
68 | | - * @param null|string $filename The filename and path to save the QrCode file |
69 | | - * |
70 | | - * @return string|void Returns a QrCode string depending on the format, or saves to a file. |
71 | | - */ |
72 | | - public function generate($text, $filename = null) |
73 | | - { |
74 | | - $qrCode = $this->writer->writeString($text, $this->encoding, $this->errorCorrection); |
| 34 | + protected $styleSize = null; |
75 | 35 |
|
76 | | - if ($this->imageMerge !== null) { |
77 | | - $merger = new ImageMerge(new Image($qrCode), new Image($this->imageMerge)); |
78 | | - $qrCode = $merger->merge($this->imagePercentage); |
79 | | - } |
| 36 | + protected $eyeStyle = null; |
80 | 37 |
|
81 | | - if ($filename === null) { |
82 | | - return $qrCode; |
83 | | - } |
| 38 | + public function generate(string $text, string $filename = null) |
| 39 | + { |
| 40 | + $render = $this->getRenderer(); |
84 | 41 |
|
85 | | - return file_put_contents($filename, $qrCode); |
| 42 | + return $this->getWriter($render)->writeString($text, $this->encoding, $this->errorCorrection); |
86 | 43 | } |
87 | 44 |
|
88 | | - /** |
89 | | - * Merges an image with the center of the QrCode. |
90 | | - * |
91 | | - * @param $filepath string The filepath to an image |
92 | | - * @param $percentage float The amount that the merged image should be placed over the qrcode. |
93 | | - * @param $absolute boolean Whether to use an absolute filepath or not. |
94 | | - * |
95 | | - * @return $this |
96 | | - */ |
97 | | - public function merge($filepath, $percentage = .2, $absolute = false) |
| 45 | + public function size(int $pixels): self |
98 | 46 | { |
99 | | - if (function_exists('base_path') && !$absolute) { |
100 | | - $filepath = base_path().$filepath; |
101 | | - } |
102 | | - |
103 | | - $this->imageMerge = file_get_contents($filepath); |
104 | | - $this->imagePercentage = $percentage; |
| 47 | + $this->pixels = $pixels; |
105 | 48 |
|
106 | 49 | return $this; |
107 | 50 | } |
108 | 51 |
|
109 | | - /** |
110 | | - * Merges an image string with the center of the QrCode, does not check for correct format. |
111 | | - * |
112 | | - * @param $content string The string contents of an image. |
113 | | - * @param $percentage float The amount that the merged image should be placed over the qrcode. |
114 | | - * |
115 | | - * @return $this |
116 | | - */ |
117 | | - public function mergeString($content, $percentage = .2) |
| 52 | + public function format(): self |
118 | 53 | { |
119 | | - $this->imageMerge = $content; |
120 | | - $this->imagePercentage = $percentage; |
121 | | - |
122 | | - return $this; |
| 54 | + //png, eps, svg, jpg |
| 55 | + // |
123 | 56 | } |
124 | 57 |
|
125 | | - /** |
126 | | - * Switches the format of the outputted QrCode or defaults to SVG. |
127 | | - * |
128 | | - * @param string $format The desired format. |
129 | | - * |
130 | | - * @throws \InvalidArgumentException |
131 | | - * |
132 | | - * @return $this |
133 | | - */ |
134 | | - public function format($format) |
| 58 | + public function color($red, $green, $blue, $alpha): self |
135 | 59 | { |
136 | | - switch ($format) { |
137 | | - case 'png': |
138 | | - $this->writer->setRenderer(new Png()); |
139 | | - break; |
140 | | - case 'eps': |
141 | | - $this->writer->setRenderer(new Eps()); |
142 | | - break; |
143 | | - case 'svg': |
144 | | - $this->writer->setRenderer(new Svg()); |
145 | | - break; |
146 | | - default: |
147 | | - throw new \InvalidArgumentException('Invalid format provided.'); |
148 | | - } |
| 60 | + } |
149 | 61 |
|
150 | | - return $this; |
| 62 | + public function backgroundColor($red, $green, $blue, $alpha): self |
| 63 | + { |
151 | 64 | } |
152 | 65 |
|
153 | | - /** |
154 | | - * Changes the size of the QrCode. |
155 | | - * |
156 | | - * @param int $pixels The size of the QrCode in pixels |
157 | | - * |
158 | | - * @return $this |
159 | | - */ |
160 | | - public function size($pixels) |
| 66 | + public function eyeColor(array $eye1, array $eye2, array $eye3): self |
161 | 67 | { |
162 | | - $this->writer->getRenderer()->setHeight($pixels); |
163 | | - $this->writer->getRenderer()->setWidth($pixels); |
| 68 | + } |
164 | 69 |
|
165 | | - return $this; |
| 70 | + public function gradient(): self |
| 71 | + { |
166 | 72 | } |
167 | 73 |
|
168 | | - /** |
169 | | - * Changes the foreground color of a QrCode. |
170 | | - * |
171 | | - * @param int $red |
172 | | - * @param int $green |
173 | | - * @param int $blue |
174 | | - * |
175 | | - * @return $this |
176 | | - */ |
177 | | - public function color($red, $green, $blue) |
| 74 | + public function eye(string $style): self |
178 | 75 | { |
179 | | - $this->writer->getRenderer()->setForegroundColor(new Rgb($red, $green, $blue)); |
| 76 | + if (! in_array($style, ['square', 'circle'])) { |
| 77 | + throw new InvalidArgumentException("\$style must be square or circle. {$style} is not a valid eye style."); |
| 78 | + } |
| 79 | + |
| 80 | + $this->eyeStyle = $style; |
180 | 81 |
|
181 | 82 | return $this; |
182 | 83 | } |
183 | 84 |
|
184 | | - /** |
185 | | - * Changes the background color of a QrCode. |
186 | | - * |
187 | | - * @param int $red |
188 | | - * @param int $green |
189 | | - * @param int $blue |
190 | | - * |
191 | | - * @return $this |
192 | | - */ |
193 | | - public function backgroundColor($red, $green, $blue) |
| 85 | + public function style(string $style, float $size = 0.5): self |
194 | 86 | { |
195 | | - $this->writer->getRenderer()->setBackgroundColor(new Rgb($red, $green, $blue)); |
| 87 | + if (! in_array($style, ['square', 'dot', 'round'])) { |
| 88 | + throw new InvalidArgumentException("\$style must be square, dot, or round. {$style} is not a valid QrCode style."); |
| 89 | + } |
| 90 | + |
| 91 | + if ($size < 0 || $size >= 1) { |
| 92 | + throw new InvalidArgumentException("\$size must be between 0 and 1. {$size} is not valid."); |
| 93 | + } |
| 94 | + |
| 95 | + $this->style = $style; |
| 96 | + $this->styleSize = $size; |
196 | 97 |
|
197 | 98 | return $this; |
198 | 99 | } |
199 | 100 |
|
200 | | - /** |
201 | | - * Changes the error correction level of a QrCode. |
202 | | - * |
203 | | - * @param string $level Desired error correction level. L = 7% M = 15% Q = 25% H = 30% |
204 | | - * |
205 | | - * @return $this |
206 | | - */ |
207 | | - public function errorCorrection($level) |
| 101 | + public function encoding(string $encoding): self |
208 | 102 | { |
209 | | - $this->errorCorrection = constant("BaconQrCode\Common\ErrorCorrectionLevel::$level"); |
| 103 | + $this->encoding = strtoupper($encoding); |
210 | 104 |
|
211 | 105 | return $this; |
212 | 106 | } |
213 | 107 |
|
214 | | - /** |
215 | | - * Creates a margin around the QrCode. |
216 | | - * |
217 | | - * @param int $margin The desired margin in pixels around the QrCode |
218 | | - * |
219 | | - * @return $this |
220 | | - */ |
221 | | - public function margin($margin) |
| 108 | + public function errorCorrection(string $errorCorrection): self |
222 | 109 | { |
223 | | - $this->writer->getRenderer()->setMargin($margin); |
| 110 | + $errorCorrection = strtoupper($errorCorrection); |
| 111 | + $this->errorCorrection = ErrorCorrectionLevel::$errorCorrection(); |
224 | 112 |
|
225 | 113 | return $this; |
226 | 114 | } |
227 | 115 |
|
228 | | - /** |
229 | | - * Sets the Encoding mode. |
230 | | - * |
231 | | - * @param string $encoding |
232 | | - * |
233 | | - * @return $this |
234 | | - */ |
235 | | - public function encoding($encoding) |
| 116 | + public function margin(int $margin): self |
236 | 117 | { |
237 | | - $this->encoding = $encoding; |
| 118 | + $this->margin = $margin; |
238 | 119 |
|
239 | 120 | return $this; |
240 | 121 | } |
241 | 122 |
|
242 | | - /** |
243 | | - * Creates a new datatype object and then generates a QrCode. |
244 | | - * |
245 | | - * @param $method |
246 | | - * @param $arguments |
247 | | - */ |
248 | | - public function __call($method, $arguments) |
| 123 | + protected function getWriter(ImageRenderer $renderer): Writer |
249 | 124 | { |
250 | | - $dataType = $this->createClass($method); |
| 125 | + return (new Writer($renderer)); |
| 126 | + } |
251 | 127 |
|
252 | | - $dataType->create($arguments); |
| 128 | + protected function getRenderer(): ImageRenderer |
| 129 | + { |
| 130 | + $renderer = new ImageRenderer( |
| 131 | + new RendererStyle($this->pixels, $this->margin, $this->getModule(), $this->getEye()), |
| 132 | + new SvgImageBackEnd |
| 133 | + ); |
253 | 134 |
|
254 | | - return $this->generate(strval($dataType)); |
| 135 | + return $renderer; |
255 | 136 | } |
256 | 137 |
|
257 | | - /** |
258 | | - * Creates a new DataType class dynamically. |
259 | | - * |
260 | | - * @param string $method |
261 | | - * |
262 | | - * @return SimpleSoftwareIO\QrCode\DataTypes\DataTypeInterface |
263 | | - */ |
264 | | - private function createClass($method) |
| 138 | + protected function getModule(): ModuleInterface |
265 | 139 | { |
266 | | - $class = $this->formatClass($method); |
| 140 | + if ($this->style === 'dot') { |
| 141 | + return (new DotsModule($this->styleSize)); |
| 142 | + } |
267 | 143 |
|
268 | | - if (!class_exists($class)) { |
269 | | - throw new \BadMethodCallException(); |
| 144 | + if ($this->style === 'round') { |
| 145 | + return (new RoundnessModule($this->styleSize)); |
270 | 146 | } |
271 | 147 |
|
272 | | - return new $class(); |
| 148 | + return SquareModule::instance(); |
273 | 149 | } |
274 | 150 |
|
275 | | - /** |
276 | | - * Formats the method name correctly. |
277 | | - * |
278 | | - * @param $method |
279 | | - * |
280 | | - * @return string |
281 | | - */ |
282 | | - private function formatClass($method) |
| 151 | + protected function getEye(): EyeInterface |
283 | 152 | { |
284 | | - $method = ucfirst($method); |
| 153 | + if ($this->eyeStyle === 'square') { |
| 154 | + return SquareEye::instance(); |
| 155 | + } |
285 | 156 |
|
286 | | - $class = "SimpleSoftwareIO\QrCode\DataTypes\\".$method; |
| 157 | + if ($this->eyeStyle === 'circle') { |
| 158 | + return SimpleCircleEye::instance(); |
| 159 | + } |
287 | 160 |
|
288 | | - return $class; |
| 161 | + return (new ModuleEye($this->getModule())); |
289 | 162 | } |
290 | 163 | } |
0 commit comments