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

Scale filter and Downscale and Upscale as derivatives, with a new feature #773

Merged
merged 3 commits into from
Aug 29, 2016
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
44 changes: 14 additions & 30 deletions Imagine/Filter/Loader/DownscaleFilterLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,25 @@

namespace Liip\ImagineBundle\Imagine\Filter\Loader;

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

/**
* downscale filter.
* Downscale filter.
*
* @author Devi Prasad <https://github.com/deviprsd21>
*/
class DownscaleFilterLoader implements LoaderInterface
class DownscaleFilterLoader extends ScaleFilterLoader
{
/**
* {@inheritdoc}
*/
public function load(ImageInterface $image, array $options = array())
public function __construct()
{
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 = min($widthRatio, $heightRatio);

$filter = new Resize(new Box($origWidth * $ratio, $origHeight * $ratio));
parent::__construct('max', 'by', false);
}

return $filter->apply($image);
}
protected function calcAbsoluteRatio($ratio)
{
return 1 - ($ratio > 1 ? $ratio - floor($ratio) : $ratio);
}

return $image;
protected function isImageProcessable($ratio)
{
return $ratio < 1;
}
}
69 changes: 69 additions & 0 deletions Imagine/Filter/Loader/ScaleFilterLoader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

namespace Liip\ImagineBundle\Imagine\Filter\Loader;

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

/**
* Scale filter.
*
* @author Devi Prasad <https://github.com/deviprsd21>
*/
class ScaleFilterLoader implements LoaderInterface
{
public function __construct($dimentionKey = 'dim', $ratioKey = 'to', $absoluteRatio = true)
{
$this->dimentionKey = $dimentionKey;
$this->ratioKey = $ratioKey;
$this->absoluteRatio = $absoluteRatio;
}

/**
* {@inheritdoc}
*/
public function load(ImageInterface $image, array $options = array())
{
if (!isset($options[$this->dimentionKey]) && !isset($options[$this->ratioKey])) {
throw new \InvalidArgumentException("Missing $this->dimentionKey or $this->ratioKey option.");
}

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

if (isset($options[$this->ratioKey])) {
$ratio = $this->absoluteRatio ? $options[$this->ratioKey] : $this->calcAbsoluteRatio($options[$this->ratioKey]);
} elseif (isset($options[$this->dimentionKey])) {
list($width, $height) = $options[$this->dimentionKey];

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

if (null == $width || null == $height) {
$ratio = max($widthRatio, $heightRatio);
} else {
$ratio = min($widthRatio, $heightRatio);
}
}

if ($this->isImageProcessable($ratio)) {
$filter = new Resize(new Box(round($origWidth * $ratio), round($origHeight * $ratio)));

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

return $image;
}

protected function calcAbsoluteRatio($ratio)
{
return $ratio;
}

protected function isImageProcessable($ratio)
{
return true;
}
}
41 changes: 12 additions & 29 deletions Imagine/Filter/Loader/UpscaleFilterLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,26 @@

namespace Liip\ImagineBundle\Imagine\Filter\Loader;

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

/**
* Upscale filter.
*
* @author Maxime Colin <[email protected]>
* @author Devi Prasad <https://github.com/deviprsd21>
*/
class UpscaleFilterLoader implements LoaderInterface
class UpscaleFilterLoader extends ScaleFilterLoader
{
/**
* {@inheritdoc}
*/
public function load(ImageInterface $image, array $options = array())
public function __construct()
{
if (!isset($options['min'])) {
throw new \InvalidArgumentException('Missing min option.');
}

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

$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(round($origWidth * $ratio), round($origHeight * $ratio)));
parent::__construct('min', 'by', false);
}

return $filter->apply($image);
}
protected function calcAbsoluteRatio($ratio)
{
return 1 + $ratio;
}

return $image;
protected function isImageProcessable($ratio)
{
return $ratio > 1;
}
}
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.watermark.class">Liip\ImagineBundle\Imagine\Filter\Loader\WatermarkFilterLoader</parameter>
<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.scale.class">Liip\ImagineBundle\Imagine\Filter\Loader\ScaleFilterLoader</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>
Expand Down Expand Up @@ -193,6 +194,10 @@
<tag name="liip_imagine.filter.loader" loader="strip" />
</service>

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

<service id="liip_imagine.filter.loader.upscale" class="%liip_imagine.filter.loader.upscale.class%">
<tag name="liip_imagine.filter.loader" loader="upscale" />
</service>
Expand Down
22 changes: 18 additions & 4 deletions Resources/doc/filters.rst
Original file line number Diff line number Diff line change
Expand Up @@ -65,34 +65,48 @@ annotated configuration examples:
filters:
relative_resize: { scale: 2.5 } # Transforms 50x40 to 125x100

The ``scale`` filter
~~~~~~~~~~~~~~~~~~~~~~

It performs an upscale or downscale transformation on your image to increase its size to the
given dimensions or ratio:

.. code-block:: yaml

liip_imagine:
filter_sets:
my_thumb:
filters:
scale: { dim: [600, 750] } #or { to: 1.56 } -> Upscales to [936, 1170] | { to: 0.66 } -> Downscales to [396, 495]


The ``upscale`` filter
~~~~~~~~~~~~~~~~~~~~~~

It performs an upscale transformation on your image to increase its size to the
given dimensions:
given dimensions or ratio:

.. code-block:: yaml

liip_imagine:
filter_sets:
my_thumb:
filters:
upscale: { min: [800, 600] }
upscale: { min: [800, 600] } #or { by: 0.7 } -> Upscales to [1360, 1020]

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

It performs a downscale transformation on your image to reduce its size to the
given dimensions:
given dimensions or ratio:

.. code-block:: yaml

liip_imagine:
filter_sets:
my_thumb:
filters:
downscale: { max: [1980, 1280] }
downscale: { max: [1980, 1280] } #or { by: 0.6 } -> Downscales to [792, 512]

The ``crop`` filter
~~~~~~~~~~~~~~~~~~~
Expand Down