Skip to content

Commit a702ac3

Browse files
committed
🚿
1 parent 991287a commit a702ac3

File tree

1 file changed

+13
-23
lines changed

1 file changed

+13
-23
lines changed

examples/svgRoundQuietzone.php

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
*/
3030
class RoundQuietzoneSVGoutput extends QRMarkupSVG{
3131

32+
protected float $center;
33+
3234
/**
3335
* @inheritDoc
3436
*/
@@ -42,13 +44,13 @@ protected function createMarkup(bool $saveToFile):string{
4244
// update the matrix dimensions to avoid errors in subsequent calculations
4345
// the moduleCount is now QR Code matrix + 2x quiet zone
4446
$this->setMatrixDimensions();
45-
// color the quiet zone
46-
$this->colorQuietzone($quietzoneSize, ($diameter / 2));
47-
47+
$this->center = ($this->moduleCount / 2);
4848
// calculate the logo space
4949
$logoSpaceSize = (int)(ceil($this->moduleCount * $this->options->svgLogoScale) + 1);
5050
// we're calling QRMatrix::setLogoSpace() manually, so QROptions::$addLogoSpace has no effect here
5151
$this->matrix->setLogoSpace($logoSpaceSize);
52+
// color the quiet zone
53+
$this->colorQuietzone($quietzoneSize, ($diameter / 2));
5254

5355
// start SVG output
5456
$svg = $this->header();
@@ -64,11 +66,6 @@ protected function createMarkup(bool $saveToFile):string{
6466
// close svg
6567
$svg .= sprintf('%1$s</svg>%1$s', $this->options->eol);
6668

67-
// transform to data URI only when not saving to file
68-
if(!$saveToFile && $this->options->outputBase64){
69-
$svg = $this->toBase64DataURI($svg, 'image/svg+xml');
70-
}
71-
7269
return $svg;
7370
}
7471

@@ -109,7 +106,7 @@ protected function colorQuietzone(int $quietzoneSize, float $radius):void{
109106

110107
// we need to add 0.5 units to the check values since we're calculating the element centers
111108
// ($x/$y is the element's assumed top left corner)
112-
if($this->checkIfInsideCircle(($x + 0.5), ($y + 0.5), $r)){
109+
if($this->checkIfInsideCircle(($x + 0.5), ($y + 0.5), $this->center, $this->center, $r)){
113110
$this->matrix->set($x, $y, (bool)rand(0, 1), QRMatrix::M_QUIETZONE);
114111
}
115112
}
@@ -120,9 +117,9 @@ protected function colorQuietzone(int $quietzoneSize, float $radius):void{
120117
/**
121118
* @see https://stackoverflow.com/a/7227057
122119
*/
123-
protected function checkIfInsideCircle(float $x, float $y, float $radius):bool{
124-
$dx = abs($x - $this->moduleCount / 2);
125-
$dy = abs($y - $this->moduleCount / 2);
120+
protected function checkIfInsideCircle(float $x, float $y, float $centerX, float $centerY, float $radius):bool{
121+
$dx = abs($x - $centerX);
122+
$dy = abs($y - $centerY);
126123

127124
if(($dx + $dy) <= $radius){
128125
return true;
@@ -132,11 +129,7 @@ protected function checkIfInsideCircle(float $x, float $y, float $radius):bool{
132129
return false;
133130
}
134131

135-
if((pow($dx, 2) + pow($dy, 2)) <= pow($radius, 2)){
136-
return true;
137-
}
138-
139-
return false;
132+
return (pow($dx, 2) + pow($dy, 2)) <= pow($radius, 2);
140133
}
141134

142135
/**
@@ -145,7 +138,7 @@ protected function checkIfInsideCircle(float $x, float $y, float $radius):bool{
145138
protected function addCircle(float $radius):string{
146139
return sprintf(
147140
'%4$s<circle id="circle" cx="%1$s" cy="%1$s" r="%2$s" stroke-width="%3$s"/>',
148-
($this->moduleCount / 2),
141+
$this->center,
149142
round($radius, 5),
150143
($this->options->circleRadius * 2),
151144
$this->options->eol
@@ -194,7 +187,7 @@ protected function collectModules(Closure $transform):array{
194187
// randomly assign another $M_TYPE_LAYER for the given types
195188
// note that the layer id has to be an integer value,
196189
// ideally outside the several bitmask values
197-
if($M_TYPE_LAYER === QRMatrix::M_DATA_DARK){
190+
if($M_TYPE_LAYER === QRMatrix::M_QUIETZONE_DARK){
198191
$M_TYPE_LAYER = array_rand($dotColors);
199192
}
200193

@@ -329,11 +322,8 @@ protected function set_svgLogoScale(float $svgLogoScale):void{
329322
$options->outputInterface = RoundQuietzoneSVGoutput::class; // load our own output class
330323
$options->drawLightModules = false; // set to true to add the light modules
331324
// common SVG options
332-
#$options->connectPaths = true; // this has been set to "always on" internally
325+
$options->connectPaths = true;
333326
$options->excludeFromConnect = [
334-
QRMatrix::M_FINDER_DARK,
335-
QRMatrix::M_FINDER_DOT,
336-
QRMatrix::M_ALIGNMENT_DARK,
337327
QRMatrix::M_QUIETZONE_DARK,
338328
];
339329
$options->drawCircularModules = true;

0 commit comments

Comments
 (0)