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

introduced DownscaleFilterLoader #610

Merged
merged 5 commits into from
Oct 27, 2015
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
45 changes: 45 additions & 0 deletions Imagine/Filter/Loader/DownscaleFilterLoader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace Liip\ImagineBundle\Imagine\Filter\Loader;

use Imagine\Filter\Basic\Resize;
use Imagine\Image\ImageInterface;
use Imagine\Image\Box;


/**
* downscale filter
*
*/
class DownscaleFilterLoader implements LoaderInterface
{
/**
* {@inheritDoc}
*/
public function load(ImageInterface $image, array $options = array())
{
if (!isset($options['max'])) {
throw new \InvalidArgumentException('Missing max option.');
}

list($width, $height) = $options['max'];

$size = $image->getSize();
$origWidth = $size->getWidth();
$origHeight = $size->getHeight();

if ($origWidth > $width || $origHeight > $height) {

$widthRatio = $width / $origWidth ;
$heightRatio = $height / $origHeight;

$ratio = $widthRatio > $heightRatio ? $widthRatio : $heightRatio;

$filter = new Resize(new Box($origWidth * $ratio, $origHeight * $ratio));

return $filter->apply($image);
}

return $image;
}
}
5 changes: 5 additions & 0 deletions Resources/config/imagine.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
<parameter key="liip_imagine.filter.loader.strip.class">Liip\ImagineBundle\Imagine\Filter\Loader\StripFilterLoader</parameter>
<parameter key="liip_imagine.filter.loader.background.class">Liip\ImagineBundle\Imagine\Filter\Loader\BackgroundFilterLoader</parameter>
<parameter key="liip_imagine.filter.loader.upscale.class">Liip\ImagineBundle\Imagine\Filter\Loader\UpscaleFilterLoader</parameter>
<parameter key="liip_imagine.filter.loader.downscale.class">Liip\ImagineBundle\Imagine\Filter\Loader\DownscaleFilterLoader</parameter>
<parameter key="liip_imagine.filter.loader.auto_rotate.class">Liip\ImagineBundle\Imagine\Filter\Loader\AutoRotateFilterLoader</parameter>
<parameter key="liip_imagine.filter.loader.rotate.class">Liip\ImagineBundle\Imagine\Filter\Loader\RotateFilterLoader</parameter>
<parameter key="liip_imagine.filter.loader.interlace.class">Liip\ImagineBundle\Imagine\Filter\Loader\InterlaceFilterLoader</parameter>
Expand Down Expand Up @@ -176,6 +177,10 @@
<tag name="liip_imagine.filter.loader" loader="upscale" />
</service>

<service id="liip_imagine.filter.loader.downscale" class="%liip_imagine.filter.loader.downscale.class%">
<tag name="liip_imagine.filter.loader" loader="downscale" />
</service>

<service id="liip_imagine.filter.loader.auto_rotate" class="%liip_imagine.filter.loader.auto_rotate.class%">
<tag name="liip_imagine.filter.loader" loader="auto_rotate" />
</service>
Expand Down
14 changes: 14 additions & 0 deletions Resources/doc/filters.rst
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,20 @@ on your image. Configuration looks like this:
filters:
upscale: { min: [800, 600] }

The ``downscale`` filter
~~~~~~~~~~~~~~~~~~~~~~

The downscale filter, as the name implies, performs a downscale transformation
on your image. Configuration looks like this:

.. code-block:: yaml

liip_imagine:
filter_sets:
my_thumb:
filters:
downscale: { max: [1980, 1280] }

The ``crop`` filter
~~~~~~~~~~~~~~~~~~~

Expand Down
Binary file added Tests/Fixtures/assets/square-300x300.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Liip\ImagineBundle\Tests\Functional\Imagine\Filter\Loader;

use Liip\ImagineBundle\Tests\Functional\WebTestCase;

class DownscaleFilterLoaderTest extends WebTestCase
{
public function testCouldBeGetFromContainerAsService()
{
$this->createClient();
$service = self::$kernel->getContainer()->get('liip_imagine.filter.loader.downscale');

$this->assertInstanceOf('Liip\ImagineBundle\Imagine\Filter\Loader\DownscaleFilterLoader', $service);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Liip\ImagineBundle\Tests\Filter;

use Imagine\Gd\Image;
use Imagine\Gd\Imagine;
use Liip\ImagineBundle\Imagine\Filter\Loader\DownscaleFilterLoader;
use Liip\ImagineBundle\Tests\AbstractTest;

/**
* @covers Liip\ImagineBundle\Imagine\Filter\Loader\DownscaleFilterLoader
*
* Due to int casting in Imagine\Image\Box which can lead to wrong pixel
* numbers ( e.g. float(201) casted to int(200) ). Solved by round the
* floating number before passing to the Box constructor.
*/
class FloatToIntCastByRoundDownscaleFilterLoaderTest extends AbstractTest
{
public function testLoad()
{
$loader = new DownscaleFilterLoader();
$imagine = new Imagine();
$image = $imagine->open(__DIR__.'/../../../Fixtures/assets/square-300x300.png');

$options = array(
'max' => array(201, 201),
);

$image = $loader->load($image, $options);
$size = $image->getSize();

$this->assertEquals($options['max'], array($size->getWidth(), $size->getHeight()));
}
}