qrencode.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  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. if($this->count < $this->dataLength) {
  111. $row = $this->count % $this->blocks;
  112. $col = $this->count / $this->blocks;
  113. if($col >= $this->rsblocks[0]->dataLength) {
  114. $row += $this->b1;
  115. }
  116. $ret = $this->rsblocks[$row]->data[$col];
  117. } else if($this->count < $this->dataLength + $this->eccLength) {
  118. $row = ($this->count - $this->dataLength) % $this->blocks;
  119. $col = ($this->count - $this->dataLength) / $this->blocks;
  120. $ret = $this->rsblocks[$row]->ecc[$col];
  121. } else {
  122. return 0;
  123. }
  124. $this->count++;
  125. return $ret;
  126. }
  127. }
  128. //##########################################################################
  129. class QRcode {
  130. public $version;
  131. public $width;
  132. public $data;
  133. //----------------------------------------------------------------------
  134. public function encodeMask(QRinput $input, $mask)
  135. {
  136. if($input->getVersion() < 0 || $input->getVersion() > QRSPEC_VERSION_MAX) {
  137. throw new Exception('wrong version');
  138. }
  139. if($input->getErrorCorrectionLevel() > QR_ECLEVEL_H) {
  140. throw new Exception('wrong level');
  141. }
  142. $raw = new QRrawcode($input);
  143. QRtools::markTime('after_raw');
  144. $version = $raw->version;
  145. $width = QRspec::getWidth($version);
  146. $frame = QRspec::newFrame($version);
  147. $filler = new FrameFiller($width, $frame);
  148. if(is_null($filler)) {
  149. return NULL;
  150. }
  151. // inteleaved data and ecc codes
  152. for($i=0; $i<$raw->dataLength + $raw->eccLength; $i++) {
  153. $code = $raw->getCode();
  154. $bit = 0x80;
  155. for($j=0; $j<8; $j++) {
  156. $addr = $filler->next();
  157. $filler->setFrameAt($addr, 0x02 | (($bit & $code) != 0));
  158. $bit = $bit >> 1;
  159. }
  160. }
  161. QRtools::markTime('after_filler');
  162. unset($raw);
  163. // remainder bits
  164. $j = QRspec::getRemainder($version);
  165. for($i=0; $i<$j; $i++) {
  166. $addr = $filler->next();
  167. $filler->setFrameAt($addr, 0x02);
  168. }
  169. $frame = $filler->frame;
  170. unset($filler);
  171. // masking
  172. $maskObj = new QRmask();
  173. if($mask < 0) {
  174. if (QR_FIND_BEST_MASK) {
  175. $masked = $maskObj->mask($width, $frame, $input->getErrorCorrectionLevel());
  176. } else {
  177. $masked = $maskObj->makeMask($width, $frame, (intval(QR_DEFAULT_MASK) % 8), $input->getErrorCorrectionLevel());
  178. }
  179. } else {
  180. $masked = $maskObj->makeMask($width, $frame, $mask, $input->getErrorCorrectionLevel());
  181. }
  182. if($masked == NULL) {
  183. return NULL;
  184. }
  185. QRtools::markTime('after_mask');
  186. $this->version = $version;
  187. $this->width = $width;
  188. $this->data = $masked;
  189. return $this;
  190. }
  191. //----------------------------------------------------------------------
  192. public function encodeInput(QRinput $input)
  193. {
  194. return $this->encodeMask($input, -1);
  195. }
  196. //----------------------------------------------------------------------
  197. public function encodeString8bit($string, $version, $level)
  198. {
  199. if(string == NULL) {
  200. throw new Exception('empty string!');
  201. return NULL;
  202. }
  203. $input = new QRinput($version, $level);
  204. if($input == NULL) return NULL;
  205. $ret = $input->append($input, QR_MODE_8, strlen($string), str_split($string));
  206. if($ret < 0) {
  207. unset($input);
  208. return NULL;
  209. }
  210. return $this->encodeInput($input);
  211. }
  212. //----------------------------------------------------------------------
  213. public function encodeString($string, $version, $level, $hint, $casesensitive)
  214. {
  215. if($hint != QR_MODE_8 && $hint != QR_MODE_KANJI) {
  216. throw new Exception('bad hint');
  217. return NULL;
  218. }
  219. $input = new QRinput($version, $level);
  220. if($input == NULL) return NULL;
  221. $ret = QRsplit::splitStringToQRinput($string, $input, $hint, $casesensitive);
  222. if($ret < 0) {
  223. return NULL;
  224. }
  225. return $this->encodeInput($input);
  226. }
  227. //----------------------------------------------------------------------
  228. public static function png($text, $outfile = false, $level = QR_ECLEVEL_L, $size = 3, $margin = 4, $saveandprint=false)
  229. {
  230. $enc = QRencode::factory($level, $size, $margin);
  231. return $enc->encodePNG($text, $outfile, $saveandprint=false);
  232. }
  233. //----------------------------------------------------------------------
  234. public static function text($text, $outfile = false, $level = QR_ECLEVEL_L, $size = 3, $margin = 4)
  235. {
  236. $enc = QRencode::factory($level, $size, $margin);
  237. return $enc->encode($text, $outfile);
  238. }
  239. //----------------------------------------------------------------------
  240. public static function raw($text, $outfile = false, $level = QR_ECLEVEL_L, $size = 3, $margin = 4)
  241. {
  242. $enc = QRencode::factory($level, $size, $margin);
  243. return $enc->encodeRAW($text, $outfile);
  244. }
  245. }
  246. //##########################################################################
  247. class FrameFiller {
  248. public $width;
  249. public $frame;
  250. public $x;
  251. public $y;
  252. public $dir;
  253. public $bit;
  254. //----------------------------------------------------------------------
  255. public function __construct($width, &$frame)
  256. {
  257. $this->width = $width;
  258. $this->frame = $frame;
  259. $this->x = $width - 1;
  260. $this->y = $width - 1;
  261. $this->dir = -1;
  262. $this->bit = -1;
  263. }
  264. //----------------------------------------------------------------------
  265. public function setFrameAt($at, $val)
  266. {
  267. $this->frame[$at['y']][$at['x']] = chr($val);
  268. }
  269. //----------------------------------------------------------------------
  270. public function getFrameAt($at)
  271. {
  272. return ord($this->frame[$at['y']][$at['x']]);
  273. }
  274. //----------------------------------------------------------------------
  275. public function next()
  276. {
  277. do {
  278. if($this->bit == -1) {
  279. $this->bit = 0;
  280. return array('x'=>$this->x, 'y'=>$this->y);
  281. }
  282. $x = $this->x;
  283. $y = $this->y;
  284. $w = $this->width;
  285. if($this->bit == 0) {
  286. $x--;
  287. $this->bit++;
  288. } else {
  289. $x++;
  290. $y += $this->dir;
  291. $this->bit--;
  292. }
  293. if($this->dir < 0) {
  294. if($y < 0) {
  295. $y = 0;
  296. $x -= 2;
  297. $this->dir = 1;
  298. if($x == 6) {
  299. $x--;
  300. $y = 9;
  301. }
  302. }
  303. } else {
  304. if($y == $w) {
  305. $y = $w - 1;
  306. $x -= 2;
  307. $this->dir = -1;
  308. if($x == 6) {
  309. $x--;
  310. $y -= 8;
  311. }
  312. }
  313. }
  314. if($x < 0 || $y < 0) return null;
  315. $this->x = $x;
  316. $this->y = $y;
  317. } while(ord($this->frame[$y][$x]) & 0x80);
  318. return array('x'=>$x, 'y'=>$y);
  319. }
  320. } ;
  321. //##########################################################################
  322. class QRencode {
  323. public $casesensitive = true;
  324. public $eightbit = false;
  325. public $version = 0;
  326. public $size = 3;
  327. public $margin = 4;
  328. public $structured = 0; // not supported yet
  329. public $level = QR_ECLEVEL_L;
  330. public $hint = QR_MODE_8;
  331. //----------------------------------------------------------------------
  332. public static function factory($level = QR_ECLEVEL_L, $size = 3, $margin = 4)
  333. {
  334. $enc = new QRencode();
  335. $enc->size = $size;
  336. $enc->margin = $margin;
  337. switch ($level.'') {
  338. case '0':
  339. case '1':
  340. case '2':
  341. case '3':
  342. $enc->level = $level;
  343. break;
  344. case 'l':
  345. case 'L':
  346. $enc->level = QR_ECLEVEL_L;
  347. break;
  348. case 'm':
  349. case 'M':
  350. $enc->level = QR_ECLEVEL_M;
  351. break;
  352. case 'q':
  353. case 'Q':
  354. $enc->level = QR_ECLEVEL_Q;
  355. break;
  356. case 'h':
  357. case 'H':
  358. $enc->level = QR_ECLEVEL_H;
  359. break;
  360. }
  361. return $enc;
  362. }
  363. //----------------------------------------------------------------------
  364. public function encodeRAW($intext, $outfile = false)
  365. {
  366. $code = new QRcode();
  367. if($this->eightbit) {
  368. $code->encodeString8bit($intext, $this->version, $this->level);
  369. } else {
  370. $code->encodeString($intext, $this->version, $this->level, $this->hint, $this->casesensitive);
  371. }
  372. return $code->data;
  373. }
  374. //----------------------------------------------------------------------
  375. public function encode($intext, $outfile = false)
  376. {
  377. $code = new QRcode();
  378. if($this->eightbit) {
  379. $code->encodeString8bit($intext, $this->version, $this->level);
  380. } else {
  381. $code->encodeString($intext, $this->version, $this->level, $this->hint, $this->casesensitive);
  382. }
  383. QRtools::markTime('after_encode');
  384. if ($outfile!== false) {
  385. file_put_contents($outfile, join("\n", QRtools::binarize($code->data)));
  386. } else {
  387. return QRtools::binarize($code->data);
  388. }
  389. }
  390. //----------------------------------------------------------------------
  391. public function encodePNG($intext, $outfile = false,$saveandprint=false)
  392. {
  393. try {
  394. ob_start();
  395. $tab = $this->encode($intext);
  396. $err = ob_get_contents();
  397. ob_end_clean();
  398. if ($err != '')
  399. QRtools::log($outfile, $err);
  400. $maxSize = (int)(QR_PNG_MAXIMUM_SIZE / (count($tab)+2*$this->margin));
  401. QRimage::png($tab, $outfile, min(max(1, $this->size), $maxSize), $this->margin,$saveandprint);
  402. } catch (Exception $e) {
  403. QRtools::log($outfile, $e->getMessage());
  404. }
  405. }
  406. }