1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ThinkQR; |
4
|
|
|
|
5
|
|
|
use BaconQrCode\Renderer\Image\ImageBackEndInterface; |
6
|
|
|
use BaconQrCode\Renderer\Image\ImagickImageBackEnd; |
7
|
|
|
use BaconQrCode\Renderer\Image\SvgImageBackEnd; |
8
|
|
|
use BaconQrCode\Renderer\ImageRenderer; |
9
|
|
|
use BaconQrCode\Renderer\RendererStyle\RendererStyle; |
10
|
|
|
use BaconQrCode\Writer; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* QR code image generator |
14
|
|
|
*/ |
15
|
|
|
class QRCode |
16
|
|
|
{ |
17
|
|
|
protected string $content; |
18
|
|
|
protected float $renderSize; |
19
|
|
|
protected float $margin; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @param string $content |
23
|
|
|
* @param array $options |
24
|
|
|
*/ |
25
|
14 |
|
public function __construct(string $content, array $options = []) |
26
|
|
|
{ |
27
|
14 |
|
$this->content = $content; |
28
|
14 |
|
$this->renderSize( |
29
|
14 |
|
$options['render_size'] ?? config('thinkqr.defaults.render_size'), |
30
|
14 |
|
$options['margin'] ?? config('thinkqr.defaults.margin') |
31
|
14 |
|
); |
32
|
|
|
} |
33
|
|
|
|
34
|
13 |
|
public static function make(...$arguments): static |
35
|
|
|
{ |
36
|
13 |
|
return new static(...$arguments); |
|
|
|
|
37
|
|
|
} |
38
|
|
|
|
39
|
5 |
|
protected function prepareWriter(?ImageBackEndInterface $imageBackEnd = null): Writer |
40
|
|
|
{ |
41
|
5 |
|
$renderer = new ImageRenderer( |
42
|
5 |
|
new RendererStyle($this->renderSize, $this->margin), |
|
|
|
|
43
|
5 |
|
$imageBackEnd ?? new SvgImageBackEnd() |
44
|
5 |
|
); |
45
|
|
|
|
46
|
5 |
|
return new Writer($renderer); |
47
|
|
|
} |
48
|
|
|
|
49
|
1 |
|
public function content(string $content): static |
50
|
|
|
{ |
51
|
1 |
|
$this->content = $content; |
52
|
|
|
|
53
|
1 |
|
return $this; |
54
|
|
|
} |
55
|
|
|
|
56
|
1 |
|
public function getContent(): string |
57
|
|
|
{ |
58
|
1 |
|
return $this->content ; |
59
|
|
|
} |
60
|
|
|
|
61
|
14 |
|
public function renderSize(float $renderSize = 1, ?float $margin = null): static |
62
|
|
|
{ |
63
|
14 |
|
if ($renderSize < 1 || $renderSize > 999999) { |
64
|
4 |
|
throw new \InvalidArgumentException('Render size should be between 1-999999px'); |
65
|
|
|
} |
66
|
12 |
|
$this->renderSize = $renderSize; |
67
|
|
|
|
68
|
12 |
|
if (!is_null($margin)) { |
69
|
12 |
|
$this->margin($margin); |
70
|
|
|
} |
71
|
|
|
|
72
|
10 |
|
return $this; |
73
|
|
|
} |
74
|
|
|
|
75
|
12 |
|
public function margin(float $margin = 0): static |
76
|
|
|
{ |
77
|
12 |
|
if ($margin < 0 || $margin > 999999) { |
78
|
4 |
|
throw new \InvalidArgumentException('Margin should be between 0-999999px'); |
79
|
|
|
} |
80
|
|
|
|
81
|
10 |
|
$this->margin = $margin; |
82
|
|
|
|
83
|
10 |
|
return $this; |
84
|
|
|
} |
85
|
|
|
|
86
|
1 |
|
public function getSvgString(): string |
87
|
|
|
{ |
88
|
1 |
|
return $this->prepareWriter()->writeString($this->content); |
89
|
|
|
} |
90
|
|
|
|
91
|
1 |
|
public function getPngString(): string |
92
|
|
|
{ |
93
|
1 |
|
return $this->prepareWriter(new ImagickImageBackEnd())->writeString($this->content); |
94
|
|
|
} |
95
|
|
|
|
96
|
1 |
|
public function writeSvgFile(string $filename): static |
97
|
|
|
{ |
98
|
1 |
|
$this->prepareWriter()->writeFile($this->content, $filename); |
99
|
|
|
|
100
|
1 |
|
return $this; |
101
|
|
|
} |
102
|
|
|
|
103
|
2 |
|
public function writePngFile(string $filename): static |
104
|
|
|
{ |
105
|
2 |
|
$this->prepareWriter(new ImagickImageBackEnd())->writeFile($this->content, $filename); |
106
|
|
|
|
107
|
2 |
|
return $this; |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|