Просмотр исходного кода

:octocat: minor performance improvements for html and string output

smiley 2 лет назад
Родитель
Сommit
b4780d7dfe
2 измененных файлов с 6 добавлено и 15 удалено
  1. 3 7
      src/Output/QRMarkupHTML.php
  2. 3 8
      src/Output/QRStringText.php

+ 3 - 7
src/Output/QRMarkupHTML.php

@@ -23,7 +23,8 @@ class QRMarkupHTML extends QRMarkup{
 	 * @inheritDoc
 	 */
 	protected function createMarkup(bool $saveToFile):string{
-		$rows = [];
+		$rows     = [];
+		$cssClass = $this->getCssClass();
 
 		foreach($this->matrix->getMatrix() as $row){
 			$element = '<span style="background: %s;"></span>';
@@ -32,12 +33,7 @@ class QRMarkupHTML extends QRMarkup{
 			$rows[]  = sprintf('<div>%s</div>%s', implode('', $modules), $this->eol);
 		}
 
-		$html = sprintf(
-			'<div class="%1$s">%3$s%2$s</div>%3$s',
-			$this->getCssClass(),
-			implode('', $rows),
-			$this->eol
-		);
+		$html = sprintf('<div class="%1$s">%3$s%2$s</div>%3$s', $cssClass, implode('', $rows), $this->eol);
 
 		// wrap the snippet into a body when saving to file
 		if($saveToFile){

+ 3 - 8
src/Output/QRStringText.php

@@ -10,6 +10,7 @@
 
 namespace chillerlan\QRCode\Output;
 
+use function array_map;
 use function implode;
 use function is_string;
 use function max;
@@ -51,14 +52,8 @@ class QRStringText extends QROutputAbstract{
 		$lines     = [];
 		$linestart = $this->options->textLineStart;
 
-		for($y = 0; $y < $this->moduleCount; $y++){
-			$r = [];
-
-			for($x = 0; $x < $this->moduleCount; $x++){
-				$r[] = $this->getModuleValueAt($x, $y);
-			}
-
-			$lines[] = $linestart.implode('', $r);
+		foreach($this->matrix->getMatrix() as $row){
+			$lines[] = $linestart.implode('', array_map([$this, 'getModuleValue'], $row));
 		}
 
 		$data = implode($this->eol, $lines);