Login | Contact Us | Site Map | Resources

Image Functions Using the GD Library

Home > PHP Tutorial > How to Add Image Filters & Effects

How to Add Image Filters & Effects

The imagefilter() function can be used to apply various fun and/or useful effects to an existing image or photograph. The selection of effects is:

Parameter Description Arguments
IMG_FILTER_NEGATE Reverses All Colors of the Image  
IMG_FILTER_GRAYSCALE Converts the Image Into Grayscale  
IMG_FILTER_BRIGHTNESS Changes the Brightness of the Image Use arg1 to Set the Level of Brightness
IMG_FILTER_CONTRAST Changes the Contrast of the Image Use arg1 to Set the Level of Contrast
IMG_FILTER_COLORIZE Like IMG_FILTER_GRAYSCALE, Except You Can Specify the Color Use arg1, arg2 & arg3 in the form of red, blue, green and arg4 for the alpha channel. The range for each color is 0 to 255.
IMG_FILTER_EDGEDETECT Uses Edge Detection to Highlight the Edges of the Image  
IMG_FILTER_EMBOSS Embosses the Image  
IMG_FILTER_GAUSSIAN_BLUR Blurs the Image Using the Gaussian Method  
IMG_FILTER_SELECTIVE_BLUR Blurs the Image  
IMG_FILTER_MEAN_REMOVAL Uses Mean Removal to Achieve a "Sketchy" Effect.  
IMG_FILTER_SMOOTH Makes the Image Smoother Use arg1 to Set the Level of Smoothness
IMG_FILTER_PIXELATE Applies Pixelation Effect to Image Use arg1 to Set Block Size & arg2 to Set Pixelation Effect Mode

The syntax is: imagefilter (resource $image, int $filtertype [, int $arg1 [, int $arg2 [, int $arg3 [, int $arg4 ]]]])

Let's look at the code:

<?php
  header('Content-type: image/jpeg');
  $jpg_image = imagecreatefromjpeg('sky.jpg');
  imagefilter($jpg_image, IMG_FILTER_GRAYSCALE);
  imagejpeg($jpg_image);
  imagedestroy($jpg_image);
?>

And now you can see the original image and the programmatically modified grayscale image.

A "sketch" effect can be applied. Example: imagefilter($jpg_image, IMG_FILTER_MEAN_REMOVAL);

Brightness can be heightened. Example: imagefilter($jpg_image, IMG_FILTER_BRIGHTNESS, 50);

Colors can be modified. Example: imagefilter($jpg_image, IMG_FILTER_COLORIZE, 0, 50, 0);

And more!

Summary:

Function Description
imagefilter() Applies a Filter to An Image