QRinputItem

The Gravity Forms QRinputItem class.

Defined (1)

The class is defined in the following location(s).

/includes/phpqrcode/phpqrcode.php  
  1. class QRinputItem { 
  2.  
  3. public $mode; 
  4. public $size; 
  5. public $data; 
  6. public $bstream; 
  7.  
  8. public function __construct($mode, $size, $data, $bstream = null) 
  9. $setData = array_slice($data, 0, $size); 
  10.  
  11. if (count($setData) < $size) { 
  12. $setData = array_merge($setData, array_fill(0, $size-count($setData), 0)); 
  13.  
  14. if(!QRinput::check($mode, $size, $setData)) { 
  15. throw new Exception('Error m:'.$mode.', s:'.$size.', d:'.join(', ', $setData)); 
  16. return null; 
  17.  
  18. $this->mode = $mode; 
  19. $this->size = $size; 
  20. $this->data = $setData; 
  21. $this->bstream = $bstream; 
  22.  
  23. //---------------------------------------------------------------------- 
  24. public function encodeModeNum($version) 
  25. try { 
  26.  
  27. $words = (int)($this->size / 3); 
  28. $bs = new QRbitstream(); 
  29.  
  30. $val = 0x1; 
  31. $bs->appendNum(4, $val); 
  32. $bs->appendNum(QRspec::lengthIndicator(QR_MODE_NUM, $version), $this->size); 
  33.  
  34. for($i=0; $i<$words; $i++) { 
  35. $val = (ord($this->data[$i*3 ]) - ord('0')) * 100; 
  36. $val += (ord($this->data[$i*3+1]) - ord('0')) * 10; 
  37. $val += (ord($this->data[$i*3+2]) - ord('0')); 
  38. $bs->appendNum(10, $val); 
  39.  
  40. if($this->size - $words * 3 == 1) { 
  41. $val = ord($this->data[$words*3]) - ord('0'); 
  42. $bs->appendNum(4, $val); 
  43. } else if($this->size - $words * 3 == 2) { 
  44. $val = (ord($this->data[$words*3 ]) - ord('0')) * 10; 
  45. $val += (ord($this->data[$words*3+1]) - ord('0')); 
  46. $bs->appendNum(7, $val); 
  47.  
  48. $this->bstream = $bs; 
  49. return 0; 
  50.  
  51. } catch (Exception $e) { 
  52. return -1; 
  53.  
  54. //---------------------------------------------------------------------- 
  55. public function encodeModeAn($version) 
  56. try { 
  57. $words = (int)($this->size / 2); 
  58. $bs = new QRbitstream(); 
  59.  
  60. $bs->appendNum(4, 0x02); 
  61. $bs->appendNum(QRspec::lengthIndicator(QR_MODE_AN, $version), $this->size); 
  62.  
  63. for($i=0; $i<$words; $i++) { 
  64. $val = (int)QRinput::lookAnTable(ord($this->data[$i*2 ])) * 45; 
  65. $val += (int)QRinput::lookAnTable(ord($this->data[$i*2+1])); 
  66.  
  67. $bs->appendNum(11, $val); 
  68.  
  69. if($this->size & 1) { 
  70. $val = QRinput::lookAnTable(ord($this->data[$words * 2])); 
  71. $bs->appendNum(6, $val); 
  72.  
  73. $this->bstream = $bs; 
  74. return 0; 
  75.  
  76. } catch (Exception $e) { 
  77. return -1; 
  78.  
  79. //---------------------------------------------------------------------- 
  80. public function encodeMode8($version) 
  81. try { 
  82. $bs = new QRbitstream(); 
  83.  
  84. $bs->appendNum(4, 0x4); 
  85. $bs->appendNum(QRspec::lengthIndicator(QR_MODE_8, $version), $this->size); 
  86.  
  87. for($i=0; $i<$this->size; $i++) { 
  88. $bs->appendNum(8, ord($this->data[$i])); 
  89.  
  90. $this->bstream = $bs; 
  91. return 0; 
  92.  
  93. } catch (Exception $e) { 
  94. return -1; 
  95.  
  96. //---------------------------------------------------------------------- 
  97. public function encodeModeKanji($version) 
  98. try { 
  99.  
  100. $bs = new QRbitrtream(); 
  101.  
  102. $bs->appendNum(4, 0x8); 
  103. $bs->appendNum(QRspec::lengthIndicator(QR_MODE_KANJI, $version), (int)($this->size / 2)); 
  104.  
  105. for($i=0; $i<$this->size; $i+=2) { 
  106. $val = (ord($this->data[$i]) << 8) | ord($this->data[$i+1]); 
  107. if($val <= 0x9ffc) { 
  108. $val -= 0x8140; 
  109. } else { 
  110. $val -= 0xc140; 
  111.  
  112. $h = ($val >> 8) * 0xc0; 
  113. $val = ($val & 0xff) + $h; 
  114.  
  115. $bs->appendNum(13, $val); 
  116.  
  117. $this->bstream = $bs; 
  118. return 0; 
  119.  
  120. } catch (Exception $e) { 
  121. return -1; 
  122.  
  123. //---------------------------------------------------------------------- 
  124. public function encodeModeStructure() 
  125. try { 
  126. $bs = new QRbitstream(); 
  127.  
  128. $bs->appendNum(4, 0x03); 
  129. $bs->appendNum(4, ord($this->data[1]) - 1); 
  130. $bs->appendNum(4, ord($this->data[0]) - 1); 
  131. $bs->appendNum(8, ord($this->data[2])); 
  132.  
  133. $this->bstream = $bs; 
  134. return 0; 
  135.  
  136. } catch (Exception $e) { 
  137. return -1; 
  138.  
  139. //---------------------------------------------------------------------- 
  140. public function estimateBitStreamSizeOfEntry($version) 
  141. $bits = 0; 
  142.  
  143. if($version == 0) 
  144. $version = 1; 
  145.  
  146. switch($this->mode) { 
  147. case QR_MODE_NUM: $bits = QRinput::estimateBitsModeNum($this->size); break; 
  148. case QR_MODE_AN: $bits = QRinput::estimateBitsModeAn($this->size); break; 
  149. case QR_MODE_8: $bits = QRinput::estimateBitsMode8($this->size); break; 
  150. case QR_MODE_KANJI: $bits = QRinput::estimateBitsModeKanji($this->size);break; 
  151. case QR_MODE_STRUCTURE: return STRUCTURE_HEADER_BITS; 
  152. default: 
  153. return 0; 
  154.  
  155. $l = QRspec::lengthIndicator($this->mode, $version); 
  156. $m = 1 << $l; 
  157. $num = (int)(($this->size + $m - 1) / $m); 
  158.  
  159. $bits += $num * (4 + $l); 
  160.  
  161. return $bits; 
  162.  
  163. //---------------------------------------------------------------------- 
  164. public function encodeBitStream($version) 
  165. try { 
  166.  
  167. unset($this->bstream); 
  168. $words = QRspec::maximumWords($this->mode, $version); 
  169.  
  170. if($this->size > $words) { 
  171.  
  172. $st1 = new QRinputItem($this->mode, $words, $this->data); 
  173. $st2 = new QRinputItem($this->mode, $this->size - $words, array_slice($this->data, $words)); 
  174.  
  175. $st1->encodeBitStream($version); 
  176. $st2->encodeBitStream($version); 
  177.  
  178. $this->bstream = new QRbitstream(); 
  179. $this->bstream->append($st1->bstream); 
  180. $this->bstream->append($st2->bstream); 
  181.  
  182. unset($st1); 
  183. unset($st2); 
  184.  
  185. } else { 
  186.  
  187. $ret = 0; 
  188.  
  189. switch($this->mode) { 
  190. case QR_MODE_NUM: $ret = $this->encodeModeNum($version); break; 
  191. case QR_MODE_AN: $ret = $this->encodeModeAn($version); break; 
  192. case QR_MODE_8: $ret = $this->encodeMode8($version); break; 
  193. case QR_MODE_KANJI: $ret = $this->encodeModeKanji($version);break; 
  194. case QR_MODE_STRUCTURE: $ret = $this->encodeModeStructure(); break; 
  195.  
  196. default: 
  197. break; 
  198.  
  199. if($ret < 0) 
  200. return -1; 
  201.  
  202. return $this->bstream->size(); 
  203.  
  204. } catch (Exception $e) { 
  205. return -1; 
  206. };