Skip to content

Commit

Permalink
Merge pull request #249 from OzanKurt/main
Browse files Browse the repository at this point in the history
Ability to resize canvas when keeping fill background a certain color
  • Loading branch information
timvandijck authored May 17, 2024
2 parents b323c78 + 3191236 commit 392385f
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 8 deletions.
10 changes: 8 additions & 2 deletions src/Drivers/Gd/GdDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,13 @@ public function getSize(): Size
return new Size($this->getWidth(), $this->getHeight());
}

public function fit(Fit $fit, ?int $desiredWidth = null, ?int $desiredHeight = null): static
public function fit(
Fit $fit,
?int $desiredWidth = null,
?int $desiredHeight = null,
bool $relative = false,
string $backgroundColor = '#ffffff'
): static
{
if ($fit === Fit::Crop) {
return $this->fitCrop($fit, $this->getWidth(), $this->getHeight(), $desiredWidth, $desiredHeight);
Expand All @@ -248,7 +254,7 @@ public function fit(Fit $fit, ?int $desiredWidth = null, ?int $desiredHeight = n
);

if ($fit->shouldResizeCanvas()) {
$this->resizeCanvas($desiredWidth, $desiredHeight, AlignPosition::Center);
$this->resizeCanvas($desiredWidth, $desiredHeight, AlignPosition::Center, $relative, $backgroundColor);
}

return $this;
Expand Down
8 changes: 7 additions & 1 deletion src/Drivers/ImageDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,13 @@ public function sharpen(float $amount): static;

public function getSize(): Size;

public function fit(Fit $fit, ?int $desiredWidth = null, ?int $desiredHeight = null): static;
public function fit(
Fit $fit,
?int $desiredWidth = null,
?int $desiredHeight = null,
bool $relative = false,
string $backgroundColor = '#ffffff'
): static;

public function pickColor(int $x, int $y, ColorFormat $colorFormat): mixed;

Expand Down
10 changes: 8 additions & 2 deletions src/Drivers/Imagick/ImagickDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,13 @@ public function blur(int $blur): static
return $this;
}

public function fit(Fit $fit, ?int $desiredWidth = null, ?int $desiredHeight = null): static
public function fit(
Fit $fit,
?int $desiredWidth = null,
?int $desiredHeight = null,
bool $relative = false,
string $backgroundColor = null,
): static
{
if ($fit === Fit::Crop) {
return $this->fitCrop($fit, $this->getWidth(), $this->getHeight(), $desiredWidth, $desiredHeight);
Expand All @@ -138,7 +144,7 @@ public function fit(Fit $fit, ?int $desiredWidth = null, ?int $desiredHeight = n
}

if ($fit->shouldResizeCanvas()) {
$this->resizeCanvas($desiredWidth, $desiredHeight, AlignPosition::Center, false, null);
$this->resizeCanvas($desiredWidth, $desiredHeight, AlignPosition::Center, $relative, $backgroundColor);
}

return $this;
Expand Down
12 changes: 9 additions & 3 deletions src/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,15 @@ public function getSize(): Size
return $this->imageDriver->getSize();
}

public function fit(Fit $fit, ?int $desiredWidth = null, ?int $desiredHeight = null): static
{
$this->imageDriver->fit($fit, $desiredWidth, $desiredHeight);
public function fit(
Fit $fit,
?int $desiredWidth = null,
?int $desiredHeight = null,
bool $relative = false,
string $backgroundColor = '#ffffff'
): static
{
$this->imageDriver->fit($fit, $desiredWidth, $desiredHeight, $relative, $backgroundColor);

return $this;
}
Expand Down

0 comments on commit 392385f

Please sign in to comment.