QRMarkupHTML.php 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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. final public const MIME_TYPE = 'text/html';
  17. protected function createMarkup(bool $saveToFile):string{
  18. $rows = [];
  19. $cssClass = $this->getCssClass();
  20. foreach($this->matrix->getMatrix() as $row){
  21. $element = '<span style="background: %s;"></span>';
  22. $modules = array_map(fn(int $M_TYPE):string => sprintf($element, $this->getModuleValue($M_TYPE)), $row);
  23. $rows[] = sprintf('<div>%s</div>%s', implode('', $modules), $this->eol);
  24. }
  25. $html = sprintf('<div class="%1$s">%3$s%2$s</div>%3$s', $cssClass, implode('', $rows), $this->eol);
  26. // wrap the snippet into a body when saving to file
  27. if($saveToFile){
  28. $html = sprintf(
  29. '<!DOCTYPE html><html lang="none">%2$s<head>%2$s<meta charset="UTF-8">%2$s'.
  30. '<title>QR Code</title></head>%2$s<body>%1$s</body>%2$s</html>',
  31. $html,
  32. $this->eol,
  33. );
  34. }
  35. return $html;
  36. }
  37. }