QRMarkup.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. /**
  3. * Class QRMarkup
  4. *
  5. * @filesource QRMarkup.php
  6. * @created 17.12.2016
  7. * @package chillerlan\QRCode\Output
  8. * @author Smiley <smiley@chillerlan.net>
  9. * @copyright 2016 Smiley
  10. * @license MIT
  11. */
  12. namespace chillerlan\QRCode\Output;
  13. use chillerlan\QRCode\QRCode;
  14. use function is_string, sprintf, strip_tags, trim;
  15. /**
  16. * Converts the matrix into markup types: HTML, SVG, ...
  17. */
  18. class QRMarkup extends QROutputAbstract{
  19. protected string $defaultMode = QRCode::OUTPUT_MARKUP_SVG;
  20. /**
  21. * @see \sprintf()
  22. */
  23. protected string $svgHeader = '<svg xmlns="http://www.w3.org/2000/svg" class="qr-svg %1$s" '.
  24. 'style="width: 100%%; height: auto;" viewBox="0 0 %2$d %2$d">';
  25. /**
  26. * @inheritDoc
  27. */
  28. protected function setModuleValues():void{
  29. foreach($this::DEFAULT_MODULE_VALUES as $M_TYPE => $defaultValue){
  30. $v = $this->options->moduleValues[$M_TYPE] ?? null;
  31. if(!is_string($v)){
  32. $this->moduleValues[$M_TYPE] = $defaultValue
  33. ? $this->options->markupDark
  34. : $this->options->markupLight;
  35. }
  36. else{
  37. $this->moduleValues[$M_TYPE] = trim(strip_tags($v), '\'"');
  38. }
  39. }
  40. }
  41. /**
  42. * HTML output
  43. */
  44. protected function html():string{
  45. $html = empty($this->options->cssClass)
  46. ? '<div>'
  47. : '<div class="'.$this->options->cssClass.'">';
  48. $html .= $this->options->eol;
  49. foreach($this->matrix->matrix() as $row){
  50. $html .= '<div>';
  51. foreach($row as $M_TYPE){
  52. $html .= '<span style="background: '.$this->moduleValues[$M_TYPE].';"></span>';
  53. }
  54. $html .= '</div>'.$this->options->eol;
  55. }
  56. $html .= '</div>'.$this->options->eol;
  57. if($this->options->cachefile){
  58. return '<!DOCTYPE html>'.
  59. '<head><meta charset="UTF-8"><title>QR Code</title></head>'.
  60. '<body>'.$this->options->eol.$html.'</body>';
  61. }
  62. return $html;
  63. }
  64. /**
  65. * SVG output
  66. *
  67. * @see https://github.com/codemasher/php-qrcode/pull/5
  68. */
  69. protected function svg():string{
  70. $matrix = $this->matrix->matrix();
  71. $svg = sprintf($this->svgHeader, $this->options->cssClass, $this->options->svgViewBoxSize ?? $this->moduleCount)
  72. .$this->options->eol
  73. .'<defs>'.$this->options->svgDefs.'</defs>'
  74. .$this->options->eol;
  75. foreach($this->moduleValues as $M_TYPE => $value){
  76. $path = '';
  77. foreach($matrix as $y => $row){
  78. //we'll combine active blocks within a single row as a lightweight compression technique
  79. $start = null;
  80. $count = 0;
  81. foreach($row as $x => $module){
  82. if($module === $M_TYPE){
  83. $count++;
  84. if($start === null){
  85. $start = $x;
  86. }
  87. if(isset($row[$x + 1])){
  88. continue;
  89. }
  90. }
  91. if($count > 0){
  92. $len = $count;
  93. $start ??= 0; // avoid type coercion in sprintf() - phan happy
  94. $path .= sprintf('M%s %s h%s v1 h-%sZ ', $start, $y, $len, $len);
  95. // reset count
  96. $count = 0;
  97. $start = null;
  98. }
  99. }
  100. }
  101. if(!empty($path)){
  102. $svg .= sprintf(
  103. '<path class="qr-%s %s" stroke="transparent" fill="%s" fill-opacity="%s" d="%s" />',
  104. $M_TYPE, $this->options->cssClass, $value, $this->options->svgOpacity, $path
  105. );
  106. }
  107. }
  108. // close svg
  109. $svg .= '</svg>'.$this->options->eol;
  110. // if saving to file, append the correct headers
  111. if($this->options->cachefile){
  112. return '<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">'.
  113. $this->options->eol.$svg;
  114. }
  115. return $svg;
  116. }
  117. }