diff --git a/Imagine/Filter/Loader/ScaleFilterLoader.php b/Imagine/Filter/Loader/ScaleFilterLoader.php index bd643963b..3ae828964 100644 --- a/Imagine/Filter/Loader/ScaleFilterLoader.php +++ b/Imagine/Filter/Loader/ScaleFilterLoader.php @@ -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); } } diff --git a/Tests/Imagine/Filter/Loader/ScaleFilterLoaderTest.php b/Tests/Imagine/Filter/Loader/ScaleFilterLoaderTest.php index b3388b533..98232bc61 100644 --- a/Tests/Imagine/Filter/Loader/ScaleFilterLoaderTest.php +++ b/Tests/Imagine/Filter/Loader/ScaleFilterLoaderTest.php @@ -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; /** @@ -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( @@ -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)), + ); + } + }