svgRandomColoredDots.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. /**
  3. * @see https://github.com/chillerlan/php-qrcode/discussions/136
  4. *
  5. * @created 09.07.2022
  6. * @author Smiley <smiley@chillerlan.net>
  7. * @copyright 2022 Smiley
  8. * @license MIT
  9. *
  10. * @noinspection PhpIllegalPsrClassPathInspection
  11. */
  12. use chillerlan\QRCode\{QRCode, QROptions};
  13. use chillerlan\QRCode\Common\EccLevel;
  14. use chillerlan\QRCode\Data\QRMatrix;
  15. use chillerlan\QRCode\Output\{QROutputInterface, QRMarkupSVG};
  16. require_once __DIR__.'/../vendor/autoload.php';
  17. /*
  18. * Class definition
  19. */
  20. // the extended SVG output module
  21. class RandomDotsSVGOutput extends QRMarkupSVG{
  22. /**
  23. * @inheritDoc
  24. */
  25. protected function path(string $path, int $M_TYPE):string{
  26. // omit the "fill" and "opacity" attributes on the path element
  27. return sprintf('<path class="%s" d="%s"/>', $this->getCssClass($M_TYPE), $path);
  28. }
  29. /**
  30. * To alter the layer a module appears on, we need to re-implement the collection method
  31. *
  32. * @inheritDoc
  33. */
  34. protected function collectModules(Closure $transform):array{
  35. $paths = [];
  36. // collect the modules for each type
  37. for($y = 0; $y < $this->moduleCount; $y++){
  38. for($x = 0; $x < $this->moduleCount; $x++){
  39. $M_TYPE = $this->matrix->get($x, $y);
  40. $M_TYPE_LAYER = $M_TYPE;
  41. if($this->options->connectPaths
  42. && !$this->matrix->checkTypeIn($x, $y, $this->options->excludeFromConnect)
  43. ){
  44. // to connect paths we'll redeclare the $M_TYPE_LAYER to data only
  45. $M_TYPE_LAYER = QRMatrix::M_DATA;
  46. if($this->matrix->check($x, $y)){
  47. $M_TYPE_LAYER = QRMatrix::M_DATA_DARK;
  48. }
  49. }
  50. // randomly assign another $M_TYPE_LAYER for the given types
  51. // note that the layer id has to be an integer value,
  52. // ideally outside the several bitmask values
  53. if($M_TYPE_LAYER === QRMatrix::M_DATA_DARK){
  54. $M_TYPE_LAYER = array_rand($this->options->dotColors);
  55. }
  56. // collect the modules per $M_TYPE
  57. $module = $transform($x, $y, $M_TYPE, $M_TYPE_LAYER);
  58. if(!empty($module)){
  59. $paths[$M_TYPE_LAYER][] = $module;
  60. }
  61. }
  62. }
  63. // beautify output
  64. ksort($paths);
  65. return $paths;
  66. }
  67. }
  68. // the extended options with the $dotColors option
  69. class RandomDotsOptions extends QROptions{
  70. /**
  71. * a map of $M_TYPE_LAYER => color
  72. *
  73. * @see \array_rand()
  74. */
  75. protected array $dotColors = [];
  76. }
  77. /*
  78. * Runtime
  79. */
  80. // our custom dot colors
  81. $dotColors = [
  82. 111 => '#e2453c',
  83. 222 => '#e07e39',
  84. 333 => '#e5d667',
  85. 444 => '#51b95b',
  86. 555 => '#1e72b7',
  87. 666 => '#6f5ba7',
  88. ];
  89. // generate the CSS for the several colored layers
  90. $layerColors = '';
  91. foreach($dotColors as $layer => $color){
  92. $layerColors .= sprintf("\n\t\t.qr-%s{ fill: %s; }", $layer, $color);
  93. }
  94. // prepare the options
  95. $options = new RandomDotsOptions([
  96. 'dotColors' => $dotColors,
  97. 'svgDefs' => '
  98. <style><![CDATA[
  99. .light{ fill: #dedede; }
  100. .dark{ fill: #424242; }
  101. '.$layerColors.'
  102. ]]></style>',
  103. 'version' => 5,
  104. 'eccLevel' => EccLevel::H,
  105. 'addQuietzone' => true,
  106. 'imageBase64' => false,
  107. 'outputType' => QROutputInterface::CUSTOM,
  108. 'outputInterface' => RandomDotsSVGOutput::class,
  109. 'drawLightModules' => false,
  110. 'connectPaths' => true,
  111. 'excludeFromConnect' => [
  112. QRMatrix::M_FINDER_DARK,
  113. QRMatrix::M_FINDER_DOT,
  114. QRMatrix::M_ALIGNMENT_DARK,
  115. ],
  116. 'drawCircularModules' => true,
  117. 'circleRadius' => 0.4,
  118. 'keepAsSquare' => [
  119. QRMatrix::M_FINDER_DARK,
  120. QRMatrix::M_FINDER_DOT,
  121. QRMatrix::M_ALIGNMENT_DARK,
  122. ],
  123. ]);
  124. // dump the output
  125. if(php_sapi_name() !== 'cli'){
  126. header('content-type: image/svg+xml');
  127. }
  128. echo (new QRCode($options))->render('https://www.youtube.com/watch?v=dQw4w9WgXcQ');
  129. exit;