1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* PHP QR Code encoder |
4
|
|
|
* |
5
|
|
|
* Toolset, handy and debug utilites. |
6
|
|
|
* |
7
|
|
|
* PHP QR Code is distributed under LGPL 3 |
8
|
|
|
* Copyright (C) 2010 Dominik Dzienia <deltalab at poczta dot fm> |
9
|
|
|
* |
10
|
|
|
* This library is free software; you can redistribute it and/or |
11
|
|
|
* modify it under the terms of the GNU Lesser General Public |
12
|
|
|
* License as published by the Free Software Foundation; either |
13
|
|
|
* version 3 of the License, or any later version. |
14
|
|
|
* |
15
|
|
|
* This library is distributed in the hope that it will be useful, |
16
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
17
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
18
|
|
|
* Lesser General Public License for more details. |
19
|
|
|
* |
20
|
|
|
* You should have received a copy of the GNU Lesser General Public |
21
|
|
|
* License along with this library; if not, write to the Free Software |
22
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
23
|
|
|
*/ |
24
|
|
|
|
25
|
|
|
class qrtools |
26
|
|
|
{ |
27
|
|
|
//---------------------------------------------------------------------- |
28
|
|
|
public static function binarize($frame) |
29
|
|
|
{ |
30
|
|
|
$len = count($frame); |
31
|
|
|
foreach ($frame as &$frameLine) { |
32
|
|
|
for ($i = 0; $i < $len; $i++) { |
33
|
|
|
$frameLine[$i] = (ord($frameLine[$i]) & 1) ? '1' : '0'; |
34
|
|
|
} |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
return $frame; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
//---------------------------------------------------------------------- |
41
|
|
|
public static function tcpdfBarcodeArray($code, $mode = 'QR,L', $tcPdfVersion = '4.5.037') |
|
|
|
|
42
|
|
|
{ |
43
|
|
|
$barcode_array = []; |
44
|
|
|
|
45
|
|
|
if (!is_array($mode)) { |
46
|
|
|
$mode = explode(',', $mode); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
$eccLevel = 'L'; |
50
|
|
|
|
51
|
|
|
if (count($mode) > 1) { |
52
|
|
|
$eccLevel = $mode[1]; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
$qrTab = QRcode::text($code, false, $eccLevel); |
56
|
|
|
$size = count($qrTab); |
57
|
|
|
|
58
|
|
|
$barcode_array['num_rows'] = $size; |
59
|
|
|
$barcode_array['num_cols'] = $size; |
60
|
|
|
$barcode_array['bcode'] = []; |
61
|
|
|
|
62
|
|
|
foreach ($qrTab as $line) { |
63
|
|
|
$arrAdd = []; |
64
|
|
|
foreach (str_split($line) as $char) { |
65
|
|
|
$arrAdd[] = ($char == '1') ? 1 : 0; |
66
|
|
|
} |
67
|
|
|
$barcode_array['bcode'][] = $arrAdd; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
return $barcode_array; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
//---------------------------------------------------------------------- |
74
|
|
|
public static function clearCache() |
75
|
|
|
{ |
76
|
|
|
self::$frames = []; |
|
|
|
|
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
//---------------------------------------------------------------------- |
80
|
|
|
public static function buildCache() |
81
|
|
|
{ |
82
|
|
|
self::markTime('before_build_cache'); |
83
|
|
|
|
84
|
|
|
$mask = new QRmask(); |
85
|
|
|
for ($a = 1; $a <= QRSPEC_VERSION_MAX; $a++) { |
86
|
|
|
$frame = QRspec::newFrame($a); |
87
|
|
|
if (QR_IMAGE) { |
88
|
|
|
$fileName = QR_CACHE_DIR.'frame_'.$a.'.png'; |
89
|
|
|
QRimage::png(self::binarize($frame), $fileName, 1, 0); |
|
|
|
|
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
$width = count($frame); |
93
|
|
|
$bitMask = array_fill(0, $width, array_fill(0, $width, 0)); |
94
|
|
|
for ($maskNo = 0; $maskNo < 8; $maskNo++) { |
95
|
|
|
$mask->makeMaskNo($maskNo, $width, $frame, $bitMask, true); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
self::markTime('after_build_cache'); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
//---------------------------------------------------------------------- |
103
|
|
|
public static function log($outfile, $err) |
104
|
|
|
{ |
105
|
|
|
if (QR_LOG_DIR !== false) { |
106
|
|
|
if ($err != '') { |
107
|
|
|
if ($outfile !== false) { |
108
|
|
|
file_put_contents(QR_LOG_DIR.basename($outfile).'-errors.txt', date('Y-m-d H:i:s').': '.$err, FILE_APPEND); |
109
|
|
|
} else { |
110
|
|
|
file_put_contents(QR_LOG_DIR.'errors.txt', date('Y-m-d H:i:s').': '.$err, FILE_APPEND); |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
//---------------------------------------------------------------------- |
117
|
|
|
public static function dumpMask($frame) |
118
|
|
|
{ |
119
|
|
|
$width = count($frame); |
120
|
|
|
for ($y = 0; $y < $width; $y++) { |
121
|
|
|
for ($x = 0; $x < $width; $x++) { |
122
|
|
|
echo ord($frame[$y][$x]).','; |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
//---------------------------------------------------------------------- |
128
|
|
|
public static function markTime($markerId) |
129
|
|
|
{ |
130
|
|
|
list($usec, $sec) = explode(' ', microtime()); |
131
|
|
|
$time = ((float) $usec + (float) $sec); |
132
|
|
|
|
133
|
|
|
if (!isset($GLOBALS['qr_time_bench'])) { |
134
|
|
|
$GLOBALS['qr_time_bench'] = []; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
$GLOBALS['qr_time_bench'][$markerId] = $time; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
//---------------------------------------------------------------------- |
141
|
|
|
public static function timeBenchmark() |
142
|
|
|
{ |
143
|
|
|
self::markTime('finish'); |
144
|
|
|
|
145
|
|
|
$lastTime = 0; |
146
|
|
|
$startTime = 0; |
147
|
|
|
$p = 0; |
148
|
|
|
|
149
|
|
|
echo '<table cellpadding="3" cellspacing="1"> |
150
|
|
|
<thead><tr style="border-bottom:1px solid silver"><td colspan="2" style="text-align:center">BENCHMARK</td></tr></thead> |
151
|
|
|
<tbody>'; |
152
|
|
|
|
153
|
|
|
foreach ($GLOBALS['qr_time_bench'] as $markerId=>$thisTime) { |
154
|
|
|
if ($p > 0) { |
155
|
|
|
echo '<tr><th style="text-align:right">till '.$markerId.': </th><td>'.number_format($thisTime - $lastTime, 6).'s</td></tr>'; |
156
|
|
|
} else { |
157
|
|
|
$startTime = $thisTime; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
$p++; |
161
|
|
|
$lastTime = $thisTime; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
echo '</tbody><tfoot> |
165
|
|
|
<tr style="border-top:2px solid black"><th style="text-align:right">TOTAL: </th><td>'.number_format($lastTime - $startTime, 6).'s</td></tr> |
166
|
|
|
</tfoot> |
167
|
|
|
</table>'; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
public static function save($content, $filename_path) |
171
|
|
|
{ |
172
|
|
|
try { |
173
|
|
|
$handle = fopen($filename_path, 'w'); |
174
|
|
|
fwrite($handle, $content); |
|
|
|
|
175
|
|
|
fclose($handle); |
|
|
|
|
176
|
|
|
|
177
|
|
|
return true; |
178
|
|
|
} catch (Exception $e) { |
179
|
|
|
echo 'Exception reçue : ', $e->getMessage(), "\n"; |
180
|
|
|
} |
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
//########################################################################## |
185
|
|
|
|
186
|
|
|
QRtools::markTime('start'); |
187
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.