custom_output.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. protected function moduleValueIsValid($value):bool{
  22. // TODO: Implement moduleValueIsValid() method. (abstract)
  23. return false;
  24. }
  25. /**
  26. * @inheritDoc
  27. */
  28. protected function getModuleValue($value){
  29. // TODO: Implement getModuleValue() 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. $options = new QROptions([
  59. 'version' => 5,
  60. 'eccLevel' => EccLevel::L,
  61. ]);
  62. $qrcode = new QRCode($options);
  63. $qrcode->addByteSegment($data);
  64. $qrOutputInterface = new MyCustomOutput($options, $qrcode->getMatrix());
  65. var_dump($qrOutputInterface->dump());
  66. // or just via the options
  67. $options = new QROptions([
  68. 'version' => 5,
  69. 'eccLevel' => EccLevel::L,
  70. 'outputType' => QROutputInterface::CUSTOM,
  71. 'outputInterface' => MyCustomOutput::class,
  72. ]);
  73. var_dump((new QRCode($options))->render($data));
  74. exit;