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

:octocat: circular modules in Imagick output

codemasher 4 лет назад
Родитель
Сommit
709a0322c9
2 измененных файлов с 23 добавлено и 12 удалено
  1. 8 5
      examples/imagick.php
  2. 15 7
      src/Output/QRImagick.php

+ 8 - 5
examples/imagick.php

@@ -17,11 +17,14 @@ require_once __DIR__.'/../vendor/autoload.php';
 $data = 'https://www.youtube.com/watch?v=DLzxrzFCyOs&t=43s';
 
 $options = new QROptions([
-	'version'      => 7,
-	'outputType'   => QRCode::OUTPUT_IMAGICK,
-	'eccLevel'     => EccLevel::L,
-	'scale'        => 5,
-	'moduleValues' => [
+	'version'             => 7,
+	'outputType'          => QRCode::OUTPUT_IMAGICK,
+	'eccLevel'            => EccLevel::L,
+	'scale'               => 20,
+	'drawCircularModules' => true,
+	'circleRadius'        => 0.4,
+	'keepAsSquare'        => [QRMatrix::M_FINDER|QRMatrix::IS_DARK, QRMatrix::M_FINDER_DOT, QRMatrix::M_ALIGNMENT|QRMatrix::IS_DARK],
+	'moduleValues'        => [
 		// finder
 		QRMatrix::M_FINDER | QRMatrix::IS_DARK     => '#A71111', // dark (true)
 		QRMatrix::M_FINDER                         => '#FFBFBF', // light (false)

+ 15 - 7
src/Output/QRImagick.php

@@ -103,7 +103,7 @@ class QRImagick extends QROutputAbstract{
 
 		foreach($this->matrix->matrix() as $y => $row){
 			foreach($row as $x => $M_TYPE){
-				$this->drawPixel($x, $y, $M_TYPE);
+				$this->setPixel($x, $y, $M_TYPE);
 			}
 		}
 
@@ -113,14 +113,22 @@ class QRImagick extends QROutputAbstract{
 	/**
 	 * draws a single pixel at the given position
 	 */
-	protected function drawPixel(int $x, int $y, int $M_TYPE):void{
+	protected function setPixel(int $x, int $y, int $M_TYPE):void{
 		$this->imagickDraw->setStrokeColor($this->moduleValues[$M_TYPE]);
 		$this->imagickDraw->setFillColor($this->moduleValues[$M_TYPE]);
-		$this->imagickDraw->rectangle(
-			$x * $this->scale,
-			$y * $this->scale,
-			($x + 1) * $this->scale,
-			($y + 1) * $this->scale
+
+		$this->options->drawCircularModules && !$this->matrix->checkTypes($x, $y, $this->options->keepAsSquare)
+			? $this->imagickDraw->circle(
+				($x + 0.5) * $this->scale,
+				($y + 0.5) * $this->scale,
+				($x + 0.5 + $this->options->circleRadius) * $this->scale,
+				($y + 0.5) * $this->scale
+			)
+			: $this->imagickDraw->rectangle(
+				$x * $this->scale,
+				$y * $this->scale,
+				($x + 1) * $this->scale,
+				($y + 1) * $this->scale
 		);
 	}