QRtools.php 5.4 KB

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