svgMeltedModules.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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. use chillerlan\QRCode\{QRCode, QROptions};
  13. use chillerlan\QRCode\Common\EccLevel;
  14. use chillerlan\QRCode\Data\QRMatrix;
  15. use chillerlan\QRCode\Output\{QROutputInterface, QRMarkupSVG};
  16. require_once __DIR__.'/../vendor/autoload.php';
  17. /*
  18. * Class definition
  19. */
  20. /**
  21. * the extended SVG output module
  22. */
  23. class MeltedSVGQRCodeOutput extends QRMarkupSVG{
  24. /**
  25. * @inheritDoc
  26. */
  27. protected function path(string $path, int $M_TYPE):string{
  28. // omit the "fill" and "opacity" attributes on the path element
  29. return sprintf('<path class="%s" d="%s"/>', $this->getCssClass($M_TYPE), $path);
  30. }
  31. /**
  32. * @inheritDoc
  33. */
  34. protected function collectModules(Closure $transform):array{
  35. $paths = [];
  36. // collect the modules for each type
  37. for($y = 0; $y < $this->moduleCount; $y++){
  38. for($x = 0; $x < $this->moduleCount; $x++){
  39. $M_TYPE = $this->matrix->get($x, $y);
  40. $M_TYPE_LAYER = $M_TYPE;
  41. if($this->options->connectPaths && !$this->matrix->checkTypeIn($x, $y, $this->options->excludeFromConnect)){
  42. // to connect paths we'll redeclare the $M_TYPE_LAYER to data only
  43. $M_TYPE_LAYER = QRMatrix::M_DATA;
  44. if($this->matrix->check($x, $y)){
  45. $M_TYPE_LAYER = QRMatrix::M_DATA_DARK;
  46. }
  47. }
  48. // if we're going to "melt" the matrix, we'll declare *all* modules as dark,
  49. // so that light modules with dark parts are rendered in the same path
  50. if($this->options->melt){
  51. $M_TYPE_LAYER |= QRMatrix::IS_DARK;
  52. }
  53. // collect the modules per $M_TYPE
  54. $module = $transform($x, $y, $M_TYPE, $M_TYPE_LAYER);
  55. if(!empty($module)){
  56. $paths[$M_TYPE_LAYER][] = $module;
  57. }
  58. }
  59. }
  60. // beautify output
  61. ksort($paths);
  62. return $paths;
  63. }
  64. /**
  65. * @inheritDoc
  66. */
  67. protected function module(int $x, int $y, int $M_TYPE):string{
  68. $bits = $this->matrix->checkNeighbours($x, $y, null);
  69. $check = fn(int $all, int $any = 0):bool => ($bits & ($all | (~$any & 0xff))) === $all;
  70. $template = ($M_TYPE & QRMatrix::IS_DARK) === QRMatrix::IS_DARK
  71. ? $this->darkModule($check, $this->options->inverseMelt)
  72. : $this->lightModule($check, $this->options->inverseMelt);
  73. $r = $this->options->meltRadius;
  74. return sprintf($template, $x, $y, $r, (1 - $r), (1 - 2 * $r));
  75. }
  76. /**
  77. * returns a dark module for the given values
  78. */
  79. protected function darkModule(Closure $check, bool $invert):string{
  80. switch(true){
  81. // 4 rounded
  82. case !$invert && $check(0b00000000, 0b01010101):
  83. case $invert && $check(0b00000000, 0b00000000):
  84. 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';
  85. // 3 rounded
  86. case $invert && $check(0b01000000, 0b00000000): // 135
  87. 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';
  88. case $invert && $check(0b00000001, 0b00000000): // 357
  89. 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';
  90. case $invert && $check(0b00000100, 0b00000000): // 571
  91. 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';
  92. case $invert && $check(0b00010000, 0b00000000): // 713
  93. 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';
  94. // 2 rounded
  95. case !$invert && $check(0b00100000, 0b01010101): // 13
  96. case $invert && $check(0b00000000, 0b01110000):
  97. 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';
  98. case !$invert && $check(0b10000000, 0b01010101): // 35
  99. case $invert && $check(0b00000000, 0b11000001):
  100. 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';
  101. case !$invert && $check(0b00000010, 0b01010101): // 57
  102. case $invert && $check(0b00000000, 0b00000111):
  103. 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';
  104. case !$invert && $check(0b00001000, 0b01010101): // 71
  105. case $invert && $check(0b00000000, 0b00011100):
  106. 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';
  107. // diagonal
  108. case $invert && $check(0b01000100, 0b00000000): // 15
  109. 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';
  110. case $invert && $check(0b00010001, 0b00000000): // 37
  111. 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';
  112. // 1 rounded
  113. case !$invert && $check(0b00101000, 0b01010101): // 1
  114. case $invert && $check(0b00000000, 0b01111100):
  115. return 'M%1$s,%2$s m0,1 h1 v-1 h-%4$s q-%3$s,0 -%3$s,%3$sZ';
  116. case !$invert && $check(0b10100000, 0b01010101): // 3
  117. case $invert && $check(0b00000000, 0b11110001):
  118. return 'M%1$s,%2$s h%4$s q%3$s,0 %3$s,%3$s v%4$s h-1Z';
  119. case !$invert && $check(0b10000010, 0b01010101): // 5
  120. case $invert && $check(0b00000000, 0b11000111):
  121. return 'M%1$s,%2$s h1 v%4$s q0,%3$s -%3$s,%3$s h-%4$sZ';
  122. case !$invert && $check(0b00001010, 0b01010101): // 7
  123. case $invert && $check(0b00000000, 0b00011111):
  124. return 'M%1$s,%2$s v%4$s q0,%3$s %3$s,%3$s h%4$s v-1Z';
  125. }
  126. // full square
  127. return 'M%1$s,%2$s h1 v1 h-1Z';
  128. }
  129. /**
  130. * returns a light module for the given values
  131. */
  132. protected function lightModule(Closure $check, bool $invert):string{
  133. switch(true){
  134. // 4 rounded
  135. case !$invert && $check(0b11111111, 0b01010101):
  136. case $invert && $check(0b10101010, 0b01010101):
  137. 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';
  138. // 3 rounded
  139. case !$invert && $check(0b10111111, 0b00000000): // 135
  140. 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';
  141. case !$invert && $check(0b11111110, 0b00000000): // 357
  142. 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';
  143. case !$invert && $check(0b11111011, 0b00000000): // 571
  144. 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';
  145. case !$invert && $check(0b11101111, 0b00000000): // 713
  146. 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';
  147. // 2 rounded
  148. case !$invert && $check(0b10001111, 0b01110000): // 13
  149. case $invert && $check(0b10001010, 0b01010101):
  150. 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';
  151. case !$invert && $check(0b00111110, 0b11000001): // 35
  152. case $invert && $check(0b00101010, 0b01010101):
  153. 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';
  154. case !$invert && $check(0b11111000, 0b00000111): // 57
  155. case $invert && $check(0b10101000, 0b01010101):
  156. 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';
  157. case !$invert && $check(0b11100011, 0b00011100): // 71
  158. case $invert && $check(0b10100010, 0b01010101):
  159. 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';
  160. // diagonal
  161. case !$invert && $check(0b10111011, 0b00000000): // 15
  162. 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';
  163. case !$invert && $check(0b11101110, 0b00000000): // 37
  164. 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';
  165. // 1 rounded
  166. case !$invert && $check(0b10000011, 0b01111100): // 1
  167. case $invert && $check(0b10000010, 0b01010101):
  168. return 'M%1$s,%2$s h%3$s q-%3$s,0 -%3$s,%3$sZ';
  169. case !$invert && $check(0b00001110, 0b11110001): // 3
  170. case $invert && $check(0b00001010, 0b01010101):
  171. return 'M%1$s,%2$s m1,0 v%3$s q0,-%3$s -%3$s,-%3$sZ';
  172. case !$invert && $check(0b00111000, 0b11000111): // 5
  173. case $invert && $check(0b00101000, 0b01010101):
  174. return 'M%1$s,%2$s m1,1 h-%3$s q%3$s,0 %3$s,-%3$sZ';
  175. case !$invert && $check(0b11100000, 0b00011111): // 7
  176. case $invert && $check(0b10100000, 0b01010101):
  177. return 'M%1$s,%2$s m0,1 v-%3$s q0,%3$s %3$s,%3$sZ';
  178. }
  179. // empty block
  180. return '';
  181. }
  182. }
  183. /**
  184. * the augmented options class
  185. */
  186. class MeltedOutputOptions extends QROptions{
  187. /**
  188. * enable "melt" effect
  189. */
  190. protected bool $melt = false;
  191. /**
  192. * whether to let the melt effect flow along the dark or light modules
  193. */
  194. protected bool $inverseMelt = false;
  195. /**
  196. * the corner radius for melted modules
  197. */
  198. protected float $meltRadius = 0.15;
  199. /**
  200. * clamp/set melt corner radius
  201. */
  202. protected function set_meltRadius(float $meltRadius):void{
  203. $this->meltRadius = max(0.01, min(0.5, $meltRadius));
  204. }
  205. }
  206. /*
  207. * Runtime
  208. */
  209. $options = new MeltedOutputOptions;
  210. // settings from the custom options class
  211. $options->melt = true;
  212. $options->inverseMelt = true;
  213. $options->meltRadius = 0.4;
  214. $options->version = 7;
  215. $options->outputType = QROutputInterface::CUSTOM;
  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_name() !== '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;