QRSvgWithLogo.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. <?php
  2. /**
  3. * Class QRSvgWithLogo
  4. *
  5. * @created 05.03.2022
  6. * @author smiley <smiley@chillerlan.net>
  7. * @copyright 2022 smiley
  8. * @license MIT
  9. */
  10. namespace chillerlan\QRCodeExamples;
  11. use chillerlan\QRCode\Output\QRMarkupSVG;
  12. use function ceil, file_get_contents, sprintf;
  13. /**
  14. * Create SVG QR Codes with embedded logos (that are also SVG)
  15. */
  16. class QRSvgWithLogo extends QRMarkupSVG{
  17. /**
  18. * @inheritDoc
  19. */
  20. protected function paths():string{
  21. $size = (int)ceil($this->moduleCount * $this->options->svgLogoScale);
  22. // we're calling QRMatrix::setLogoSpace() manually, so QROptions::$addLogoSpace has no effect here
  23. $this->matrix->setLogoSpace($size, $size);
  24. $svg = parent::paths();
  25. $svg .= $this->getLogo();
  26. return $svg;
  27. }
  28. /**
  29. * returns a <g> element that contains the SVG logo and positions it properly within the QR Code
  30. *
  31. * @see https://developer.mozilla.org/en-US/docs/Web/SVG/Element/g
  32. * @see https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/transform
  33. */
  34. protected function getLogo():string{
  35. // @todo: customize the <g> element to your liking (css class, style...)
  36. return sprintf(
  37. '%5$s<g transform="translate(%1$s %1$s) scale(%2$s)" class="%3$s">%5$s %4$s%5$s</g>',
  38. ($this->moduleCount - ($this->moduleCount * $this->options->svgLogoScale)) / 2,
  39. $this->options->svgLogoScale,
  40. $this->options->svgLogoCssClass,
  41. file_get_contents($this->options->svgLogo),
  42. $this->options->eol
  43. );
  44. }
  45. }