filter — apply filter on image

Description

public Intervention\Image\Image filter( Intervention\Image\Filters\FilterInterface $filter )

The method applies custom filter on current image. Filters are bundles of commands which apply combined operations and effects on an image. Any filters must implement the Intervention\Image\Filters\FilterInterface and call any method that can be applied to an Intervention Image instance.

Parameters

filter

Instance of your custom filter. See example of DemoFilter below which combines a greyscale/pixelate effect.

Return Values

Instance of Intervention\Image\Image

Examples

  1. // init new image instance
  2. $img = Image::make('foo.jpg');
  3. // apply filter
  4. $img->filter(new DemoFilter(45));

DemoFilter Example

  1. <?php
  2. namespace Intervention\Image\Filters;
  3. class DemoFilter implements FilterInterface
  4. {
  5. /**
  6. * Default size of filter effects
  7. */
  8. const DEFAULT_SIZE = 10;
  9. /**
  10. * Size of filter effects
  11. *
  12. * @var integer
  13. */
  14. private $size;
  15. /**
  16. * Creates new instance of filter
  17. *
  18. * @param integer $size
  19. */
  20. public function __construct($size = null)
  21. {
  22. $this->size = is_numeric($size) ? intval($size) : self::DEFAULT_SIZE;
  23. }
  24. /**
  25. * Applies filter effects to given image
  26. *
  27. * @param Intervention\Image\Image $image
  28. * @return Intervention\Image\Image
  29. */
  30. public function applyFilter(\Intervention\Image\Image $image)
  31. {
  32. $image->pixelate($this->size);
  33. $image->greyscale();
  34. return $image;
  35. }
  36. }