svgModuleJitter.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. /**
  3. * A jitter effect for square modules (Mosaic)
  4. *
  5. * @created 17.09.2024
  6. * @author smiley <smiley@chillerlan.net>
  7. * @copyright 2024 smiley
  8. * @license MIT
  9. */
  10. declare(strict_types=1);
  11. use chillerlan\QRCode\{QRCode, QROptions};
  12. use chillerlan\QRCode\Data\QRMatrix;
  13. use chillerlan\QRCode\Output\QRMarkupSVG;
  14. use chillerlan\Settings\SettingsContainerInterface;
  15. require_once __DIR__.'/../vendor/autoload.php';
  16. /*
  17. * Class definition
  18. */
  19. /**
  20. * the extended SVG output module
  21. */
  22. class ModuleJitterSVGoutput extends QRMarkupSVG{
  23. protected const ROUND_PRECISION = 5;
  24. protected readonly float $sideLength;
  25. public function __construct(QROptions|SettingsContainerInterface $options, QRMatrix $matrix){
  26. parent::__construct($options, $matrix);
  27. // copy the value to a local property to avoid excessive magic getter calls
  28. $this->sideLength = $this->options->sideLength;
  29. }
  30. // emulates JS Math.random()
  31. protected function random():float{
  32. return (random_int(0, PHP_INT_MAX) / PHP_INT_MAX);
  33. }
  34. protected function module(int $x, int $y, int $M_TYPE):string{
  35. // skip light modules
  36. if((!$this->options->drawLightModules && !$this->matrix->check($x, $y))){
  37. return '';
  38. }
  39. // early exit on pure square modules
  40. if($this->matrix->checkTypeIn($x, $y, $this->options->keepAsSquare)){
  41. // phpcs:ignore
  42. return "M$x,$y h1 v1 h-1Z";
  43. }
  44. // calculate the maximum tilt angle of the square with the previously determined side length
  45. $maxAngle = (45 - rad2deg(acos(1 / hypot($this->sideLength, $this->sideLength))));
  46. // set the maximum angle from the options and clamp between valid min/max
  47. $maxAngle = max(0, min($maxAngle, $this->options->maxAngle));
  48. // randomize the tilt angle
  49. $a = ($this->random() * $maxAngle);
  50. // calculate the opposite and adjacent sides of the triangle
  51. $opp = round((cos(deg2rad($a)) * $this->sideLength), self::ROUND_PRECISION);
  52. $adj = round((sin(deg2rad($a)) * $this->sideLength), self::ROUND_PRECISION);
  53. // tilt to the left
  54. if($this->random() > 0.5){
  55. $x = round(($x + 0.5 - $opp / 2 - $adj / 2), self::ROUND_PRECISION);
  56. $y = round(($y + 0.5 - $opp / 2 + $adj / 2), self::ROUND_PRECISION);
  57. // phpcs:ignore
  58. return "M$x,$y l$opp,-$adj l$adj,$opp l-$opp,$adj Z";
  59. }
  60. // tilt right
  61. $x = round(($x + 0.5 - $opp / 2 + $adj / 2), self::ROUND_PRECISION);
  62. $y = round(($y + 0.5 - $opp / 2 - $adj / 2), self::ROUND_PRECISION);
  63. // phpcs:ignore
  64. return "M$x,$y l$opp,$adj l-$adj,$opp l-$opp,-$adj Z";
  65. }
  66. }
  67. /**
  68. * the augmented options class
  69. *
  70. * @property float $sideLength
  71. * @property float $maxAngle
  72. */
  73. class ModuleJitterOptions extends QROptions{
  74. /**
  75. * the side length of the modules (calmped internally between square root of 0.5 (at 45°) and 1 (full length))
  76. */
  77. protected float $sideLength = 0.8;
  78. /**
  79. * The maximum tilt angle (clamped inside the 1x1 module, at a maximum of 45 degrees)
  80. */
  81. protected float $maxAngle = 45.0;
  82. /**
  83. * clamp the side length
  84. */
  85. protected function set_sideLength(float $sideLength):void{
  86. $this->sideLength = max(M_SQRT1_2, min(1.0, $sideLength));
  87. }
  88. }
  89. /*
  90. * Runtime
  91. */
  92. $options = new ModuleJitterOptions;
  93. // settings from the custom options class
  94. $options->sideLength = 0.85;
  95. $options->maxAngle = 45.0;
  96. $options->version = 7;
  97. $options->outputInterface = ModuleJitterSVGoutput::class;
  98. $options->drawLightModules = false;
  99. $options->svgUseFillAttributes = false;
  100. $options->outputBase64 = false;
  101. $options->addQuietzone = true;
  102. $options->connectPaths = true;
  103. $options->keepAsSquare = [
  104. QRMatrix::M_FINDER_DARK,
  105. QRMatrix::M_FINDER_DOT,
  106. QRMatrix::M_ALIGNMENT_DARK,
  107. ];
  108. $options->svgDefs = '
  109. <linearGradient id="rainbow" x1="100%" y2="100%">
  110. <stop stop-color="#e2453c" offset="0"/>
  111. <stop stop-color="#e07e39" offset="0.2"/>
  112. <stop stop-color="#e5d667" offset="0.4"/>
  113. <stop stop-color="#51b95b" offset="0.6"/>
  114. <stop stop-color="#1e72b7" offset="0.8"/>
  115. <stop stop-color="#6f5ba7" offset="1"/>
  116. </linearGradient>
  117. <style><![CDATA[
  118. .dark{fill: url(#rainbow);}
  119. ]]></style>';
  120. $out = (new QRCode($options))->render('https://www.youtube.com/watch?v=dQw4w9WgXcQ');
  121. if(PHP_SAPI !== 'cli'){
  122. header('Content-type: image/svg+xml');
  123. if(extension_loaded('zlib')){
  124. header('Vary: Accept-Encoding');
  125. header('Content-Encoding: gzip');
  126. $out = gzencode($out, 9);
  127. }
  128. }
  129. echo $out;
  130. exit;