QRMarkupSVG.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. <?php
  2. /**
  3. * Class QRMarkupSVG
  4. *
  5. * @created 06.06.2022
  6. * @author smiley <smiley@chillerlan.net>
  7. * @copyright 2022 smiley
  8. * @license MIT
  9. */
  10. namespace chillerlan\QRCode\Output;
  11. use chillerlan\QRCode\Data\QRMatrix;
  12. use function array_chunk, implode, is_string, preg_match, sprintf, trim;
  13. /**
  14. * SVG output
  15. *
  16. * @see https://github.com/codemasher/php-qrcode/pull/5
  17. * @see https://developer.mozilla.org/en-US/docs/Web/SVG
  18. * @see https://www.sarasoueidan.com/demos/interactive-svg-coordinate-system/
  19. * @see http://apex.infogridpacific.com/SVG/svg-tutorial-contents.html
  20. */
  21. class QRMarkupSVG extends QRMarkup{
  22. /**
  23. * @todo: XSS proof
  24. *
  25. * @see https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/fill
  26. * @inheritDoc
  27. */
  28. public static function moduleValueIsValid($value):bool{
  29. if(!is_string($value)){
  30. return false;
  31. }
  32. $value = trim($value);
  33. // url(...)
  34. if(preg_match('~^url\([-/#a-z\d]+\)$~i', $value)){
  35. return true;
  36. }
  37. // otherwise check for standard css notation
  38. return parent::moduleValueIsValid($value);
  39. }
  40. /**
  41. * @inheritDoc
  42. */
  43. protected function getOutputDimensions():array{
  44. return [$this->moduleCount, $this->moduleCount];
  45. }
  46. /**
  47. * @inheritDoc
  48. */
  49. protected function createMarkup(bool $saveToFile):string{
  50. $svg = $this->header();
  51. if(!empty($this->options->svgDefs)){
  52. $svg .= sprintf('<defs>%1$s%2$s</defs>%2$s', $this->options->svgDefs, $this->eol);
  53. }
  54. $svg .= $this->paths();
  55. // close svg
  56. $svg .= sprintf('%1$s</svg>%1$s', $this->eol);
  57. // transform to data URI only when not saving to file
  58. if(!$saveToFile && $this->options->outputBase64){
  59. $svg = $this->toBase64DataURI($svg, 'image/svg+xml');
  60. }
  61. return $svg;
  62. }
  63. /**
  64. * returns the value for the SVG viewBox attribute
  65. *
  66. * @see https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/viewBox
  67. * @see https://css-tricks.com/scale-svg/#article-header-id-3
  68. */
  69. protected function getViewBox():string{
  70. [$width, $height] = $this->getOutputDimensions();
  71. return sprintf('0 0 %s %s', ($this->options->svgViewBoxSize ?? $width), ($this->options->svgViewBoxSize ?? $height));
  72. }
  73. /**
  74. * returns the <svg> header with the given options parsed
  75. *
  76. * @see https://developer.mozilla.org/en-US/docs/Web/SVG/Element/svg
  77. */
  78. protected function header():string{
  79. $header = sprintf(
  80. '<svg xmlns="http://www.w3.org/2000/svg" class="qr-svg %1$s" viewBox="%2$s" preserveAspectRatio="%3$s">%4$s',
  81. $this->options->cssClass,
  82. $this->getViewBox(),
  83. $this->options->svgPreserveAspectRatio,
  84. $this->eol
  85. );
  86. if($this->options->svgAddXmlHeader){
  87. $header = sprintf('<?xml version="1.0" encoding="UTF-8"?>%s%s', $this->eol, $header);
  88. }
  89. return $header;
  90. }
  91. /**
  92. * returns one or more SVG <path> elements
  93. */
  94. protected function paths():string{
  95. $paths = $this->collectModules(fn(int $x, int $y, int $M_TYPE):string => $this->module($x, $y, $M_TYPE));
  96. $svg = [];
  97. // create the path elements
  98. foreach($paths as $M_TYPE => $modules){
  99. // limit the total line length
  100. $chunks = array_chunk($modules, 100);
  101. $chonks = [];
  102. foreach($chunks as $chunk){
  103. $chonks[] = implode(' ', $chunk);
  104. }
  105. $path = implode($this->eol, $chonks);
  106. if(empty($path)){
  107. continue;
  108. }
  109. $svg[] = $this->path($path, $M_TYPE);
  110. }
  111. return implode($this->eol, $svg);
  112. }
  113. /**
  114. * renders and returns a single <path> element
  115. *
  116. * @see https://developer.mozilla.org/en-US/docs/Web/SVG/Element/path
  117. */
  118. protected function path(string $path, int $M_TYPE):string{
  119. if($this->options->svgUseFillAttributes){
  120. return sprintf(
  121. '<path class="%s" fill="%s" d="%s"/>',
  122. $this->getCssClass($M_TYPE),
  123. $this->getModuleValue($M_TYPE),
  124. $path
  125. );
  126. }
  127. return sprintf('<path class="%s" d="%s"/>', $this->getCssClass($M_TYPE), $path);
  128. }
  129. /**
  130. * @inheritDoc
  131. */
  132. protected function getCssClass(int $M_TYPE = 0):string{
  133. return implode(' ', [
  134. 'qr-'.($this::LAYERNAMES[$M_TYPE] ?? $M_TYPE),
  135. $this->matrix->isDark($M_TYPE) ? 'dark' : 'light',
  136. $this->options->cssClass,
  137. ]);
  138. }
  139. /**
  140. * returns a path segment for a single module
  141. *
  142. * @see https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/d
  143. */
  144. protected function module(int $x, int $y, int $M_TYPE):string{
  145. if(!$this->drawLightModules && !$this->matrix->isDark($M_TYPE)){
  146. return '';
  147. }
  148. if($this->drawCircularModules && !$this->matrix->checkTypeIn($x, $y, $this->keepAsSquare)){
  149. $r = $this->circleRadius;
  150. return sprintf(
  151. 'M%1$s %2$s a%3$s %3$s 0 1 0 %4$s 0 a%3$s %3$s 0 1 0 -%4$s 0Z',
  152. ($x + 0.5 - $r),
  153. ($y + 0.5),
  154. $r,
  155. ($r * 2)
  156. );
  157. }
  158. return sprintf('M%1$s %2$s h1 v1 h-1Z', $x, $y);
  159. }
  160. }