Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Background color and margin for logo #429

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 30 additions & 1 deletion src/Logo/Logo.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,18 @@

namespace Endroid\QrCode\Logo;

use Endroid\QrCode\Color\Color;
use Endroid\QrCode\Color\ColorInterface;

final class Logo implements LogoInterface
{
public function __construct(
private string $path,
private int|null $resizeToWidth = null,
private int|null $resizeToHeight = null,
private bool $punchoutBackground = false
private bool $punchoutBackground = false,
private int $margin = 0,
private ColorInterface $backgroundColor = new Color(255, 255, 255, 127)
) {
}

Expand Down Expand Up @@ -66,4 +71,28 @@ public function setPunchoutBackground(bool $punchoutBackground): self

return $this;
}

public function getMargin(): int
{
return $this->margin;
}

public function setMargin(int $margin): self
{
$this->margin = $margin;

return $this;
}

public function getBackgroundColor(): ColorInterface
{
return $this->backgroundColor;
}

public function setBackgroundColor(ColorInterface $backgroundColor): self
{
$this->backgroundColor = $backgroundColor;

return $this;
}
}
6 changes: 6 additions & 0 deletions src/Logo/LogoInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace Endroid\QrCode\Logo;

use Endroid\QrCode\Color\ColorInterface;

interface LogoInterface
{
public function getPath(): string;
Expand All @@ -13,4 +15,8 @@ public function getResizeToWidth(): int|null;
public function getResizeToHeight(): int|null;

public function getPunchoutBackground(): bool;

public function getMargin(): int;

public function getBackgroundColor(): ColorInterface;
}
52 changes: 36 additions & 16 deletions src/Writer/AbstractGdWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,33 +127,53 @@ private function addLogo(LogoInterface $logo, GdResult $result): GdResult
throw new \Exception('PNG Writer does not support SVG logo');
}

$logoImage = $logoImageData->getImage();
$targetImage = $result->getImage();
$matrix = $result->getMatrix();

if ($logoImageData->getPunchoutBackground()) {
/** @var int $transparent */
$transparent = imagecolorallocatealpha($targetImage, 255, 255, 255, 127);
imagealphablending($targetImage, false);
$xOffsetStart = intval($matrix->getOuterSize() / 2 - $logoImageData->getWidth() / 2);
$yOffsetStart = intval($matrix->getOuterSize() / 2 - $logoImageData->getHeight() / 2);
for ($xOffset = $xOffsetStart; $xOffset < $xOffsetStart + $logoImageData->getWidth(); ++$xOffset) {
for ($yOffset = $yOffsetStart; $yOffset < $yOffsetStart + $logoImageData->getHeight(); ++$yOffset) {
imagesetpixel($targetImage, $xOffset, $yOffset, $transparent);
}
$logoImage = imagecreatetruecolor($logoImageData->getWidth() + $logo->getMargin() * 2, $logoImageData->getHeight() + $logo->getMargin() * 2);

if (!$logoImage) {
throw new \Exception('Unable to generate image: please check if the GD extension is enabled and configured correctly');
}

/** @var int $backgroundColor */
$backgroundColor = imagecolorallocatealpha(
$logoImage,
$logo->getBackgroundColor()->getRed(),
$logo->getBackgroundColor()->getGreen(),
$logo->getBackgroundColor()->getBlue(),
$logo->getBackgroundColor()->getAlpha()
);

imagefill($logoImage, 0, 0, $backgroundColor);

imagecopyresampled(
$logoImage,
$logoImageData->getImage(),
$logo->getMargin(),
$logo->getMargin(),
0,
0,
$logoImageData->getWidth(),
$logoImageData->getHeight(),
imagesx($logoImageData->getImage()),
imagesy($logoImageData->getImage())
);
}

imagecopyresampled(
$targetImage,
$logoImageData->getImage(),
intval($matrix->getOuterSize() / 2 - $logoImageData->getWidth() / 2),
intval($matrix->getOuterSize() / 2 - $logoImageData->getHeight() / 2),
$logoImage,
intval($matrix->getOuterSize() / 2 - $logoImageData->getWidth() / 2 - $logo->getMargin()),
intval($matrix->getOuterSize() / 2 - $logoImageData->getHeight() / 2 - $logo->getMargin()),
0,
0,
$logoImageData->getWidth(),
$logoImageData->getHeight(),
imagesx($logoImageData->getImage()),
imagesy($logoImageData->getImage())
$logoImageData->getWidth() + $logo->getMargin() * 2,
$logoImageData->getHeight() + $logo->getMargin() * 2,
imagesx($logoImage),
imagesy($logoImage)
);

return new GdResult($matrix, $targetImage);
Expand Down