QRMarkup.php 3.0 KB

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