svgWithLogo.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. * @property string $svgLogo
  64. * @property float $svgLogoScale
  65. * @property string $svgLogoCssClass
  66. */
  67. class SVGWithLogoOptions extends QROptions{
  68. // path to svg logo
  69. protected string $svgLogo;
  70. // logo scale in % of QR Code size, clamped to 10%-30%
  71. protected float $svgLogoScale = 0.20;
  72. // css class for the logo (defined in $svgDefs)
  73. protected string $svgLogoCssClass = '';
  74. // check logo
  75. protected function set_svgLogo(string $svgLogo):void{
  76. if(!file_exists($svgLogo) || !is_readable($svgLogo)){
  77. throw new QRCodeException('invalid svg logo');
  78. }
  79. // @todo: validate svg
  80. $this->svgLogo = $svgLogo;
  81. }
  82. // clamp logo scale
  83. protected function set_svgLogoScale(float $svgLogoScale):void{
  84. $this->svgLogoScale = max(0.05, min(0.3, $svgLogoScale));
  85. }
  86. }
  87. /*
  88. * Runtime
  89. */
  90. $options = new SVGWithLogoOptions;
  91. // SVG logo options (see extended class)
  92. $options->svgLogo = __DIR__.'/github.svg'; // logo from: https://github.com/simple-icons/simple-icons
  93. $options->svgLogoScale = 0.25;
  94. $options->svgLogoCssClass = 'dark';
  95. // QROptions
  96. $options->version = 5;
  97. $options->outputInterface = QRSvgWithLogo::class;
  98. $options->outputBase64 = false;
  99. $options->eccLevel = EccLevel::H; // ECC level H is necessary when using logos
  100. $options->addQuietzone = true;
  101. $options->drawLightModules = true;
  102. $options->connectPaths = true;
  103. $options->drawCircularModules = true;
  104. $options->circleRadius = 0.45;
  105. $options->keepAsSquare = [
  106. QRMatrix::M_FINDER_DARK,
  107. QRMatrix::M_FINDER_DOT,
  108. QRMatrix::M_ALIGNMENT_DARK,
  109. ];
  110. // https://developer.mozilla.org/en-US/docs/Web/SVG/Element/linearGradient
  111. $options->svgDefs = '
  112. <linearGradient id="gradient" x1="100%" y2="100%">
  113. <stop stop-color="#D70071" offset="0"/>
  114. <stop stop-color="#9C4E97" offset="0.5"/>
  115. <stop stop-color="#0035A9" offset="1"/>
  116. </linearGradient>
  117. <style><![CDATA[
  118. .dark{fill: url(#gradient);}
  119. .light{fill: #eaeaea;}
  120. ]]></style>';
  121. $out = (new QRCode($options))->render('https://www.youtube.com/watch?v=dQw4w9WgXcQ');
  122. if(php_sapi_name() !== 'cli'){
  123. header('Content-type: image/svg+xml');
  124. if(extension_loaded('zlib')){
  125. header('Vary: Accept-Encoding');
  126. header('Content-Encoding: gzip');
  127. $out = gzencode($out, 9);
  128. }
  129. }
  130. echo $out;
  131. exit;