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