svg.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. /**
  3. *
  4. * @filesource svg.php
  5. * @created 21.12.2017
  6. * @author Smiley <smiley@chillerlan.net>
  7. * @copyright 2017 Smiley
  8. * @license MIT
  9. */
  10. namespace chillerlan\QRCodeExamples;
  11. use chillerlan\QRCode\{QRCode, QROptions};
  12. use chillerlan\QRCode\Data\QRMatrix;
  13. use chillerlan\QRCode\Common\EccLevel;
  14. require_once __DIR__.'/../vendor/autoload.php';
  15. $data = 'https://www.youtube.com/watch?v=DLzxrzFCyOs&t=43s';
  16. $gzip = true;
  17. $options = new QROptions([
  18. 'version' => 7,
  19. 'outputType' => QRCode::OUTPUT_MARKUP_SVG,
  20. 'imageBase64' => false,
  21. 'eccLevel' => EccLevel::L,
  22. 'svgViewBoxSize' => 530,
  23. 'addQuietzone' => true,
  24. 'cssClass' => 'my-css-class',
  25. 'svgOpacity' => 1.0,
  26. 'svgDefs' => '
  27. <linearGradient id="g2">
  28. <stop offset="0%" stop-color="#39F" />
  29. <stop offset="100%" stop-color="#F3F" />
  30. </linearGradient>
  31. <linearGradient id="g1">
  32. <stop offset="0%" stop-color="#F3F" />
  33. <stop offset="100%" stop-color="#39F" />
  34. </linearGradient>
  35. <style>rect{shape-rendering:crispEdges}</style>',
  36. 'moduleValues' => [
  37. // finder
  38. QRMatrix::M_FINDER | QRMatrix::IS_DARK => 'url(#g1)', // dark (true)
  39. QRMatrix::M_FINDER => '#fff', // light (false)
  40. QRMatrix::M_FINDER_DOT | QRMatrix::IS_DARK => 'url(#g2)', // finder dot, dark (true)
  41. // alignment
  42. QRMatrix::M_ALIGNMENT | QRMatrix::IS_DARK => 'url(#g1)',
  43. QRMatrix::M_ALIGNMENT => '#fff',
  44. // timing
  45. QRMatrix::M_TIMING | QRMatrix::IS_DARK => 'url(#g1)',
  46. QRMatrix::M_TIMING => '#fff',
  47. // format
  48. QRMatrix::M_FORMAT | QRMatrix::IS_DARK => 'url(#g1)',
  49. QRMatrix::M_FORMAT => '#fff',
  50. // version
  51. QRMatrix::M_VERSION | QRMatrix::IS_DARK => 'url(#g1)',
  52. QRMatrix::M_VERSION => '#fff',
  53. // data
  54. QRMatrix::M_DATA | QRMatrix::IS_DARK => 'url(#g2)',
  55. QRMatrix::M_DATA => '#fff',
  56. // darkmodule
  57. QRMatrix::M_DARKMODULE | QRMatrix::IS_DARK => 'url(#g1)',
  58. // separator
  59. QRMatrix::M_SEPARATOR => '#fff',
  60. // quietzone
  61. QRMatrix::M_QUIETZONE => '#fff',
  62. ],
  63. ]);
  64. $qrcode = (new QRCode($options))->render($data);
  65. header('Content-type: image/svg+xml');
  66. if($gzip === true){
  67. header('Vary: Accept-Encoding');
  68. header('Content-Encoding: gzip');
  69. $qrcode = gzencode($qrcode, 9);
  70. }
  71. echo $qrcode;