Skip to content

Commit

Permalink
Merge pull request #258 from spatie/feature/fit-fill-max
Browse files Browse the repository at this point in the history
Implement Fit::FillMax
  • Loading branch information
timvandijck authored Jun 3, 2024
2 parents 933d81e + adca76e commit 324a7bf
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 4 deletions.
2 changes: 1 addition & 1 deletion docs/image-manipulations/resizing-images.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ Resizes the image to fit within the width and height boundaries without cropping
# Example of how to set background colour to fill remaining pixels

$image
->fit(fit: Fit::Fill, desiredWidth: 497, desiredHeight: 290, backgroundColor: '#ff5733');
->fit(fit: Fit::Fit::FillMax, desiredWidth: 497, desiredHeight: 290, backgroundColor: '#ff5733');
```

![Blue background on fit filled JPG](../../images/example-background.png)
Expand Down
17 changes: 17 additions & 0 deletions src/Drivers/Gd/GdDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use Spatie\Image\Enums\Orientation;
use Spatie\Image\Exceptions\CouldNotLoadImage;
use Spatie\Image\Exceptions\InvalidFont;
use Spatie\Image\Exceptions\MissingParameter;
use Spatie\Image\Exceptions\UnsupportedImageFormat;
use Spatie\Image\Point;
use Spatie\Image\Size;
Expand Down Expand Up @@ -236,6 +237,14 @@ public function fit(
return $this->fitCrop($fit, $this->getWidth(), $this->getHeight(), $desiredWidth, $desiredHeight);
}

if ($fit === Fit::FillMax) {
if (is_null($desiredWidth) || is_null($desiredHeight)) {
throw new MissingParameter('Both desiredWidth and desiredHeight must be set when using Fit::FillMax');
}

return $this->fitFillMax($desiredWidth, $desiredHeight, $backgroundColor);
}

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

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

return $this;
}

protected function modify(
int $desiredWidth,
int $desiredHeight,
Expand Down
17 changes: 17 additions & 0 deletions src/Drivers/Imagick/ImagickDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use Spatie\Image\Enums\FlipDirection;
use Spatie\Image\Enums\Orientation;
use Spatie\Image\Exceptions\InvalidFont;
use Spatie\Image\Exceptions\MissingParameter;
use Spatie\Image\Exceptions\UnsupportedImageFormat;
use Spatie\Image\Point;
use Spatie\Image\Size;
Expand Down Expand Up @@ -131,6 +132,14 @@ public function fit(
return $this->fitCrop($fit, $this->getWidth(), $this->getHeight(), $desiredWidth, $desiredHeight);
}

if ($fit === Fit::FillMax) {
if (is_null($desiredWidth) || is_null($desiredHeight)) {
throw new MissingParameter('Both desiredWidth and desiredHeight must be set when using Fit::FillMax');
}

return $this->fitFillMax($desiredWidth, $desiredHeight, $backgroundColor);

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

View workflow job for this annotation

GitHub Actions / phpstan

Parameter #3 $backgroundColor of method Spatie\Image\Drivers\Imagick\ImagickDriver::fitFillMax() expects string, string|null given.

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

View workflow job for this annotation

GitHub Actions / phpstan

Parameter #3 $backgroundColor of method Spatie\Image\Drivers\Imagick\ImagickDriver::fitFillMax() expects string, string|null given.
}

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

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

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]);
}
}
9 changes: 9 additions & 0 deletions src/Exceptions/MissingParameter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Spatie\Image\Exceptions;

use Exception;

class MissingParameter extends Exception
{
}
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 324a7bf

Please sign in to comment.