svgRoundQuietzone.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. <?php
  2. /**
  3. * @see https://github.com/chillerlan/php-qrcode/discussions/137
  4. *
  5. * @created 09.07.2022
  6. * @author smiley <smiley@chillerlan.net>
  7. * @copyright 2022 smiley
  8. * @license MIT
  9. *
  10. * @noinspection PhpIllegalPsrClassPathInspection
  11. */
  12. use chillerlan\QRCode\Common\EccLevel;
  13. use chillerlan\QRCode\Data\QRMatrix;
  14. use chillerlan\QRCode\Output\QRMarkupSVG;
  15. use chillerlan\QRCode\{QRCode, QROptions};
  16. require_once __DIR__.'/../vendor/autoload.php';
  17. /*
  18. * Class definition
  19. */
  20. /**
  21. * the extended SVG output module
  22. */
  23. class RoundQuietzoneSVGoutput extends QRMarkupSVG{
  24. /**
  25. * @inheritDoc
  26. */
  27. protected function createMarkup(bool $saveToFile):string{
  28. // some Pythagorean magick
  29. $diameter = sqrt(2 * pow($this->moduleCount + $this->options->additionalModules, 2));
  30. // calculate the quiet zone size, add 1 to it as the outer circle stroke may go outside of it
  31. $quietzoneSize = (int)ceil(($diameter - $this->moduleCount) / 2) + 1;
  32. // add the quiet zone to fill the circle
  33. $this->matrix->setQuietZone($quietzoneSize);
  34. // update the matrix dimensions to avoid errors in subsequent calculations
  35. // the moduleCount is now QR Code matrix + 2x quiet zone
  36. $this->setMatrixDimensions();
  37. // color the quiet zone
  38. $this->colorQuietzone($quietzoneSize, $diameter / 2);
  39. // start SVG output
  40. $svg = $this->header();
  41. if(!empty($this->options->svgDefs)){
  42. $svg .= sprintf('<defs>%1$s%2$s</defs>%2$s', $this->options->svgDefs, $this->options->eol);
  43. }
  44. $svg .= $this->paths();
  45. $svg .= $this->addCircle($diameter / 2);
  46. // close svg
  47. $svg .= sprintf('%1$s</svg>%1$s', $this->options->eol);
  48. // transform to data URI only when not saving to file
  49. if(!$saveToFile && $this->options->imageBase64){
  50. $svg = $this->base64encode($svg, 'image/svg+xml');
  51. }
  52. return $svg;
  53. }
  54. /**
  55. * Sets random modules of the quiet zone to dark
  56. */
  57. protected function colorQuietzone(int $quietzoneSize, float $radius):void{
  58. $l1 = $quietzoneSize - 1;
  59. $l2 = $this->moduleCount - $quietzoneSize;
  60. // substract 1/2 stroke width and module radius from the circle radius to not cut off modules
  61. $r = $radius - $this->options->circleRadius * 2;
  62. foreach($this->matrix->matrix() as $y => $row){
  63. foreach($row as $x => $value){
  64. // skip anything that's not quiet zone
  65. if($value !== QRMatrix::M_QUIETZONE){
  66. continue;
  67. }
  68. // leave one row of quiet zone around the matrix
  69. if(
  70. ($x === $l1 && $y >= $l1 && $y <= $l2)
  71. || ($x === $l2 && $y >= $l1 && $y <= $l2)
  72. || ($y === $l1 && $x >= $l1 && $x <= $l2)
  73. || ($y === $l2 && $x >= $l1 && $x <= $l2)
  74. ){
  75. continue;
  76. }
  77. // we need to add 0.5 units to the check values since we're calculating the element centers
  78. // ($x/$y is the element's assumed top left corner)
  79. if($this->checkIfInsideCircle($x + 0.5, $y + 0.5, $r)){
  80. $this->matrix->set($x, $y, (bool)rand(0, 1), QRMatrix::M_QUIETZONE);
  81. }
  82. }
  83. }
  84. }
  85. /**
  86. * @see https://stackoverflow.com/a/7227057
  87. */
  88. protected function checkIfInsideCircle(float $x, float $y, float $radius):bool{
  89. $dx = abs($x - $this->moduleCount / 2);
  90. $dy = abs($y - $this->moduleCount / 2);
  91. if($dx + $dy <= $radius){
  92. return true;
  93. }
  94. if($dx > $radius || $dy > $radius){
  95. return false;
  96. }
  97. if(pow($dx, 2) + pow($dy, 2) <= pow($radius, 2)){
  98. return true;
  99. }
  100. return false;
  101. }
  102. /**
  103. * add a solid circle around the matrix
  104. */
  105. protected function addCircle(float $radius):string{
  106. return sprintf(
  107. '%4$s<circle id="circle" cx="%1$s" cy="%1$s" r="%2$s" stroke-width="%3$s"/>',
  108. $this->moduleCount / 2,
  109. round($radius, 5),
  110. $this->options->circleRadius * 2,
  111. $this->options->eol
  112. );
  113. }
  114. }
  115. /**
  116. * the augmented options class
  117. */
  118. class RoundQuietzoneOptions extends QROptions{
  119. /**
  120. * The amount of additional modules to be used in the circle diameter calculation
  121. *
  122. * Note that the middle of the circle stroke goes through the (assumed) outer corners
  123. * or centers of the QR Code (excluding quiet zone)
  124. *
  125. * Example:
  126. *
  127. * - a value of -1 would go through the center of the outer corner modules of the finder patterns
  128. * - a value of 0 would go through the corner of the outer modules of the finder patterns
  129. * - a value of 3 would go through the center of the module outside next to the finder patterns, in a 45 degree angle
  130. */
  131. protected int $additionalModules = 0;
  132. }
  133. /*
  134. * Runtime
  135. */
  136. $options = new RoundQuietzoneOptions([
  137. 'additionalModules' => 5,
  138. 'version' => 7,
  139. 'eccLevel' => EccLevel::H, // maximum error correction capacity, esp. for print
  140. 'addQuietzone' => false, // we're not adding a quiet zone, this is done internally in our own module
  141. 'imageBase64' => false, // avoid base64 URI output
  142. 'outputType' => QRCode::OUTPUT_CUSTOM,
  143. 'outputInterface' => RoundQuietzoneSVGoutput::class, // load our own output class
  144. 'markupDark' => '', // avoid "fill" attributes on paths
  145. 'markupLight' => '',
  146. 'imageTransparent' => true, // set to false to add the light modules
  147. 'connectPaths' => true,
  148. 'excludeFromConnect' => [
  149. QRMatrix::M_FINDER|QRMatrix::IS_DARK,
  150. QRMatrix::M_FINDER_DOT|QRMatrix::IS_DARK,
  151. QRMatrix::M_ALIGNMENT|QRMatrix::IS_DARK,
  152. QRMatrix::M_QUIETZONE|QRMatrix::IS_DARK
  153. ],
  154. 'drawCircularModules' => true,
  155. 'circleRadius' => 0.4,
  156. 'keepAsSquare' => [
  157. QRMatrix::M_FINDER|QRMatrix::IS_DARK,
  158. QRMatrix::M_FINDER_DOT|QRMatrix::IS_DARK,
  159. QRMatrix::M_ALIGNMENT|QRMatrix::IS_DARK,
  160. ],
  161. // https://developer.mozilla.org/en-US/docs/Web/SVG/Element/linearGradient
  162. 'svgDefs' => '
  163. <linearGradient id="blurple" x1="100%" y2="100%">
  164. <stop stop-color="#D70071" offset="0"/>
  165. <stop stop-color="#9C4E97" offset="0.5"/>
  166. <stop stop-color="#0035A9" offset="1"/>
  167. </linearGradient>
  168. <linearGradient id="rainbow" x1="100%" y2="100%">
  169. <stop stop-color="#e2453c" offset="2.5%"/>
  170. <stop stop-color="#e07e39" offset="21.5%"/>
  171. <stop stop-color="#e5d667" offset="40.5%"/>
  172. <stop stop-color="#51b95b" offset="59.5%"/>
  173. <stop stop-color="#1e72b7" offset="78.5%"/>
  174. <stop stop-color="#6f5ba7" offset="97.5%"/>
  175. </linearGradient>
  176. <style><![CDATA[
  177. .dark{ fill: url(#rainbow); }
  178. .light{ fill: #dedede; }
  179. .qr-2304{ fill: url(#blurple); }
  180. #circle{ fill: none; stroke: url(#blurple); }
  181. ]]></style>',
  182. ]);
  183. $qrcode = (new QRCode($options))->render('https://www.youtube.com/watch?v=dQw4w9WgXcQ');
  184. if(php_sapi_name() !== 'cli'){
  185. header('Content-type: image/svg+xml');
  186. if(extension_loaded('zlib')){
  187. header('Vary: Accept-Encoding');
  188. header('Content-Encoding: gzip');
  189. $qrcode = gzencode($qrcode, 9);
  190. }
  191. }
  192. echo $qrcode;
  193. exit;