svgRandomColoredDots.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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\{QROutputInterface, 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. /**
  25. * @inheritDoc
  26. */
  27. protected function path(string $path, int $M_TYPE):string{
  28. // omit the "fill" and "opacity" attributes on the path element
  29. return sprintf('<path class="%s" d="%s"/>', $this->getCssClass($M_TYPE), $path);
  30. }
  31. /**
  32. * To alter the layer a module appears on, we need to re-implement the collection method
  33. *
  34. * @inheritDoc
  35. */
  36. protected function collectModules(Closure $transform):array{
  37. $paths = [];
  38. $dotColors = $this->options->dotColors; // avoid magic getter in long loops
  39. // collect the modules for each type
  40. foreach($this->matrix->getMatrix() as $y => $row){
  41. foreach($row as $x => $M_TYPE){
  42. $M_TYPE_LAYER = $M_TYPE;
  43. if($this->connectPaths && !$this->matrix->checkTypeIn($x, $y, $this->excludeFromConnect)){
  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->isDark($M_TYPE)){
  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($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. // prepare the options
  81. $options = new RandomDotsOptions;
  82. // our custom dot colors
  83. // adding the IS_DARK flag so that the proper layer css class is assigned
  84. $options->dotColors = [
  85. (111 | QRMatrix::IS_DARK) => '#e2453c',
  86. (222 | QRMatrix::IS_DARK) => '#e07e39',
  87. (333 | QRMatrix::IS_DARK) => '#e5d667',
  88. (444 | QRMatrix::IS_DARK) => '#51b95b',
  89. (555 | QRMatrix::IS_DARK) => '#1e72b7',
  90. (666 | QRMatrix::IS_DARK) => '#6f5ba7',
  91. ];
  92. // generate the CSS for the several colored layers
  93. $layerColors = '';
  94. foreach($options->dotColors as $layer => $color){
  95. $layerColors .= sprintf("\n\t\t.qr-%s{ fill: %s; }", $layer, $color);
  96. }
  97. $options->svgDefs = '
  98. <style><![CDATA[
  99. .dark{ fill: #424242; }
  100. '.$layerColors.'
  101. ]]></style>';
  102. // set the custom output interface
  103. $options->outputType = QROutputInterface::CUSTOM;
  104. $options->outputInterface = RandomDotsSVGOutput::class;
  105. // common qrcode options
  106. $options->version = 5;
  107. $options->eccLevel = EccLevel::H;
  108. $options->addQuietzone = true;
  109. $options->outputBase64 = false;
  110. $options->drawLightModules = false;
  111. $options->connectPaths = true;
  112. $options->excludeFromConnect = [
  113. QRMatrix::M_FINDER_DARK,
  114. QRMatrix::M_FINDER_DOT,
  115. QRMatrix::M_ALIGNMENT_DARK,
  116. ];
  117. $options->drawCircularModules = true;
  118. $options->circleRadius = 0.4;
  119. $options->keepAsSquare = [
  120. QRMatrix::M_FINDER_DARK,
  121. QRMatrix::M_FINDER_DOT,
  122. QRMatrix::M_ALIGNMENT_DARK,
  123. ];
  124. $out = (new QRCode($options))->render('https://www.youtube.com/watch?v=dQw4w9WgXcQ');
  125. // dump the output
  126. if(PHP_SAPI !== 'cli'){
  127. header('content-type: image/svg+xml');
  128. }
  129. echo $out;
  130. exit;