svgMeltedModules.php 10 KB

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