QRMarkupSVG.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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/Element/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 createMarkup(bool $saveToFile):string{
  44. $svg = $this->header();
  45. if(!empty($this->options->svgDefs)){
  46. $svg .= sprintf('<defs>%1$s%2$s</defs>%2$s', $this->options->svgDefs, $this->options->eol);
  47. }
  48. $svg .= $this->paths();
  49. // close svg
  50. $svg .= sprintf('%1$s</svg>%1$s', $this->options->eol);
  51. // transform to data URI only when not saving to file
  52. if(!$saveToFile && $this->options->imageBase64){
  53. $svg = $this->toBase64DataURI($svg, 'image/svg+xml');
  54. }
  55. return $svg;
  56. }
  57. /**
  58. * returns the <svg> header with the given options parsed
  59. */
  60. protected function header():string{
  61. $width = ($this->options->svgWidth !== null) ? sprintf(' width="%s"', $this->options->svgWidth) : '';
  62. $height = ($this->options->svgHeight !== null) ? sprintf(' height="%s"', $this->options->svgHeight) : '';
  63. /** @noinspection HtmlUnknownAttribute */
  64. $header = sprintf(
  65. '<svg xmlns="http://www.w3.org/2000/svg" class="qr-svg %1$s" viewBox="0 0 %2$s %2$s" preserveAspectRatio="%3$s"%4$s%5$s>%6$s',
  66. $this->options->cssClass,
  67. ($this->options->svgViewBoxSize ?? $this->moduleCount),
  68. $this->options->svgPreserveAspectRatio,
  69. $width,
  70. $height,
  71. $this->options->eol
  72. );
  73. if($this->options->svgAddXmlHeader){
  74. $header = sprintf('<?xml version="1.0" encoding="UTF-8"?>%s%s', $this->options->eol, $header);
  75. }
  76. return $header;
  77. }
  78. /**
  79. * returns one or more SVG <path> elements
  80. */
  81. protected function paths():string{
  82. $paths = $this->collectModules(fn(int $x, int $y, int $M_TYPE):string => $this->module($x, $y, $M_TYPE));
  83. $svg = [];
  84. // create the path elements
  85. foreach($paths as $M_TYPE => $modules){
  86. // limit the total line length
  87. $chunks = array_chunk($modules, 100);
  88. $chonks = [];
  89. foreach($chunks as $chunk){
  90. $chonks[] = implode(' ', $chunk);
  91. }
  92. $path = implode($this->options->eol, $chonks);
  93. if(empty($path)){
  94. continue;
  95. }
  96. $svg[] = $this->path($path, $M_TYPE);
  97. }
  98. return implode($this->options->eol, $svg);
  99. }
  100. /**
  101. * renders and returns a single <path> element
  102. *
  103. * @see https://developer.mozilla.org/en-US/docs/Web/SVG/Element/path
  104. */
  105. protected function path(string $path, int $M_TYPE):string{
  106. $val = $this->getModuleValue($M_TYPE);
  107. // ignore non-existent module values
  108. $format = empty($val)
  109. ? '<path class="%1$s" d="%2$s"/>'
  110. : '<path class="%1$s" fill="%3$s" fill-opacity="%4$s" d="%2$s"/>';
  111. return sprintf(
  112. $format,
  113. $this->getCssClass($M_TYPE),
  114. $path,
  115. ($val ?? ''), // value may or may not exist
  116. $this->options->svgOpacity
  117. );
  118. }
  119. /**
  120. * @inheritDoc
  121. */
  122. protected function getCssClass(int $M_TYPE = 0):string{
  123. return implode(' ', [
  124. 'qr-'.($this::LAYERNAMES[$M_TYPE] ?? $M_TYPE),
  125. (($M_TYPE & QRMatrix::IS_DARK) === QRMatrix::IS_DARK) ? 'dark' : 'light',
  126. $this->options->cssClass,
  127. ]);
  128. }
  129. /**
  130. * returns a path segment for a single module
  131. *
  132. * @see https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/d
  133. */
  134. protected function module(int $x, int $y, int $M_TYPE):string{
  135. if(!$this->options->drawLightModules && !$this->matrix->check($x, $y)){
  136. return '';
  137. }
  138. if($this->options->drawCircularModules && !$this->matrix->checkTypeIn($x, $y, $this->options->keepAsSquare)){
  139. $r = $this->options->circleRadius;
  140. return sprintf(
  141. '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',
  142. ($x + 0.5 - $r),
  143. ($y + 0.5),
  144. $r,
  145. ($r * 2)
  146. );
  147. }
  148. return sprintf('M%1$s %2$s h1 v1 h-1Z', $x, $y);
  149. }
  150. }