QRCode.php 7.1 KB

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