text.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. /**
  3. * @created 21.12.2017
  4. * @author Smiley <smiley@chillerlan.net>
  5. * @copyright 2017 Smiley
  6. * @license MIT
  7. */
  8. use chillerlan\QRCode\{QRCode, QROptions};
  9. use chillerlan\QRCode\Data\QRMatrix;
  10. require_once __DIR__.'/../vendor/autoload.php';
  11. /**
  12. * a little helper to create a proper ANSI 8-bit color escape sequence
  13. *
  14. * @see https://en.wikipedia.org/wiki/ANSI_escape_code#8-bit
  15. * @see https://en.wikipedia.org/wiki/Block_Elements
  16. *
  17. * @codeCoverageIgnore
  18. */
  19. function ansi8(string $str, int $color, bool $background = null):string{
  20. $color = max(0, min($color, 255));
  21. $background = ($background === true) ? 48 : 38;
  22. return sprintf("\x1b[%s;5;%sm%s\x1b[0m", $background, $color, $str);
  23. }
  24. $options = new QROptions([
  25. 'version' => 5,
  26. 'outputType' => QRCode::OUTPUT_STRING_TEXT,
  27. 'eccLevel' => QRCode::ECC_L,
  28. 'moduleValues' => [
  29. // finder
  30. QRMatrix::M_FINDER_DARK => ansi8('██', 124), // dark (true)
  31. QRMatrix::M_FINDER_DOT => ansi8('██', 124), // dark (true)
  32. QRMatrix::M_FINDER => ansi8('░░', 124), // light (false)
  33. // alignment
  34. QRMatrix::M_ALIGNMENT_DARK => ansi8('██', 2),
  35. QRMatrix::M_ALIGNMENT => ansi8('░░', 2),
  36. // timing
  37. QRMatrix::M_TIMING_DARK => ansi8('██', 184),
  38. QRMatrix::M_TIMING => ansi8('░░', 184),
  39. // format
  40. QRMatrix::M_FORMAT_DARK => ansi8('██', 200),
  41. QRMatrix::M_FORMAT => ansi8('░░', 200),
  42. // version
  43. QRMatrix::M_VERSION_DARK => ansi8('██', 21),
  44. QRMatrix::M_VERSION => ansi8('░░', 21),
  45. // data
  46. QRMatrix::M_DATA_DARK => ansi8('██', 166),
  47. QRMatrix::M_DATA => ansi8('░░', 166),
  48. // darkmodule
  49. QRMatrix::M_DARKMODULE => ansi8('██', 53),
  50. // separator
  51. QRMatrix::M_SEPARATOR => ansi8('░░', 253),
  52. // quietzone
  53. QRMatrix::M_QUIETZONE => ansi8('░░', 253),
  54. ],
  55. ]);
  56. echo (new QRCode($options))->render('https://www.youtube.com/watch?v=dQw4w9WgXcQ');