-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathQR.php
181 lines (160 loc) · 3.99 KB
/
QR.php
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
<?php
namespace TEC\Tickets\QR;
use TEC\Common\phpqrcode\QRcode;
/**
* A Facade for the QR code generator.
*
* @since 5.6.7
*
* @package TEC\Tickets\QR
*/
class QR {
/**
* The level of the QR code.
*
* @since 5.6.7
*
* @var int What type of error correction will be used on the QR code.
*/
protected $level = TEC_COMMON_QR_ECLEVEL_L;
/**
* The size of the QR code.
*
* @since 5.6.7
*
* @var int Stores the size of the QR code.
*/
protected $size = 3;
/**
* The margin of the QR code.
*
* @since 5.6.7
*
* @var int Stores the margin used to generate the QR code.
*/
protected $margin = 4;
/**
* Change the level of Error Correction will be used on the QR code.
*
* @since 5.6.7
*
* @param int $value What value will be set on level.
*
* @return $this
*/
public function level( int $value ): self {
$this->level = $value;
return $this;
}
/**
* Change the size of the QR code image.
*
* @since 5.6.7
*
* @param int $value What value will be set on size.
*
* @return $this
*/
public function size( int $value ): self {
$this->size = $value;
return $this;
}
/**
* Change the margin of the QR code image.
*
* @since 5.6.7
*
* @param int $value What value will be set on margin.
*
* @return $this
*/
public function margin( int $value ): self {
$this->margin = $value;
return $this;
}
/**
* Get the EC level of the QR code.
*
* @since 5.6.7
*
* @return int Type of QR code used.
*/
protected function get_level(): int {
return $this->level;
}
/**
* Get the size of the QR code.
*
* @since 5.6.7
*
* @return int Size of the QR code.
*/
protected function get_size(): int {
return $this->size;
}
/**
* Get the margin of the QR code.
*
* @since 5.6.7
*
* @return int Margin used to be included in the QR code, helps with readability.
*/
protected function get_margin(): int {
return $this->margin;
}
/**
* Get the QR code as a string.
*
* @since 5.6.7
*
* @param string $data String used to generate the QR code.
*
* @return string The QR code as a string, not an actual readable string, it's a binary.
*/
public function get_png_as_string( string $data ): string {
ob_start();
QRcode::png( $data, false, $this->get_level(), $this->get_size(), $this->get_margin() );
$png_string = ob_get_clean();
return $png_string;
}
/**
* Get the QR code as a PNG base64 image, helpful to use when uploading the file would create duplicates.
*
* @since 5.6.7
*
* @param string $data String used to generate the QR code.
*
* @return string QR Code as an embeddable Base64 image.
*/
public function get_png_as_base64( string $data ): string {
$src = base64_encode( $this->get_png_as_string( $data ) );
return "data:image/png;base64," . $src;
}
/**
* Get the QR code as a file uploaded to WordPress.
*
* @since 5.6.7
*
* @param string $data String used to generate the QR code.
* @param string $name File name without the extension.
* @param string $folder Which folder under WP_CONTENT_DIR/uploads/ will be used to store the file.
*
* @return array{file: string, url: string, type: string, error: string|false} The QR uploaded file information.
*/
public function get_png_as_file( string $data, string $name, string $folder = 'tec-tickets-qr' ): array {
$folder = '/' . ltrim( $folder, '/' );
$png_as_string = $this->get_png_as_string( $data );
// Filters the upload directory but still use `wp_upload_bits` to create the file.
$upload_bits_filter = static function( $arr ) use ( $folder ) {
$arr['url'] = str_replace( $arr['subdir'], $folder, $arr['url'] );
$arr['path'] = str_replace( $arr['subdir'], $folder, $arr['path'] );
$arr['subdir'] = $folder;
return $arr;
};
add_filter( 'upload_dir', $upload_bits_filter );
$filename = sanitize_file_name( $name ) . '.png';
$file_upload = wp_upload_bits( $filename, null, $png_as_string );
remove_filter( 'upload_dir', $upload_bits_filter );
return $file_upload;
}
}