Image filtering these days are all over the web. It all got hot when Instagram started it. So in today's post we will be dealing with how to create those image filter effects using PHP and trust me they are really simple.
CSS also got a property called filter that could do the same job but the biggest problem with that is Cross Browser Compatibility. Chrome 18.0+ and Safari 6.0+ and Opera 15.0+ support them completely. Its difficult to implement in Firefox and the so called browser from Microsoft doesn't support that as usual.
So there is a PHP solution which could help you out. This is using the PHP GD library. Lets get started and see on how to do this thing. Your first job would be to install the PHP GD library. It usually comes along with PHP and is pre installed and enabled in most cases. If not you could find the install instructions here. To check if it is already enabled or to enable it follow the steps below :
1. Open up your php.ini file.
2. Find the following line.
In this example we will be dealing with JPG images. But you could use other formats including gif, png and even bmp. You just need to change the parts which says jpeg from the code that we are dealing with, with the name of the extension that you wanna use. So lets get started.
2. In the second line of the code where we say imagefilter($image, IMG_FILTER_GRAYSCALE); we are just applying the filter, in this case a black and white effect, to the image stream that is mentioned as the first argument.
3. And in the third line of the code where we say imagejpeg($image, ‘output.jpg’) we are just writing the image stream which we just modified into a new file named output.jpg which php would create.
4. Finally in the fourth line where we say imagedestroy($image); we are just shutting down the image stream and cleaning that.
And similarly if you wanna apply the filter to a png file then you need to modify the code to look like the following :
Okay. So like these two examples you could use a wide range of fitlers available. The following are the available ones.
• IMG_FILTER_NEGATE - Reverses all colors of the image.
• IMG_FILTER_GRAYSCALE - Converts the image into grayscale.
• IMG_FILTER_BRIGHTNESS - Changes brightness of the image (Takes one more argument)
• IMG_FILTER_CONTRAST - Changes the contrast of the image (Takes one more argument)
• IMG_FILTER_COLORIZE - Like IMG_FILTER_GRAYSCALE, except you can specify the color.
• 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 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 smoothy (Takes one more argument)
• IMG_FILTER_PIXELATE - Applies pixelation effect to the image (Takes two more arguments)
You can find the complete guide to use these effects from this PHP manual. So you could try out your own image filter effects. Happy coding :)
CSS also got a property called filter that could do the same job but the biggest problem with that is Cross Browser Compatibility. Chrome 18.0+ and Safari 6.0+ and Opera 15.0+ support them completely. Its difficult to implement in Firefox and the so called browser from Microsoft doesn't support that as usual.
So there is a PHP solution which could help you out. This is using the PHP GD library. Lets get started and see on how to do this thing. Your first job would be to install the PHP GD library. It usually comes along with PHP and is pre installed and enabled in most cases. If not you could find the install instructions here. To check if it is already enabled or to enable it follow the steps below :
1. Open up your php.ini file.
2. Find the following line.
extension=php_gd2.dll3. If this line is prefixed with a semicolon then remove it. If not its already enabled and you are good to go.
In this example we will be dealing with JPG images. But you could use other formats including gif, png and even bmp. You just need to change the parts which says jpeg from the code that we are dealing with, with the name of the extension that you wanna use. So lets get started.
Black And White Filter :
Technically its called grayscale. So varying your grayscale-ness would increase and decrease your black and white intensity accordingly. The following code would produce a complete black and white effect to the image.<?php1. In the first line of code we say $image = imageccreatefromjpeg(‘me.jpg’) and in this line we are simply creating a image stream with the image specified as the argument. This is image that we would convert.
$image = imagecreatefromjpeg('me.jpg');
imagefilter($image, IMG_FILTER_GRAYSCALE);
imagejpeg($image, 'output.jpg');
imagedestroy($image);
?>
2. In the second line of the code where we say imagefilter($image, IMG_FILTER_GRAYSCALE); we are just applying the filter, in this case a black and white effect, to the image stream that is mentioned as the first argument.
3. And in the third line of the code where we say imagejpeg($image, ‘output.jpg’) we are just writing the image stream which we just modified into a new file named output.jpg which php would create.
4. Finally in the fourth line where we say imagedestroy($image); we are just shutting down the image stream and cleaning that.
And similarly if you wanna apply the filter to a png file then you need to modify the code to look like the following :
<?phpSo you are just applying the filter to a png file and you are just outputting it as a png file as well.
$image = imagecreatefrompng('me.png');
imagefilter($image, IMG_FILTER_GRAYSCALE);
imagepng($image, ‘output.png');
imagedestroy($image);
?>
Brightness Filter :
And lets just discuss another example and in this case we will be increasing the brightness of the image. The following code would do your job.<?phpThe only change in this code compared to the code above is the third line where are say imagefilter($image, IMG_FILTER_BRIGHTNESS, 100); You could notice that we changed the second parameter so that it asks the compiler to change the brightness and not into a black and white image and as a third parameter we just specify the amount of brightness that we would want. You could use positive values to increase the brightness and negative values to decrease the brightness.
$image = imagecreatefromjpeg('me.jpg');
imagefilter($image, IMG_FILTER_BRIGHTNESS, 100);
imagejpeg($image, 'output.jpg');
imagedestroy($image);
?>
Okay. So like these two examples you could use a wide range of fitlers available. The following are the available ones.
• IMG_FILTER_NEGATE - Reverses all colors of the image.
• IMG_FILTER_GRAYSCALE - Converts the image into grayscale.
• IMG_FILTER_BRIGHTNESS - Changes brightness of the image (Takes one more argument)
• IMG_FILTER_CONTRAST - Changes the contrast of the image (Takes one more argument)
• IMG_FILTER_COLORIZE - Like IMG_FILTER_GRAYSCALE, except you can specify the color.
• 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 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 smoothy (Takes one more argument)
• IMG_FILTER_PIXELATE - Applies pixelation effect to the image (Takes two more arguments)
You can find the complete guide to use these effects from this PHP manual. So you could try out your own image filter effects. Happy coding :)

