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

Add "fill-max" resizing option #347

Open
wants to merge 2 commits into
base: 1.x
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
3 changes: 2 additions & 1 deletion docs/1.0/api/size.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,12 @@ Sets how the image is fitted to its target dimensions.
- `contain`: Default. Resizes the image to fit within the width and height boundaries without cropping, distorting or altering the aspect ratio.
- `max`: Resizes the image to fit within the width and height boundaries without cropping, distorting or altering the aspect ratio, and will also not increase the size of the image if it is smaller than the output size.
- `fill`: Resizes the image to fit within the width and height boundaries without cropping or distorting the image, and the remaining space is filled with the background color. The resulting image will match the constraining dimensions.
- `fill-max`: Resizes the image to fit within the width and height boundaries without cropping but with distorting/blurring the image if it's smaller. The finished image will have remaining space on either width or height (except if the aspect ratio of the new image is the same as the old image). The remaining space will be filled with the background color. The resulting image will match the constraining dimensions.
- `stretch`: Stretches the image to fit the constraining dimensions exactly. The resulting image will fill the dimensions, and will not maintain the aspect ratio of the input image.
- `crop`: Resizes the image to fill the width and height boundaries and crops any excess image data. The resulting image will match the width and height constraints without distorting the image. See the [crop](api/crop/) page for more information.

~~~ html
<img src="kayaks.jpg?w=300&h=300&fit=stretch">
~~~

[![© Photo Joel Reynolds](https://glide.herokuapp.com/1.0/kayaks.jpg?w=300&h=300&fit=stretch)](https://glide.herokuapp.com/1.0/kayaks.jpg?w=300&h=300&fit=stretch)
[![© Photo Joel Reynolds](https://glide.herokuapp.com/1.0/kayaks.jpg?w=300&h=300&fit=stretch)](https://glide.herokuapp.com/1.0/kayaks.jpg?w=300&h=300&fit=stretch)
28 changes: 25 additions & 3 deletions src/Manipulators/Size.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,9 @@ public function getFit()
{
if (null === $this->fit) {
return 'contain';
}

if (in_array($this->fit, ['contain', 'fill', 'max', 'stretch'], true)) {
}
if (in_array($this->fit, ['contain', 'fill', 'max', 'fill-max', 'stretch'], true)) {
return $this->fit;
}

Expand Down Expand Up @@ -251,6 +251,10 @@ public function runResize(Image $image, $fit, $width, $height)
return $this->runMaxResize($image, $width, $height);
}

if ($fit === 'fill-max') {
return $this->runFillMaxResize($image, $width, $height);
}

if ('stretch' === $fit) {
return $this->runStretchResize($image, $width, $height);
}
Expand Down Expand Up @@ -295,6 +299,24 @@ public function runMaxResize(Image $image, $width, $height)
});
}

/**
* Perform fill-max resize image manipulation.
*
* @param Image $image The source image.
* @param int $width The width.
* @param int $height The height.
*
* @return Image The manipulated image.
*/
public function runFillMaxResize(Image $image, $width, $height)
{
$image = $image->resize($width, $height, function ($constraint) {
$constraint->aspectRatio();
});

return $image->resizeCanvas($width, $height, 'center');
}

/**
* Perform fill resize image manipulation.
*
Expand Down
10 changes: 8 additions & 2 deletions tests/Manipulators/SizeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ public function testGetFit()
$this->assertSame('contain', $this->manipulator->setParams(['fit' => 'contain'])->getFit());
$this->assertSame('fill', $this->manipulator->setParams(['fit' => 'fill'])->getFit());
$this->assertSame('max', $this->manipulator->setParams(['fit' => 'max'])->getFit());
$this->assertSame('fill-max', $this->manipulator->setParams(['fit' => 'fill-max'])->getFit());
$this->assertSame('stretch', $this->manipulator->setParams(['fit' => 'stretch'])->getFit());
$this->assertSame('crop', $this->manipulator->setParams(['fit' => 'crop'])->getFit());
$this->assertSame('contain', $this->manipulator->setParams(['fit' => 'invalid'])->getFit());
Expand Down Expand Up @@ -150,9 +151,9 @@ public function testRunResize()
$mock->shouldReceive('width')->andReturn(100)->times(4);
$mock->shouldReceive('height')->andReturn(100)->times(4);
$mock->shouldReceive('crop')->andReturn($mock)->once();
$mock->shouldReceive('resize')->with(100, 100, $this->callback)->andReturn($mock)->times(4);
$mock->shouldReceive('resize')->with(100, 100, $this->callback)->andReturn($mock)->times(5);
$mock->shouldReceive('resize')->with(100, 100)->andReturn($mock)->once();
$mock->shouldReceive('resizeCanvas')->with(100, 100, 'center')->andReturn($mock)->once();
$mock->shouldReceive('resizeCanvas')->with(100, 100, 'center')->andReturn($mock)->times(2);
});

$this->assertInstanceOf(
Expand All @@ -170,6 +171,11 @@ public function testRunResize()
$this->manipulator->runResize($image, 'max', 100, 100)
);

$this->assertInstanceOf(
'Intervention\Image\Image',
$this->manipulator->runResize($image, 'fill-max', 100, 100)
);

$this->assertInstanceOf(
'Intervention\Image\Image',
$this->manipulator->runResize($image, 'stretch', 100, 100)
Expand Down