qrtools.php (6332B)
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 33 for($i=0; $i<$len; $i++) { 34 $frameLine[$i] = (ord($frameLine[$i])&1)?'1':'0'; 35 } 36 } 37 38 return $frame; 39 } 40 41 //---------------------------------------------------------------------- 42 public static function tcpdfBarcodeArray($code, $mode = 'QR,L', $tcPdfVersion = '4.5.037') 43 { 44 $barcode_array = array(); 45 46 if (!is_array($mode)) 47 $mode = explode(',', $mode); 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'] = array(); 61 62 foreach ($qrTab as $line) { 63 $arrAdd = array(); 64 foreach(str_split($line) as $char) 65 $arrAdd[] = ($char=='1')?1:0; 66 $barcode_array['bcode'][] = $arrAdd; 67 } 68 69 return $barcode_array; 70 } 71 72 //---------------------------------------------------------------------- 73 public static function clearCache() 74 { 75 self::$frames = array(); 76 } 77 78 //---------------------------------------------------------------------- 79 public static function buildCache() 80 { 81 QRtools::markTime('before_build_cache'); 82 83 $mask = new QRmask(); 84 for ($a=1; $a <= QRSPEC_VERSION_MAX; $a++) { 85 $frame = QRspec::newFrame($a); 86 if (QR_IMAGE) { 87 $fileName = QR_CACHE_DIR.'frame_'.$a.'.png'; 88 QRimage::png(self::binarize($frame), $fileName, 1, 0); 89 } 90 91 $width = count($frame); 92 $bitMask = array_fill(0, $width, array_fill(0, $width, 0)); 93 for ($maskNo=0; $maskNo<8; $maskNo++) 94 $mask->makeMaskNo($maskNo, $width, $frame, $bitMask, true); 95 } 96 97 QRtools::markTime('after_build_cache'); 98 } 99 100 //---------------------------------------------------------------------- 101 public static function log($outfile, $err) 102 { 103 if (QR_LOG_DIR !== false) { 104 if ($err != '') { 105 if ($outfile !== false) { 106 file_put_contents(QR_LOG_DIR.basename($outfile).'-errors.txt', date('Y-m-d H:i:s').': '.$err, FILE_APPEND); 107 } else { 108 file_put_contents(QR_LOG_DIR.'errors.txt', date('Y-m-d H:i:s').': '.$err, FILE_APPEND); 109 } 110 } 111 } 112 } 113 114 //---------------------------------------------------------------------- 115 public static function dumpMask($frame) 116 { 117 $width = count($frame); 118 for($y=0;$y<$width;$y++) { 119 for($x=0;$x<$width;$x++) { 120 echo ord($frame[$y][$x]).','; 121 } 122 } 123 } 124 125 //---------------------------------------------------------------------- 126 public static function markTime($markerId) 127 { 128 list($usec, $sec) = explode(" ", microtime()); 129 $time = ((float)$usec + (float)$sec); 130 131 if (!isset($GLOBALS['qr_time_bench'])) 132 $GLOBALS['qr_time_bench'] = array(); 133 134 $GLOBALS['qr_time_bench'][$markerId] = $time; 135 } 136 137 //---------------------------------------------------------------------- 138 public static function timeBenchmark() 139 { 140 self::markTime('finish'); 141 142 $lastTime = 0; 143 $startTime = 0; 144 $p = 0; 145 146 echo '<table cellpadding="3" cellspacing="1"> 147 <thead><tr style="border-bottom:1px solid silver"><td colspan="2" style="text-align:center">BENCHMARK</td></tr></thead> 148 <tbody>'; 149 150 foreach($GLOBALS['qr_time_bench'] as $markerId=>$thisTime) { 151 if ($p > 0) { 152 echo '<tr><th style="text-align:right">till '.$markerId.': </th><td>'.number_format($thisTime-$lastTime, 6).'s</td></tr>'; 153 } else { 154 $startTime = $thisTime; 155 } 156 157 $p++; 158 $lastTime = $thisTime; 159 } 160 161 echo '</tbody><tfoot> 162 <tr style="border-top:2px solid black"><th style="text-align:right">TOTAL: </th><td>'.number_format($lastTime-$startTime, 6).'s</td></tr> 163 </tfoot> 164 </table>'; 165 } 166 167 } 168 169 //########################################################################## 170 171 QRtools::markTime('start'); 172