qrtools.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. class QRtools {
  25. //----------------------------------------------------------------------
  26. public static function binarize($frame)
  27. {
  28. $len = count($frame);
  29. foreach ($frame as &$frameLine) {
  30. for($i=0; $i<$len; $i++) {
  31. $frameLine[$i] = (ord($frameLine[$i])&1)?'1':'0';
  32. }
  33. }
  34. return $frame;
  35. }
  36. //----------------------------------------------------------------------
  37. public static function tcpdfBarcodeArray($code, $mode = 'QR,L', $tcPdfVersion = '4.5.037')
  38. {
  39. $barcode_array = array();
  40. if (!is_array($mode))
  41. $mode = explode(',', $mode);
  42. $eccLevel = 'L';
  43. if (count($mode) > 1) {
  44. $eccLevel = $mode[1];
  45. }
  46. $qrTab = QRcode::text($code, false, $eccLevel);
  47. $size = count($qrTab);
  48. $barcode_array['num_rows'] = $size;
  49. $barcode_array['num_cols'] = $size;
  50. $barcode_array['bcode'] = array();
  51. foreach ($qrTab as $line) {
  52. $arrAdd = array();
  53. foreach(str_split($line) as $char)
  54. $arrAdd[] = ($char=='1')?1:0;
  55. $barcode_array['bcode'][] = $arrAdd;
  56. }
  57. return $barcode_array;
  58. }
  59. //----------------------------------------------------------------------
  60. public static function clearCache()
  61. {
  62. self::$frames = array();
  63. }
  64. //----------------------------------------------------------------------
  65. public static function buildCache()
  66. {
  67. QRtools::markTime('before_build_cache');
  68. $mask = new QRmask();
  69. for ($a=1; $a <= QRSPEC_VERSION_MAX; $a++) {
  70. $frame = QRspec::newFrame($a);
  71. if (QR_IMAGE) {
  72. $fileName = QR_CACHE_DIR.'frame_'.$a.'.png';
  73. QRimage::png(self::binarize($frame), $fileName, 1, 0);
  74. }
  75. $width = count($frame);
  76. $bitMask = array_fill(0, $width, array_fill(0, $width, 0));
  77. for ($maskNo=0; $maskNo<8; $maskNo++)
  78. $mask->makeMaskNo($maskNo, $width, $frame, $bitMask, true);
  79. }
  80. QRtools::markTime('after_build_cache');
  81. }
  82. //----------------------------------------------------------------------
  83. public static function log($outfile, $err)
  84. {
  85. if (QR_LOG_DIR !== false) {
  86. if ($err != '') {
  87. if ($outfile !== false) {
  88. file_put_contents(QR_LOG_DIR.basename($outfile).'-errors.txt', date('Y-m-d H:i:s').': '.$err, FILE_APPEND);
  89. } else {
  90. file_put_contents(QR_LOG_DIR.'errors.txt', date('Y-m-d H:i:s').': '.$err, FILE_APPEND);
  91. }
  92. }
  93. }
  94. }
  95. //----------------------------------------------------------------------
  96. public static function dumpMask($frame)
  97. {
  98. $width = count($frame);
  99. for($y=0;$y<$width;$y++) {
  100. for($x=0;$x<$width;$x++) {
  101. echo ord($frame[$y][$x]).',';
  102. }
  103. }
  104. }
  105. //----------------------------------------------------------------------
  106. public static function markTime($markerId)
  107. {
  108. list($usec, $sec) = explode(" ", microtime());
  109. $time = ((float)$usec + (float)$sec);
  110. if (!isset($GLOBALS['qr_time_bench']))
  111. $GLOBALS['qr_time_bench'] = array();
  112. $GLOBALS['qr_time_bench'][$markerId] = $time;
  113. }
  114. //----------------------------------------------------------------------
  115. public static function timeBenchmark()
  116. {
  117. self::markTime('finish');
  118. $lastTime = 0;
  119. $startTime = 0;
  120. $p = 0;
  121. echo '<table cellpadding="3" cellspacing="1">
  122. <thead><tr style="border-bottom:1px solid silver"><td colspan="2" style="text-align:center">BENCHMARK</td></tr></thead>
  123. <tbody>';
  124. foreach($GLOBALS['qr_time_bench'] as $markerId=>$thisTime) {
  125. if ($p > 0) {
  126. echo '<tr><th style="text-align:right">till '.$markerId.': </th><td>'.number_format($thisTime-$lastTime, 6).'s</td></tr>';
  127. } else {
  128. $startTime = $thisTime;
  129. }
  130. $p++;
  131. $lastTime = $thisTime;
  132. }
  133. echo '</tbody><tfoot>
  134. <tr style="border-top:2px solid black"><th style="text-align:right">TOTAL: </th><td>'.number_format($lastTime-$startTime, 6).'s</td></tr>
  135. </tfoot>
  136. </table>';
  137. }
  138. }
  139. //##########################################################################
  140. QRtools::markTime('start');