svg.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. /**
  3. * SVG output example
  4. *
  5. * @created 21.12.2017
  6. * @author Smiley <smiley@chillerlan.net>
  7. * @copyright 2017 Smiley
  8. * @license MIT
  9. *
  10. * @noinspection PhpComposerExtensionStubsInspection
  11. */
  12. declare(strict_types=1);
  13. use chillerlan\QRCode\{QRCode, QROptions};
  14. use chillerlan\QRCode\Data\QRMatrix;
  15. use chillerlan\QRCode\Output\QRMarkupSVG;
  16. require_once __DIR__.'/../vendor/autoload.php';
  17. $options = new QROptions;
  18. $options->version = 7;
  19. $options->outputInterface = QRMarkupSVG::class;
  20. $options->outputBase64 = false;
  21. // if set to false, the light modules won't be rendered
  22. $options->drawLightModules = true;
  23. $options->svgUseFillAttributes = false;
  24. // draw the modules as circles isntead of squares
  25. $options->drawCircularModules = true;
  26. $options->circleRadius = 0.4;
  27. // connect paths
  28. $options->connectPaths = true;
  29. // keep modules of these types as square
  30. $options->keepAsSquare = [
  31. QRMatrix::M_FINDER_DARK,
  32. QRMatrix::M_FINDER_DOT,
  33. QRMatrix::M_ALIGNMENT_DARK,
  34. ];
  35. // https://developer.mozilla.org/en-US/docs/Web/SVG/Element/linearGradient
  36. $options->svgDefs = '
  37. <linearGradient id="rainbow" x1="1" y2="1">
  38. <stop stop-color="#e2453c" offset="0"/>
  39. <stop stop-color="#e07e39" offset="0.2"/>
  40. <stop stop-color="#e5d667" offset="0.4"/>
  41. <stop stop-color="#51b95b" offset="0.6"/>
  42. <stop stop-color="#1e72b7" offset="0.8"/>
  43. <stop stop-color="#6f5ba7" offset="1"/>
  44. </linearGradient>
  45. <style><![CDATA[
  46. .dark{fill: url(#rainbow);}
  47. .light{fill: #eee;}
  48. ]]></style>';
  49. try{
  50. $out = (new QRCode($options))->render('https://www.youtube.com/watch?v=dQw4w9WgXcQ');
  51. }
  52. catch(Throwable $e){
  53. // handle the exception in whatever way you need
  54. exit($e->getMessage());
  55. }
  56. if(PHP_SAPI !== 'cli'){
  57. header('Content-type: image/svg+xml');
  58. if(extension_loaded('zlib')){
  59. header('Vary: Accept-Encoding');
  60. header('Content-Encoding: gzip');
  61. $out = gzencode($out, 9);
  62. }
  63. }
  64. echo $out;
  65. exit;