custom_output.php 1.8 KB

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