svgRandomColoredDots.php 3.5 KB

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