国内流行的内容管理系统(CMS)多端全媒体解决方案 https://www.dedebiz.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

181 lines
5.1KB

  1. <?php
  2. /*
  3. * PHP QR Code encoder
  4. *
  5. * Bitstream class
  6. *
  7. * Based on libqrencode C library distributed under LGPL 2.1
  8. * Copyright (C) 2006, 2007, 2008, 2009 Kentaro Fukuchi <fukuchi@megaui.net>
  9. *
  10. * PHP QR Code is distributed under LGPL 3
  11. * Copyright (C) 2010 Dominik Dzienia <deltalab at poczta dot fm>
  12. *
  13. * This library is free software; you can redistribute it and/or
  14. * modify it under the terms of the GNU Lesser General Public
  15. * License as published by the Free Software Foundation; either
  16. * version 3 of the License, or any later version.
  17. *
  18. * This library is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  21. * Lesser General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Lesser General Public
  24. * License along with this library; if not, write to the Free Software
  25. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  26. */
  27. class QRbitstream {
  28. public $data = array();
  29. //----------------------------------------------------------------------
  30. public function size()
  31. {
  32. return count($this->data);
  33. }
  34. //----------------------------------------------------------------------
  35. public function allocate($setLength)
  36. {
  37. $this->data = array_fill(0, $setLength, 0);
  38. return 0;
  39. }
  40. //----------------------------------------------------------------------
  41. public static function newFromNum($bits, $num)
  42. {
  43. $bstream = new QRbitstream();
  44. $bstream->allocate($bits);
  45. $mask = 1 << ($bits - 1);
  46. for($i=0; $i<$bits; $i++) {
  47. if($num & $mask) {
  48. $bstream->data[$i] = 1;
  49. } else {
  50. $bstream->data[$i] = 0;
  51. }
  52. $mask = $mask >> 1;
  53. }
  54. return $bstream;
  55. }
  56. //----------------------------------------------------------------------
  57. public static function newFromBytes($size, $data)
  58. {
  59. $bstream = new QRbitstream();
  60. $bstream->allocate($size * 8);
  61. $p=0;
  62. for($i=0; $i<$size; $i++) {
  63. $mask = 0x80;
  64. for($j=0; $j<8; $j++) {
  65. if($data[$i] & $mask) {
  66. $bstream->data[$p] = 1;
  67. } else {
  68. $bstream->data[$p] = 0;
  69. }
  70. $p++;
  71. $mask = $mask >> 1;
  72. }
  73. }
  74. return $bstream;
  75. }
  76. //----------------------------------------------------------------------
  77. public function append(QRbitstream $arg)
  78. {
  79. if (is_null($arg)) {
  80. return -1;
  81. }
  82. if($arg->size() == 0) {
  83. return 0;
  84. }
  85. if($this->size() == 0) {
  86. $this->data = $arg->data;
  87. return 0;
  88. }
  89. $this->data = array_values(array_merge($this->data, $arg->data));
  90. return 0;
  91. }
  92. //----------------------------------------------------------------------
  93. public function appendNum($bits, $num)
  94. {
  95. if ($bits == 0)
  96. return 0;
  97. $b = QRbitstream::newFromNum($bits, $num);
  98. if(is_null($b))
  99. return -1;
  100. $ret = $this->append($b);
  101. unset($b);
  102. return $ret;
  103. }
  104. //----------------------------------------------------------------------
  105. public function appendBytes($size, $data)
  106. {
  107. if ($size == 0)
  108. return 0;
  109. $b = QRbitstream::newFromBytes($size, $data);
  110. if(is_null($b))
  111. return -1;
  112. $ret = $this->append($b);
  113. unset($b);
  114. return $ret;
  115. }
  116. //----------------------------------------------------------------------
  117. public function toByte()
  118. {
  119. $size = $this->size();
  120. if($size == 0) {
  121. return array();
  122. }
  123. $data = array_fill(0, (int)(($size + 7) / 8), 0);
  124. $bytes = (int)($size / 8);
  125. $p = 0;
  126. for($i=0; $i<$bytes; $i++) {
  127. $v = 0;
  128. for($j=0; $j<8; $j++) {
  129. $v = $v << 1;
  130. $v |= $this->data[$p];
  131. $p++;
  132. }
  133. $data[$i] = $v;
  134. }
  135. if($size & 7) {
  136. $v = 0;
  137. for($j=0; $j<($size & 7); $j++) {
  138. $v = $v << 1;
  139. $v |= $this->data[$p];
  140. $p++;
  141. }
  142. $data[$bytes] = $v;
  143. }
  144. return $data;
  145. }
  146. }