Skip to content

Commit

Permalink
Merge pull request #638 from gregumo/master
Browse files Browse the repository at this point in the history
Implement Imagine Grayscale filter
  • Loading branch information
lsmith77 authored Jul 12, 2016
2 parents 4bf0e7d + c206212 commit bd98616
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 1 deletion.
24 changes: 24 additions & 0 deletions Imagine/Filter/Loader/GrayscaleFilterLoader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Liip\ImagineBundle\Imagine\Filter\Loader;

use Imagine\Filter\Advanced\Grayscale;
use Imagine\Image\ImageInterface;

/**
* GrayscaleFilterLoader - apply grayscale filter.
*
* @author Gregoire Humeau <gregoire.humeau@gmail.com>
*/
class GrayscaleFilterLoader implements LoaderInterface
{
/**
* {@inheritdoc}
*/
public function load(ImageInterface $image, array $options = array())
{
$filter = new Grayscale();

return $filter->apply($image);
}
}
5 changes: 5 additions & 0 deletions Resources/config/imagine.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
<parameter key="liip_imagine.filter.loader.resize.class">Liip\ImagineBundle\Imagine\Filter\Loader\ResizeFilterLoader</parameter>
<parameter key="liip_imagine.filter.loader.thumbnail.class">Liip\ImagineBundle\Imagine\Filter\Loader\ThumbnailFilterLoader</parameter>
<parameter key="liip_imagine.filter.loader.crop.class">Liip\ImagineBundle\Imagine\Filter\Loader\CropFilterLoader</parameter>
<parameter key="liip_imagine.filter.loader.grayscale.class">Liip\ImagineBundle\Imagine\Filter\Loader\GrayscaleFilterLoader</parameter>
<parameter key="liip_imagine.filter.loader.paste.class">Liip\ImagineBundle\Imagine\Filter\Loader\PasteFilterLoader</parameter>
<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>
Expand Down Expand Up @@ -162,6 +163,10 @@
<tag name="liip_imagine.filter.loader" loader="crop" />
</service>

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

<service id="liip_imagine.filter.loader.paste" class="%liip_imagine.filter.loader.paste.class%">
<tag name="liip_imagine.filter.loader" loader="paste" />
<argument type="service" id="liip_imagine" />
Expand Down
13 changes: 13 additions & 0 deletions Resources/doc/filters.rst
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,19 @@ It modifies the way the image is loaded progressively:
# mode can be one of: 'none', 'line', 'plane' and 'partition'
mode: line
The ``grayscale`` filter
~~~~~~~~~~~~~~~~~~~~~~~~

It modifies the image colors by calculating the gray-value based on RGB:

.. code-block:: yaml
liip_imagine:
filter_sets:
my_thumb:
filters:
grayscale: ~
Load your Custom Filters
------------------------

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

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

use Liip\ImagineBundle\Tests\Functional\WebTestCase;

/**
* Functional test cases for GrayscaleFilterLoader class.
*
* @author Gregoire Humeau <gregoire.humeau@gmail.com>
*/
class GrayscaleFilterLoaderTest extends WebTestCase
{
public function testCouldBeGetFromContainerAsService()
{
$this->createClient();
$service = self::$kernel->getContainer()->get('liip_imagine.filter.loader.grayscale');

$this->assertInstanceOf('Liip\ImagineBundle\Imagine\Filter\Loader\GrayscaleFilterLoader', $service);
}
}
37 changes: 37 additions & 0 deletions Tests/Imagine/Filter/Loader/GrayscaleFilterLoaderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace Liip\ImagineBundle\Tests\Filter;

use Imagine\Image\Box;
use Imagine\Image\Palette\RGB;
use Imagine\Image\Point;
use Imagine\Gd\Imagine;
use Liip\ImagineBundle\Imagine\Filter\Loader\GrayscaleFilterLoader;
use Liip\ImagineBundle\Tests\AbstractTest;

/**
* Test cases for GrayscaleFilterLoader class.
*
* @covers Liip\ImagineBundle\Imagine\Filter\Loader\GrayscaleFilterLoader
*
* @author Gregoire Humeau <gregoire.humeau@gmail.com>
*/
class GrayscaleFilterLoaderTest extends AbstractTest
{
public function testLoadGrayscale()
{
$loader = new GrayscaleFilterLoader();
$palette = new RGB();
$imagine = new Imagine();

// Generate blue image
$image = $imagine->create(new Box(20, 20), $palette->color(array(20, 90, 240)));

//Apply Grayscale filter
$result = $loader->load($image);

//Test result
$pixel = $result->getColorAt(new Point(10, 10));
$this->assertEquals('#565656', (string) $pixel);
}
}
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
},
"require": {
"php": "^5.3.9|^7.0",
"imagine/Imagine": "~0.5,<0.7",
"imagine/Imagine": "^0.6.3,<0.7",
"symfony/finder": "~2.3|~3.0",
"symfony/filesystem": "~2.3|~3.0",
"symfony/options-resolver": "~2.3|~3.0",
Expand Down

0 comments on commit bd98616

Please sign in to comment.