svg.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. /**
  3. * @created 21.12.2017
  4. * @author Smiley <smiley@chillerlan.net>
  5. * @copyright 2017 Smiley
  6. * @license MIT
  7. *
  8. * @noinspection PhpComposerExtensionStubsInspection
  9. */
  10. namespace chillerlan\QRCodeExamples;
  11. use chillerlan\QRCode\{QRCode, QROptions};
  12. use chillerlan\QRCode\Data\QRMatrix;
  13. use chillerlan\QRCode\Common\EccLevel;
  14. use function gzencode, header;
  15. require_once __DIR__.'/../vendor/autoload.php';
  16. $data = 'https://www.youtube.com/watch?v=dQw4w9WgXcQ';
  17. $gzip = true;
  18. $options = new QROptions([
  19. 'version' => 7,
  20. 'outputType' => QRCode::OUTPUT_MARKUP_SVG,
  21. 'imageBase64' => false,
  22. 'eccLevel' => EccLevel::L,
  23. 'addQuietzone' => true,
  24. // if set to true, the light modules won't be rendered
  25. 'imageTransparent' => false,
  26. // empty the default value to remove the fill* attributes from the <path> elements
  27. 'markupDark' => '',
  28. 'markupLight' => '',
  29. // draw the modules as circles isntead of squares
  30. 'drawCircularModules' => true,
  31. 'circleRadius' => 0.4,
  32. // connect paths
  33. 'connectPaths' => true,
  34. // keep modules of thhese types as square
  35. 'keepAsSquare' => [
  36. QRMatrix::M_FINDER|QRMatrix::IS_DARK,
  37. QRMatrix::M_FINDER_DOT,
  38. QRMatrix::M_ALIGNMENT|QRMatrix::IS_DARK,
  39. ],
  40. // https://developer.mozilla.org/en-US/docs/Web/SVG/Element/linearGradient
  41. 'svgDefs' => '
  42. <linearGradient id="rainbow" x1="100%" y2="100%">
  43. <stop stop-color="#e2453c" offset="2.5%"/>
  44. <stop stop-color="#e07e39" offset="21.5%"/>
  45. <stop stop-color="#e5d667" offset="40.5%"/>
  46. <stop stop-color="#51b95b" offset="59.5%"/>
  47. <stop stop-color="#1e72b7" offset="78.5%"/>
  48. <stop stop-color="#6f5ba7" offset="97.5%"/>
  49. </linearGradient>
  50. <style><![CDATA[
  51. .dark{fill: url(#rainbow);}
  52. .light{fill: #eee;}
  53. ]]></style>',
  54. ]);
  55. $qrcode = (new QRCode($options))->render($data);
  56. header('Content-type: image/svg+xml');
  57. if($gzip){
  58. header('Vary: Accept-Encoding');
  59. header('Content-Encoding: gzip');
  60. $qrcode = gzencode($qrcode, 9);
  61. }
  62. echo $qrcode;