svgMeltedModules.php 10 KB

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