QRMarkupSVG.md.txt 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. # QRMarkupSVG
  2. [Class `QRMarkupSVG`](https://github.com/chillerlan/php-qrcode/blob/main/src/Output/QRMarkupSVG.php):
  3. [Scalable Vector Graphics](https://developer.mozilla.org/en-US/docs/Glossary/SVG) (SVG) output.
  4. ## Example
  5. See: [SVG example](https://github.com/chillerlan/php-qrcode/blob/main/examples/svg.php)
  6. Set the options:
  7. ```php
  8. $options = new QROptions;
  9. $options->version = 7;
  10. $options->outputInterface = QRMarkupSVG::class;
  11. // if set to false, the light modules won't be rendered
  12. $options->drawLightModules = true;
  13. $options->svgUseFillAttributes = true;
  14. // draw the modules as circles isntead of squares
  15. $options->drawCircularModules = true;
  16. $options->circleRadius = 0.4;
  17. // connect paths to avoid render glitches
  18. // @see https://github.com/chillerlan/php-qrcode/issues/57
  19. $options->connectPaths = true;
  20. // keep modules of these types as square
  21. $options->keepAsSquare = [
  22. QRMatrix::M_FINDER_DARK,
  23. QRMatrix::M_FINDER_DOT,
  24. QRMatrix::M_ALIGNMENT_DARK,
  25. ];
  26. // add a gradient via the <defs> element
  27. // @see https://developer.mozilla.org/en-US/docs/Web/SVG/Element/defs
  28. // @see https://developer.mozilla.org/en-US/docs/Web/SVG/Element/linearGradient
  29. $options->svgDefs = '
  30. <linearGradient id="rainbow" x1="1" y2="1">
  31. <stop stop-color="#e2453c" offset="0"/>
  32. <stop stop-color="#e07e39" offset="0.2"/>
  33. <stop stop-color="#e5d667" offset="0.4"/>
  34. <stop stop-color="#51b95b" offset="0.6"/>
  35. <stop stop-color="#1e72b7" offset="0.8"/>
  36. <stop stop-color="#6f5ba7" offset="1"/>
  37. </linearGradient>
  38. <style><![CDATA[
  39. .dark{fill: url(#rainbow);}
  40. .light{fill: #eee;}
  41. ]]></style>';
  42. ```
  43. Render the output:
  44. ```php
  45. $data = 'https://www.youtube.com/watch?v=dQw4w9WgXcQ';
  46. $out = (new QRCode($options))->render($data); // -> data:image/svg+xml;base64,PD94bWwgdmVyc2...
  47. printf('<img alt="%s" src="%s" />', $alt, $out);
  48. ```
  49. ## Additional methods
  50. | method | return | description |
  51. |---------------------------------------------------|----------|---------------------------------------------------------------|
  52. | (protected) `getCssClass(int $M_TYPE = 0)` | `string` | returns a string with all css classes for the current element |
  53. | (protected) `getViewBox()` | `string` | returns the value for the SVG viewBox attribute |
  54. | (protected) `header()` | `string` | returns the `<svg>` header with the given options parsed |
  55. | (protected) `path(string $path, int $M_TYPE)` | `string` | renders and returns a single `<path>` element |
  56. | (protected) `paths()` | `string` | returns one or more SVG `<path>` elements |
  57. | (protected) `module(int $x, int $y, int $M_TYPE)` | `string` | returns a path segment for a single module |
  58. ## Options that affect this class
  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. | `$outputBase64` | `bool` |
  70. | `$svgAddXmlHeader` | `bool` |
  71. | `$svgDefs` | `string` |
  72. | `$svgOpacity` | `float` |
  73. | `$svgPreserveAspectRatio` | `string` |
  74. | `$svgViewBoxSize` | `int\|null` |
  75. | `$svgUseFillAttributes` | `bool` |