svgRandomColoredDots.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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\Common\EccLevel;
  13. use chillerlan\QRCode\Data\QRMatrix;
  14. use chillerlan\QRCode\Output\{QROutputInterface, QRMarkupSVG};
  15. use chillerlan\QRCode\{QRCode, QROptions};
  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. foreach($this->matrix->matrix() as $y => $row){
  38. foreach($row as $x => $M_TYPE){
  39. $M_TYPE_LAYER = $M_TYPE;
  40. if($this->options->connectPaths
  41. && !$this->matrix->checkTypeIn($x, $y, $this->options->excludeFromConnect)
  42. ){
  43. // to connect paths we'll redeclare the $M_TYPE_LAYER to data only
  44. $M_TYPE_LAYER = QRMatrix::M_DATA;
  45. if($this->matrix->check($x, $y)){
  46. $M_TYPE_LAYER |= QRMatrix::IS_DARK;
  47. }
  48. }
  49. // randomly assign another $M_TYPE_LAYER for the given types
  50. // note that the layer id has to be an integer value,
  51. // ideally outside of the several bitmask values
  52. if($M_TYPE_LAYER === (QRMatrix::M_DATA | QRMatrix::IS_DARK)){
  53. $M_TYPE_LAYER = array_rand($this->options->dotColors);
  54. }
  55. // collect the modules per $M_TYPE
  56. $module = $transform($x, $y, $M_TYPE, $M_TYPE_LAYER);
  57. if(!empty($module)){
  58. $paths[$M_TYPE_LAYER][] = $module;
  59. }
  60. }
  61. }
  62. // beautify output
  63. ksort($paths);
  64. return $paths;
  65. }
  66. }
  67. // the extended options with the $dotColors option
  68. class RandomDotsOptions extends QROptions{
  69. /**
  70. * a map of $M_TYPE_LAYER => color
  71. *
  72. * @see \array_rand()
  73. */
  74. protected array $dotColors = [];
  75. }
  76. /*
  77. * Runtime
  78. */
  79. // our custom dot colors
  80. $dotColors = [
  81. 111 => '#e2453c',
  82. 222 => '#e07e39',
  83. 333 => '#e5d667',
  84. 444 => '#51b95b',
  85. 555 => '#1e72b7',
  86. 666 => '#6f5ba7',
  87. ];
  88. // generate the CSS for the several colored layers
  89. $layerColors = '';
  90. foreach($dotColors as $layer => $color){
  91. $layerColors .= sprintf("\n\t\t.qr-%s{ fill: %s; }", $layer, $color);
  92. }
  93. // prepare the options
  94. $options = new RandomDotsOptions([
  95. 'dotColors' => $dotColors,
  96. 'svgDefs' => '
  97. <style><![CDATA[
  98. .light{ fill: #dedede; }
  99. .dark{ fill: #424242; }
  100. '.$layerColors.'
  101. ]]></style>',
  102. 'version' => 5,
  103. 'eccLevel' => EccLevel::H,
  104. 'addQuietzone' => true,
  105. 'imageBase64' => false,
  106. 'outputType' => QROutputInterface::CUSTOM,
  107. 'outputInterface' => RandomDotsSVGOutput::class,
  108. 'drawLightModules' => false,
  109. 'connectPaths' => true,
  110. 'excludeFromConnect' => [
  111. QRMatrix::M_FINDER|QRMatrix::IS_DARK,
  112. QRMatrix::M_FINDER_DOT|QRMatrix::IS_DARK,
  113. QRMatrix::M_ALIGNMENT|QRMatrix::IS_DARK,
  114. ],
  115. 'drawCircularModules' => true,
  116. 'circleRadius' => 0.4,
  117. 'keepAsSquare' => [
  118. QRMatrix::M_FINDER|QRMatrix::IS_DARK,
  119. QRMatrix::M_FINDER_DOT|QRMatrix::IS_DARK,
  120. QRMatrix::M_ALIGNMENT|QRMatrix::IS_DARK,
  121. ],
  122. ]);
  123. // dump the output
  124. if(php_sapi_name() !== 'cli'){
  125. header('content-type: image/svg+xml');
  126. }
  127. echo (new QRCode($options))->render('https://www.youtube.com/watch?v=dQw4w9WgXcQ');
  128. exit;