Skip to content

Commit

Permalink
Merge pull request endroid#334 from theianjohnson/add_logo_punchout_s…
Browse files Browse the repository at this point in the history
…upport

Implements logo background punchout functionality
  • Loading branch information
endroid authored Jul 3, 2021
2 parents d6d964b + 2f0324b commit 1989f53
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 6 deletions.
7 changes: 7 additions & 0 deletions src/Builder/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,13 @@ public function logoResizeToHeight(int $logoResizeToHeight): BuilderInterface
return $this;
}

public function logoPunchoutBackground(bool $logoPunchoutBackground): BuilderInterface
{
$this->options['logoPunchoutBackground'] = $logoPunchoutBackground;

return $this;
}

public function labelText(string $labelText): BuilderInterface
{
$this->options['labelText'] = $labelText;
Expand Down
2 changes: 2 additions & 0 deletions src/Builder/BuilderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ public function logoResizeToWidth(int $logoResizeToWidth): BuilderInterface;

public function logoResizeToHeight(int $logoResizeToHeight): BuilderInterface;

public function logoPunchoutBackground(bool $logoPunchoutBackground): BuilderInterface;

public function labelText(string $labelText): BuilderInterface;

public function labelFont(FontInterface $labelFont): BuilderInterface;
Expand Down
20 changes: 15 additions & 5 deletions src/ImageData/LogoImageData.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,24 @@ class LogoImageData
/** @var int */
private $height;

/** @var bool */
private $punchoutBackground;

/** @param mixed $image */
private function __construct(
string $data,
$image,
string $mimeType,
int $width,
int $height
int $height,
bool $punchoutBackground
) {
$this->data = $data;
$this->image = $image;
$this->mimeType = $mimeType;
$this->width = $width;
$this->height = $height;
$this->punchoutBackground = $punchoutBackground;
}

public static function createForLogo(LogoInterface $logo): self
Expand Down Expand Up @@ -71,20 +76,20 @@ public static function createForLogo(LogoInterface $logo): self

// No target width and height specified: use from original image
if (null !== $width && null !== $height) {
return new self($data, $image, $mimeType, $width, $height);
return new self($data, $image, $mimeType, $width, $height, $logo->getPunchoutBackground());
}

// Only target width specified: calculate height
if (null !== $width && null === $height) {
return new self($data, $image, $mimeType, $width, intval(imagesy($image) * $width / imagesx($image)));
return new self($data, $image, $mimeType, $width, intval(imagesy($image) * $width / imagesx($image)), $logo->getPunchoutBackground());
}

// Only target height specified: calculate width
if (null === $width && null !== $height) {
return new self($data, $image, $mimeType, intval(imagesx($image) * $height / imagesy($image)), $height);
return new self($data, $image, $mimeType, intval(imagesx($image) * $height / imagesy($image)), $height, $logo->getPunchoutBackground());
}

return new self($data, $image, $mimeType, imagesx($image), imagesy($image));
return new self($data, $image, $mimeType, imagesx($image), imagesy($image), $logo->getPunchoutBackground());
}

public function getData(): string
Expand Down Expand Up @@ -113,6 +118,11 @@ public function getHeight(): int
return $this->height;
}

public function getPunchoutBackground(): bool
{
return $this->punchoutBackground;
}

public function createDataUri(): string
{
return 'data:'.$this->mimeType.';base64,'.base64_encode($this->data);
Expand Down
15 changes: 14 additions & 1 deletion src/Logo/Logo.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@ final class Logo implements LogoInterface
/** @var int|null */
private $resizeToHeight;

public function __construct(string $path, ?int $resizeToWidth = null, ?int $resizeToHeight = null)
public function __construct(string $path, ?int $resizeToWidth = null, ?int $resizeToHeight = null, ?bool $punchoutBackground = false)
{
$this->path = $path;
$this->resizeToWidth = $resizeToWidth;
$this->resizeToHeight = $resizeToHeight;
$this->punchoutBackground = $punchoutBackground;
}

public static function create(string $path): self
Expand Down Expand Up @@ -62,4 +63,16 @@ public function setResizeToHeight(?int $resizeToHeight): self

return $this;
}

public function getPunchoutBackground(): ?bool
{
return $this->punchoutBackground;
}

public function setPunchoutBackground(?bool $punchoutBackground): self
{
$this->punchoutBackground = $punchoutBackground;

return $this;
}
}
2 changes: 2 additions & 0 deletions src/Logo/LogoInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,6 @@ public function getPath(): string;
public function getResizeToWidth(): ?int;

public function getResizeToHeight(): ?int;

public function getPunchoutBackground(): ?bool;
}
23 changes: 23 additions & 0 deletions src/Writer/PngWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,29 @@ private function addLogo(LogoInterface $logo, PngResult $result): PngResult

$targetImage = $result->getImage();

if($logoImageData->getPunchoutBackground()) {
$transparent = imagecolorallocatealpha($targetImage, 255, 255, 255, 127);
imagealphablending($targetImage, false);
for(
$x_offset = intval(imagesx($targetImage) / 2 - $logoImageData->getWidth() / 2);
$x_offset < intval(imagesx($targetImage) / 2 - $logoImageData->getWidth() / 2) + $logoImageData->getWidth();
$x_offset++
) {
for(
$y_offset = intval(imagesy($targetImage) / 2 - $logoImageData->getHeight() / 2);
$y_offset < intval(imagesy($targetImage) / 2 - $logoImageData->getHeight() / 2) + $logoImageData->getHeight();
$y_offset++
) {
imagesetpixel(
$targetImage,
$x_offset,
$y_offset,
$transparent
);
}
}
}

imagecopyresampled(
$targetImage,
$logoImageData->getImage(),
Expand Down

0 comments on commit 1989f53

Please sign in to comment.