QRMarkup.php 3.5 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. *
  16. */
  17. class QRMarkup extends QROutputAbstract{
  18. /**
  19. * @var string
  20. */
  21. protected $optionsInterface = QRMarkupOptions::class;
  22. /**
  23. * @var array
  24. */
  25. protected $types = [
  26. QRCode::OUTPUT_MARKUP_HTML,
  27. QRCode::OUTPUT_MARKUP_SVG,
  28. ];
  29. /**
  30. * @return string
  31. */
  32. public function dump() {
  33. switch($this->options->type){
  34. case QRCode::OUTPUT_MARKUP_SVG : return $this->toSVG();
  35. case QRCode::OUTPUT_MARKUP_HTML:
  36. default:
  37. return $this->toHTML();
  38. }
  39. }
  40. /**
  41. * @return string|bool
  42. */
  43. protected function toHTML(){
  44. $html = '';
  45. foreach($this->matrix as $row){
  46. // in order to not bloat the output too much, we use the shortest possible valid HTML tags
  47. $html .= '<'.$this->options->htmlRowTag.'>';
  48. foreach($row as $col){
  49. $tag = $col
  50. ? 'b' // dark
  51. : 'i'; // light
  52. $html .= '<'.$tag.'></'.$tag.'>';
  53. }
  54. if(!(bool)$this->options->htmlOmitEndTag){
  55. $html .= '</'.$this->options->htmlRowTag.'>';
  56. }
  57. $html .= $this->options->eol;
  58. }
  59. if($this->options->cachefile){
  60. $html = '<!DOCTYPE html><head><meta charset="UTF-8"></head><body>'.$this->options->eol.$html.'</body>';
  61. return $this->saveToFile($html);
  62. }
  63. return $html;
  64. }
  65. /**
  66. * @link https://github.com/codemasher/php-qrcode/pull/5
  67. *
  68. * @return string|bool
  69. */
  70. protected function toSVG(){
  71. $length = $this->pixelCount * $this->options->pixelSize + $this->options->marginSize * 2;
  72. $class = !empty($this->options->cssClass) ? $this->options->cssClass : hash('crc32', microtime(true));
  73. // svg header
  74. $svg = '<svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="'.$length.'" height="'.$length.'" viewBox="0 0 '.$length.' '.$length.'" style="background-color:'.$this->options->bgColor.'">'.$this->options->eol.
  75. '<defs><style>.'.$class.'{fill:'.$this->options->fgColor.'} rect{shape-rendering:crispEdges}</style></defs>'.$this->options->eol;
  76. // svg body
  77. foreach($this->matrix as $r => $row){
  78. //we'll combine active blocks within a single row as a lightweight compression technique
  79. $from = -1;
  80. $count = 0;
  81. foreach($row as $c => $pixel){
  82. if($pixel){
  83. $count++;
  84. if($from < 0){
  85. $from = $c;
  86. }
  87. }
  88. elseif($from >= 0){
  89. $svg .= '<rect x="'.($from * $this->options->pixelSize + $this->options->marginSize).'" y="'.($r * $this->options->pixelSize + $this->options->marginSize)
  90. .'" width="'.($this->options->pixelSize * $count).'" height="'.$this->options->pixelSize.'" class="'.$class.'" />'.$this->options->eol;
  91. // reset count
  92. $from = -1;
  93. $count = 0;
  94. }
  95. }
  96. // close off the row, if applicable
  97. if($from >= 0){
  98. $svg .= '<rect x="'.($from * $this->options->pixelSize + $this->options->marginSize).'" y="'.($r * $this->options->pixelSize + $this->options->marginSize)
  99. .'" width="'.($this->options->pixelSize * $count).'" height="'.$this->options->pixelSize.'" class="'.$class.'" />'.$this->options->eol;
  100. }
  101. }
  102. // close svg
  103. $svg .= '</svg>'.$this->options->eol;
  104. // if saving to file, append the correct headers
  105. if($this->options->cachefile){
  106. $svg = '<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">'.$this->options->eol.$svg;
  107. return $this->saveToFile($svg);
  108. }
  109. return $svg;
  110. }
  111. }