svg.php 2.0 KB

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