svg.php 2.1 KB

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