* @copyright 2016 Smiley * @license MIT */ namespace chillerlan\QRCode\Output; use chillerlan\QRCode\QRCode; /** * */ class QRMarkup extends QROutputAbstract{ /** * @var string */ protected $optionsInterface = QRMarkupOptions::class; /** * @var array */ protected $types = [ QRCode::OUTPUT_MARKUP_HTML, QRCode::OUTPUT_MARKUP_SVG, ]; /** * @return string */ public function dump() { switch($this->options->type){ case QRCode::OUTPUT_MARKUP_SVG : return $this->toSVG(); case QRCode::OUTPUT_MARKUP_HTML: default: return $this->toHTML(); } } /** * @return string|bool */ protected function toHTML(){ $html = ''; foreach($this->matrix as $row){ // in order to not bloat the output too much, we use the shortest possible valid HTML tags $html .= '<'.$this->options->htmlRowTag.'>'; foreach($row as $col){ $tag = $col ? 'b' // dark : 'i'; // light $html .= '<'.$tag.'>'; } if(!(bool)$this->options->htmlOmitEndTag){ $html .= 'options->htmlRowTag.'>'; } $html .= $this->options->eol; } if($this->options->cachefile){ $html = ''.$this->options->eol.$html.''; return $this->saveToFile($html); } return $html; } /** * @link https://github.com/codemasher/php-qrcode/pull/5 * * @return string|bool */ protected function toSVG(){ $length = $this->pixelCount * $this->options->pixelSize + $this->options->marginSize * 2; $class = !empty($this->options->cssClass) ? $this->options->cssClass : hash('crc32', microtime(true)); // svg header $svg = ''.$this->options->eol. ''.$this->options->eol; // svg body foreach($this->matrix as $r => $row){ //we'll combine active blocks within a single row as a lightweight compression technique $from = -1; $count = 0; foreach($row as $c => $pixel){ if($pixel){ $count++; if($from < 0){ $from = $c; } } elseif($from >= 0){ $svg .= ''.$this->options->eol; // reset count $from = -1; $count = 0; } } // close off the row, if applicable if($from >= 0){ $svg .= ''.$this->options->eol; } } // close svg $svg .= ''.$this->options->eol; // if saving to file, append the correct headers if($this->options->cachefile){ $svg = ''.$this->options->eol.$svg; return $this->saveToFile($svg); } return $svg; } }