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
|
|
|
namespace tinymeng\code\Gateways\qrcode; |
25
|
|
|
|
26
|
|
|
class QRtools { |
27
|
|
|
|
28
|
|
|
//---------------------------------------------------------------------- |
29
|
|
|
public static function binarize($frame) |
30
|
|
|
{ |
31
|
|
|
$len = count($frame); |
32
|
|
|
foreach ($frame as &$frameLine) { |
33
|
|
|
|
34
|
|
|
for($i=0; $i<$len; $i++) { |
35
|
|
|
$frameLine[$i] = (ord($frameLine[$i])&1)?'1':'0'; |
36
|
|
|
} |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
return $frame; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
//---------------------------------------------------------------------- |
43
|
|
|
public static function tcpdfBarcodeArray($code, $mode = 'QR,L', $tcPdfVersion = '4.5.037') |
|
|
|
|
44
|
|
|
{ |
45
|
|
|
$barcode_array = array(); |
46
|
|
|
|
47
|
|
|
if (!is_array($mode)) |
48
|
|
|
$mode = explode(',', $mode); |
49
|
|
|
|
50
|
|
|
$eccLevel = 'L'; |
51
|
|
|
|
52
|
|
|
if (count($mode) > 1) { |
53
|
|
|
$eccLevel = $mode[1]; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
$qrTab = QRcode::text($code, false, $eccLevel); |
57
|
|
|
$size = count($qrTab); |
58
|
|
|
|
59
|
|
|
$barcode_array['num_rows'] = $size; |
60
|
|
|
$barcode_array['num_cols'] = $size; |
61
|
|
|
$barcode_array['bcode'] = array(); |
62
|
|
|
|
63
|
|
|
foreach ($qrTab as $line) { |
64
|
|
|
$arrAdd = array(); |
65
|
|
|
foreach(str_split($line) as $char) |
66
|
|
|
$arrAdd[] = ($char=='1')?1:0; |
67
|
|
|
$barcode_array['bcode'][] = $arrAdd; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
return $barcode_array; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
//---------------------------------------------------------------------- |
74
|
|
|
public static function clearCache() |
75
|
|
|
{ |
76
|
|
|
self::$frames = array(); |
|
|
|
|
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
//---------------------------------------------------------------------- |
80
|
|
|
public static function buildCache() |
81
|
|
|
{ |
82
|
|
|
QRtools::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
|
|
|
QRtools::markTime('after_build_cache'); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
//---------------------------------------------------------------------- |
102
|
|
|
public static function log($outfile, $err) |
103
|
|
|
{ |
104
|
|
|
if (QR_LOG_DIR !== false) { |
105
|
|
|
if ($err != '') { |
106
|
|
|
if ($outfile !== false) { |
107
|
|
|
file_put_contents(QR_LOG_DIR.basename($outfile).'-errors.txt', date('Y-m-d H:i:s').': '.$err, FILE_APPEND); |
108
|
|
|
} else { |
109
|
|
|
file_put_contents(QR_LOG_DIR.'errors.txt', date('Y-m-d H:i:s').': '.$err, FILE_APPEND); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
//---------------------------------------------------------------------- |
116
|
|
|
public static function dumpMask($frame) |
117
|
|
|
{ |
118
|
|
|
$width = count($frame); |
119
|
|
|
for($y=0;$y<$width;$y++) { |
120
|
|
|
for($x=0;$x<$width;$x++) { |
121
|
|
|
echo ord($frame[$y][$x]).','; |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
//---------------------------------------------------------------------- |
127
|
|
|
public static function markTime($markerId) |
128
|
|
|
{ |
129
|
|
|
list($usec, $sec) = explode(" ", microtime()); |
130
|
|
|
$time = ((float)$usec + (float)$sec); |
131
|
|
|
|
132
|
|
|
if (!isset($GLOBALS['qr_time_bench'])) |
133
|
|
|
$GLOBALS['qr_time_bench'] = array(); |
134
|
|
|
|
135
|
|
|
$GLOBALS['qr_time_bench'][$markerId] = $time; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
//---------------------------------------------------------------------- |
139
|
|
|
public static function timeBenchmark() |
140
|
|
|
{ |
141
|
|
|
self::markTime('finish'); |
142
|
|
|
|
143
|
|
|
$lastTime = 0; |
144
|
|
|
$startTime = 0; |
145
|
|
|
$p = 0; |
146
|
|
|
|
147
|
|
|
echo '<table cellpadding="3" cellspacing="1"> |
148
|
|
|
<thead><tr style="border-bottom:1px solid silver"><td colspan="2" style="text-align:center">BENCHMARK</td></tr></thead> |
149
|
|
|
<tbody>'; |
150
|
|
|
|
151
|
|
|
foreach($GLOBALS['qr_time_bench'] as $markerId=>$thisTime) { |
152
|
|
|
if ($p > 0) { |
153
|
|
|
echo '<tr><th style="text-align:right">till '.$markerId.': </th><td>'.number_format($thisTime-$lastTime, 6).'s</td></tr>'; |
154
|
|
|
} else { |
155
|
|
|
$startTime = $thisTime; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
$p++; |
159
|
|
|
$lastTime = $thisTime; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
echo '</tbody><tfoot> |
163
|
|
|
<tr style="border-top:2px solid black"><th style="text-align:right">TOTAL: </th><td>'.number_format($lastTime-$startTime, 6).'s</td></tr> |
164
|
|
|
</tfoot> |
165
|
|
|
</table>'; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
//########################################################################## |
171
|
|
|
QRtools::markTime('start'); |
172
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.