custom_output.php 2.1 KB

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