QRCode.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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, QRImagick, QRMarkup, QROutputInterface, QRString
  18. };
  19. use chillerlan\Settings\SettingsContainerInterface;
  20. use function call_user_func_array, class_exists, in_array, mb_internal_encoding, ord, strlen;
  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_CUSTOM = 'custom';
  99. /**
  100. * Map of built-in output modules => capabilities
  101. *
  102. * @var string[][]
  103. */
  104. public const OUTPUT_MODES = [
  105. QRMarkup::class => [
  106. self::OUTPUT_MARKUP_SVG,
  107. self::OUTPUT_MARKUP_HTML,
  108. ],
  109. QRImage::class => [
  110. self::OUTPUT_IMAGE_PNG,
  111. self::OUTPUT_IMAGE_GIF,
  112. self::OUTPUT_IMAGE_JPG,
  113. ],
  114. QRString::class => [
  115. self::OUTPUT_STRING_JSON,
  116. self::OUTPUT_STRING_TEXT,
  117. ],
  118. QRImagick::class => [
  119. self::OUTPUT_IMAGICK,
  120. ],
  121. ];
  122. /**
  123. * Map of data mode => interface
  124. *
  125. * @var string[]
  126. */
  127. protected const DATA_INTERFACES = [
  128. 'Number' => Number::class,
  129. 'AlphaNum' => AlphaNum::class,
  130. 'Kanji' => Kanji::class,
  131. 'Byte' => Byte::class,
  132. ];
  133. /**
  134. * The settings container
  135. *
  136. * @var \chillerlan\QRCode\QROptions|\chillerlan\Settings\SettingsContainerInterface
  137. */
  138. protected SettingsContainerInterface $options;
  139. /**
  140. * The selected data interface (Number, AlphaNum, Kanji, Byte)
  141. */
  142. protected QRDataInterface $dataInterface;
  143. /**
  144. * The current encoding (before the QRCode instance was invoked)
  145. *
  146. * @see http://php.net/manual/function.mb-internal-encoding.php mb_internal_encoding()
  147. */
  148. protected string $mbCurrentEncoding;
  149. /**
  150. * QRCode constructor.
  151. *
  152. * Sets the options instance, determines the current mb-encoding and sets it to UTF-8
  153. */
  154. public function __construct(SettingsContainerInterface $options = null){
  155. // save the current mb-encoding (in case it differs from UTF-8)
  156. $this->mbCurrentEncoding = mb_internal_encoding();
  157. // use UTF-8 from here on
  158. mb_internal_encoding('UTF-8');
  159. $this->options = $options ?? new QROptions;
  160. }
  161. /**
  162. * Restores the previous mb-encoding setting, so that we don't mess up the rest of the script
  163. *
  164. * @return void
  165. */
  166. public function __destruct(){
  167. mb_internal_encoding($this->mbCurrentEncoding);
  168. }
  169. /**
  170. * Renders a QR Code for the given $data and QROptions
  171. *
  172. * @return mixed
  173. */
  174. public function render(string $data, string $file = null){
  175. return $this->initOutputInterface($data)->dump($file);
  176. }
  177. /**
  178. * Returns a QRMatrix object for the given $data and current QROptions
  179. *
  180. * @throws \chillerlan\QRCode\Data\QRCodeDataException
  181. */
  182. public function getMatrix(string $data):QRMatrix{
  183. if(empty($data)){
  184. throw new QRCodeDataException('QRCode::getMatrix() No data given.');
  185. }
  186. $this->dataInterface = $this->initDataInterface($data);
  187. $maskPattern = $this->options->maskPattern === $this::MASK_PATTERN_AUTO
  188. ? (new MaskPatternTester($this->dataInterface))->getBestMaskPattern()
  189. : $this->options->maskPattern;
  190. $matrix = $this->dataInterface->initMatrix($maskPattern);
  191. if((bool)$this->options->addQuietzone){
  192. $matrix->setQuietZone($this->options->quietzoneSize);
  193. }
  194. return $matrix;
  195. }
  196. /**
  197. * returns a fresh QRDataInterface for the given $data
  198. *
  199. * @throws \chillerlan\QRCode\Data\QRCodeDataException
  200. */
  201. public function initDataInterface(string $data):QRDataInterface{
  202. // allow forcing the data mode
  203. // see https://github.com/chillerlan/php-qrcode/issues/39
  204. $interface = $this::DATA_INTERFACES[$this->options->dataMode] ?? null;
  205. if($interface !== null){
  206. return new $interface($this->options, $data);
  207. }
  208. foreach($this::DATA_INTERFACES as $mode => $dataInterface){
  209. if(call_user_func_array([$this, 'is'.$mode], [$data])){
  210. return new $dataInterface($this->options, $data);
  211. }
  212. }
  213. throw new QRCodeDataException('invalid data type'); // @codeCoverageIgnore
  214. }
  215. /**
  216. * returns a fresh (built-in) QROutputInterface
  217. *
  218. * @throws \chillerlan\QRCode\Output\QRCodeOutputException
  219. */
  220. protected function initOutputInterface(string $data):QROutputInterface{
  221. if($this->options->outputType === $this::OUTPUT_CUSTOM && class_exists($this->options->outputInterface)){
  222. return new $this->options->outputInterface($this->options, $this->getMatrix($data));
  223. }
  224. foreach($this::OUTPUT_MODES as $outputInterface => $modes){
  225. if(in_array($this->options->outputType, $modes, true) && class_exists($outputInterface)){
  226. return new $outputInterface($this->options, $this->getMatrix($data));
  227. }
  228. }
  229. throw new QRCodeOutputException('invalid output type');
  230. }
  231. /**
  232. * checks if a string qualifies as numeric
  233. */
  234. public function isNumber(string $string):bool{
  235. return $this->checkString($string, QRDataInterface::CHAR_MAP_NUMBER);
  236. }
  237. /**
  238. * checks if a string qualifies as alphanumeric
  239. */
  240. public function isAlphaNum(string $string):bool{
  241. return $this->checkString($string, QRDataInterface::CHAR_MAP_ALPHANUM);
  242. }
  243. /**
  244. * checks is a given $string matches the characters of a given $charmap, returns false on the first invalid occurence.
  245. */
  246. protected function checkString(string $string, array $charmap):bool{
  247. $len = strlen($string);
  248. for($i = 0; $i < $len; $i++){
  249. if(!isset($charmap[$string[$i]])){
  250. return false;
  251. }
  252. }
  253. return true;
  254. }
  255. /**
  256. * checks if a string qualifies as Kanji
  257. */
  258. public function isKanji(string $string):bool{
  259. $i = 0;
  260. $len = strlen($string);
  261. while($i + 1 < $len){
  262. $c = ((0xff & ord($string[$i])) << 8) | (0xff & ord($string[$i + 1]));
  263. if(!($c >= 0x8140 && $c <= 0x9FFC) && !($c >= 0xE040 && $c <= 0xEBBF)){
  264. return false;
  265. }
  266. $i += 2;
  267. }
  268. return $i >= $len;
  269. }
  270. /**
  271. * a dummy
  272. */
  273. protected function isByte(string $data):bool{
  274. return !empty($data);
  275. }
  276. }