svgRandomColoredDots.php 3.8 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\{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. // collect the modules for each type
  39. for($y = 0; $y < $this->moduleCount; $y++){
  40. for($x = 0; $x < $this->moduleCount; $x++){
  41. $M_TYPE = $this->matrix->get($x, $y);
  42. $M_TYPE_LAYER = $M_TYPE;
  43. if($this->options->connectPaths
  44. && !$this->matrix->checkTypeIn($x, $y, $this->options->excludeFromConnect)
  45. ){
  46. // to connect paths we'll redeclare the $M_TYPE_LAYER to data only
  47. $M_TYPE_LAYER = QRMatrix::M_DATA;
  48. if($this->matrix->check($x, $y)){
  49. $M_TYPE_LAYER = QRMatrix::M_DATA_DARK;
  50. }
  51. }
  52. // randomly assign another $M_TYPE_LAYER for the given types
  53. // note that the layer id has to be an integer value,
  54. // ideally outside the several bitmask values
  55. if($M_TYPE_LAYER === QRMatrix::M_DATA_DARK){
  56. $M_TYPE_LAYER = array_rand($this->options->dotColors);
  57. }
  58. // collect the modules per $M_TYPE
  59. $module = $transform($x, $y, $M_TYPE, $M_TYPE_LAYER);
  60. if(!empty($module)){
  61. $paths[$M_TYPE_LAYER][] = $module;
  62. }
  63. }
  64. }
  65. // beautify output
  66. ksort($paths);
  67. return $paths;
  68. }
  69. }
  70. // the extended options with the $dotColors option
  71. class RandomDotsOptions extends QROptions{
  72. /**
  73. * a map of $M_TYPE_LAYER => color
  74. *
  75. * @see \array_rand()
  76. */
  77. protected array $dotColors = [];
  78. }
  79. /*
  80. * Runtime
  81. */
  82. // prepare the options
  83. $options = new RandomDotsOptions;
  84. // our custom dot colors
  85. $options->dotColors = [
  86. 111 => '#e2453c',
  87. 222 => '#e07e39',
  88. 333 => '#e5d667',
  89. 444 => '#51b95b',
  90. 555 => '#1e72b7',
  91. 666 => '#6f5ba7',
  92. ];
  93. // generate the CSS for the several colored layers
  94. $layerColors = '';
  95. foreach($options->dotColors as $layer => $color){
  96. $layerColors .= sprintf("\n\t\t.qr-%s{ fill: %s; }", $layer, $color);
  97. }
  98. $options->svgDefs = '
  99. <style><![CDATA[
  100. .light{ fill: #dedede; }
  101. .dark{ fill: #424242; }
  102. '.$layerColors.'
  103. ]]></style>';
  104. // set the custom output interface
  105. $options->outputType = QROutputInterface::CUSTOM;
  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_name() !== 'cli'){
  129. header('content-type: image/svg+xml');
  130. }
  131. echo $out;
  132. exit;