Using PHP's imagefilter() Function
As I said before one of my favorite things to do with PHP is image manipulation. In PHP5 is an image function imagefilter(). It has six paremeters: the image resource, the type of filter to apply, and four different values to be passed to the function. In order to reduce the server load as well as load time the images here have been saved and are not generated on the fly. I found the picture used in all of the examples at Stock Vault.
The code I used to generate all of the images is as follows.
header("content-type: image/png");
$im = imagecreatefromjpeg("photo.jpg");
imagefilter($im, IMG_FILTER_TYPE, $args);
imagepng($im);
imagedestroy($im);
?>
The original photo, again from Stock Vault.

Example one is just a simple negative effect. Achieved by using the filter type IMG_FILTER_NEGATE, which accepts no parameters.

Example two is just a simple grayscale effect. Achieved by using the filter type IMG_FILTER_GRAYSCALE, which accepts no parameters.

Example three combines the above two effects by using both IMG_FILTER_NEGATE and IMG_FILTER_GRAYSCALE. Note that both of these are used in two seperate function calls, not combined into one. They both use the same image resource.

Examples four, five six and seven all use the IMG_FILTER_BRIGHTNESS filter type. This function accpets one parameter. In example four it is 100, in example five it is 10, in example six it is -10 and in example seven it is -100.




Example eight and nine use the filter type IMG_FILTER_CONTRAST. Example eight uses a parameter value of 100 while example nine uses a value of -50.


Example ten continues to show how effects can be combined by using IMG_FILTER_GRAYSCALE and IMG_FILTER_CONTRAST, respectively. Note that again these are two seperate function calls, just using the same image resource. The IMG_FILTER_CONTRAST filter is using a parameter value of 15.

Examples eleven and twelve use the IMG_FILTER_COLORIZE filter. This filter type accepts all four parameters, being the red, green, blue, and aplha values, however the alpha value can be omitted. Example eleven used values of 200 for red, 0 for green, 0 for blue, and the alpha parameter is omitted. Example twelve uses values of 0 for red, 200 for green, 0 for blue, and 50 for alpha.


Example thirteen uses the IMG_FILTER_EDGEDETECT filter. It accpets no parameters.

Example fourteen uses the IMG_FILTER_EMBOSS filter. It also accepts no parameters.

Example fifteen creates the gaussian blur effect using the IMG_FILTER_GAUSSIAN_BLUR filter. This filter also accpets no parameters.

Example sixteen is also a blur effect using the IMG_FILTER_SELECTIVE_BLUR filter. This also accpets no parameters.

Example seventeen uses the IMG_FILTER_MEAN_REMOVAL filter to achieve a "sketchy" effect. This filter also accpets no parameters.

Example eighteen and nineteen examine the smooting filter IMG_FILTER_SMOOTH. Example eighteen uses a parameter value of 50, while example nineteen uses a parameter value of -50.


Hope this helps someone out visualizing the various effects of imagefilter().
Posted on Sunday, June 22, 2008
Login or register to post comments.
Comments:
Cool stuff!
Thank you for this quick and very handy tip!
I have been using ImageMagick forever, this works really well though.
I used these methods to add-on an optical illusion to my gallery :)
Check out an example here: http://www.peteandkarthi.com/gallery/fun.php?mypicy=images/thumbnailz/img_3752~1024~450.jpg
Or click 'Fun' on any image in my gallery :)
http://peteandkarthi.com/gallery/
Posted on Tuesday, June 24, 2008 by petemayo.