svgMeltedModules.php 10 KB

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