QRMarkup.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. /**
  20. * @var string
  21. */
  22. protected $defaultMode = QRCode::OUTPUT_MARKUP_SVG;
  23. /**
  24. * @see \sprintf()
  25. *
  26. * @var string
  27. */
  28. protected $svgHeader = '<svg xmlns="http://www.w3.org/2000/svg" version="1.1" class="qr-svg %1$s" style="width: 100%%; height: auto;" viewBox="0 0 %2$d %2$d">';
  29. /**
  30. * @return void
  31. */
  32. protected function setModuleValues():void{
  33. foreach($this::DEFAULT_MODULE_VALUES as $M_TYPE => $defaultValue){
  34. $v = $this->options->moduleValues[$M_TYPE] ?? null;
  35. if(!is_string($v)){
  36. $this->moduleValues[$M_TYPE] = $defaultValue
  37. ? $this->options->markupDark
  38. : $this->options->markupLight;
  39. }
  40. else{
  41. $this->moduleValues[$M_TYPE] = trim(strip_tags($v), '\'"');
  42. }
  43. }
  44. }
  45. /**
  46. * @return string
  47. */
  48. protected function html():string{
  49. $html = '<div class="'.$this->options->cssClass.'">'.$this->options->eol;
  50. foreach($this->matrix->matrix() as $row){
  51. $html .= '<div>';
  52. foreach($row as $M_TYPE){
  53. $html .= '<span style="background: '.$this->moduleValues[$M_TYPE].';"></span>';
  54. }
  55. $html .= '</div>'.$this->options->eol;
  56. }
  57. $html .= '</div>'.$this->options->eol;
  58. if($this->options->cachefile){
  59. return '<!DOCTYPE html><head><meta charset="UTF-8"></head><body>'.$this->options->eol.$html.'</body>';
  60. }
  61. return $html;
  62. }
  63. /**
  64. * @link https://github.com/codemasher/php-qrcode/pull/5
  65. *
  66. * @return string
  67. */
  68. protected function svg():string{
  69. $matrix = $this->matrix->matrix();
  70. $svg = sprintf($this->svgHeader, $this->options->cssClass, $this->options->svgViewBoxSize ?? $this->moduleCount)
  71. .$this->options->eol
  72. .'<defs>'.$this->options->svgDefs.'</defs>'
  73. .$this->options->eol;
  74. foreach($this->moduleValues as $M_TYPE => $value){
  75. $path = '';
  76. foreach($matrix as $y => $row){
  77. //we'll combine active blocks within a single row as a lightweight compression technique
  78. $start = null;
  79. $count = 0;
  80. foreach($row as $x => $module){
  81. if($module === $M_TYPE){
  82. $count++;
  83. if($start === null){
  84. $start = $x;
  85. }
  86. if(isset($row[$x + 1])){
  87. continue;
  88. }
  89. }
  90. if($count > 0){
  91. $len = $count;
  92. $path .= sprintf('M%s %s h%s v1 h-%sZ ', $start, $y, $len, $len);
  93. // reset count
  94. $count = 0;
  95. $start = null;
  96. }
  97. }
  98. }
  99. if(!empty($path)){
  100. $svg .= sprintf('<path class="qr-%s %s" stroke="transparent" fill="%s" fill-opacity="%s" d="%s" />', $M_TYPE, $this->options->cssClass, $value, $this->options->svgOpacity, $path);
  101. }
  102. }
  103. // close svg
  104. $svg .= '</svg>'.$this->options->eol;
  105. // if saving to file, append the correct headers
  106. if($this->options->cachefile){
  107. return '<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">'.$this->options->eol.$svg;
  108. }
  109. return $svg;
  110. }
  111. }