QRMarkup.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 $file = null):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($file !== null){
  58. /** @noinspection HtmlRequiredLangAttribute */
  59. return sprintf(
  60. '<!DOCTYPE html><html><head><meta charset="UTF-8"><title>QR Code</title></head><body>%s</body></html>',
  61. $this->options->eol.$html
  62. );
  63. }
  64. return $html;
  65. }
  66. /**
  67. * SVG output
  68. *
  69. * @see https://github.com/codemasher/php-qrcode/pull/5
  70. */
  71. protected function svg(string $file = null):string{
  72. $matrix = $this->matrix->matrix();
  73. $svg = sprintf($this->svgHeader, $this->options->cssClass, $this->options->svgViewBoxSize ?? $this->moduleCount)
  74. .$this->options->eol
  75. .'<defs>'.$this->options->svgDefs.'</defs>'
  76. .$this->options->eol;
  77. foreach($this->moduleValues as $M_TYPE => $value){
  78. $path = '';
  79. foreach($matrix as $y => $row){
  80. //we'll combine active blocks within a single row as a lightweight compression technique
  81. $start = null;
  82. $count = 0;
  83. foreach($row as $x => $module){
  84. if($module === $M_TYPE){
  85. $count++;
  86. if($start === null){
  87. $start = $x;
  88. }
  89. if(isset($row[$x + 1])){
  90. continue;
  91. }
  92. }
  93. if($count > 0){
  94. $len = $count;
  95. $start ??= 0; // avoid type coercion in sprintf() - phan happy
  96. $path .= sprintf('M%s %s h%s v1 h-%sZ ', $start, $y, $len, $len);
  97. // reset count
  98. $count = 0;
  99. $start = null;
  100. }
  101. }
  102. }
  103. if(!empty($path)){
  104. $svg .= sprintf(
  105. '<path class="qr-%s %s" stroke="transparent" fill="%s" fill-opacity="%s" d="%s" />',
  106. $M_TYPE, $this->options->cssClass, $value, $this->options->svgOpacity, $path
  107. );
  108. }
  109. }
  110. // close svg
  111. $svg .= '</svg>'.$this->options->eol;
  112. // if saving to file, append the correct headers
  113. if($file !== null){
  114. return '<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">'.
  115. $this->options->eol.$svg;
  116. }
  117. if($this->options->imageBase64){
  118. $svg = sprintf('data:image/svg+xml;base64,%s', base64_encode($svg));
  119. }
  120. return $svg;
  121. }
  122. }