svgRandomColoredDots.php 3.9 KB

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