QRString.md.txt 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. # QRString
  2. [Class `QRString`](https://github.com/chillerlan/php-qrcode/blob/main/src/Output/QRString.php): String output: plain text, [JSON](https://developer.mozilla.org/en-US/docs/Glossary/JSON)
  3. ## Plain text
  4. Render in a CLI console, using [ANSI colors](https://en.wikipedia.org/wiki/ANSI_escape_code#Colors) and [block elements](https://en.wikipedia.org/wiki/Block_Elements):
  5. ```php
  6. // a little helper to a create proper ANSI 8-bit color escape sequence
  7. function ansi8(string $str, int $color, bool $background = false):string{
  8. $color = max(0, min($color, 255));
  9. $background = ($background ? 48 : 38);
  10. return sprintf("\x1b[%s;5;%sm%s\x1b[0m", $background, $color, $str);
  11. }
  12. $options = new QROptions;
  13. $options->outputType = QROutputInterface::STRING_TEXT;
  14. $options->eol = "\n";
  15. // add some space on the line start
  16. $options->textLineStart = str_repeat(' ', 6);
  17. // default values for unassigned module types
  18. $options->textDark = QRString::ansi8('██', 253);
  19. $options->textLight = QRString::ansi8('░░', 253);
  20. $options->moduleValues = [
  21. QRMatrix::M_FINDER_DARK => QRString::ansi8('██', 124),
  22. QRMatrix::M_FINDER => QRString::ansi8('░░', 124),
  23. QRMatrix::M_FINDER_DOT => QRString::ansi8('██', 124),
  24. QRMatrix::M_ALIGNMENT_DARK => QRString::ansi8('██', 2),
  25. QRMatrix::M_ALIGNMENT => QRString::ansi8('░░', 2),
  26. QRMatrix::M_VERSION_DARK => QRString::ansi8('██', 21),
  27. QRMatrix::M_VERSION => QRString::ansi8('░░', 21),
  28. ];
  29. ```
  30. Output:
  31. ```php
  32. $data = 'https://www.youtube.com/watch?v=dQw4w9WgXcQ';
  33. $qrcode = (new QRCode($options))->render($data);
  34. echo "\n\n$qrcode\n\n";
  35. ```
  36. ## JSON
  37. ```php
  38. $options = new QROptions;
  39. $options->outputType = QROutputInterface::STRING_JSON;
  40. // output the integer values ($M_TYPE) held in the matrix object
  41. $options->jsonAsBooleans = false;
  42. header('Content-type: application/json');
  43. echo (new QRCode($options))->render($data);
  44. ```
  45. ## Additional methods
  46. | method | return | description |
  47. |-----------------------------------------------------------|----------|---------------------------------------------------------------------|
  48. | (protected) `text()` | `string` | string output |
  49. | (protected) `json()` | `string` | JSON output |
  50. | `ansi8(string $str, int $color, bool $background = null)` | `string` | a little helper to create a proper ANSI 8-bit color escape sequence |
  51. ## Options that affect this module
  52. | property | type |
  53. |-------------------|----------|
  54. | `$eol` | `string` |
  55. | `$jsonAsBooleans` | `bool` |
  56. | `$textDark` | `string` |
  57. | `$textLight` | `string` |
  58. | `$textLineStart` | `string` |
  59. ### Options that have no effect
  60. | property | reason |
  61. |------------------------|-----------------|
  62. | `$bgColor` | N/A |
  63. | `$circleRadius` | N/A |
  64. | `$connectPaths` | N/A |
  65. | `$drawCircularModules` | N/A |
  66. | `$drawLightModules` | not implemented |
  67. | `$excludeFromConnect` | N/A |
  68. | `$imageTransparent` | N/A |
  69. | `$keepAsSquare` | N/A |
  70. | `$outputBase64` | N/A |
  71. | `$returnResource` | N/A |
  72. | `$scale` | N/A |