svgWithLogoAndCustomShapes.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. <?php
  2. /**
  3. * @see https://github.com/chillerlan/php-qrcode/discussions/150
  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\{QROutputInterface, 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 QRSvgWithLogoAndCustomShapes extends QRMarkupSVG{
  24. /**
  25. * @inheritDoc
  26. */
  27. protected function paths():string{
  28. // make sure connect paths is enabled
  29. $this->options->connectPaths = true;
  30. // we're calling QRMatrix::setLogoSpace() manually, so QROptions::$addLogoSpace has no effect here
  31. $this->matrix->setLogoSpace((int)ceil($this->moduleCount * $this->options->svgLogoScale));
  32. // generate the path element(s) - in this case it's just one element as we've "disabled" several options
  33. $svg = parent::paths();
  34. // now we're lazy modifying the generated path to add the custom shapes for the finder patterns
  35. $svg = str_replace('"/>', $this->getFinderPatterns().'"/>', $svg);
  36. // and add the custom logo
  37. $svg .= $this->getLogo();
  38. return $svg;
  39. }
  40. /**
  41. * @inheritDoc
  42. */
  43. protected function path(string $path, int $M_TYPE):string{
  44. // omit the "fill" and "opacity" attributes on the path element
  45. return sprintf('<path class="%s" d="%s"/>', $this->getCssClass($M_TYPE), $path);
  46. }
  47. /**
  48. * returns a path segment for a single module
  49. *
  50. * @see https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/d
  51. */
  52. protected function module(int $x, int $y, int $M_TYPE):string{
  53. if(
  54. !$this->matrix->check($x, $y)
  55. // we're skipping the finder patterns here
  56. || $this->matrix->checkType($x, $y, QRMatrix::M_FINDER)
  57. || $this->matrix->checkType($x, $y, QRMatrix::M_FINDER_DOT)
  58. ){
  59. return '';
  60. }
  61. // return a heart shape (or any custom shape for that matter)
  62. return sprintf('M%1$s %2$s m0.5,0.96 l-0.412,-0.412 a0.3 0.3 0 0 1 0.412,-0.435 a0.3 0.3 0 0 1 0.412,0.435Z', $x, $y);
  63. }
  64. /**
  65. * returns a custom path for the 3 finder patterns
  66. */
  67. protected function getFinderPatterns():string{
  68. $qz = ($this->options->addQuietzone) ? $this->options->quietzoneSize : 0;
  69. // the positions for the finder patterns (top left corner)
  70. // $this->moduleCount includes 2* the quiet zone size already, so we need to take this into account
  71. $pos = [
  72. [(0 + $qz), (0 + $qz)],
  73. [(0 + $qz), ($this->moduleCount - $qz - 7)],
  74. [($this->moduleCount - $qz - 7), (0 + $qz)],
  75. ];
  76. // the custom path for one finder pattern - the first move (M) is parametrized, the rest are relative coordinates
  77. $path = 'M%1$s,%2$s m2,0 h3 q2,0 2,2 v3 q0,2 -2,2 h-3 q-2,0 -2,-2 v-3 q0,-2 2,-2z m0,1 q-1,0 -1,1 v3 '.
  78. 'q0,1 1,1 h3 q1,0 1,-1 v-3 q0,-1 -1,-1z m0,2.5 a1.5,1.5 0 1 0 3,0 a1.5,1.5 0 1 0 -3,0Z';
  79. $finder = [];
  80. foreach($pos as $coord){
  81. [$ix, $iy] = $coord;
  82. $finder[] = sprintf($path, $ix, $iy);
  83. }
  84. return implode(' ', $finder);
  85. }
  86. /**
  87. * returns a <g> element that contains the SVG logo and positions it properly within the QR Code
  88. *
  89. * @see https://developer.mozilla.org/en-US/docs/Web/SVG/Element/g
  90. * @see https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/transform
  91. */
  92. protected function getLogo():string{
  93. // @todo: customize the <g> element to your liking (css class, style...)
  94. return sprintf(
  95. '%5$s<g transform="translate(%1$s %1$s) scale(%2$s)" class="%3$s">%5$s %4$s%5$s</g>',
  96. (($this->moduleCount - ($this->moduleCount * $this->options->svgLogoScale)) / 2),
  97. $this->options->svgLogoScale,
  98. $this->options->svgLogoCssClass,
  99. file_get_contents($this->options->svgLogo),
  100. $this->options->eol
  101. );
  102. }
  103. }
  104. /**
  105. * augment the QROptions class
  106. */
  107. class SVGWithLogoAndCustomShapesOptions extends QROptions{
  108. // path to svg logo
  109. protected string $svgLogo;
  110. // logo scale in % of QR Code size, clamped to 10%-30%
  111. protected float $svgLogoScale = 0.20;
  112. // css class for the logo (defined in $svgDefs)
  113. protected string $svgLogoCssClass = '';
  114. // check logo
  115. protected function set_svgLogo(string $svgLogo):void{
  116. if(!file_exists($svgLogo) || !is_readable($svgLogo)){
  117. throw new QRCodeException('invalid svg logo');
  118. }
  119. // @todo: validate svg
  120. $this->svgLogo = $svgLogo;
  121. }
  122. // clamp logo scale
  123. protected function set_svgLogoScale(float $svgLogoScale):void{
  124. $this->svgLogoScale = max(0.05, min(0.3, $svgLogoScale));
  125. }
  126. }
  127. /*
  128. * Runtime
  129. */
  130. // please excuse the IDE yelling https://youtrack.jetbrains.com/issue/WI-66549
  131. $options = new SVGWithLogoAndCustomShapesOptions;
  132. // SVG logo options (see extended class below)
  133. $options->svgLogo = __DIR__.'/github.svg'; // logo from: https://github.com/simple-icons/simple-icons
  134. $options->svgLogoScale = 0.25;
  135. $options->svgLogoCssClass = 'dark';
  136. // QROptions
  137. $options->version = 5;
  138. $options->quietzoneSize = 4;
  139. $options->outputType = QROutputInterface::CUSTOM;
  140. $options->outputInterface = QRSvgWithLogoAndCustomShapes::class;
  141. $options->imageBase64 = false;
  142. $options->eccLevel = EccLevel::H; // ECC level H is required when using logos
  143. $options->addQuietzone = true;
  144. // https://developer.mozilla.org/en-US/docs/Web/SVG/Element/linearGradient
  145. $options->svgDefs = '
  146. <linearGradient id="gradient" x1="100%" y2="100%">
  147. <stop stop-color="#D70071" offset="0"/>
  148. <stop stop-color="#9C4E97" offset="0.5"/>
  149. <stop stop-color="#0035A9" offset="1"/>
  150. </linearGradient>
  151. <style><![CDATA[
  152. .dark{fill: url(#gradient);}
  153. .light{fill: #eaeaea;}
  154. ]]></style>';
  155. $qrcode = (new QRCode($options))->render('https://www.youtube.com/watch?v=dQw4w9WgXcQ');
  156. if(php_sapi_name() !== 'cli'){
  157. header('Content-type: image/svg+xml');
  158. if(extension_loaded('zlib')){
  159. header('Vary: Accept-Encoding');
  160. header('Content-Encoding: gzip');
  161. $qrcode = gzencode($qrcode, 9);
  162. }
  163. }
  164. echo $qrcode;
  165. exit;