Skip to content

Commit

Permalink
Implement Fit::FillMax for Imagick.
Browse files Browse the repository at this point in the history
  • Loading branch information
timvandijck committed May 24, 2024
1 parent c5a38f8 commit 67e29b5
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 3 deletions.
12 changes: 12 additions & 0 deletions src/Drivers/Imagick/ImagickDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,10 @@ public function fit(
return $this->fitCrop($fit, $this->getWidth(), $this->getHeight(), $desiredWidth, $desiredHeight);
}

if ($fit === Fit::FillMax) {
return $this->fitFillMax($desiredWidth, $desiredHeight, $backgroundColor);
}

$calculatedSize = $fit->calculateSize(
$this->getWidth(),
$this->getHeight(),
Expand All @@ -149,6 +153,14 @@ public function fit(
return $this;
}

public function fitFillMax(?int $desiredWidth = null, ?int $desiredHeight = null, ?string $backgroundColor = null, ?bool $relative = false): static
{
$this->resize($desiredWidth, $desiredHeight, [Constraint::PreserveAspectRatio]);

Check failure on line 158 in src/Drivers/Imagick/ImagickDriver.php

View workflow job for this annotation

GitHub Actions / phpstan

Parameter #1 $width of method Spatie\Image\Drivers\Imagick\ImagickDriver::resize() expects int, int|null given.

Check failure on line 158 in src/Drivers/Imagick/ImagickDriver.php

View workflow job for this annotation

GitHub Actions / phpstan

Parameter #2 $height of method Spatie\Image\Drivers\Imagick\ImagickDriver::resize() expects int, int|null given.
$this->resizeCanvas($desiredWidth, $desiredHeight, AlignPosition::Center, $relative, $backgroundColor);

Check failure on line 159 in src/Drivers/Imagick/ImagickDriver.php

View workflow job for this annotation

GitHub Actions / phpstan

Parameter #4 $relative of method Spatie\Image\Drivers\Imagick\ImagickDriver::resizeCanvas() expects bool, bool|null given.

return $this;
}

public function resizeCanvas(
?int $width = null,
?int $height = null,
Expand Down
6 changes: 4 additions & 2 deletions src/Enums/Fit.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ enum Fit: string
case Stretch = 'stretch';
case Crop = 'crop';

case FillMax = 'fill-max';

public function calculateSize(
int $originalWidth,
int $originalHeight,
Expand All @@ -25,7 +27,7 @@ public function calculateSize(

$constraints = match ($this) {
Fit::Contain => [Constraint::PreserveAspectRatio],
Fit::Fill, Fit::Max => [Constraint::PreserveAspectRatio, Constraint::DoNotUpsize],
Fit::Fill, Fit::Max, Fit::FillMax => [Constraint::PreserveAspectRatio, Constraint::DoNotUpsize],
Fit::Stretch, Fit::Crop => [],
};

Expand All @@ -34,6 +36,6 @@ public function calculateSize(

public function shouldResizeCanvas(): bool
{
return in_array($this, [self::Fill]);
return in_array($this, [self::Fill, self::FillMax]);
}
}
2 changes: 1 addition & 1 deletion tests/Manipulations/FitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@
$targetFile = $this->tempDir->path("{$driver->driverName()}/fit-background.png");

$driver->loadFile(getTestJpg())
->fit(fit: Fit::Fill, desiredWidth: 800, desiredHeight: 200, backgroundColor: '#0073ff')
->fit(fit: Fit::FillMax, desiredWidth: 800, desiredHeight: 400, backgroundColor: '#0073ff')
->save($targetFile);

assertMatchesImageSnapshot($targetFile);
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 67e29b5

Please sign in to comment.