FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
simple-qrcode/src/Generator.php at develop · SimpleSoftwareIO/simple-qrcode · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
SimpleSoftwareIO
/
simple-qrcode
Public
Notifications
You must be signed in to change notification settings
Fork
458
Star
2.9k
Code
Issues
38
Pull requests
20
Actions
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Security and quality
Insights
Expand file tree
Breadcrumbs
simple-qrcode
/
src
/
Generator.php
Copy path
More file actions
More file actions
Latest commit
History
History
History
578 lines (498 loc) · 14.6 KB
Breadcrumbs
simple-qrcode
/
src
/
Generator.php
Copy path
File metadata and controls
578 lines (498 loc) · 14.6 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
<?php
namespace
SimpleSoftwareIO
\
QrCode
;
use
BaconQrCode
\
Common
\
ErrorCorrectionLevel
;
use
BaconQrCode
\
Encoder
\
Encoder
;
use
BaconQrCode
\
Exception
\
WriterException
;
use
BaconQrCode
\
Renderer
\
Color
\
Alpha
;
use
BaconQrCode
\
Renderer
\
Color
\
ColorInterface
;
use
BaconQrCode
\
Renderer
\
Color
\
Rgb
;
use
BaconQrCode
\
Renderer
\
Eye
\
EyeInterface
;
use
BaconQrCode
\
Renderer
\
Eye
\
ModuleEye
;
use
BaconQrCode
\
Renderer
\
Eye
\
SimpleCircleEye
;
use
BaconQrCode
\
Renderer
\
Eye
\
SquareEye
;
use
BaconQrCode
\
Renderer
\
Image
\
EpsImageBackEnd
;
use
BaconQrCode
\
Renderer
\
Image
\
ImageBackEndInterface
;
use
BaconQrCode
\
Renderer
\
Image
\
ImagickImageBackEnd
;
use
BaconQrCode
\
Renderer
\
Image
\
SvgImageBackEnd
;
use
BaconQrCode
\
Renderer
\
ImageRenderer
;
use
BaconQrCode
\
Renderer
\
Module
\
DotsModule
;
use
BaconQrCode
\
Renderer
\
Module
\
ModuleInterface
;
use
BaconQrCode
\
Renderer
\
Module
\
RoundnessModule
;
use
BaconQrCode
\
Renderer
\
Module
\
SquareModule
;
use
BaconQrCode
\
Renderer
\
RendererStyle
\
EyeFill
;
use
BaconQrCode
\
Renderer
\
RendererStyle
\
Fill
;
use
BaconQrCode
\
Renderer
\
RendererStyle
\
Gradient
;
use
BaconQrCode
\
Renderer
\
RendererStyle
\
GradientType
;
use
BaconQrCode
\
Renderer
\
RendererStyle
\
RendererStyle
;
use
BaconQrCode
\
Writer
;
use
BadMethodCallException
;
use
InvalidArgumentException
;
use
SimpleSoftwareIO
\
QrCode
\
DataTypes
\
DataTypeInterface
;
class
Generator
{
/**
* Holds the selected formatter.
*
* @var string
*/
protected
$
format
=
'
svg
'
;
/**
* Holds the size of the QrCode in pixels.
*
* @var int
*/
protected
$
pixels
=
100
;
/**
* Holds the margin size of the QrCode.
*
* @var int
*/
protected
$
margin
=
0
;
/**
* Holds the selected error correction.
* L: 7% loss.
* M: 15% loss.
* Q: 25% loss.
* H: 30% loss.
*
* @var string|null
*/
protected
$
errorCorrection
=
null
;
/**
* Holds the selected encoder. Possible values are
* ISO-8859-2, ISO-8859-3, ISO-8859-4, ISO-8859-5, ISO-8859-6,
* ISO-8859-7, ISO-8859-8, ISO-8859-9, ISO-8859-10, ISO-8859-11,
* ISO-8859-12, ISO-8859-13, ISO-8859-14, ISO-8859-15, ISO-8859-16,
* SHIFT-JIS, WINDOWS-1250, WINDOWS-1251, WINDOWS-1252, WINDOWS-1256,
* UTF-16BE, UTF-8, ASCII, GBK, EUC-KR.
*
* @var string
*/
protected
$
encoding
= Encoder::
DEFAULT_BYTE_MODE_ECODING
;
/**
* The style of the blocks within the QrCode.
* Possible values are square, dot, and round.
*
* @var string
*/
protected
$
style
=
'
square
'
;
/**
* The size of the selected style between 0 and 1.
* This only applies to dot and round.
*
* @var float|null
*/
protected
$
styleSize
=
null
;
/**
* The style to apply to the eye.
* Possible values are circle and square.
*
* @var string|null
*/
protected
$
eyeStyle
=
null
;
/**
* The foreground color of the QrCode.
*
* @var ColorInterface|null
*/
protected
$
color
=
null
;
/**
* The background color of the QrCode.
*
* @var ColorInterface|null
*/
protected
$
backgroundColor
=
null
;
/**
* An array that holds EyeFills of the color of the eyes.
*
* @var array
*/
protected
$
eyeColors
= [];
/**
* The gradient to apply to the QrCode.
*
* @var Gradient
*/
protected
$
gradient
;
/**
* Holds an image string that will be merged with the QrCode.
*
* @var null|string
*/
protected
$
imageMerge
=
null
;
/**
* The percentage that a merged image should take over the source image.
*
* @var float
*/
protected
$
imagePercentage
=
.2
;
/**
* Creates a new datatype object and then generates a QrCode.
*
* @param $method
* @param array $arguments
*/
public
function
__call
(
$
method
,
array
$
arguments
)
{
$
dataType
=
$
this
->
createClass
(
$
method
);
$
dataType
->
create
(
$
arguments
);
return
$
this
->
generate
(
strval
(
$
dataType
));
}
/**
* Generates the QrCode.
*
* @param string $text
* @param string|null $filename
* @return void|\Illuminate\Support\HtmlString|string
* @throws WriterException
* @throws InvalidArgumentException
*/
public
function
generate
(
string
$
text
,
string
$
filename
=
null
)
{
$
qrCode
=
$
this
->
getWriter
(
$
this
->
getRenderer
())->
writeString
(
$
text
,
$
this
->
encoding
,
$
this
->
errorCorrection
);
if
(
$
this
->
imageMerge
!==
null
&&
$
this
->
format
===
'
png
'
) {
$
merger
=
new
ImageMerge
(
new
Image
(
$
qrCode
),
new
Image
(
$
this
->
imageMerge
));
$
qrCode
=
$
merger
->
merge
(
$
this
->
imagePercentage
);
}
if
(
$
filename
) {
file_put_contents
(
$
filename
,
$
qrCode
);
return
;
}
if
(
class_exists
(\
Illuminate
\
Support
\HtmlString::class)) {
return
new
\
Illuminate
\
Support
\
HtmlString
(
$
qrCode
);
}
return
$
qrCode
;
}
/**
* Merges an image over the QrCode.
*
* @param string $filepath
* @param float $percentage
* @param SimpleSoftwareIO\QrCode\boolean|bool $absolute
* @return Generator
*/
public
function
merge
(
string
$
filepath
,
float
$
percentage
=
.2
,
bool
$
absolute
=
false
):
self
{
if
(
function_exists
(
'
base_path
'
) && !
$
absolute
) {
$
filepath
=
base_path
().
$
filepath
;
}
$
this
->
imageMerge
=
file_get_contents
(
$
filepath
);
$
this
->
imagePercentage
=
$
percentage
;
return
$
this
;
}
/**
* Merges an image string with the center of the QrCode.
*
* @param string $content
* @param float $percentage
* @return Generator
*/
public
function
mergeString
(
string
$
content
,
float
$
percentage
=
.2
):
self
{
$
this
->
imageMerge
=
$
content
;
$
this
->
imagePercentage
=
$
percentage
;
return
$
this
;
}
/**
* Sets the size of the QrCode.
*
* @param int $pixels
* @return Generator
*/
public
function
size
(
int
$
pixels
):
self
{
$
this
->
pixels
=
$
pixels
;
return
$
this
;
}
/**
* Sets the format of the QrCode.
*
* @param string $format
* @return Generator
* @throws InvalidArgumentException
*/
public
function
format
(
string
$
format
):
self
{
if
(!
in_array
(
$
format
, [
'
svg
'
,
'
eps
'
,
'
png
'
])) {
throw
new
InvalidArgumentException
(
"\$
format must be svg, eps, or png.
{
$
format
}
is not a valid.
"
);
}
$
this
->
format
=
$
format
;
return
$
this
;
}
/**
* Sets the foreground color of the QrCode.
*
* @param int $red
* @param int $green
* @param int $blue
* @param null|int $alpha
* @return Generator
*/
public
function
color
(
int
$
red
,
int
$
green
,
int
$
blue
, ?
int
$
alpha
=
null
):
self
{
$
this
->
color
=
$
this
->
createColor
(
$
red
,
$
green
,
$
blue
,
$
alpha
);
return
$
this
;
}
/**
* Sets the background color of the QrCode.
*
* @param int $red
* @param int $green
* @param int $blue
* @param null|int $alpha
* @return Generator
*/
public
function
backgroundColor
(
int
$
red
,
int
$
green
,
int
$
blue
, ?
int
$
alpha
=
null
):
self
{
$
this
->
backgroundColor
=
$
this
->
createColor
(
$
red
,
$
green
,
$
blue
,
$
alpha
);
return
$
this
;
}
/**
* Sets the eye color for the provided eye index.
*
* @param int $eyeNumber
* @param int $innerRed
* @param int $innerGreen
* @param int $innerBlue
* @param int $outterRed
* @param int $outterGreen
* @param int $outterBlue
* @return Generator
* @throws InvalidArgumentException
*/
public
function
eyeColor
(
int
$
eyeNumber
,
int
$
innerRed
,
int
$
innerGreen
,
int
$
innerBlue
,
int
$
outterRed
=
0
,
int
$
outterGreen
=
0
,
int
$
outterBlue
=
0
):
self
{
if
(
$
eyeNumber
<
0
||
$
eyeNumber
>
2
) {
throw
new
InvalidArgumentException
(
"\$
eyeNumber must be 0, 1, or 2.
{
$
eyeNumber
}
is not valid.
"
);
}
$
this
->
eyeColors
[
$
eyeNumber
] =
new
EyeFill
(
$
this
->
createColor
(
$
innerRed
,
$
innerGreen
,
$
innerBlue
),
$
this
->
createColor
(
$
outterRed
,
$
outterGreen
,
$
outterBlue
)
);
return
$
this
;
}
public
function
gradient
(
$
startRed
,
$
startGreen
,
$
startBlue
,
$
endRed
,
$
endGreen
,
$
endBlue
,
string
$
type
):
self
{
$
type
=
strtoupper
(
$
type
);
$
this
->
gradient
=
new
Gradient
(
$
this
->
createColor
(
$
startRed
,
$
startGreen
,
$
startBlue
),
$
this
->
createColor
(
$
endRed
,
$
endGreen
,
$
endBlue
),
GradientType::
$
type
()
);
return
$
this
;
}
/**
* Sets the eye style.
*
* @param string $style
* @return Generator
* @throws InvalidArgumentException
*/
public
function
eye
(
string
$
style
):
self
{
if
(!
in_array
(
$
style
, [
'
square
'
,
'
circle
'
])) {
throw
new
InvalidArgumentException
(
"\$
style must be square or circle.
{
$
style
}
is not a valid eye style.
"
);
}
$
this
->
eyeStyle
=
$
style
;
return
$
this
;
}
/**
* Sets the style of the blocks for the QrCode.
*
* @param string $style
* @param float $size
* @return Generator
* @throws InvalidArgumentException
*/
public
function
style
(
string
$
style
,
float
$
size
=
0.5
):
self
{
if
(!
in_array
(
$
style
, [
'
square
'
,
'
dot
'
,
'
round
'
])) {
throw
new
InvalidArgumentException
(
"\$
style must be square, dot, or round.
{
$
style
}
is not a valid.
"
);
}
if
(
$
size
<
0
||
$
size
>=
1
) {
throw
new
InvalidArgumentException
(
"\$
size must be between 0 and 1.
{
$
size
}
is not valid.
"
);
}
$
this
->
style
=
$
style
;
$
this
->
styleSize
=
$
size
;
return
$
this
;
}
/**
* Sets the encoding for the QrCode.
* Possible values are
* ISO-8859-2, ISO-8859-3, ISO-8859-4, ISO-8859-5, ISO-8859-6,
* ISO-8859-7, ISO-8859-8, ISO-8859-9, ISO-8859-10, ISO-8859-11,
* ISO-8859-12, ISO-8859-13, ISO-8859-14, ISO-8859-15, ISO-8859-16,
* SHIFT-JIS, WINDOWS-1250, WINDOWS-1251, WINDOWS-1252, WINDOWS-1256,
* UTF-16BE, UTF-8, ASCII, GBK, EUC-KR.
*
* @param string $encoding
* @return Generator
*/
public
function
encoding
(
string
$
encoding
):
self
{
$
this
->
encoding
=
strtoupper
(
$
encoding
);
return
$
this
;
}
/**
* Sets the error correction for the QrCode.
* L: 7% loss.
* M: 15% loss.
* Q: 25% loss.
* H: 30% loss.
*
* @param string $errorCorrection
* @return Generator
*/
public
function
errorCorrection
(
string
$
errorCorrection
):
self
{
$
errorCorrection
=
strtoupper
(
$
errorCorrection
);
$
this
->
errorCorrection
= ErrorCorrectionLevel::
$
errorCorrection
();
return
$
this
;
}
/**
* Sets the margin of the QrCode.
*
* @param int $margin
* @return Generator
*/
public
function
margin
(
int
$
margin
):
self
{
$
this
->
margin
=
$
margin
;
return
$
this
;
}
/**
* Fetches the Writer.
*
* @param ImageRenderer $renderer
* @return Writer
*/
public
function
getWriter
(
ImageRenderer
$
renderer
):
Writer
{
return
new
Writer
(
$
renderer
);
}
/**
* Fetches the Image Renderer.
*
* @return ImageRenderer
*/
public
function
getRenderer
():
ImageRenderer
{
$
renderer
=
new
ImageRenderer
(
$
this
->
getRendererStyle
(),
$
this
->
getFormatter
()
);
return
$
renderer
;
}
/**
* Returns the Renderer Style.
*
* @return RendererStyle
*/
public
function
getRendererStyle
():
RendererStyle
{
return
new
RendererStyle
(
$
this
->
pixels
,
$
this
->
margin
,
$
this
->
getModule
(),
$
this
->
getEye
(),
$
this
->
getFill
());
}
/**
* Fetches the formatter.
*
* @return ImageBackEndInterface
*/
public
function
getFormatter
():
ImageBackEndInterface
{
if
(
$
this
->
format
===
'
png
'
) {
return
new
ImagickImageBackEnd
(
'
png
'
);
}
if
(
$
this
->
format
===
'
eps
'
) {
return
new
EpsImageBackEnd
;
}
return
new
SvgImageBackEnd
;
}
/**
* Fetches the module.
*
* @return ModuleInterface
*/
public
function
getModule
():
ModuleInterface
{
if
(
$
this
->
style
===
'
dot
'
) {
return
new
DotsModule
(
$
this
->
styleSize
);
}
if
(
$
this
->
style
===
'
round
'
) {
return
new
RoundnessModule
(
$
this
->
styleSize
);
}
return
SquareModule::
instance
();
}
/**
* Fetches the eye style.
*
* @return EyeInterface
*/
public
function
getEye
():
EyeInterface
{
if
(
$
this
->
eyeStyle
===
'
square
'
) {
return
SquareEye::
instance
();
}
if
(
$
this
->
eyeStyle
===
'
circle
'
) {
return
SimpleCircleEye::
instance
();
}
return
new
ModuleEye
(
$
this
->
getModule
());
}
/**
* Fetches the color fill.
*
* @return Fill
*/
public
function
getFill
():
Fill
{
$
foregroundColor
=
$
this
->
color
??
new
Rgb
(
0
,
0
,
0
);
$
backgroundColor
=
$
this
->
backgroundColor
??
new
Rgb
(
255
,
255
,
255
);
$
eye0
=
$
this
->
eyeColors
[
0
] ?? EyeFill::
inherit
();
$
eye1
=
$
this
->
eyeColors
[
1
] ?? EyeFill::
inherit
();
$
eye2
=
$
this
->
eyeColors
[
2
] ?? EyeFill::
inherit
();
if
(
$
this
->
gradient
) {
return
Fill::
withForegroundGradient
(
$
backgroundColor
,
$
this
->
gradient
,
$
eye0
,
$
eye1
,
$
eye2
);
}
return
Fill::
withForegroundColor
(
$
backgroundColor
,
$
foregroundColor
,
$
eye0
,
$
eye1
,
$
eye2
);
}
/**
* Creates a RGB or Alpha channel color.
* @param int $red
* @param int $green
* @param int $blue
* @param null|int $alpha
* @return ColorInterface
*/
public
function
createColor
(
int
$
red
,
int
$
green
,
int
$
blue
, ?
int
$
alpha
=
null
):
ColorInterface
{
if
(
is_null
(
$
alpha
)) {
return
new
Rgb
(
$
red
,
$
green
,
$
blue
);
}
return
new
Alpha
(
$
alpha
,
new
Rgb
(
$
red
,
$
green
,
$
blue
));
}
/**
* Creates a new DataType class dynamically.
*
* @param string $method
* @return DataTypeInterface
*/
protected
function
createClass
(
string
$
method
):
DataTypeInterface
{
$
class
=
$
this
->
formatClass
(
$
method
);
if
(!
class_exists
(
$
class
)) {
throw
new
BadMethodCallException
();
}
return
new
$
class
();
}
/**
* Formats the method name correctly.
*
* @param $method
* @return string
*/
protected
function
formatClass
(
string
$
method
):
string
{
$
method
=
ucfirst
(
$
method
);
$
class
=
"
SimpleSoftwareIO\QrCode\DataTypes
\\"
.
$
method
;
return
$
class
;
}
}
Back
|
FazBrowse Home
|
New Git URL