smiley 3 tahun lalu
induk
melakukan
fc90b6e0e3
2 mengubah file dengan 33 tambahan dan 6 penghapusan
  1. 3 3
      examples/Readme.md
  2. 30 3
      examples/svgRoundQuietzone.php

+ 3 - 3
examples/Readme.md

@@ -17,7 +17,7 @@
 - [Custom output](./custom_output.php): a simple example that demonstrates the usage of custom output classes
 - [Custom output](./custom_output.php): a simple example that demonstrates the usage of custom output classes
 - [Interactive output](./qrcode-interactive.php): interactive demo (via [index.html](./index.html))
 - [Interactive output](./qrcode-interactive.php): interactive demo (via [index.html](./index.html))
 - [GD Image with logo](./imageWithLogo.php): a logo on top of the QR Code
 - [GD Image with logo](./imageWithLogo.php): a logo on top of the QR Code
-- [GD image with text](./imageWithText.php): description text under the QR Code
+- [GD image with text](./imageWithText.php): description text under the QR Code (#35)
 - [SVG with logo](./svgWithLogo.php): an SVG QR Code with embedded logo (that is also SVG)
 - [SVG with logo](./svgWithLogo.php): an SVG QR Code with embedded logo (that is also SVG)
-- [SVG with randomly colored modules](./svgRandomColoredDots.php): a visual effect using multiple colors for the matrix modules
-- [SVG with a round shape and randomly filled quiet zone](./svgRoundQuietzone.php): example similar to the QR Codes of a certain vendor
+- [SVG with randomly colored modules](./svgRandomColoredDots.php): a visual effect using multiple colors for the matrix modules (#136)
+- [SVG with a round shape and randomly filled quiet zone](./svgRoundQuietzone.php): example similar to the QR Codes of a certain vendor (#137)

+ 30 - 3
examples/svgRoundQuietzone.php

@@ -6,12 +6,14 @@
  * @author       smiley <smiley@chillerlan.net>
  * @author       smiley <smiley@chillerlan.net>
  * @copyright    2022 smiley
  * @copyright    2022 smiley
  * @license      MIT
  * @license      MIT
+ *
+ * @see https://github.com/chillerlan/php-qrcode/discussions/137
  */
  */
 
 
 use chillerlan\QRCode\Common\EccLevel;
 use chillerlan\QRCode\Common\EccLevel;
 use chillerlan\QRCode\Data\QRMatrix;
 use chillerlan\QRCode\Data\QRMatrix;
 use chillerlan\QRCode\Output\QRMarkupSVG;
 use chillerlan\QRCode\Output\QRMarkupSVG;
-use chillerlan\QRCode\{QRCode, QRCodeException, QROptions};
+use chillerlan\QRCode\{QRCode, QROptions};
 
 
 require_once __DIR__.'/../vendor/autoload.php';
 require_once __DIR__.'/../vendor/autoload.php';
 
 
@@ -19,6 +21,9 @@ require_once __DIR__.'/../vendor/autoload.php';
  * Class definition
  * Class definition
  */
  */
 
 
+/**
+ * the extended SVG output module
+ */
 class RoundQuietzoneSVGoutput extends QRMarkupSVG{
 class RoundQuietzoneSVGoutput extends QRMarkupSVG{
 
 
 	/**
 	/**
@@ -27,10 +32,12 @@ class RoundQuietzoneSVGoutput extends QRMarkupSVG{
 	protected function createMarkup(bool $saveToFile):string{
 	protected function createMarkup(bool $saveToFile):string{
 		// some Pythagorean magick
 		// some Pythagorean magick
 		$diameter      = sqrt(2 * pow($this->moduleCount + $this->options->additionalModules, 2));
 		$diameter      = sqrt(2 * pow($this->moduleCount + $this->options->additionalModules, 2));
+		// calculate the quiet zone size, add 1 to it as the outer circle stroke may go outside of it
 		$quietzoneSize = (int)ceil(($diameter - $this->moduleCount) / 2) + 1;
 		$quietzoneSize = (int)ceil(($diameter - $this->moduleCount) / 2) + 1;
 		// add the quiet zone to fill the circle
 		// add the quiet zone to fill the circle
 		$this->matrix->setQuietZone($quietzoneSize);
 		$this->matrix->setQuietZone($quietzoneSize);
 		// update the matrix dimensions to avoid errors in subsequent calculations
 		// update the matrix dimensions to avoid errors in subsequent calculations
+		// the moduleCount is now QR Code matrix + 2x quiet zone
 		$this->setMatrixDimensions();
 		$this->setMatrixDimensions();
 		// color the quiet zone
 		// color the quiet zone
 		$this->colorQuietzone($quietzoneSize, $diameter / 2);
 		$this->colorQuietzone($quietzoneSize, $diameter / 2);
@@ -56,6 +63,9 @@ class RoundQuietzoneSVGoutput extends QRMarkupSVG{
 		return $svg;
 		return $svg;
 	}
 	}
 
 
+	/**
+	 * Sets random modules of the quiet zone to dark
+	 */
 	protected function colorQuietzone(int $quietzoneSize, float $radius):void{
 	protected function colorQuietzone(int $quietzoneSize, float $radius):void{
 		$l1 = $quietzoneSize - 1;
 		$l1 = $quietzoneSize - 1;
 		$l2 = $this->moduleCount - $quietzoneSize;
 		$l2 = $this->moduleCount - $quietzoneSize;
@@ -70,7 +80,7 @@ class RoundQuietzoneSVGoutput extends QRMarkupSVG{
 
 
 				// leave one row of quiet zone around the matrix
 				// leave one row of quiet zone around the matrix
 				if(
 				if(
-					($x === $l1 && $y >= $l1 && $y <= $l2)
+					   ($x === $l1 && $y >= $l1 && $y <= $l2)
 					|| ($x === $l2 && $y >= $l1 && $y <= $l2)
 					|| ($x === $l2 && $y >= $l1 && $y <= $l2)
 					|| ($y === $l1 && $x > $l1 && $x < $l2)
 					|| ($y === $l1 && $x > $l1 && $x < $l2)
 					|| ($y === $l2 && $x > $l1 && $x < $l2)
 					|| ($y === $l2 && $x > $l1 && $x < $l2)
@@ -124,9 +134,24 @@ class RoundQuietzoneSVGoutput extends QRMarkupSVG{
 
 
 }
 }
 
 
+/**
+ * the augmented options class
+ */
 class RoundQuietzoneOptions extends QROptions{
 class RoundQuietzoneOptions extends QROptions{
 
 
-	protected int $additionalModules = 5;
+	/**
+	 * The amount of additional modules to be used in the circle diameter calculation
+	 *
+	 * Note that the middle of the circle stroke goes through the (assumed) outer corners
+	 * or centers of the QR Code (excluding quiet zone)
+	 *
+	 * Example:
+	 *
+	 * - a value of -1 would go through the center of the outer corner modules of the finder patterns
+	 * - a value of 0 would go through the corner of the outer modules of the finder patterns
+	 * - a value of 3 would go through the center of the module outside next to the finder patterns, in a 45 degree angle
+	 */
+	protected int $additionalModules = 0;
 
 
 }
 }
 
 
@@ -136,6 +161,8 @@ class RoundQuietzoneOptions extends QROptions{
  */
  */
 
 
 $options = new RoundQuietzoneOptions([
 $options = new RoundQuietzoneOptions([
+	'additionalModules'   => 5,
+
 	'version'             => 7,
 	'version'             => 7,
 	'eccLevel'            => EccLevel::H, // maximum error correction capacity, esp. for print
 	'eccLevel'            => EccLevel::H, // maximum error correction capacity, esp. for print
 	'addQuietzone'        => false, // we're not adding a quiet zone, this is done internally in our own module
 	'addQuietzone'        => false, // we're not adding a quiet zone, this is done internally in our own module