svgModuleGroupShape.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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(!empty($module)){
  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{
  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. $r = $this->options->meltRadius;
  63. return sprintf($template, $x, $y, $r, (1 - $r), (1 - 2 * $r));
  64. }
  65. /**
  66. * returns a dark module for the given values
  67. */
  68. protected function darkModule(Closure $check, bool $invert):string {
  69. return match (true) {
  70. !$invert && $check(0b00000000, 0b01010101),
  71. $invert && $check(0b00000000, 0b00000000)
  72. => 'M%1$s %2$s m0.5,0 l0.5,0.5 l-0.5,0.5 l-0.5,-0.5Z',
  73. $invert && $check(0b01000000, 0b00000000)
  74. => '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',
  75. $invert && $check(0b00000001, 0b00000000)
  76. => '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',
  77. $invert && $check(0b00000100, 0b00000000)
  78. => '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',
  79. $invert && $check(0b00010000, 0b00000000)
  80. => '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',
  81. !$invert && $check(0b00100000, 0b01010101),
  82. $invert && $check(0b00000000, 0b01110000)
  83. => 'M%1$s,%2$s m0,1 h1 l-0.5,-1Z',
  84. !$invert && $check(0b10000000, 0b01010101),
  85. $invert && $check(0b00000000, 0b11000001)
  86. => 'M%1$s,%2$s v1 l1,-0.5Z',
  87. !$invert && $check(0b00000010, 0b01010101),
  88. $invert && $check(0b00000000, 0b00000111)
  89. => 'M%1$s,%2$s h1 l-0.5,1Z',
  90. !$invert && $check(0b00001000, 0b01010101),
  91. $invert && $check(0b00000000, 0b00011100)
  92. => 'M%1$s,%2$s m1,1 v-1 l-1,0.5Z',
  93. $invert && $check(0b01000100, 0b00000000)
  94. => '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',
  95. $invert && $check(0b00010001, 0b00000000)
  96. => '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',
  97. !$invert && $check(0b00101000, 0b01010101),
  98. $invert && $check(0b00000000, 0b01111100)
  99. => 'M%1$s,%2$s m0,1 h1 v-1 h-%4$s q-%3$s,0 -%3$s,%3$sZ',
  100. !$invert && $check(0b10100000, 0b01010101),
  101. $invert && $check(0b00000000, 0b11110001)
  102. => 'M%1$s,%2$s h%4$s q%3$s,0 %3$s,%3$s v%4$s h-1Z',
  103. !$invert && $check(0b10000010, 0b01010101),
  104. $invert && $check(0b00000000, 0b11000111)
  105. => 'M%1$s,%2$s h1 v%4$s q0,%3$s -%3$s,%3$s h-%4$sZ',
  106. !$invert && $check(0b00001010, 0b01010101),
  107. $invert && $check(0b00000000, 0b00011111)
  108. => 'M%1$s,%2$s v%4$s q0,%3$s %3$s,%3$s h%4$s v-1Z',
  109. default
  110. => 'M%1$s,%2$s h1 v1 h-1Z',
  111. };
  112. }
  113. /**
  114. * returns a light module for the given values
  115. */
  116. protected function lightModule(Closure $check, bool $invert):string {
  117. return match (true) {
  118. !$invert && $check(0b11111111, 0b01010101), $invert && $check(0b10101010, 0b01010101)
  119. => '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',
  120. !$invert && $check(0b10111111, 0b00000000)
  121. => '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',
  122. !$invert && $check(0b11111110, 0b00000000)
  123. => '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',
  124. !$invert && $check(0b11111011, 0b00000000)
  125. => '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',
  126. !$invert && $check(0b11101111, 0b00000000)
  127. => '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',
  128. !$invert && $check(0b10001111, 0b01110000),
  129. $invert && $check(0b10001010, 0b01010101),
  130. !$invert && $check(0b00111110, 0b11000001),
  131. $invert && $check(0b00101010, 0b01010101),
  132. !$invert && $check(0b11111000, 0b00000111),
  133. $invert && $check(0b10101000, 0b01010101),
  134. !$invert && $check(0b11100011, 0b00011100),
  135. $invert && $check(0b10100010, 0b01010101),
  136. !$invert && $check(0b10111011, 0b00000000),
  137. !$invert && $check(0b11101110, 0b00000000),
  138. !$invert && $check(0b10000011, 0b01111100),
  139. $invert && $check(0b10000010, 0b01010101),
  140. !$invert && $check(0b00001110, 0b11110001),
  141. $invert && $check(0b00001010, 0b01010101),
  142. !$invert && $check(0b00111000, 0b11000111),
  143. $invert && $check(0b00101000, 0b01010101),
  144. !$invert && $check(0b11100000, 0b00011111),
  145. $invert && $check(0b10100000, 0b01010101)
  146. => 'M%1$s,%2$s ',
  147. default => '',
  148. };
  149. }
  150. }
  151. /**
  152. * the augmented options class
  153. *
  154. * @property bool $melt
  155. * @property bool $inverseMelt
  156. * @property float $meltRadius
  157. */
  158. class GroupShapeOutputOptions extends QROptions{
  159. /**
  160. * enable "melt" effect
  161. */
  162. protected bool $melt = false;
  163. /**
  164. * whether to let the melt effect flow along the dark or light modules
  165. */
  166. protected bool $inverseMelt = false;
  167. /**
  168. * the corner radius for melted modules
  169. */
  170. protected float $meltRadius = 0.15;
  171. /**
  172. * clamp/set melt corner radius
  173. */
  174. protected function set_meltRadius(float $meltRadius):void{
  175. $this->meltRadius = max(0.01, min(0.5, $meltRadius));
  176. }
  177. }
  178. /*
  179. * Runtime
  180. */
  181. $options = new GroupShapeOutputOptions;
  182. // settings from the custom options class
  183. $options->melt = true;
  184. $options->inverseMelt = true;
  185. $options->meltRadius = 0.5;
  186. $options->version = 5;
  187. $options->outputInterface = GroupShapeSVGQRCodeOutput::class;
  188. $options->outputBase64 = false;
  189. $options->addQuietzone = true;
  190. $options->eccLevel = EccLevel::H;
  191. $options->addLogoSpace = true;
  192. $options->logoSpaceWidth = 13;
  193. $options->logoSpaceHeight = 13;
  194. $options->connectPaths = true;
  195. $options->excludeFromConnect = [
  196. QRMatrix::M_FINDER_DARK,
  197. QRMatrix::M_FINDER_DOT,
  198. ];
  199. $options->svgDefs = '
  200. <linearGradient id="rainbow" x1="100%" y2="100%">
  201. <stop stop-color="#e2453c" offset="2.5%"/>
  202. <stop stop-color="#e07e39" offset="21.5%"/>
  203. <stop stop-color="#e5d667" offset="40.5%"/>
  204. <stop stop-color="#51b95b" offset="59.5%"/>
  205. <stop stop-color="#1e72b7" offset="78.5%"/>
  206. <stop stop-color="#6f5ba7" offset="97.5%"/>
  207. </linearGradient>
  208. <style><![CDATA[
  209. .light, .dark{fill: url(#rainbow);}
  210. ]]></style>';
  211. $out = (new QRCode($options))->render('https://www.youtube.com/watch?v=dQw4w9WgXcQ');
  212. if(PHP_SAPI !== 'cli'){
  213. header('Content-type: image/svg+xml');
  214. if(extension_loaded('zlib')){
  215. header('Vary: Accept-Encoding');
  216. header('Content-Encoding: gzip');
  217. $out = gzencode($out, 9);
  218. }
  219. }
  220. echo $out;
  221. exit;