QRCode.php 7.0 KB

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