如何在图像上应用灰度滤镜?
how to apply grayscale filter on an image?
你能告诉我如何在 LiipImagineBundle
的图像上应用灰度滤镜吗?
注意: 我正在使用 Symfony2
谢谢,
LiipImagineBundle 中没有内置灰度滤镜,因此您可以通过 3 步创建自定义滤镜加载器:
1) 首先,您应该创建 GrayscaleFilterLoader class,它应该像任何其他自定义过滤器一样实现 Liip\ImagineBundle\Imagine\Filter\Loader\LoaderInterface:
namespace AppBundle\Imagine\Filter\Loader;
...
/**
* Class GrayscaleFilterLoader
* @package AppBundle\Imagine\Filter\Loader
*/
class GrayscaleFilterLoader implements LoaderInterface
{
/**
* @param ImageInterface $image
* @param array $options
* @return ImageInterface
*/
public function load(ImageInterface $image, array $options = [])
{
$image->effects()->grayscale();
return $image;
}
}
2) 然后在服务容器中注册并添加liip_imagine.filter.loader标签,loader标签就是过滤器的名称:
#app/config/services.yml
liip_imagine.filter.loader.grayscale:
class: AppBundle\Imagine\Filter\Loader\GrayscaleFilterLoader
tags:
- { name: liip_imagine.filter.loader, loader: grayscale}
3) 最后,用它配置过滤器集:
#app/config/config.yml
your_filter_set_name:
filters:
...
grayscale: {}
就是这样!
这对我有用:
<?php
namespace AppBundle\Imagine\Filter\Loader;
use Imagine\Filter\Advanced\Grayscale;
use Imagine\Image\ImageInterface;
use Liip\ImagineBundle\Imagine\Filter\Loader\LoaderInterface;
/**
* Apply a grayscale effect to image.
*/
class GrayscaleFilterLoader implements LoaderInterface
{
/**
* {@inheritdoc}
*/
public function load(ImageInterface $image, array $options = [])
{
$filter = new Grayscale();
return $filter->apply($image);
}
}
只需像这样定义一个服务:
services:
liip_imagine.filter.loader.grayscale:
class: AppBundle\Imagine\Filter\Loader\GrayscaleFilterLoader
tags:
- { name: liip_imagine.filter.loader, loader: grayscale}
然后是过滤器:
liip_imagine:
filter_sets:
grayscale:
filters:
grayscale: ~
你能告诉我如何在 LiipImagineBundle
的图像上应用灰度滤镜吗?
注意: 我正在使用 Symfony2
谢谢,
LiipImagineBundle 中没有内置灰度滤镜,因此您可以通过 3 步创建自定义滤镜加载器:
1) 首先,您应该创建 GrayscaleFilterLoader class,它应该像任何其他自定义过滤器一样实现 Liip\ImagineBundle\Imagine\Filter\Loader\LoaderInterface:
namespace AppBundle\Imagine\Filter\Loader;
...
/**
* Class GrayscaleFilterLoader
* @package AppBundle\Imagine\Filter\Loader
*/
class GrayscaleFilterLoader implements LoaderInterface
{
/**
* @param ImageInterface $image
* @param array $options
* @return ImageInterface
*/
public function load(ImageInterface $image, array $options = [])
{
$image->effects()->grayscale();
return $image;
}
}
2) 然后在服务容器中注册并添加liip_imagine.filter.loader标签,loader标签就是过滤器的名称:
#app/config/services.yml
liip_imagine.filter.loader.grayscale:
class: AppBundle\Imagine\Filter\Loader\GrayscaleFilterLoader
tags:
- { name: liip_imagine.filter.loader, loader: grayscale}
3) 最后,用它配置过滤器集:
#app/config/config.yml
your_filter_set_name:
filters:
...
grayscale: {}
就是这样!
这对我有用:
<?php
namespace AppBundle\Imagine\Filter\Loader;
use Imagine\Filter\Advanced\Grayscale;
use Imagine\Image\ImageInterface;
use Liip\ImagineBundle\Imagine\Filter\Loader\LoaderInterface;
/**
* Apply a grayscale effect to image.
*/
class GrayscaleFilterLoader implements LoaderInterface
{
/**
* {@inheritdoc}
*/
public function load(ImageInterface $image, array $options = [])
{
$filter = new Grayscale();
return $filter->apply($image);
}
}
只需像这样定义一个服务:
services:
liip_imagine.filter.loader.grayscale:
class: AppBundle\Imagine\Filter\Loader\GrayscaleFilterLoader
tags:
- { name: liip_imagine.filter.loader, loader: grayscale}
然后是过滤器:
liip_imagine:
filter_sets:
grayscale:
filters:
grayscale: ~