Skip to content

Commit f6df783

Browse files
committed
Add filters callback before save image.
1 parent 405eeef commit f6df783

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

lib/ImageResize.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ class ImageResize
4343

4444
protected $source_info;
4545

46+
47+
protected $filters = [];
48+
4649
/**
4750
* Create instance from a strng
4851
*
@@ -59,6 +62,31 @@ public static function createFromString($image_data)
5962
return $resize;
6063
}
6164

65+
66+
/**
67+
* Add filter function for use right before save image to file.
68+
*
69+
* @param callable $filter
70+
* @return $this
71+
*/
72+
public function addFilter(callable $filter)
73+
{
74+
$this->filters[] = $filter;
75+
return $this;
76+
}
77+
78+
/**
79+
* Apply filters.
80+
*
81+
* @param $image resource an image resource identifier
82+
*/
83+
protected function applyFilter($image)
84+
{
85+
foreach ($this->filters as $function) {
86+
$function($image);
87+
}
88+
}
89+
6290
/**
6391
* Loads image source and its properties to the instanciated object
6492
*
@@ -235,6 +263,9 @@ public function save($filename, $image_type = null, $quality = null, $permission
235263
$this->source_h
236264
);
237265

266+
267+
$this->applyFilter($dest_image);
268+
238269
switch ($image_type) {
239270
case IMAGETYPE_GIF:
240271
imagegif($dest_image, $filename);

0 commit comments

Comments
 (0)