QRcode.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. /**
  3. * QRcode.php
  4. *
  5. * Created by arielferrandini
  6. */
  7. namespace PHPQRCode;
  8. use Exception;
  9. class QRcode {
  10. public $version;
  11. public $width;
  12. public $data;
  13. //----------------------------------------------------------------------
  14. public function encodeMask(QRinput $input, $mask)
  15. {
  16. if($input->getVersion() < 0 || $input->getVersion() > Constants::QRSPEC_VERSION_MAX) {
  17. throw new Exception('wrong version');
  18. }
  19. if($input->getErrorCorrectionLevel() > Constants::QR_ECLEVEL_H) {
  20. throw new Exception('wrong level');
  21. }
  22. $raw = new QRrawcode($input);
  23. QRtools::markTime('after_raw');
  24. $version = $raw->version;
  25. $width = QRspec::getWidth($version);
  26. $frame = QRspec::newFrame($version);
  27. $filler = new FrameFiller($width, $frame);
  28. if(is_null($filler)) {
  29. return NULL;
  30. }
  31. // inteleaved data and ecc codes
  32. for($i=0; $i<$raw->dataLength + $raw->eccLength; $i++) {
  33. $code = $raw->getCode();
  34. $bit = 0x80;
  35. for($j=0; $j<8; $j++) {
  36. $addr = $filler->next();
  37. $filler->setFrameAt($addr, 0x02 | (($bit & $code) != 0));
  38. $bit = $bit >> 1;
  39. }
  40. }
  41. QRtools::markTime('after_filler');
  42. unset($raw);
  43. // remainder bits
  44. $j = QRspec::getRemainder($version);
  45. for($i=0; $i<$j; $i++) {
  46. $addr = $filler->next();
  47. $filler->setFrameAt($addr, 0x02);
  48. }
  49. $frame = $filler->frame;
  50. unset($filler);
  51. // masking
  52. $maskObj = new QRmask();
  53. if($mask < 0) {
  54. if (Constants::QR_FIND_BEST_MASK) {
  55. $masked = $maskObj->mask($width, $frame, $input->getErrorCorrectionLevel());
  56. } else {
  57. $masked = $maskObj->makeMask($width, $frame, (intval(Constants::QR_DEFAULT_MASK) % 8), $input->getErrorCorrectionLevel());
  58. }
  59. } else {
  60. $masked = $maskObj->makeMask($width, $frame, $mask, $input->getErrorCorrectionLevel());
  61. }
  62. if($masked == NULL) {
  63. return NULL;
  64. }
  65. QRtools::markTime('after_mask');
  66. $this->version = $version;
  67. $this->width = $width;
  68. $this->data = $masked;
  69. return $this;
  70. }
  71. //----------------------------------------------------------------------
  72. public function encodeInput(QRinput $input)
  73. {
  74. return $this->encodeMask($input, -1);
  75. }
  76. //----------------------------------------------------------------------
  77. public function encodeString8bit($string, $version, $level)
  78. {
  79. if(string == NULL) {
  80. throw new Exception('empty string!');
  81. return NULL;
  82. }
  83. $input = new QRinput($version, $level);
  84. if($input == NULL) return NULL;
  85. $ret = $input->append($input, Constants::QR_MODE_8, strlen($string), str_split($string));
  86. if($ret < 0) {
  87. unset($input);
  88. return NULL;
  89. }
  90. return $this->encodeInput($input);
  91. }
  92. //----------------------------------------------------------------------
  93. public function encodeString($string, $version, $level, $hint, $casesensitive)
  94. {
  95. if($hint != Constants::QR_MODE_8 && $hint != Constants::QR_MODE_KANJI) {
  96. throw new Exception('bad hint');
  97. return NULL;
  98. }
  99. $input = new QRinput($version, $level);
  100. if($input == NULL) return NULL;
  101. $ret = QRsplit::splitStringToQRinput($string, $input, $hint, $casesensitive);
  102. if($ret < 0) {
  103. return NULL;
  104. }
  105. return $this->encodeInput($input);
  106. }
  107. //----------------------------------------------------------------------
  108. public static function png($text, $outfile = false, $level = Constants::QR_ECLEVEL_L, $size = 3, $margin = 4, $saveandprint=false)
  109. {
  110. $enc = QRencode::factory($level, $size, $margin);
  111. return $enc->encodePNG($text, $outfile, $saveandprint=false);
  112. }
  113. //----------------------------------------------------------------------
  114. public static function text($text, $outfile = false, $level = Constants::QR_ECLEVEL_L, $size = 3, $margin = 4)
  115. {
  116. $enc = QRencode::factory($level, $size, $margin);
  117. return $enc->encode($text, $outfile);
  118. }
  119. //----------------------------------------------------------------------
  120. public static function raw($text, $outfile = false, $level = Constants::QR_ECLEVEL_L, $size = 3, $margin = 4)
  121. {
  122. $enc = QRencode::factory($level, $size, $margin);
  123. return $enc->encodeRAW($text, $outfile);
  124. }
  125. }