svgWithLogo.php 4.1 KB

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