qrencode.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  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, $back_color = 0xFFFFFF, $fore_color = 0x000000)
  230. {
  231. $enc = QRencode::factory($level, $size, $margin, $back_color, $fore_color);
  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 eps($text, $outfile = false, $level = QR_ECLEVEL_L, $size = 3, $margin = 4, $saveandprint=false, $back_color = 0xFFFFFF, $fore_color = 0x000000)
  242. {
  243. $enc = QRencode::factory($level, $size, $margin, $back_color, $fore_color);
  244. return $enc->encodeEPS($text, $outfile, $saveandprint=false);
  245. }
  246. //----------------------------------------------------------------------
  247. public static function svg($text, $outfile = false, $level = QR_ECLEVEL_L, $size = 3, $margin = 4, $saveandprint=false, $back_color = 0xFFFFFF, $fore_color = 0x000000)
  248. {
  249. $enc = QRencode::factory($level, $size, $margin, $back_color, $fore_color);
  250. return $enc->encodeSVG($text, $outfile, $saveandprint=false);
  251. }
  252. //----------------------------------------------------------------------
  253. public static function raw($text, $outfile = false, $level = QR_ECLEVEL_L, $size = 3, $margin = 4)
  254. {
  255. $enc = QRencode::factory($level, $size, $margin);
  256. return $enc->encodeRAW($text, $outfile);
  257. }
  258. }
  259. //##########################################################################
  260. class FrameFiller {
  261. public $width;
  262. public $frame;
  263. public $x;
  264. public $y;
  265. public $dir;
  266. public $bit;
  267. //----------------------------------------------------------------------
  268. public function __construct($width, &$frame)
  269. {
  270. $this->width = $width;
  271. $this->frame = $frame;
  272. $this->x = $width - 1;
  273. $this->y = $width - 1;
  274. $this->dir = -1;
  275. $this->bit = -1;
  276. }
  277. //----------------------------------------------------------------------
  278. public function setFrameAt($at, $val)
  279. {
  280. $this->frame[$at['y']][$at['x']] = chr($val);
  281. }
  282. //----------------------------------------------------------------------
  283. public function getFrameAt($at)
  284. {
  285. return ord($this->frame[$at['y']][$at['x']]);
  286. }
  287. //----------------------------------------------------------------------
  288. public function next()
  289. {
  290. do {
  291. if($this->bit == -1) {
  292. $this->bit = 0;
  293. return array('x'=>$this->x, 'y'=>$this->y);
  294. }
  295. $x = $this->x;
  296. $y = $this->y;
  297. $w = $this->width;
  298. if($this->bit == 0) {
  299. $x--;
  300. $this->bit++;
  301. } else {
  302. $x++;
  303. $y += $this->dir;
  304. $this->bit--;
  305. }
  306. if($this->dir < 0) {
  307. if($y < 0) {
  308. $y = 0;
  309. $x -= 2;
  310. $this->dir = 1;
  311. if($x == 6) {
  312. $x--;
  313. $y = 9;
  314. }
  315. }
  316. } else {
  317. if($y == $w) {
  318. $y = $w - 1;
  319. $x -= 2;
  320. $this->dir = -1;
  321. if($x == 6) {
  322. $x--;
  323. $y -= 8;
  324. }
  325. }
  326. }
  327. if($x < 0 || $y < 0) return null;
  328. $this->x = $x;
  329. $this->y = $y;
  330. } while(ord($this->frame[$y][$x]) & 0x80);
  331. return array('x'=>$x, 'y'=>$y);
  332. }
  333. } ;
  334. //##########################################################################
  335. class QRencode {
  336. public $casesensitive = true;
  337. public $eightbit = false;
  338. public $version = 0;
  339. public $size = 3;
  340. public $margin = 4;
  341. public $back_color = 0xFFFFFF;
  342. public $fore_color = 0x000000;
  343. public $structured = 0; // not supported yet
  344. public $level = QR_ECLEVEL_L;
  345. public $hint = QR_MODE_8;
  346. //----------------------------------------------------------------------
  347. public static function factory($level = QR_ECLEVEL_L, $size = 3, $margin = 4, $back_color = 0xFFFFFF, $fore_color = 0x000000)
  348. {
  349. $enc = new QRencode();
  350. $enc->size = $size;
  351. $enc->margin = $margin;
  352. $enc->fore_color = $fore_color;
  353. $enc->back_color = $back_color;
  354. switch ($level.'') {
  355. case '0':
  356. case '1':
  357. case '2':
  358. case '3':
  359. $enc->level = $level;
  360. break;
  361. case 'l':
  362. case 'L':
  363. $enc->level = QR_ECLEVEL_L;
  364. break;
  365. case 'm':
  366. case 'M':
  367. $enc->level = QR_ECLEVEL_M;
  368. break;
  369. case 'q':
  370. case 'Q':
  371. $enc->level = QR_ECLEVEL_Q;
  372. break;
  373. case 'h':
  374. case 'H':
  375. $enc->level = QR_ECLEVEL_H;
  376. break;
  377. }
  378. return $enc;
  379. }
  380. //----------------------------------------------------------------------
  381. public function encodeRAW($intext, $outfile = false)
  382. {
  383. $code = new QRcode();
  384. if($this->eightbit) {
  385. $code->encodeString8bit($intext, $this->version, $this->level);
  386. } else {
  387. $code->encodeString($intext, $this->version, $this->level, $this->hint, $this->casesensitive);
  388. }
  389. return $code->data;
  390. }
  391. //----------------------------------------------------------------------
  392. public function encode($intext, $outfile = false)
  393. {
  394. $code = new QRcode();
  395. if($this->eightbit) {
  396. $code->encodeString8bit($intext, $this->version, $this->level);
  397. } else {
  398. $code->encodeString($intext, $this->version, $this->level, $this->hint, $this->casesensitive);
  399. }
  400. QRtools::markTime('after_encode');
  401. if ($outfile!== false) {
  402. file_put_contents($outfile, join("\n", QRtools::binarize($code->data)));
  403. } else {
  404. return QRtools::binarize($code->data);
  405. }
  406. }
  407. //----------------------------------------------------------------------
  408. public function encodePNG($intext, $outfile = false,$saveandprint=false)
  409. {
  410. try {
  411. ob_start();
  412. $tab = $this->encode($intext);
  413. $err = ob_get_contents();
  414. ob_end_clean();
  415. if ($err != '')
  416. QRtools::log($outfile, $err);
  417. $maxSize = (int)(QR_PNG_MAXIMUM_SIZE / (count($tab)+2*$this->margin));
  418. QRimage::png($tab, $outfile, min(max(1, $this->size), $maxSize), $this->margin,$saveandprint, $this->back_color, $this->fore_color);
  419. } catch (Exception $e) {
  420. QRtools::log($outfile, $e->getMessage());
  421. }
  422. }
  423. //----------------------------------------------------------------------
  424. public function encodeEPS($intext, $outfile = false,$saveandprint=false)
  425. {
  426. try {
  427. ob_start();
  428. $tab = $this->encode($intext);
  429. $err = ob_get_contents();
  430. ob_end_clean();
  431. if ($err != '')
  432. QRtools::log($outfile, $err);
  433. $maxSize = (int)(QR_PNG_MAXIMUM_SIZE / (count($tab)+2*$this->margin));
  434. return QRvect::eps($tab, $outfile, min(max(1, $this->size), $maxSize), $this->margin,$saveandprint, $this->back_color, $this->fore_color);
  435. } catch (Exception $e) {
  436. QRtools::log($outfile, $e->getMessage());
  437. }
  438. }
  439. //----------------------------------------------------------------------
  440. public function encodeSVG($intext, $outfile = false,$saveandprint=false)
  441. {
  442. try {
  443. ob_start();
  444. $tab = $this->encode($intext);
  445. $err = ob_get_contents();
  446. ob_end_clean();
  447. if ($err != '')
  448. QRtools::log($outfile, $err);
  449. $maxSize = (int)(QR_PNG_MAXIMUM_SIZE / (count($tab)+2*$this->margin));
  450. return QRvect::svg($tab, $outfile, min(max(1, $this->size), $maxSize), $this->margin,$saveandprint, $this->back_color, $this->fore_color);
  451. } catch (Exception $e) {
  452. QRtools::log($outfile, $e->getMessage());
  453. }
  454. }
  455. }