QRMarkupHTML.php 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. <?php
  2. /**
  3. * Class QRMarkupHTML
  4. *
  5. * @created 06.06.2022
  6. * @author smiley <smiley@chillerlan.net>
  7. * @copyright 2022 smiley
  8. * @license MIT
  9. */
  10. namespace chillerlan\QRCode\Output;
  11. use function implode, sprintf;
  12. /**
  13. * HTML output (a cheap markup substitute when SVG is not available or not an option)
  14. */
  15. class QRMarkupHTML extends QRMarkup{
  16. public const MIME_TYPE = 'text/html';
  17. /**
  18. * @inheritDoc
  19. */
  20. protected function createMarkup(bool $saveToFile):string{
  21. $rows = [];
  22. $cssClass = $this->getCssClass();
  23. foreach($this->matrix->getMatrix() as $row){
  24. $element = '<span style="background: %s;"></span>';
  25. $modules = array_map(fn(int $M_TYPE):string => sprintf($element, $this->getModuleValue($M_TYPE)), $row);
  26. $rows[] = sprintf('<div>%s</div>%s', implode('', $modules), $this->eol);
  27. }
  28. $html = sprintf('<div class="%1$s">%3$s%2$s</div>%3$s', $cssClass, implode('', $rows), $this->eol);
  29. // wrap the snippet into a body when saving to file
  30. if($saveToFile){
  31. $html = sprintf(
  32. '<!DOCTYPE html><html lang="none">%2$s<head>%2$s<meta charset="UTF-8">%2$s'.
  33. '<title>QR Code</title></head>%2$s<body>%1$s</body>%2$s</html>',
  34. $html,
  35. $this->eol
  36. );
  37. }
  38. return $html;
  39. }
  40. }