Skip to content

Commit 23c2320

Browse files
Merge pull request #98 from AndyDune/master
Add filters callback before save image.
2 parents 485a4be + 6a8488b commit 23c2320

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

README.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,37 @@ try{
309309
}
310310
```
311311

312+
Filters
313+
--------
314+
315+
You can apply special effects for new image like blur or add banner.
316+
317+
```php
318+
$image = new ImageResize('image.jpg');
319+
320+
// Add blure
321+
$image->addFIlter(function ($imageDesc) {
322+
imagefilter($imageDesc, IMG_FILTER_GAUSSIAN_BLUR);
323+
});
324+
325+
// Add banner on bottom left corner
326+
$image18Plus = 'banner.png'
327+
$image->addFIlter(function ($imageDesc) use ($image18Plus) {
328+
$logo = imagecreatefrompng($image18Plus);
329+
$logo_width = imagesx($logo);
330+
$logo_height = imagesy($logo);
331+
$image_width = imagesx($imageDesc);
332+
$image_height = imagesy($imageDesc);
333+
$image_x = $image_width - $logo_width - 10;
334+
$image_y = $image_height - $logo_height - 10;
335+
imagecopy($imageDesc, $logo, $image_x, $image_y, 0, 0, $logo_width, $logo_height);
336+
});
337+
338+
```
339+
340+
Both functions will be used in the order in which they were added.
341+
342+
312343
API Doc
313344
-------
314345

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)