svg.php 2.4 KB

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