svgRandomColoredDots.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. /**
  3. * Randomly colored modules example
  4. *
  5. * @see https://github.com/chillerlan/php-qrcode/discussions/136
  6. *
  7. * @created 09.07.2022
  8. * @author Smiley <smiley@chillerlan.net>
  9. * @copyright 2022 Smiley
  10. * @license MIT
  11. *
  12. * @noinspection PhpIllegalPsrClassPathInspection
  13. */
  14. declare(strict_types=1);
  15. use chillerlan\QRCode\{QRCode, QROptions};
  16. use chillerlan\QRCode\Common\EccLevel;
  17. use chillerlan\QRCode\Data\QRMatrix;
  18. use chillerlan\QRCode\Output\QRMarkupSVG;
  19. require_once __DIR__.'/../vendor/autoload.php';
  20. /*
  21. * Class definition
  22. */
  23. // the extended SVG output module
  24. class RandomDotsSVGOutput extends QRMarkupSVG{
  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. $dotColors = $this->options->dotColors; // avoid magic getter in long loops
  37. // collect the modules for each type
  38. foreach($this->matrix->getMatrix() as $y => $row){
  39. foreach($row as $x => $M_TYPE){
  40. $M_TYPE_LAYER = $M_TYPE;
  41. if($this->connectPaths && !$this->matrix->checkTypeIn($x, $y, $this->excludeFromConnect)){
  42. // to connect paths we'll redeclare the $M_TYPE_LAYER to data only
  43. $M_TYPE_LAYER = QRMatrix::M_DATA;
  44. if($this->matrix->isDark($M_TYPE)){
  45. $M_TYPE_LAYER = QRMatrix::M_DATA_DARK;
  46. }
  47. }
  48. // randomly assign another $M_TYPE_LAYER for the given types
  49. // note that the layer id has to be an integer value,
  50. // ideally outside the several bitmask values
  51. if($M_TYPE_LAYER === QRMatrix::M_DATA_DARK){
  52. $M_TYPE_LAYER = array_rand($dotColors);
  53. }
  54. // collect the modules per $M_TYPE
  55. $module = $transform($x, $y, $M_TYPE, $M_TYPE_LAYER);
  56. if(!empty($module)){
  57. $paths[$M_TYPE_LAYER][] = $module;
  58. }
  59. }
  60. }
  61. // beautify output
  62. ksort($paths);
  63. return $paths;
  64. }
  65. }
  66. /**
  67. * the extended options with the $dotColors option
  68. *
  69. * @property array<int, string> $dotColors
  70. */
  71. class RandomDotsOptions extends QROptions{
  72. /**
  73. * a map of $M_TYPE_LAYER => color
  74. *
  75. * @see \array_rand()
  76. * @var array<int, string>
  77. */
  78. protected array $dotColors = [];
  79. }
  80. /*
  81. * Runtime
  82. */
  83. // prepare the options
  84. $options = new RandomDotsOptions;
  85. // our custom dot colors
  86. // adding the IS_DARK flag so that the proper layer css class is assigned
  87. $options->dotColors = [
  88. (111 | QRMatrix::IS_DARK) => '#e2453c',
  89. (222 | QRMatrix::IS_DARK) => '#e07e39',
  90. (333 | QRMatrix::IS_DARK) => '#e5d667',
  91. (444 | QRMatrix::IS_DARK) => '#51b95b',
  92. (555 | QRMatrix::IS_DARK) => '#1e72b7',
  93. (666 | QRMatrix::IS_DARK) => '#6f5ba7',
  94. ];
  95. // generate the CSS for the several colored layers
  96. $layerColors = '';
  97. foreach($options->dotColors as $layer => $color){
  98. $layerColors .= sprintf("\n\t\t.qr-%s{ fill: %s; }", $layer, $color);
  99. }
  100. $options->svgDefs = '
  101. <style><![CDATA[
  102. .dark{ fill: #424242; }
  103. '.$layerColors.'
  104. ]]></style>';
  105. // set the custom output interface
  106. $options->outputInterface = RandomDotsSVGOutput::class;
  107. // common qrcode options
  108. $options->version = 5;
  109. $options->eccLevel = EccLevel::H;
  110. $options->addQuietzone = true;
  111. $options->outputBase64 = false;
  112. $options->drawLightModules = false;
  113. $options->connectPaths = true;
  114. $options->excludeFromConnect = [
  115. QRMatrix::M_FINDER_DARK,
  116. QRMatrix::M_FINDER_DOT,
  117. QRMatrix::M_ALIGNMENT_DARK,
  118. ];
  119. $options->drawCircularModules = true;
  120. $options->circleRadius = 0.4;
  121. $options->keepAsSquare = [
  122. QRMatrix::M_FINDER_DARK,
  123. QRMatrix::M_FINDER_DOT,
  124. QRMatrix::M_ALIGNMENT_DARK,
  125. ];
  126. $out = (new QRCode($options))->render('https://www.youtube.com/watch?v=dQw4w9WgXcQ');
  127. // dump the output
  128. if(PHP_SAPI !== 'cli'){
  129. header('content-type: image/svg+xml');
  130. }
  131. echo $out;
  132. exit;