Skip to content

Commit 7395795

Browse files
committed
:octocat: clarify several QROptions: $imageTransparent and $imageTransparencyBG, introduced $drawLightModules and $bgColor (see #122)
1 parent 7bacc8b commit 7395795

File tree

11 files changed

+57
-31
lines changed

11 files changed

+57
-31
lines changed

examples/image.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
'eccLevel' => EccLevel::L,
2020
'scale' => 10,
2121
'imageBase64' => false,
22+
'bgColor' => [200, 200, 200],
2223
'imageTransparent' => false,
2324
'drawCircularModules' => true,
2425
'circleRadius' => 0.4,

examples/imagick.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@
1717
'version' => 7,
1818
'outputType' => QROutputInterface::IMAGICK,
1919
'eccLevel' => EccLevel::L,
20-
'imagickBG' => '#FFFFFF',
20+
'bgColor' => '#cccccc', // overrides the imageTransparent setting
21+
'imageTransparent' => true,
2122
'scale' => 20,
23+
'drawLightModules' => true,
2224
'drawCircularModules' => true,
2325
'circleRadius' => 0.4,
2426
'keepAsSquare' => [QRMatrix::M_FINDER|QRMatrix::IS_DARK, QRMatrix::M_FINDER_DOT, QRMatrix::M_ALIGNMENT|QRMatrix::IS_DARK],

examples/svg.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222
'imageBase64' => false,
2323
'eccLevel' => EccLevel::L,
2424
'addQuietzone' => true,
25-
// if set to true, the light modules won't be rendered
26-
'imageTransparent' => false,
25+
// if set to false, the light modules won't be rendered
26+
'drawLightModules' => true,
2727
// empty the default value to remove the fill* attributes from the <path> elements
2828
'markupDark' => '',
2929
'markupLight' => '',

examples/svgRandomColoredDots.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ class RandomDotsOptions extends QROptions{
124124
'outputInterface' => RandomDotsSVGOutput::class,
125125
'markupDark' => '',
126126
'markupLight' => '',
127-
'imageTransparent' => true,
127+
'drawLightModules' => false,
128128

129129
'connectPaths' => true,
130130
'excludeFromConnect' => [

examples/svgRoundQuietzone.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ class RoundQuietzoneOptions extends QROptions{
174174
'outputInterface' => RoundQuietzoneSVGoutput::class, // load our own output class
175175
'markupDark' => '', // avoid "fill" attributes on paths
176176
'markupLight' => '',
177-
'imageTransparent' => true, // set to false to add the light modules
177+
'drawLightModules' => false, // set to true to add the light modules
178178

179179
'connectPaths' => true,
180180
'excludeFromConnect' => [

examples/svgWithLogo.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ protected function set_svgLogoScale(float $svgLogoScale):void{
109109
'eccLevel' => EccLevel::H,
110110
'addQuietzone' => true,
111111
// if set to true, the light modules won't be rendered
112-
'imageTransparent' => false,
112+
'drawLightModules' => true,
113113
// empty the default value to remove the fill* attributes from the <path> elements
114114
'markupDark' => '',
115115
'markupLight' => '',

src/Output/QRGdImage.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,14 +95,21 @@ public function dump(string $file = null){
9595

9696
$this->image = imagecreatetruecolor($this->length, $this->length);
9797

98-
// avoid: "Indirect modification of overloaded property $imageTransparencyBG has no effect"
98+
// avoid: "Indirect modification of overloaded property $x has no effect"
9999
// https://stackoverflow.com/a/10455217
100-
$tbg = $this->options->imageTransparencyBG;
100+
$bgColor = $this->options->imageTransparencyBG;
101+
102+
if($this->moduleValueIsValid($this->options->bgColor)){
103+
$bgColor = $this->getModuleValue($this->options->bgColor);
104+
}
105+
101106
/** @phan-suppress-next-line PhanParamTooFewInternalUnpack */
102-
$background = imagecolorallocate($this->image, ...$tbg);
107+
$background = imagecolorallocate($this->image, ...$bgColor);
103108

104109
if($this->options->imageTransparent && $this->options->outputType !== QROutputInterface::GDIMAGE_JPG){
105-
imagecolortransparent($this->image, $background);
110+
$tbg = $this->options->imageTransparencyBG;
111+
/** @phan-suppress-next-line PhanParamTooFewInternalUnpack */
112+
imagecolortransparent($this->image, imagecolorallocate($this->image, ...$tbg));
106113
}
107114

108115
imagefilledrectangle($this->image, 0, 0, $this->length, $this->length, $background);

src/Output/QRImagick.php

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -73,14 +73,14 @@ public function dump(string $file = null){
7373
$file ??= $this->options->cachefile;
7474
$this->imagick = new Imagick;
7575

76-
$this->imagick->newImage(
77-
$this->length,
78-
$this->length,
79-
new ImagickPixel($this->options->imagickBG ?? 'transparent'),
80-
$this->options->imagickFormat
81-
);
76+
$bgColor = $this->options->imageTransparent ? 'transparent' : 'white';
8277

83-
$this->imagick->setImageType(Imagick::IMGTYPE_TRUECOLOR);
78+
// keep the imagickBG property for now (until v6)
79+
if($this->moduleValueIsValid($this->options->bgColor ?? $this->options->imagickBG)){
80+
$bgColor = $this->options->bgColor ?? $this->options->imagickBG;
81+
}
82+
83+
$this->imagick->newImage($this->length, $this->length, new ImagickPixel($bgColor), $this->options->imagickFormat);
8484

8585
$this->drawImage();
8686

@@ -119,6 +119,11 @@ protected function drawImage():void{
119119
* draws a single pixel at the given position
120120
*/
121121
protected function setPixel(int $x, int $y, int $M_TYPE):void{
122+
123+
if(!$this->options->drawLightModules && !$this->matrix->check($x, $y)){
124+
return;
125+
}
126+
122127
$this->imagickDraw->setFillColor($this->moduleValues[$M_TYPE]);
123128

124129
$this->options->drawCircularModules && $this->matrix->checkTypeNotIn($x, $y, $this->options->keepAsSquare)

src/Output/QRMarkupSVG.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ protected function getCssClass(int $M_TYPE):string{
126126
*/
127127
protected function module(int $x, int $y, int $M_TYPE):string{
128128

129-
if($this->options->imageTransparent && !$this->matrix->check($x, $y)){
129+
if(!$this->options->drawLightModules && !$this->matrix->check($x, $y)){
130130
return '';
131131
}
132132

src/QROptionsTrait.php

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -232,32 +232,41 @@ trait QROptionsTrait{
232232
/**
233233
* toggle background transparency
234234
*
235-
* - In GdImage mode (png, gif) it sets imagecolortransparent() with QROptions::$imageTransparencyBG.
236-
* It also sets the "normal" background color without transparency switch.
235+
* - GdImage: (png, gif) it sets imagecolortransparent() with {@see \chillerlan\QRCode\QROptions::$imageTransparencyBG}
237236
*
238-
* - In SVG mode (as of v5), it won't render the "light" modules,
239-
* as opacity/transparency can easily be set with css properties.
240237
*
241-
* - It has no effect in the FPDF and Imagick output modules.
242-
*
243-
* @see \chillerlan\QRCode\QROptions::$imageTransparencyBG
244238
* @see https://github.com/chillerlan/php-qrcode/discussions/121
245239
*/
246240
protected bool $imageTransparent = true;
247241

248242
/**
249-
* Sets the background color in GD mode.
243+
* whether to draw the light (false) modules
244+
*
245+
* @var bool
246+
*/
247+
protected bool $drawLightModules = true;
248+
249+
/**
250+
* Sets the background color in GD mode: [R, G, B].
250251
*
251-
* When QROptions::$imageTransparent is set to true, this color is set as transparent in imagecolortransparent()
252+
* When $imageTransparent is set to true, this color is set as transparent in imagecolortransparent()
252253
*
253254
* @see \chillerlan\QRCode\Output\QRGdImage
254255
* @see \chillerlan\QRCode\QROptions::$imageTransparent
255256
* @see imagecolortransparent()
256-
*
257-
* [R, G, B]
258257
*/
259258
protected array $imageTransparencyBG = [255, 255, 255];
260259

260+
/**
261+
* Sets the image background color (if applicable)
262+
*
263+
* - Imagick: defaults to "transparent" or "white", depending on $imageTransparent, {@see \ImagickPixel::__construct()}
264+
* - GdImage: defaults to $imageTransparencyBG, {@see \chillerlan\QRCode\QROptions::$imageTransparencyBG}
265+
*
266+
* @var mixed|null
267+
*/
268+
protected $bgColor = null;
269+
261270
/**
262271
* @see imagepng()
263272
*/
@@ -277,8 +286,10 @@ trait QROptionsTrait{
277286
protected string $imagickFormat = 'png32';
278287

279288
/**
280-
* Imagick background color (defaults to "transparent")
289+
* Imagick background color
281290
*
291+
* @deprecated 5.0.0 use QROptions::$bgColor instead
292+
* @see \chillerlan\QRCode\QROptions::$bgColor
282293
* @see \ImagickPixel::__construct()
283294
*/
284295
protected ?string $imagickBG = null;

0 commit comments

Comments
 (0)