Image Filters

Image Filters in Intervention Image give you the useful possibility to group image transformations commands into a dedicated object. This object defines which command, in which order and with which arguments should be called on an image instance.

Intervention Image provides the basic Intervention\Image\Filters\FilterInterface, which all filters need to implement.

Once you have created your own filters, you can apply them using the filter() method.

Applying the demo filter

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

Take a look at the example of the DemoFilter src/Intervention/Image/Filters/DemoFilter.php which combines a greyscale/pixelate effect.

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. }