QRCode.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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, QRFpdf, QRImage, QRImagick, QRMarkup, QROutputInterface, QRString
  18. };
  19. use chillerlan\Settings\SettingsContainerInterface;
  20. use function call_user_func_array, class_exists, in_array, ord, strlen, strtolower, str_split;
  21. /**
  22. * Turns a text string into a Model 2 QR Code
  23. *
  24. * @see https://github.com/kazuhikoarase/qrcode-generator/tree/master/php
  25. * @see http://www.qrcode.com/en/codes/model12.html
  26. * @see https://www.swisseduc.ch/informatik/theoretische_informatik/qr_codes/docs/qr_standard.pdf
  27. * @see https://en.wikipedia.org/wiki/QR_code
  28. * @see http://www.thonky.com/qr-code-tutorial/
  29. */
  30. class QRCode{
  31. /** @var int */
  32. public const VERSION_AUTO = -1;
  33. /** @var int */
  34. public const MASK_PATTERN_AUTO = -1;
  35. // ISO/IEC 18004:2000 Table 2
  36. /** @var int */
  37. public const DATA_NUMBER = 0b0001;
  38. /** @var int */
  39. public const DATA_ALPHANUM = 0b0010;
  40. /** @var int */
  41. public const DATA_BYTE = 0b0100;
  42. /** @var int */
  43. public const DATA_KANJI = 0b1000;
  44. /**
  45. * References to the keys of the following tables:
  46. *
  47. * @see \chillerlan\QRCode\Data\QRDataInterface::MAX_LENGTH
  48. *
  49. * @var int[]
  50. */
  51. public const DATA_MODES = [
  52. self::DATA_NUMBER => 0,
  53. self::DATA_ALPHANUM => 1,
  54. self::DATA_BYTE => 2,
  55. self::DATA_KANJI => 3,
  56. ];
  57. // ISO/IEC 18004:2000 Tables 12, 25
  58. /** @var int */
  59. public const ECC_L = 0b01; // 7%.
  60. /** @var int */
  61. public const ECC_M = 0b00; // 15%.
  62. /** @var int */
  63. public const ECC_Q = 0b11; // 25%.
  64. /** @var int */
  65. public const ECC_H = 0b10; // 30%.
  66. /**
  67. * References to the keys of the following tables:
  68. *
  69. * @see \chillerlan\QRCode\Data\QRDataInterface::MAX_BITS
  70. * @see \chillerlan\QRCode\Data\QRDataInterface::RSBLOCKS
  71. * @see \chillerlan\QRCode\Data\QRMatrix::formatPattern
  72. *
  73. * @var int[]
  74. */
  75. public const ECC_MODES = [
  76. self::ECC_L => 0,
  77. self::ECC_M => 1,
  78. self::ECC_Q => 2,
  79. self::ECC_H => 3,
  80. ];
  81. /** @var string */
  82. public const OUTPUT_MARKUP_HTML = 'html';
  83. /** @var string */
  84. public const OUTPUT_MARKUP_SVG = 'svg';
  85. /** @var string */
  86. public const OUTPUT_IMAGE_PNG = 'png';
  87. /** @var string */
  88. public const OUTPUT_IMAGE_JPG = 'jpg';
  89. /** @var string */
  90. public const OUTPUT_IMAGE_GIF = 'gif';
  91. /** @var string */
  92. public const OUTPUT_STRING_JSON = 'json';
  93. /** @var string */
  94. public const OUTPUT_STRING_TEXT = 'text';
  95. /** @var string */
  96. public const OUTPUT_IMAGICK = 'imagick';
  97. /** @var string */
  98. public const OUTPUT_FPDF = 'fpdf';
  99. /** @var string */
  100. public const OUTPUT_CUSTOM = 'custom';
  101. /**
  102. * Map of built-in output modules => capabilities
  103. *
  104. * @var string[][]
  105. */
  106. public const OUTPUT_MODES = [
  107. QRMarkup::class => [
  108. self::OUTPUT_MARKUP_SVG,
  109. self::OUTPUT_MARKUP_HTML,
  110. ],
  111. QRImage::class => [
  112. self::OUTPUT_IMAGE_PNG,
  113. self::OUTPUT_IMAGE_GIF,
  114. self::OUTPUT_IMAGE_JPG,
  115. ],
  116. QRString::class => [
  117. self::OUTPUT_STRING_JSON,
  118. self::OUTPUT_STRING_TEXT,
  119. ],
  120. QRImagick::class => [
  121. self::OUTPUT_IMAGICK,
  122. ],
  123. QRFpdf::class => [
  124. self::OUTPUT_FPDF
  125. ]
  126. ];
  127. /**
  128. * Map of data mode => interface
  129. *
  130. * @var string[]
  131. */
  132. protected const DATA_INTERFACES = [
  133. 'number' => Number::class,
  134. 'alphanum' => AlphaNum::class,
  135. 'kanji' => Kanji::class,
  136. 'byte' => Byte::class,
  137. ];
  138. /**
  139. * The settings container
  140. *
  141. * @var \chillerlan\QRCode\QROptions|\chillerlan\Settings\SettingsContainerInterface
  142. */
  143. protected SettingsContainerInterface $options;
  144. /**
  145. * The selected data interface (Number, AlphaNum, Kanji, Byte)
  146. */
  147. protected QRDataInterface $dataInterface;
  148. /**
  149. * QRCode constructor.
  150. *
  151. * Sets the options instance, determines the current mb-encoding and sets it to UTF-8
  152. */
  153. public function __construct(?SettingsContainerInterface $options = null){
  154. $this->options = $options ?? new QROptions;
  155. }
  156. /**
  157. * Renders a QR Code for the given $data and QROptions
  158. *
  159. * @return mixed
  160. */
  161. public function render(string $data, ?string $file = null){
  162. return $this->initOutputInterface($data)->dump($file);
  163. }
  164. /**
  165. * Returns a QRMatrix object for the given $data and current QROptions
  166. *
  167. * @throws \chillerlan\QRCode\Data\QRCodeDataException
  168. */
  169. public function getMatrix(string $data):QRMatrix{
  170. if(empty($data)){
  171. throw new QRCodeDataException('QRCode::getMatrix() No data given.');
  172. }
  173. $this->dataInterface = $this->initDataInterface($data);
  174. $maskPattern = $this->options->maskPattern === $this::MASK_PATTERN_AUTO
  175. ? (new MaskPatternTester($this->dataInterface))->getBestMaskPattern()
  176. : $this->options->maskPattern;
  177. $matrix = $this->dataInterface->initMatrix($maskPattern);
  178. if((bool)$this->options->addQuietzone){
  179. $matrix->setQuietZone($this->options->quietzoneSize);
  180. }
  181. return $matrix;
  182. }
  183. /**
  184. * returns a fresh QRDataInterface for the given $data
  185. *
  186. * @throws \chillerlan\QRCode\Data\QRCodeDataException
  187. */
  188. public function initDataInterface(string $data):QRDataInterface{
  189. // allow forcing the data mode
  190. // see https://github.com/chillerlan/php-qrcode/issues/39
  191. $interface = $this::DATA_INTERFACES[strtolower($this->options->dataModeOverride)] ?? null;
  192. if($interface !== null){
  193. return new $interface($this->options, $data);
  194. }
  195. foreach($this::DATA_INTERFACES as $mode => $dataInterface){
  196. if(call_user_func_array([$this, 'is'.$mode], [$data])){
  197. return new $dataInterface($this->options, $data);
  198. }
  199. }
  200. throw new QRCodeDataException('invalid data type'); // @codeCoverageIgnore
  201. }
  202. /**
  203. * returns a fresh (built-in) QROutputInterface
  204. *
  205. * @throws \chillerlan\QRCode\Output\QRCodeOutputException
  206. */
  207. protected function initOutputInterface(string $data):QROutputInterface{
  208. if($this->options->outputType === $this::OUTPUT_CUSTOM && class_exists($this->options->outputInterface)){
  209. /** @phan-suppress-next-line PhanTypeExpectedObjectOrClassName */
  210. return new $this->options->outputInterface($this->options, $this->getMatrix($data));
  211. }
  212. foreach($this::OUTPUT_MODES as $outputInterface => $modes){
  213. if(in_array($this->options->outputType, $modes, true) && class_exists($outputInterface)){
  214. return new $outputInterface($this->options, $this->getMatrix($data));
  215. }
  216. }
  217. throw new QRCodeOutputException('invalid output type');
  218. }
  219. /**
  220. * checks if a string qualifies as numeric
  221. */
  222. public function isNumber(string $string):bool{
  223. return $this->checkString($string, QRDataInterface::CHAR_MAP_NUMBER);
  224. }
  225. /**
  226. * checks if a string qualifies as alphanumeric
  227. */
  228. public function isAlphaNum(string $string):bool{
  229. return $this->checkString($string, QRDataInterface::CHAR_MAP_ALPHANUM);
  230. }
  231. /**
  232. * checks is a given $string matches the characters of a given $charmap, returns false on the first invalid occurence.
  233. */
  234. protected function checkString(string $string, array $charmap):bool{
  235. foreach(str_split($string) as $chr){
  236. if(!isset($charmap[$chr])){
  237. return false;
  238. }
  239. }
  240. return true;
  241. }
  242. /**
  243. * checks if a string qualifies as Kanji
  244. */
  245. public function isKanji(string $string):bool{
  246. $i = 0;
  247. $len = strlen($string);
  248. while($i + 1 < $len){
  249. $c = ((0xff & ord($string[$i])) << 8) | (0xff & ord($string[$i + 1]));
  250. if(!($c >= 0x8140 && $c <= 0x9FFC) && !($c >= 0xE040 && $c <= 0xEBBF)){
  251. return false;
  252. }
  253. $i += 2;
  254. }
  255. return $i >= $len;
  256. }
  257. /**
  258. * a dummy
  259. */
  260. public function isByte(string $data):bool{
  261. return $data !== '';
  262. }
  263. }