custom_output.php 2.0 KB

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