Built-In-Output-QRMarkupSVG.md.txt 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. # QRMarkupSVG
  2. [Class `QRMarkupSVG`](https://github.com/chillerlan/php-qrcode/blob/main/src/Output/QRMarkupSVG.php): [Scalable Vector Graphics](https://developer.mozilla.org/en-US/docs/Glossary/SVG) (SVG) output
  3. ## Example
  4. See: [ImageMagick example](https://github.com/chillerlan/php-qrcode/blob/main/examples/imagick.php)
  5. Set the options:
  6. ```php
  7. $options = new QROptions;
  8. $options->version = 7;
  9. $options->outputType = QROutputInterface::MARKUP_SVG;
  10. // if set to false, the light modules won't be rendered
  11. $options->drawLightModules = true;
  12. // empty the default value to remove the fill* and opacity* attributes from the <path> elements
  13. $options->markupDark = '';
  14. $options->markupLight = '';
  15. // draw the modules as circles isntead of squares
  16. $options->drawCircularModules = true;
  17. $options->circleRadius = 0.4;
  18. // connect paths to avoid render glitches
  19. // @see https://github.com/chillerlan/php-qrcode/issues/57
  20. $options->connectPaths = true;
  21. // keep modules of these types as square
  22. $options->keepAsSquare = [
  23. QRMatrix::M_FINDER_DARK,
  24. QRMatrix::M_FINDER_DOT,
  25. QRMatrix::M_ALIGNMENT_DARK,
  26. ];
  27. // add a gradient via the <defs> element
  28. // @see https://developer.mozilla.org/en-US/docs/Web/SVG/Element/defs
  29. // @see https://developer.mozilla.org/en-US/docs/Web/SVG/Element/linearGradient
  30. $options->svgDefs = '
  31. <linearGradient id="rainbow" x1="1" y2="1">
  32. <stop stop-color="#e2453c" offset="0"/>
  33. <stop stop-color="#e07e39" offset="0.2"/>
  34. <stop stop-color="#e5d667" offset="0.4"/>
  35. <stop stop-color="#51b95b" offset="0.6"/>
  36. <stop stop-color="#1e72b7" offset="0.8"/>
  37. <stop stop-color="#6f5ba7" offset="1"/>
  38. </linearGradient>
  39. <style><![CDATA[
  40. .dark{fill: url(#rainbow);}
  41. .light{fill: #eee;}
  42. ]]></style>';
  43. ```
  44. Render the output:
  45. ```php
  46. $data = 'https://www.youtube.com/watch?v=dQw4w9WgXcQ';
  47. $out = (new QRCode($options))->render($data); // -> data:image/svg+xml;base64,PD94bWwgdmVyc2...
  48. printf('<img alt="%s" src="%s" />', $alt, $out);
  49. ```
  50. ## Additional methods
  51. | method | return | description |
  52. |---------------------------------------------------|----------|---------------------------------------------------------------|
  53. | (protected) `getCssClass(int $M_TYPE = 0)` | `string` | returns a string with all css classes for the current element |
  54. | (protected) `header()` | `string` | returns the `<svg>` header with the given options parsed |
  55. | (protected) `module(int $x, int $y, int $M_TYPE)` | `string` | returns a path segment for a single module |
  56. | (protected) `path(string $path, int $M_TYPE)` | `string` | renders and returns a single `<path>` element |
  57. | (protected) `paths()` | `string` | returns one or more SVG `<path>` elements |
  58. ## Options that affect this module
  59. | property | type |
  60. |---------------------------|----------------|
  61. | `$circleRadius` | `float` |
  62. | `$connectPaths` | `bool` |
  63. | `$cssClass` | `string` |
  64. | `$drawCircularModules` | `bool` |
  65. | `$drawLightModules` | `bool` |
  66. | `$eol` | `string` |
  67. | `$excludeFromConnect` | `array` |
  68. | `$keepAsSquare` | `array` |
  69. | `$markupDark` | `string` |
  70. | `$markupLight` | `string` |
  71. | `$outputBase64` | `bool` |
  72. | `$svgAddXmlHeader` | `bool` |
  73. | `$svgDefs` | `string` |
  74. | `$svgOpacity` | `float` |
  75. | `$svgPreserveAspectRatio` | `string` |
  76. | `$svgViewBoxSize` | `int\|null` |
  77. ### Options that have no effect
  78. | property | reason |
  79. |---------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
  80. | `$bgColor` | background color can be achieved via CSS, attributes or the `<defs>` element, see also [php-qrcode/discussions/199 (comment)](https://github.com/chillerlan/php-qrcode/discussions/199#discussioncomment-5747471) |
  81. | `$imageTransparent` | SVG is - similar to a HTML element - transparent by default |
  82. | `$returnResource` | N/A |
  83. | `$scale` | `$scale` (pixel size of a qr module) is intended for raster image types, use `$svgViewBoxSize` instead |