Skip to content

Commit

Permalink
:octocat: clarify several QROptions: $imageTransparent and $imageTransparency…
Browse files Browse the repository at this point in the history
…BG, introduced $drawLightModules and $bgColor (see #122)
  • Loading branch information
codemasher committed Jul 31, 2022
1 parent 7bacc8b commit 7395795
Show file tree
Hide file tree
Showing 11 changed files with 57 additions and 31 deletions.
1 change: 1 addition & 0 deletions examples/image.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
'eccLevel' => EccLevel::L,
'scale' => 10,
'imageBase64' => false,
'bgColor' => [200, 200, 200],
'imageTransparent' => false,
'drawCircularModules' => true,
'circleRadius' => 0.4,
Expand Down
4 changes: 3 additions & 1 deletion examples/imagick.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@
'version' => 7,
'outputType' => QROutputInterface::IMAGICK,
'eccLevel' => EccLevel::L,
'imagickBG' => '#FFFFFF',
'bgColor' => '#cccccc', // overrides the imageTransparent setting
'imageTransparent' => true,
'scale' => 20,
'drawLightModules' => true,
'drawCircularModules' => true,
'circleRadius' => 0.4,
'keepAsSquare' => [QRMatrix::M_FINDER|QRMatrix::IS_DARK, QRMatrix::M_FINDER_DOT, QRMatrix::M_ALIGNMENT|QRMatrix::IS_DARK],
Expand Down
4 changes: 2 additions & 2 deletions examples/svg.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
'imageBase64' => false,
'eccLevel' => EccLevel::L,
'addQuietzone' => true,
// if set to true, the light modules won't be rendered
'imageTransparent' => false,
// if set to false, the light modules won't be rendered
'drawLightModules' => true,
// empty the default value to remove the fill* attributes from the <path> elements
'markupDark' => '',
'markupLight' => '',
Expand Down
2 changes: 1 addition & 1 deletion examples/svgRandomColoredDots.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ class RandomDotsOptions extends QROptions{
'outputInterface' => RandomDotsSVGOutput::class,
'markupDark' => '',
'markupLight' => '',
'imageTransparent' => true,
'drawLightModules' => false,

'connectPaths' => true,
'excludeFromConnect' => [
Expand Down
2 changes: 1 addition & 1 deletion examples/svgRoundQuietzone.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ class RoundQuietzoneOptions extends QROptions{
'outputInterface' => RoundQuietzoneSVGoutput::class, // load our own output class
'markupDark' => '', // avoid "fill" attributes on paths
'markupLight' => '',
'imageTransparent' => true, // set to false to add the light modules
'drawLightModules' => false, // set to true to add the light modules

'connectPaths' => true,
'excludeFromConnect' => [
Expand Down
2 changes: 1 addition & 1 deletion examples/svgWithLogo.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ protected function set_svgLogoScale(float $svgLogoScale):void{
'eccLevel' => EccLevel::H,
'addQuietzone' => true,
// if set to true, the light modules won't be rendered
'imageTransparent' => false,
'drawLightModules' => true,
// empty the default value to remove the fill* attributes from the <path> elements
'markupDark' => '',
'markupLight' => '',
Expand Down
15 changes: 11 additions & 4 deletions src/Output/QRGdImage.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,14 +95,21 @@ public function dump(string $file = null){

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

// avoid: "Indirect modification of overloaded property $imageTransparencyBG has no effect"
// avoid: "Indirect modification of overloaded property $x has no effect"
// https://stackoverflow.com/a/10455217
$tbg = $this->options->imageTransparencyBG;
$bgColor = $this->options->imageTransparencyBG;

if($this->moduleValueIsValid($this->options->bgColor)){
$bgColor = $this->getModuleValue($this->options->bgColor);
}

/** @phan-suppress-next-line PhanParamTooFewInternalUnpack */
$background = imagecolorallocate($this->image, ...$tbg);
$background = imagecolorallocate($this->image, ...$bgColor);

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

imagefilledrectangle($this->image, 0, 0, $this->length, $this->length, $background);
Expand Down
19 changes: 12 additions & 7 deletions src/Output/QRImagick.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,14 @@ public function dump(string $file = null){
$file ??= $this->options->cachefile;
$this->imagick = new Imagick;

$this->imagick->newImage(
$this->length,
$this->length,
new ImagickPixel($this->options->imagickBG ?? 'transparent'),
$this->options->imagickFormat
);
$bgColor = $this->options->imageTransparent ? 'transparent' : 'white';

$this->imagick->setImageType(Imagick::IMGTYPE_TRUECOLOR);
// keep the imagickBG property for now (until v6)
if($this->moduleValueIsValid($this->options->bgColor ?? $this->options->imagickBG)){
$bgColor = $this->options->bgColor ?? $this->options->imagickBG;
}

$this->imagick->newImage($this->length, $this->length, new ImagickPixel($bgColor), $this->options->imagickFormat);

$this->drawImage();

Expand Down Expand Up @@ -119,6 +119,11 @@ protected function drawImage():void{
* draws a single pixel at the given position
*/
protected function setPixel(int $x, int $y, int $M_TYPE):void{

if(!$this->options->drawLightModules && !$this->matrix->check($x, $y)){
return;
}

$this->imagickDraw->setFillColor($this->moduleValues[$M_TYPE]);

$this->options->drawCircularModules && $this->matrix->checkTypeNotIn($x, $y, $this->options->keepAsSquare)
Expand Down
2 changes: 1 addition & 1 deletion src/Output/QRMarkupSVG.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ protected function getCssClass(int $M_TYPE):string{
*/
protected function module(int $x, int $y, int $M_TYPE):string{

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

Expand Down
35 changes: 23 additions & 12 deletions src/QROptionsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -232,32 +232,41 @@ trait QROptionsTrait{
/**
* toggle background transparency
*
* - In GdImage mode (png, gif) it sets imagecolortransparent() with QROptions::$imageTransparencyBG.
* It also sets the "normal" background color without transparency switch.
* - GdImage: (png, gif) it sets imagecolortransparent() with {@see \chillerlan\QRCode\QROptions::$imageTransparencyBG}
*
* - In SVG mode (as of v5), it won't render the "light" modules,
* as opacity/transparency can easily be set with css properties.
*
* - It has no effect in the FPDF and Imagick output modules.
*
* @see \chillerlan\QRCode\QROptions::$imageTransparencyBG
* @see https://github.com/chillerlan/php-qrcode/discussions/121
*/
protected bool $imageTransparent = true;

/**
* Sets the background color in GD mode.
* whether to draw the light (false) modules
*
* @var bool
*/
protected bool $drawLightModules = true;

/**
* Sets the background color in GD mode: [R, G, B].
*
* When QROptions::$imageTransparent is set to true, this color is set as transparent in imagecolortransparent()
* When $imageTransparent is set to true, this color is set as transparent in imagecolortransparent()
*
* @see \chillerlan\QRCode\Output\QRGdImage
* @see \chillerlan\QRCode\QROptions::$imageTransparent
* @see imagecolortransparent()
*
* [R, G, B]
*/
protected array $imageTransparencyBG = [255, 255, 255];

/**
* Sets the image background color (if applicable)
*
* - Imagick: defaults to "transparent" or "white", depending on $imageTransparent, {@see \ImagickPixel::__construct()}
* - GdImage: defaults to $imageTransparencyBG, {@see \chillerlan\QRCode\QROptions::$imageTransparencyBG}
*
* @var mixed|null
*/
protected $bgColor = null;

/**
* @see imagepng()
*/
Expand All @@ -277,8 +286,10 @@ trait QROptionsTrait{
protected string $imagickFormat = 'png32';

/**
* Imagick background color (defaults to "transparent")
* Imagick background color
*
* @deprecated 5.0.0 use QROptions::$bgColor instead
* @see \chillerlan\QRCode\QROptions::$bgColor
* @see \ImagickPixel::__construct()
*/
protected ?string $imagickBG = null;
Expand Down
2 changes: 1 addition & 1 deletion tests/Output/QRMarkupTestAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ abstract class QRMarkupTestAbstract extends QROutputTestAbstract{
*/
public function testSetModuleValues():void{
$this->options->imageBase64 = false;
$this->options->imageTransparent = false;
$this->options->drawLightModules = true;
$this->options->moduleValues = [
// data
QRMatrix::M_DATA | QRMatrix::IS_DARK => '#4A6000',
Expand Down

0 comments on commit 7395795

Please sign in to comment.