svg.php 1.9 KB

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