svgWithLogo.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. /**
  3. * SVG with logo example
  4. *
  5. * @created 05.03.2022
  6. * @author smiley <smiley@chillerlan.net>
  7. * @copyright 2022 smiley
  8. * @license MIT
  9. *
  10. * @noinspection PhpComposerExtensionStubsInspection
  11. */
  12. use chillerlan\QRCode\{QRCode, QRCodeException, QROptions};
  13. use chillerlan\QRCode\Common\EccLevel;
  14. use chillerlan\QRCode\Data\QRMatrix;
  15. use chillerlan\QRCode\Output\QRMarkupSVG;
  16. require_once __DIR__.'/../vendor/autoload.php';
  17. /*
  18. * Class definition
  19. */
  20. /**
  21. * Create SVG QR Codes with embedded logos (that are also SVG)
  22. */
  23. class QRSvgWithLogo extends QRMarkupSVG{
  24. /**
  25. * @inheritDoc
  26. */
  27. protected function paths():string{
  28. $size = (int)ceil($this->moduleCount * $this->options->svgLogoScale);
  29. // we're calling QRMatrix::setLogoSpace() manually, so QROptions::$addLogoSpace has no effect here
  30. $this->matrix->setLogoSpace($size, $size);
  31. $svg = parent::paths();
  32. $svg .= $this->getLogo();
  33. return $svg;
  34. }
  35. /**
  36. * @inheritDoc
  37. */
  38. protected function path(string $path, int $M_TYPE):string{
  39. // omit the "fill" and "opacity" attributes on the path element
  40. return sprintf('<path class="%s" d="%s"/>', $this->getCssClass($M_TYPE), $path);
  41. }
  42. /**
  43. * returns a <g> element that contains the SVG logo and positions it properly within the QR Code
  44. *
  45. * @see https://developer.mozilla.org/en-US/docs/Web/SVG/Element/g
  46. * @see https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/transform
  47. */
  48. protected function getLogo():string{
  49. // @todo: customize the <g> element to your liking (css class, style...)
  50. return sprintf(
  51. '%5$s<g transform="translate(%1$s %1$s) scale(%2$s)" class="%3$s">%5$s %4$s%5$s</g>',
  52. (($this->moduleCount - ($this->moduleCount * $this->options->svgLogoScale)) / 2),
  53. $this->options->svgLogoScale,
  54. $this->options->svgLogoCssClass,
  55. file_get_contents($this->options->svgLogo),
  56. $this->options->eol
  57. );
  58. }
  59. }
  60. /**
  61. * augment the QROptions class
  62. */
  63. class SVGWithLogoOptions extends QROptions{
  64. // path to svg logo
  65. protected string $svgLogo;
  66. // logo scale in % of QR Code size, clamped to 10%-30%
  67. protected float $svgLogoScale = 0.20;
  68. // css class for the logo (defined in $svgDefs)
  69. protected string $svgLogoCssClass = '';
  70. // check logo
  71. protected function set_svgLogo(string $svgLogo):void{
  72. if(!file_exists($svgLogo) || !is_readable($svgLogo)){
  73. throw new QRCodeException('invalid svg logo');
  74. }
  75. // @todo: validate svg
  76. $this->svgLogo = $svgLogo;
  77. }
  78. // clamp logo scale
  79. protected function set_svgLogoScale(float $svgLogoScale):void{
  80. $this->svgLogoScale = max(0.05, min(0.3, $svgLogoScale));
  81. }
  82. }
  83. /*
  84. * Runtime
  85. */
  86. $options = new SVGWithLogoOptions;
  87. // SVG logo options (see extended class)
  88. $options->svgLogo = __DIR__.'/github.svg'; // logo from: https://github.com/simple-icons/simple-icons
  89. $options->svgLogoScale = 0.25;
  90. $options->svgLogoCssClass = 'dark';
  91. // QROptions
  92. $options->version = 5;
  93. $options->outputInterface = QRSvgWithLogo::class;
  94. $options->outputBase64 = false;
  95. $options->eccLevel = EccLevel::H; // ECC level H is necessary when using logos
  96. $options->addQuietzone = true;
  97. $options->drawLightModules = true;
  98. $options->connectPaths = true;
  99. $options->drawCircularModules = true;
  100. $options->circleRadius = 0.45;
  101. $options->keepAsSquare = [
  102. QRMatrix::M_FINDER_DARK,
  103. QRMatrix::M_FINDER_DOT,
  104. QRMatrix::M_ALIGNMENT_DARK,
  105. ];
  106. // https://developer.mozilla.org/en-US/docs/Web/SVG/Element/linearGradient
  107. $options->svgDefs = '
  108. <linearGradient id="gradient" x1="100%" y2="100%">
  109. <stop stop-color="#D70071" offset="0"/>
  110. <stop stop-color="#9C4E97" offset="0.5"/>
  111. <stop stop-color="#0035A9" offset="1"/>
  112. </linearGradient>
  113. <style><![CDATA[
  114. .dark{fill: url(#gradient);}
  115. .light{fill: #eaeaea;}
  116. ]]></style>';
  117. $out = (new QRCode($options))->render('https://www.youtube.com/watch?v=dQw4w9WgXcQ');
  118. if(php_sapi_name() !== 'cli'){
  119. header('Content-type: image/svg+xml');
  120. if(extension_loaded('zlib')){
  121. header('Vary: Accept-Encoding');
  122. header('Content-Encoding: gzip');
  123. $out = gzencode($out, 9);
  124. }
  125. }
  126. echo $out;
  127. exit;