qrencode.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. <?php
  2. class QRrsblock
  3. {
  4. public $dataLength;
  5. public $data = array();
  6. public $eccLength;
  7. public $ecc = array();
  8. public function __construct($dl, $data, $el, &$ecc, QRrsItem $rs)
  9. {
  10. $rs->encode_rs_char($data, $ecc);
  11. $this->dataLength = $dl;
  12. $this->data = $data;
  13. $this->eccLength = $el;
  14. $this->ecc = $ecc;
  15. }
  16. }
  17. class QRrawcode
  18. {
  19. public $version;
  20. public $datacode = array();
  21. public $ecccode = array();
  22. public $blocks;
  23. public $rsblocks = array();
  24. public $count;
  25. public $dataLength;
  26. public $eccLength;
  27. public $b1;
  28. public function __construct(QRinput $input)
  29. {
  30. $spec = array(0, 0, 0, 0, 0);
  31. $this->datacode = $input->getByteStream();
  32. if (is_null($this->datacode)) {
  33. throw new Exception('null imput string');
  34. }
  35. QRspec::getEccSpec($input->getVersion(), $input->getErrorCorrectionLevel(), $spec);
  36. $this->version = $input->getVersion();
  37. $this->b1 = QRspec::rsBlockNum1($spec);
  38. $this->dataLength = QRspec::rsDataLength($spec);
  39. $this->eccLength = QRspec::rsEccLength($spec);
  40. $this->ecccode = array_fill(0, $this->eccLength, 0);
  41. $this->blocks = QRspec::rsBlockNum($spec);
  42. $ret = $this->init($spec);
  43. if ($ret < 0) {
  44. throw new Exception('block alloc error');
  45. return NULL;
  46. }
  47. $this->count = 0;
  48. }
  49. public function init(array $spec)
  50. {
  51. $dl = QRspec::rsDataCodes1($spec);
  52. $el = QRspec::rsEccCodes1($spec);
  53. $rs = QRrs::init_rs(8, 285, 0, 1, $el, 255 - $dl - $el);
  54. $blockNo = 0;
  55. $dataPos = 0;
  56. $eccPos = 0;
  57. for ($i = 0; $i < QRspec::rsBlockNum1($spec); $i++) {
  58. $ecc = array_slice($this->ecccode, $eccPos);
  59. $this->rsblocks[$blockNo] = new QRrsblock($dl, array_slice($this->datacode, $dataPos), $el, $ecc, $rs);
  60. $this->ecccode = array_merge(array_slice($this->ecccode, 0, $eccPos), $ecc);
  61. $dataPos += $dl;
  62. $eccPos += $el;
  63. $blockNo++;
  64. }
  65. if (QRspec::rsBlockNum2($spec) == 0) {
  66. return 0;
  67. }
  68. $dl = QRspec::rsDataCodes2($spec);
  69. $el = QRspec::rsEccCodes2($spec);
  70. $rs = QRrs::init_rs(8, 285, 0, 1, $el, 255 - $dl - $el);
  71. if ($rs == NULL) {
  72. return -1;
  73. }
  74. for ($i = 0; $i < QRspec::rsBlockNum2($spec); $i++) {
  75. $ecc = array_slice($this->ecccode, $eccPos);
  76. $this->rsblocks[$blockNo] = new QRrsblock($dl, array_slice($this->datacode, $dataPos), $el, $ecc, $rs);
  77. $this->ecccode = array_merge(array_slice($this->ecccode, 0, $eccPos), $ecc);
  78. $dataPos += $dl;
  79. $eccPos += $el;
  80. $blockNo++;
  81. }
  82. return 0;
  83. }
  84. public function getCode()
  85. {
  86. if ($this->count < $this->dataLength) {
  87. $row = $this->count % $this->blocks;
  88. $col = $this->count / $this->blocks;
  89. if ($this->rsblocks[0]->dataLength <= $col) {
  90. $row += $this->b1;
  91. }
  92. $ret = $this->rsblocks[$row]->data[$col];
  93. }
  94. else if ($this->count < $this->dataLength + $this->eccLength) {
  95. $row = ($this->count - $this->dataLength) % $this->blocks;
  96. $col = ($this->count - $this->dataLength) / $this->blocks;
  97. $ret = $this->rsblocks[$row]->ecc[$col];
  98. }
  99. else {
  100. return 0;
  101. }
  102. $this->count++;
  103. return $ret;
  104. }
  105. }
  106. class QRcode
  107. {
  108. public $version;
  109. public $width;
  110. public $data;
  111. public function encodeMask(QRinput $input, $mask)
  112. {
  113. if ($input->getVersion() < 0 || QRSPEC_VERSION_MAX < $input->getVersion()) {
  114. throw new Exception('wrong version');
  115. }
  116. if (QR_ECLEVEL_H < $input->getErrorCorrectionLevel()) {
  117. throw new Exception('wrong level');
  118. }
  119. $raw = new QRrawcode($input);
  120. QRtools::markTime('after_raw');
  121. $version = $raw->version;
  122. $width = QRspec::getWidth($version);
  123. $frame = QRspec::newFrame($version);
  124. $filler = new FrameFiller($width, $frame);
  125. if (is_null($filler)) {
  126. return NULL;
  127. }
  128. for ($i = 0; $i < $raw->dataLength + $raw->eccLength; $i++) {
  129. $code = $raw->getCode();
  130. $bit = 128;
  131. for ($j = 0; $j < 8; $j++) {
  132. $addr = $filler->next();
  133. $filler->setFrameAt($addr, 2 | ($bit & $code) != 0);
  134. $bit = $bit >> 1;
  135. }
  136. }
  137. QRtools::markTime('after_filler');
  138. unset($raw);
  139. $j = QRspec::getRemainder($version);
  140. for ($i = 0; $i < $j; $i++) {
  141. $addr = $filler->next();
  142. $filler->setFrameAt($addr, 2);
  143. }
  144. $frame = $filler->frame;
  145. unset($filler);
  146. $maskObj = new QRmask();
  147. if ($mask < 0) {
  148. if (QR_FIND_BEST_MASK) {
  149. $masked = $maskObj->mask($width, $frame, $input->getErrorCorrectionLevel());
  150. }
  151. else {
  152. $masked = $maskObj->makeMask($width, $frame, intval(QR_DEFAULT_MASK) % 8, $input->getErrorCorrectionLevel());
  153. }
  154. }
  155. else {
  156. $masked = $maskObj->makeMask($width, $frame, $mask, $input->getErrorCorrectionLevel());
  157. }
  158. if ($masked == NULL) {
  159. return NULL;
  160. }
  161. QRtools::markTime('after_mask');
  162. $this->version = $version;
  163. $this->width = $width;
  164. $this->data = $masked;
  165. return $this;
  166. }
  167. public function encodeInput(QRinput $input)
  168. {
  169. return $this->encodeMask($input, -1);
  170. }
  171. public function encodeString8bit($string, $version, $level)
  172. {
  173. if (string == NULL) {
  174. throw new Exception('empty string!');
  175. return NULL;
  176. }
  177. $input = new QRinput($version, $level);
  178. if ($input == NULL) {
  179. return NULL;
  180. }
  181. $ret = $input->append($input, QR_MODE_8, strlen($string), str_split($string));
  182. if ($ret < 0) {
  183. unset($input);
  184. return NULL;
  185. }
  186. return $this->encodeInput($input);
  187. }
  188. public function encodeString($string, $version, $level, $hint, $casesensitive)
  189. {
  190. if ($hint != QR_MODE_8 && $hint != QR_MODE_KANJI) {
  191. throw new Exception('bad hint');
  192. return NULL;
  193. }
  194. $input = new QRinput($version, $level);
  195. if ($input == NULL) {
  196. return NULL;
  197. }
  198. $ret = QRsplit::splitStringToQRinput($string, $input, $hint, $casesensitive);
  199. if ($ret < 0) {
  200. return NULL;
  201. }
  202. return $this->encodeInput($input);
  203. }
  204. static public function png($text, $outfile = false, $level = QR_ECLEVEL_L, $size = 3, $margin = 4, $saveandprint = false)
  205. {
  206. $enc = QRencode::factory($level, $size, $margin);
  207. return $enc->encodePNG($text, $outfile, $saveandprint = false);
  208. }
  209. static public function text($text, $outfile = false, $level = QR_ECLEVEL_L, $size = 3, $margin = 4)
  210. {
  211. $enc = QRencode::factory($level, $size, $margin);
  212. return $enc->encode($text, $outfile);
  213. }
  214. static public function raw($text, $outfile = false, $level = QR_ECLEVEL_L, $size = 3, $margin = 4)
  215. {
  216. $enc = QRencode::factory($level, $size, $margin);
  217. return $enc->encodeRAW($text, $outfile);
  218. }
  219. }
  220. class FrameFiller
  221. {
  222. public $width;
  223. public $frame;
  224. public $x;
  225. public $y;
  226. public $dir;
  227. public $bit;
  228. public function __construct($width, &$frame)
  229. {
  230. $this->width = $width;
  231. $this->frame = $frame;
  232. $this->x = $width - 1;
  233. $this->y = $width - 1;
  234. $this->dir = -1;
  235. $this->bit = -1;
  236. }
  237. public function setFrameAt($at, $val)
  238. {
  239. $this->frame[$at['y']][$at['x']] = chr($val);
  240. }
  241. public function getFrameAt($at)
  242. {
  243. return ord($this->frame[$at['y']][$at['x']]);
  244. }
  245. public function next()
  246. {
  247. do {
  248. if ($this->bit == -1) {
  249. $this->bit = 0;
  250. return array('x' => $this->x, 'y' => $this->y);
  251. }
  252. $x = $this->x;
  253. $y = $this->y;
  254. $w = $this->width;
  255. if ($this->bit == 0) {
  256. $x--;
  257. $this->bit++;
  258. }
  259. else {
  260. $x++;
  261. $y += $this->dir;
  262. $this->bit--;
  263. }
  264. if ($this->dir < 0) {
  265. if ($y < 0) {
  266. $y = 0;
  267. $x -= 2;
  268. $this->dir = 1;
  269. if ($x == 6) {
  270. $x--;
  271. $y = 9;
  272. }
  273. }
  274. }
  275. else if ($y == $w) {
  276. $y = $w - 1;
  277. $x -= 2;
  278. $this->dir = -1;
  279. if ($x == 6) {
  280. $x--;
  281. $y -= 8;
  282. }
  283. }
  284. if ($x < 0 || $y < 0) {
  285. return NULL;
  286. }
  287. $this->x = $x;
  288. $this->y = $y;
  289. } while (ord($this->frame[$y][$x]) & 128);
  290. return array('x' => $x, 'y' => $y);
  291. }
  292. }
  293. class QRencode
  294. {
  295. public $casesensitive = true;
  296. public $eightbit = false;
  297. public $version = 0;
  298. public $size = 3;
  299. public $margin = 4;
  300. public $structured = 0;
  301. public $level = QR_ECLEVEL_L;
  302. public $hint = QR_MODE_8;
  303. static public function factory($level = QR_ECLEVEL_L, $size = 3, $margin = 4)
  304. {
  305. $enc = new QRencode();
  306. $enc->size = $size;
  307. $enc->margin = $margin;
  308. switch ($level . '') {
  309. case '0':
  310. case '1':
  311. case '2':
  312. case '3':
  313. $enc->level = $level;
  314. break;
  315. case 'l':
  316. case 'L':
  317. $enc->level = QR_ECLEVEL_L;
  318. break;
  319. case 'm':
  320. case 'M':
  321. $enc->level = QR_ECLEVEL_M;
  322. break;
  323. case 'q':
  324. case 'Q':
  325. $enc->level = QR_ECLEVEL_Q;
  326. break;
  327. case 'h':
  328. case 'H':
  329. $enc->level = QR_ECLEVEL_H;
  330. break;
  331. }
  332. return $enc;
  333. }
  334. public function encodeRAW($intext, $outfile = false)
  335. {
  336. $code = new QRcode();
  337. if ($this->eightbit) {
  338. $code->encodeString8bit($intext, $this->version, $this->level);
  339. }
  340. else {
  341. $code->encodeString($intext, $this->version, $this->level, $this->hint, $this->casesensitive);
  342. }
  343. return $code->data;
  344. }
  345. public function encode($intext, $outfile = false)
  346. {
  347. $code = new QRcode();
  348. if ($this->eightbit) {
  349. $code->encodeString8bit($intext, $this->version, $this->level);
  350. }
  351. else {
  352. $code->encodeString($intext, $this->version, $this->level, $this->hint, $this->casesensitive);
  353. }
  354. QRtools::markTime('after_encode');
  355. if ($outfile !== false) {
  356. file_put_contents($outfile, join('
  357. ', QRtools::binarize($code->data)));
  358. }
  359. else {
  360. return QRtools::binarize($code->data);
  361. }
  362. }
  363. public function encodePNG($intext, $outfile = false, $saveandprint = false)
  364. {
  365. try {
  366. ob_start();
  367. $tab = $this->encode($intext);
  368. $err = ob_get_contents();
  369. ob_end_clean();
  370. if ($err != '') {
  371. QRtools::log($outfile, $err);
  372. }
  373. $maxSize = (int) (QR_PNG_MAXIMUM_SIZE / (count($tab) + 2 * $this->margin));
  374. QRimage::png($tab, $outfile, min(max(1, $this->size), $maxSize), $this->margin, $saveandprint);
  375. }
  376. catch (Exception $e) {
  377. QRtools::log($outfile, $e->getMessage());
  378. }
  379. }
  380. }
  381. ?>