custom_output.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. /**
  3. * custom output example
  4. *
  5. * @created 24.12.2017
  6. * @author Smiley <smiley@chillerlan.net>
  7. * @copyright 2017 Smiley
  8. * @license MIT
  9. *
  10. * @noinspection PhpIllegalPsrClassPathInspection
  11. */
  12. declare(strict_types=1);
  13. use chillerlan\QRCode\{QRCode, QROptions};
  14. use chillerlan\QRCode\Output\QROutputAbstract;
  15. require_once __DIR__.'/../vendor/autoload.php';
  16. /*
  17. * Class definition
  18. */
  19. class MyCustomOutput extends QROutputAbstract{
  20. public static function moduleValueIsValid(mixed $value):bool{
  21. // TODO: Implement moduleValueIsValid() method. (interface)
  22. return false;
  23. }
  24. protected function prepareModuleValue(mixed $value):mixed{
  25. // TODO: Implement prepareModuleValue() method. (abstract)
  26. return null;
  27. }
  28. protected function getDefaultModuleValue(bool $isDark):mixed{
  29. // TODO: Implement getDefaultModuleValue() method. (abstract)
  30. return null;
  31. }
  32. public function dump(string|null $file = null):string{
  33. $output = '';
  34. for($y = 0; $y < $this->moduleCount; $y++){
  35. for($x = 0; $x < $this->moduleCount; $x++){
  36. $output .= (int)$this->matrix->check($x, $y);
  37. }
  38. $output .= $this->options->eol;
  39. }
  40. return $output;
  41. }
  42. }
  43. /*
  44. * Runtime
  45. */
  46. $options = new QROptions;
  47. $options->version = 5;
  48. $options->eccLevel = 'L';
  49. $data = 'https://www.youtube.com/watch?v=dQw4w9WgXcQ';
  50. // invoke the QROutputInterface manually
  51. // please note that an QROutputInterface invoked this way might become unusable after calling dump().
  52. // the clean way would be to extend the QRCode class to ensure a new QROutputInterface instance on each call to render().
  53. $qrcode = new QRCode($options);
  54. $qrcode->addByteSegment($data);
  55. $qrOutputInterface = new MyCustomOutput($options, $qrcode->getQRMatrix());
  56. var_dump($qrOutputInterface->dump());
  57. // or just via the options
  58. $options->outputInterface = MyCustomOutput::class;
  59. var_dump((new QRCode($options))->render($data));
  60. exit;