qrencode.php 17 KB

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