Skip to content

Commit

Permalink
Merge pull request liip#856 from Rattler3/upscale-fitler
Browse files Browse the repository at this point in the history
Upscale filter should use the highest dimension
  • Loading branch information
cedricziel authored Jan 27, 2017
2 parents 63e1f6b + 9ac41ae commit 409e151
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 1 deletion.
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)),
);
}

}

0 comments on commit 409e151

Please sign in to comment.