svgModuleGroupShape.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. <?php
  2. /**
  3. * Module group shape, by Rastian95
  4. *
  5. * This example works very similar to the "melted" modules, but creates a different effect
  6. *
  7. * @see https://github.com/chillerlan/php-qrcode/discussions/233
  8. */
  9. declare(strict_types=1);
  10. use chillerlan\QRCode\{QRCode, QROptions};
  11. use chillerlan\QRCode\Common\EccLevel;
  12. use chillerlan\QRCode\Data\QRMatrix;
  13. use chillerlan\QRCode\Output\QRMarkupSVG;
  14. require_once __DIR__.'/../vendor/autoload.php';
  15. /*
  16. * Class definition
  17. */
  18. /**
  19. * the extended SVG output module
  20. */
  21. class GroupShapeSVGQRCodeOutput extends QRMarkupSVG{
  22. protected function path(string $path, int $M_TYPE):string{
  23. // omit the "fill" and "opacity" attributes on the path element
  24. return sprintf('<path class="%s" d="%s"/>', $this->getCssClass($M_TYPE), $path);
  25. }
  26. protected function collectModules():array{
  27. $paths = [];
  28. $melt = $this->options->melt; // avoid magic getter in long loops
  29. // collect the modules for each type
  30. foreach($this->matrix->getMatrix() as $y => $row){
  31. foreach($row as $x => $M_TYPE){
  32. $M_TYPE_LAYER = $M_TYPE;
  33. if($this->connectPaths && !$this->matrix->checkTypeIn($x, $y, $this->excludeFromConnect)){
  34. // to connect paths we'll redeclare the $M_TYPE_LAYER to data only
  35. $M_TYPE_LAYER = QRMatrix::M_DATA;
  36. if($this->matrix->isDark($M_TYPE)){
  37. $M_TYPE_LAYER = QRMatrix::M_DATA_DARK;
  38. }
  39. }
  40. // if we're going to "melt" the matrix, we'll declare *all* modules as dark,
  41. // so that light modules with dark parts are rendered in the same path
  42. if($melt){
  43. $M_TYPE_LAYER |= QRMatrix::IS_DARK;
  44. }
  45. // collect the modules per $M_TYPE
  46. $module = $this->moduleTransform($x, $y, $M_TYPE, $M_TYPE_LAYER);
  47. if($module !== null){
  48. $paths[$M_TYPE_LAYER][] = $module;
  49. }
  50. }
  51. }
  52. // beautify output
  53. ksort($paths);
  54. return $paths;
  55. }
  56. protected function moduleTransform(int $x, int $y, int $M_TYPE, int $M_TYPE_LAYER):string|null{
  57. $bits = $this->matrix->checkNeighbours($x, $y, null);
  58. $check = fn(int $all, int $any = 0):bool => ($bits & ($all | (~$any & 0xff))) === $all;
  59. $template = ($M_TYPE & QRMatrix::IS_DARK) === QRMatrix::IS_DARK
  60. ? $this->darkModule($check, $this->options->inverseMelt)
  61. : $this->lightModule($check, $this->options->inverseMelt);
  62. if($template === ''){
  63. return null;
  64. }
  65. $r = $this->options->meltRadius;
  66. return sprintf($template, $x, $y, $r, (1 - $r), (1 - 2 * $r));
  67. }
  68. /**
  69. * returns a dark module for the given values
  70. */
  71. protected function darkModule(Closure $check, bool $invert):string {
  72. return match (true) {
  73. !$invert && $check(0b00000000, 0b01010101),
  74. $invert && $check(0b00000000, 0b00000000)
  75. => 'M%1$s %2$s m0.5,0 l0.5,0.5 l-0.5,0.5 l-0.5,-0.5Z',
  76. $invert && $check(0b01000000, 0b00000000)
  77. => 'M%1$s,%2$s m0,1 h%4$s q%3$s,0 %3$s,-%3$s v-%5$s q0,-%3$s -%3$s,-%3$s h-%5$s q-%3$s,0 -%3$s,%3$sZ',
  78. $invert && $check(0b00000001, 0b00000000)
  79. => 'M%1$s,%2$s v%4$s q0,%3$s %3$s,%3$s h%5$s q%3$s,0 %3$s,-%3$s v-%5$s q0,-%3$s -%3$s,-%3$sZ',
  80. $invert && $check(0b00000100, 0b00000000)
  81. => 'M%1$s,%2$s m1,0 v%4$s q0,%3$s -%3$s,%3$s h-%5$s q-%3$s,0 -%3$s,-%3$s v-%5$s q0,-%3$s %3$s,-%3$sZ',
  82. $invert && $check(0b00010000, 0b00000000)
  83. => 'M%1$s,%2$s m1,1 h-%4$s q-%3$s,0 -%3$s,-%3$s v-%5$s q0,-%3$s %3$s,-%3$s h%5$s q%3$s,0 %3$s,%3$sZ',
  84. !$invert && $check(0b00100000, 0b01010101),
  85. $invert && $check(0b00000000, 0b01110000)
  86. => 'M%1$s,%2$s m0,1 h1 l-0.5,-1Z',
  87. !$invert && $check(0b10000000, 0b01010101),
  88. $invert && $check(0b00000000, 0b11000001)
  89. => 'M%1$s,%2$s v1 l1,-0.5Z',
  90. !$invert && $check(0b00000010, 0b01010101),
  91. $invert && $check(0b00000000, 0b00000111)
  92. => 'M%1$s,%2$s h1 l-0.5,1Z',
  93. !$invert && $check(0b00001000, 0b01010101),
  94. $invert && $check(0b00000000, 0b00011100)
  95. => 'M%1$s,%2$s m1,1 v-1 l-1,0.5Z',
  96. $invert && $check(0b01000100, 0b00000000)
  97. => 'M%1$s,%2$s m0,1 h%4$s q%3$s,0 %3$s,-%3$s v-%4$s h-%4$s q-%3$s,0 -%3$s,%3$sZ',
  98. $invert && $check(0b00010001, 0b00000000)
  99. => 'M%1$s,%2$s h%4$s q%3$s,0 %3$s,%3$s v%4$s h-%4$s q-%3$s,0 -%3$s,-%3$sZ',
  100. !$invert && $check(0b00101000, 0b01010101),
  101. $invert && $check(0b00000000, 0b01111100)
  102. => 'M%1$s,%2$s m0,1 h1 v-1 h-%4$s q-%3$s,0 -%3$s,%3$sZ',
  103. !$invert && $check(0b10100000, 0b01010101),
  104. $invert && $check(0b00000000, 0b11110001)
  105. => 'M%1$s,%2$s h%4$s q%3$s,0 %3$s,%3$s v%4$s h-1Z',
  106. !$invert && $check(0b10000010, 0b01010101),
  107. $invert && $check(0b00000000, 0b11000111)
  108. => 'M%1$s,%2$s h1 v%4$s q0,%3$s -%3$s,%3$s h-%4$sZ',
  109. !$invert && $check(0b00001010, 0b01010101),
  110. $invert && $check(0b00000000, 0b00011111)
  111. => 'M%1$s,%2$s v%4$s q0,%3$s %3$s,%3$s h%4$s v-1Z',
  112. default
  113. => 'M%1$s,%2$s h1 v1 h-1Z',
  114. };
  115. }
  116. /**
  117. * returns a light module for the given values
  118. */
  119. protected function lightModule(Closure $check, bool $invert):string {
  120. return match (true) {
  121. !$invert && $check(0b11111111, 0b01010101), $invert && $check(0b10101010, 0b01010101)
  122. => 'M%1$s,%2$s h%3$s q-%3$s,0 -%3$s,%3$sz m1,0 v%3$s q0,-%3$s -%3$s,-%3$sz m0,1 h-%3$s q%3$s,0 %3$s,-%3$sz m-1,0 v-%3$s q0,%3$s %3$s,%3$sZ',
  123. !$invert && $check(0b10111111, 0b00000000)
  124. => 'M%1$s,%2$s h%3$sz m1,0 v%3$s q0,-%3$s -%3$s,-%3$sz m0,1 h-%3$s Z',
  125. !$invert && $check(0b11111110, 0b00000000)
  126. => 'M%1$s,%2$s m1,0 v%3$s z m0,1 h-%3$s q%3$s,0 %3$s,-%3$sz m-1,0 v-%3$s Z',
  127. !$invert && $check(0b11111011, 0b00000000)
  128. => 'M%1$s,%2$s h%3$s z m0,1 v-%3$s q0,%3$s %3$s,%3$sz m1,0 h-%3$s Z',
  129. !$invert && $check(0b11101111, 0b00000000)
  130. => 'M%1$s,%2$s h%3$s q-%3$s,0 -%3$s,%3$sz m0,1 v-%3$s z m1,-1 v%3$s Z',
  131. !$invert && $check(0b10001111, 0b01110000),
  132. $invert && $check(0b10001010, 0b01010101),
  133. !$invert && $check(0b00111110, 0b11000001),
  134. $invert && $check(0b00101010, 0b01010101),
  135. !$invert && $check(0b11111000, 0b00000111),
  136. $invert && $check(0b10101000, 0b01010101),
  137. !$invert && $check(0b11100011, 0b00011100),
  138. $invert && $check(0b10100010, 0b01010101),
  139. !$invert && $check(0b10111011, 0b00000000),
  140. !$invert && $check(0b11101110, 0b00000000),
  141. !$invert && $check(0b10000011, 0b01111100),
  142. $invert && $check(0b10000010, 0b01010101),
  143. !$invert && $check(0b00001110, 0b11110001),
  144. $invert && $check(0b00001010, 0b01010101),
  145. !$invert && $check(0b00111000, 0b11000111),
  146. $invert && $check(0b00101000, 0b01010101),
  147. !$invert && $check(0b11100000, 0b00011111),
  148. $invert && $check(0b10100000, 0b01010101)
  149. => 'M%1$s,%2$s ',
  150. default => '',
  151. };
  152. }
  153. }
  154. /**
  155. * the augmented options class
  156. *
  157. * @property bool $melt
  158. * @property bool $inverseMelt
  159. * @property float $meltRadius
  160. */
  161. class GroupShapeOutputOptions extends QROptions{
  162. /**
  163. * enable "melt" effect
  164. */
  165. protected bool $melt = false;
  166. /**
  167. * whether to let the melt effect flow along the dark or light modules
  168. */
  169. protected bool $inverseMelt = false;
  170. /**
  171. * the corner radius for melted modules
  172. */
  173. protected float $meltRadius = 0.15;
  174. /**
  175. * clamp/set melt corner radius
  176. */
  177. protected function set_meltRadius(float $meltRadius):void{
  178. $this->meltRadius = max(0.01, min(0.5, $meltRadius));
  179. }
  180. }
  181. /*
  182. * Runtime
  183. */
  184. $options = new GroupShapeOutputOptions;
  185. // settings from the custom options class
  186. $options->melt = true;
  187. $options->inverseMelt = true;
  188. $options->meltRadius = 0.5;
  189. $options->version = 5;
  190. $options->outputInterface = GroupShapeSVGQRCodeOutput::class;
  191. $options->outputBase64 = false;
  192. $options->addQuietzone = true;
  193. $options->eccLevel = EccLevel::H;
  194. $options->addLogoSpace = true;
  195. $options->logoSpaceWidth = 13;
  196. $options->logoSpaceHeight = 13;
  197. $options->connectPaths = true;
  198. $options->excludeFromConnect = [
  199. QRMatrix::M_FINDER_DARK,
  200. QRMatrix::M_FINDER_DOT,
  201. ];
  202. $options->svgDefs = '
  203. <linearGradient id="rainbow" x1="100%" y2="100%">
  204. <stop stop-color="#e2453c" offset="2.5%"/>
  205. <stop stop-color="#e07e39" offset="21.5%"/>
  206. <stop stop-color="#e5d667" offset="40.5%"/>
  207. <stop stop-color="#51b95b" offset="59.5%"/>
  208. <stop stop-color="#1e72b7" offset="78.5%"/>
  209. <stop stop-color="#6f5ba7" offset="97.5%"/>
  210. </linearGradient>
  211. <style><![CDATA[
  212. .light, .dark{fill: url(#rainbow);}
  213. ]]></style>';
  214. $out = (new QRCode($options))->render('https://www.youtube.com/watch?v=dQw4w9WgXcQ');
  215. if(PHP_SAPI !== 'cli'){
  216. header('Content-type: image/svg+xml');
  217. if(extension_loaded('zlib')){
  218. header('Vary: Accept-Encoding');
  219. header('Content-Encoding: gzip');
  220. $out = gzencode($out, 9);
  221. }
  222. }
  223. echo $out;
  224. exit;