qrtools.php 3.3 KB

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