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

[Filter] Upscale filter should use the highest dimension to calculate ratio #856

Merged
merged 2 commits into from
Jan 27, 2017
Merged
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
2 changes: 1 addition & 1 deletion Imagine/Filter/Loader/ScaleFilterLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public function load(ImageInterface $image, array $options = array())
if (null == $width || null == $height) {
$ratio = max($widthRatio, $heightRatio);
} else {
$ratio = min($widthRatio, $heightRatio);
$ratio = ('min' === $this->dimensionKey) ? max($widthRatio, $heightRatio) : min($widthRatio, $heightRatio);
}
}

Expand Down
87 changes: 87 additions & 0 deletions Tests/Imagine/Filter/Loader/ScaleFilterLoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Imagine\Image\Box;
use Liip\ImagineBundle\Imagine\Filter\Loader\ScaleFilterLoader;
use Liip\ImagineBundle\Imagine\Filter\Loader\UpscaleFilterLoader;
use Liip\ImagineBundle\Tests\AbstractTest;

/**
Expand All @@ -34,6 +35,31 @@ class ScaleFilterLoaderTest extends AbstractTest
*/
const DUMMY_IMAGE_HEIGHT = 600;

/**
* @var int
*/
const UPSCALE_DUMMY_IMAGE_WIDTH = 600;

/**
* @var int
*/
const UPSCALE_DUMMY_IMAGE_HEIGHT = 400;

protected function getUpscaleMockImage()
{
$mockImageSize = new Box(
self::UPSCALE_DUMMY_IMAGE_WIDTH,
self::UPSCALE_DUMMY_IMAGE_HEIGHT
);
$mockImage = parent::getMockImage();
$mockImage->method('getSize')->willReturn(new Box(
self::UPSCALE_DUMMY_IMAGE_WIDTH,
self::UPSCALE_DUMMY_IMAGE_HEIGHT
));

return $mockImage;
}

protected function getMockImage()
{
$mockImageSize = new Box(
Expand Down Expand Up @@ -116,4 +142,65 @@ public function dimensionsDataProvider()
array(array(1000, 1200), new Box(1000, 1200)),
);
}

/**
* @dataProvider minScaleDataProvider
*/
public function testShouldScale($dimensions, $expected)
{
$loader = new UpscaleFilterLoader();
$image = $this->getUpscaleMockImage();
$image->expects($this->once())
->method('resize')
->with($expected)
->willReturn($image);

$options = array(
'min' => $dimensions,
);

$result = $loader->load($image, $options);
}

/**
* @returns array Array containing coordinate and width/height pairs.
*/
public function minScaleDataProvider()
{
return array(
array(array(1000, 600), new Box(1000, 667)),
array(array(1200, 300), new Box(1200, 800)),
);
}

/**
* @dataProvider minNotScaleDataProvider
*/
public function testShouldNotScale($dimensions, $expected)
{
$loader = new UpscaleFilterLoader();
$image = $this->getUpscaleMockImage();
$image->expects($this->never())
->method('resize')
->with($expected)
->willReturn($image);

$options = array(
'min' => $dimensions,
);

$result = $loader->load($image, $options);
}

/**
* @returns array Array containing coordinate and width/height pairs.
*/
public function minNotScaleDataProvider()
{
return array(
array(array(300, 200), new Box(600, 400)),
array(array(600, 400), new Box(600, 400)),
);
}

}