QRCode.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. <?php
  2. /**
  3. * Class QRCode
  4. *
  5. * @filesource QRCode.php
  6. * @created 26.11.2015
  7. * @package chillerlan\QRCode
  8. * @author Smiley <smiley@chillerlan.net>
  9. * @copyright 2015 Smiley
  10. * @license MIT
  11. */
  12. namespace chillerlan\QRCode;
  13. use chillerlan\QRCode\Data\{
  14. AlphaNum, Byte, Kanji, MaskPatternTester, Number, QRCodeDataException, QRDataInterface, QRMatrix
  15. };
  16. use chillerlan\QRCode\Output\{
  17. QRCodeOutputException, QRImage, QRMarkup, QROutputInterface, QRString
  18. };
  19. use chillerlan\Settings\SettingsContainerInterface;
  20. /**
  21. * Turns a text string into a Model 2 QR Code
  22. *
  23. * @link https://github.com/kazuhikoarase/qrcode-generator/tree/master/php
  24. * @link http://www.qrcode.com/en/codes/model12.html
  25. * @link http://www.thonky.com/qr-code-tutorial/
  26. */
  27. class QRCode{
  28. /**
  29. * API constants
  30. */
  31. const OUTPUT_MARKUP_HTML = 'html';
  32. const OUTPUT_MARKUP_SVG = 'svg';
  33. # const OUTPUT_MARKUP_EPS = 'eps';
  34. # const OUTPUT_MARKUP_XML = 'xml'; // anyone?
  35. const OUTPUT_IMAGE_PNG = 'png';
  36. const OUTPUT_IMAGE_JPG = 'jpg';
  37. const OUTPUT_IMAGE_GIF = 'gif';
  38. const OUTPUT_STRING_JSON = 'json';
  39. const OUTPUT_STRING_TEXT = 'text';
  40. const OUTPUT_CUSTOM = 'custom';
  41. const VERSION_AUTO = -1;
  42. const MASK_PATTERN_AUTO = -1;
  43. const ECC_L = 0b01; // 7%.
  44. const ECC_M = 0b00; // 15%.
  45. const ECC_Q = 0b11; // 25%.
  46. const ECC_H = 0b10; // 30%.
  47. const DATA_NUMBER = 0b0001;
  48. const DATA_ALPHANUM = 0b0010;
  49. const DATA_BYTE = 0b0100;
  50. const DATA_KANJI = 0b1000;
  51. const ECC_MODES = [
  52. self::ECC_L => 0,
  53. self::ECC_M => 1,
  54. self::ECC_Q => 2,
  55. self::ECC_H => 3,
  56. ];
  57. const DATA_MODES = [
  58. self::DATA_NUMBER => 0,
  59. self::DATA_ALPHANUM => 1,
  60. self::DATA_BYTE => 2,
  61. self::DATA_KANJI => 3,
  62. ];
  63. const OUTPUT_MODES = [
  64. QRMarkup::class => [
  65. self::OUTPUT_MARKUP_SVG,
  66. self::OUTPUT_MARKUP_HTML,
  67. # self::OUTPUT_MARKUP_EPS,
  68. ],
  69. QRImage::class => [
  70. self::OUTPUT_IMAGE_PNG,
  71. self::OUTPUT_IMAGE_GIF,
  72. self::OUTPUT_IMAGE_JPG,
  73. ],
  74. QRString::class => [
  75. self::OUTPUT_STRING_JSON,
  76. self::OUTPUT_STRING_TEXT,
  77. ]
  78. ];
  79. /**
  80. * @var \chillerlan\QRCode\QROptions
  81. */
  82. protected $options;
  83. /**
  84. * @var \chillerlan\QRCode\Data\QRDataInterface
  85. */
  86. protected $dataInterface;
  87. /**
  88. * QRCode constructor.
  89. *
  90. * @param \chillerlan\Settings\SettingsContainerInterface|null $options
  91. */
  92. public function __construct(SettingsContainerInterface $options = null){
  93. mb_internal_encoding('UTF-8');
  94. $this->options = $options ?? new QROptions;
  95. }
  96. /**
  97. * Renders a QR Code for the given $data and QROptions
  98. *
  99. * @param string $data
  100. * @param string|null $file
  101. *
  102. * @return mixed
  103. */
  104. public function render(string $data, string $file = null){
  105. return $this->initOutputInterface($data)->dump($file);
  106. }
  107. /**
  108. * Returns a QRMatrix object for the given $data and current QROptions
  109. *
  110. * @param string $data
  111. *
  112. * @return \chillerlan\QRCode\Data\QRMatrix
  113. * @throws \chillerlan\QRCode\Data\QRCodeDataException
  114. */
  115. public function getMatrix(string $data):QRMatrix {
  116. // https://github.com/chillerlan/php-qrcode/pull/15
  117. // NOTE: input sanitization should be done outside
  118. // $data = trim($data);
  119. if(empty($data)){
  120. throw new QRCodeDataException('QRCode::getMatrix() No data given.');
  121. }
  122. $this->dataInterface = $this->initDataInterface($data);
  123. $maskPattern = $this->options->maskPattern === $this::MASK_PATTERN_AUTO
  124. ? $this->getBestMaskPattern()
  125. : min(7, max(0, (int)$this->options->maskPattern));
  126. $matrix = $this
  127. ->dataInterface
  128. ->initMatrix($maskPattern)
  129. ;
  130. if((bool)$this->options->addQuietzone){
  131. $matrix->setQuietZone($this->options->quietzoneSize);
  132. }
  133. return $matrix;
  134. }
  135. /**
  136. * shoves a QRMatrix through the MaskPatternTester to find the lowest penalty mask pattern
  137. *
  138. * @see \chillerlan\QRCode\Data\MaskPatternTester
  139. *
  140. * @return int
  141. */
  142. protected function getBestMaskPattern():int{
  143. $penalties = [];
  144. for($testPattern = 0; $testPattern < 8; $testPattern++){
  145. $matrix = $this
  146. ->dataInterface
  147. ->initMatrix($testPattern, true);
  148. $penalties[$testPattern] = (new MaskPatternTester($matrix))->testPattern();
  149. }
  150. return array_search(min($penalties), $penalties, true);
  151. }
  152. /**
  153. * returns a fresh QRDataInterface for the given $data
  154. *
  155. * @param string $data
  156. *
  157. * @return \chillerlan\QRCode\Data\QRDataInterface
  158. * @throws \chillerlan\QRCode\Data\QRCodeDataException
  159. */
  160. public function initDataInterface(string $data):QRDataInterface{
  161. $DATA_MODES = [
  162. Number::class => 'Number',
  163. AlphaNum::class => 'AlphaNum',
  164. Kanji::class => 'Kanji',
  165. Byte::class => 'Byte',
  166. ];
  167. foreach($DATA_MODES as $dataInterface => $mode){
  168. if(call_user_func_array([$this, 'is'.$mode], [$data]) === true && class_exists($dataInterface)){
  169. return new $dataInterface($this->options, $data);
  170. }
  171. }
  172. throw new QRCodeDataException('invalid data type'); // @codeCoverageIgnore
  173. }
  174. /**
  175. * returns a fresh (built-in) QROutputInterface
  176. *
  177. * @param string $data
  178. *
  179. * @return \chillerlan\QRCode\Output\QROutputInterface
  180. * @throws \chillerlan\QRCode\Output\QRCodeOutputException
  181. */
  182. protected function initOutputInterface(string $data):QROutputInterface{
  183. if($this->options->outputType === $this::OUTPUT_CUSTOM && class_exists($this->options->outputInterface)){
  184. return new $this->options->outputInterface($this->options, $this->getMatrix($data));
  185. }
  186. foreach($this::OUTPUT_MODES as $outputInterface => $modes){
  187. if(in_array($this->options->outputType, $modes, true) && class_exists($outputInterface)){
  188. return new $outputInterface($this->options, $this->getMatrix($data));
  189. }
  190. }
  191. throw new QRCodeOutputException('invalid output type');
  192. }
  193. /**
  194. * checks if a string qualifies as numeric
  195. *
  196. * @param string $string
  197. *
  198. * @return bool
  199. */
  200. public function isNumber(string $string):bool {
  201. return $this->checkString($string, Number::CHAR_MAP);
  202. }
  203. /**
  204. * checks if a string qualifies as alphanumeric
  205. *
  206. * @param string $string
  207. *
  208. * @return bool
  209. */
  210. public function isAlphaNum(string $string):bool {
  211. return $this->checkString($string, AlphaNum::CHAR_MAP);
  212. }
  213. /**
  214. * checks is a given $string matches the characters of a given $charmap, returns false on the first invalid occurence.
  215. *
  216. * @param string $string
  217. * @param array $charmap
  218. *
  219. * @return bool
  220. */
  221. protected function checkString(string $string, array $charmap):bool{
  222. $len = strlen($string);
  223. for($i = 0; $i < $len; $i++){
  224. if(!in_array($string[$i], $charmap, true)){
  225. return false;
  226. }
  227. }
  228. return true;
  229. }
  230. /**
  231. * checks if a string qualifies as Kanji
  232. *
  233. * @param string $string
  234. *
  235. * @return bool
  236. */
  237. public function isKanji(string $string):bool {
  238. $i = 0;
  239. $len = strlen($string);
  240. while($i + 1 < $len){
  241. $c = ((0xff&ord($string[$i])) << 8)|(0xff&ord($string[$i + 1]));
  242. if(!($c >= 0x8140 && $c <= 0x9FFC) && !($c >= 0xE040 && $c <= 0xEBBF)){
  243. return false;
  244. }
  245. $i += 2;
  246. }
  247. return $i >= $len;
  248. }
  249. /**
  250. * a dummy
  251. *
  252. * @param $data
  253. *
  254. * @return bool
  255. */
  256. protected function isByte(string $data):bool{
  257. return !empty($data);
  258. }
  259. }