Usage-Advanced-usage.md.txt 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. # Advanced usage
  2. ## Configuration via `QROptions`
  3. The [`QROptions`](https://github.com/chillerlan/php-qrcode/blob/main/src/QROptions.php) class is a container based on [chillerlan/php-settings-container](https://github.com/chillerlan/php-settings-container) that behaves similar to a [`\stdClass`](https://www.php.net/manual/class.stdclass) object, but with fixed properties.
  4. ```php
  5. $options = new QROptions;
  6. // set some values
  7. $options->version = 7; // property "version" exists
  8. $options->foo = 'bar'; // property "foo" does not exist (and will not be created)
  9. // retrieve values
  10. var_dump($options->version); // -> 7
  11. var_dump($options->foo); // -> null (no error will be thrown)
  12. ```
  13. ### Supply an `iterable` of options
  14. The constructor takes an `iterable` of `$key => $value` pairs. For each setting an optional setter will be called if present.
  15. ```php
  16. $myOptions = [
  17. 'version' => 5,
  18. 'outputType' => QROutputInterface::GDIMAGE_PNG,
  19. 'eccLevel' => EccLevel::M,
  20. ];
  21. $options = new QROptions($myOptions);
  22. ```
  23. You can also set an `iterable` of options on an existing QROptions instance:
  24. ```php
  25. $options->fromIterable($myOptions);
  26. ```
  27. ### Load and save JSON
  28. The settings can be saved to and loaded from JSON, e.g. to store them in a database:
  29. ```php
  30. $json = $options->toJSON(JSON_THROW_ON_ERROR);
  31. // via JsonSerializable interface
  32. $json = json_encode($options, JSON_THROW_ON_ERROR);
  33. // via __toString()
  34. $json = (string)$options;
  35. $options = (new QROptions)->fromJSON($json);
  36. // on an existing instance - properties will be overwriten
  37. $options->fromJSON($json);
  38. ```
  39. ### Extending the `QROptions` class
  40. In case you need additional settings for your output module, just extend `QROptions`...
  41. ```php
  42. class MyCustomOptions extends QROptions{
  43. protected string $myParam = 'defaultValue';
  44. // ...
  45. }
  46. ```
  47. ...or use the [`SettingsContainerInterface`](https://github.com/chillerlan/php-settings-container/blob/main/src/SettingsContainerInterface.php), which is the more flexible approach.
  48. ```php
  49. trait MyCustomOptionsTrait{
  50. protected string $myParam = 'defaultValue';
  51. // ...
  52. // an optional magic setter, named "set_" + property name
  53. protected function set_myParam(string $myParam):void{
  54. $this->myParam = trim($myParam);
  55. }
  56. // an optional magic getter, named "get_" + property name
  57. protected function get_myParam():string{
  58. return strtoupper($this->myParam);
  59. }
  60. }
  61. class MyCustomOptions extends SettingsContainerAbstract{
  62. use QROptionsTrait, MyCustomOptionsTrait;
  63. }
  64. // set the options
  65. $myCustomOptions = new MyCustomOptions;
  66. $myCustomOptions->myParam = 'whatever value';
  67. ```
  68. Extend the `SettingsContainerInterface` on-the-fly:
  69. ```php
  70. $myOptions = [
  71. 'myParam' => 'whatever value',
  72. // ...
  73. ];
  74. $myCustomOptions = new class($myOptions) extends SettingsContainerAbstract{
  75. use QROptionsTrait, MyCustomOptionsTrait;
  76. };
  77. ```
  78. ## `QRCode` methods
  79. Aside of invoking a `QRCode` instance with an optional `QROptions` object as parameter, you can also set the options instance after invocation.
  80. After invocation of the `QROptions` instance, values can be set without calling `QRCode::setOptions()` again (instance is backreferenced), however, this may create side effects.
  81. ```php
  82. // instance will be invoked with default settings
  83. $qrcode = new QRCode;
  84. // set options after QRCode invocation
  85. $options = new QROptions;
  86. $qrcode->setOptions($options);
  87. ```
  88. ### Save to file
  89. Save the QR Code output to `/path/to/qrcode.svg`:
  90. ```php
  91. $options->outputBase64 = false;
  92. $options->cachefile = '/path/to/qrcode.svg';
  93. $qrcode->render($data);
  94. ```
  95. Alternatively, you can specify an output path, which will override the `QROptions::$cachefile` setting:
  96. ```php
  97. $qrcode->render($data, '/other/path/to/qrcode.svg');
  98. printf('<img src="%s" alt="QR Code" />', '/other/path/to/qrcode.svg');
  99. ```
  100. ### Render a `QRMatrix` instance
  101. You can render a [`QRMatrix`](https://github.com/chillerlan/php-qrcode/blob/main/src/Data/QRMatrix.php) instance directly:
  102. ```php
  103. // a matrix from the current data segments
  104. $matrix = $qrcode->getQRMatrix();
  105. // from the QR Code reader
  106. $matrix = $readerResult->getQRMatrix();
  107. // manually invoked
  108. $matrix = (new QRMatrix(new Version(7), new EccLevel(EccLevel::M)))->initFunctionalPatterns();
  109. $output = $qrcode->renderMatrix($matrix);
  110. // save to file
  111. $qrcode->renderMatrix($matrix, '/path/to/qrcode.svg');
  112. ```
  113. Manual invocation of a `QROutputInterface` to render a `QRMatrix` instance:
  114. ```php
  115. class MyCustomOutput extends QRMarkupSVG{
  116. // ...
  117. }
  118. $qrOutputInterface = new MyCustomOutput($options, $matrix);
  119. $output = $qrOutputInterface->dump()
  120. // render to file
  121. $qrOutputInterface->dump('/path/to/qrcode.svg');
  122. ```
  123. ### Mixed mode
  124. Mixed mode QR Codes can be generated by adding several data segments:
  125. ```php
  126. // make sure to set a proper internal encoding character set
  127. // ideally, this should be set in php.ini internal_encoding,
  128. // default_charset or mbstring.internal_encoding
  129. mb_internal_encoding('UTF-8');
  130. // clear any existing data segments
  131. $qrcode->clearSegments();
  132. $qrcode
  133. ->addNumericSegment($numericData)
  134. ->addAlphaNumSegment($alphaNumData)
  135. ->addKanjiSegment($kanjiData)
  136. ->addHanziSegment($hanziData)
  137. ->addByteSegment($binaryData)
  138. ->addEciSegment(ECICharset::GB18030, $encodedEciData)
  139. ;
  140. $output = $qrcode->render();
  141. // render to file
  142. $qrcode->render(null, '/path/to/qrcode.svg');
  143. ```
  144. The [`QRDataModeInterface`](https://github.com/chillerlan/php-qrcode/blob/main/src/Data/QRDataModeInterface.php) offers the `validateString()` method (implemended for `AlphaNum`, `Byte`, `Hanzi`, `Kanji` and `Number`).
  145. This method is used internally when a data mode is invoked, but it can come in handy if you need to check input data beforehand.
  146. ```php
  147. if(!Hanzi::validateString($data)){
  148. throw new Exception('invalid GB2312 data');
  149. }
  150. $qrcode->addHanziSegment($data);
  151. ```
  152. ### QR Code reader
  153. In some cases it might be necessary to increase the contrast of a QR Code image:
  154. ```php
  155. $options->readerUseImagickIfAvailable = true;
  156. $options->readerIncreaseContrast = true;
  157. $options->readerGrayscale = true;
  158. $result = (new QRCode($options))->readFromFile('path/to/qrcode.png');
  159. ```
  160. The `QRMatrix` object from the [`DecoderResult`](https://github.com/chillerlan/php-qrcode/blob/main/src/Decoder/DecoderResult.php) can be reused:
  161. ```php
  162. $matrix = $result->getQRMatrix();
  163. // ...matrix modification...
  164. $output = (new QRCode($options))->renderMatrix($matrix);
  165. // ...output
  166. ```