How To Apply Special Effects To Your Images Using PHP Cover Image

How To Apply Special Effects To Your Images Using PHP

Published:

Rate Article (Sign in and be the first to rate this article.)

Article Details

Views: 3839

First Published:

Word Count: 3674

Edition: 1

Now in-order to apply special effects to an image or in other words a filter to an image using PHP you will need to use PHP's imagefilter() function, which was introduced to us in PHP 5. You should also take note that the imagefilter() function is only available if PHP is compiled with the bundled version of the GD library. The GD library is basically used for dynamic image creation. The GD library is an open source code library for the dynamic creation of images by programmers. The Letters GD originally stood for "GIF Draw". But, since Unisys revoked the royalty-free license, it has informally stood for "Graphics Draw", alright that's enough info about the GD library.

The imagefilter() function applies a given filter type to an image. When the imagefilter() function successfully applies a filter to an image it will return TRUE on success or FALSE on failure. You won't be able to see anything unless you echo the result of the imagefilter() function, at which point the boolean value will be converted to a string, and then will read true or false. The imagefilter() function has six parameters each of which that do very different things.

Now let me list and explain the imagefilter() functions six parameters below:

image

The image parameter states an image resource, that is returned by one of the image creation functions, such as the imagecreatefromgif(), imagecreatetruecolor() or the imagecreatefromjpeg() to name a few.

Here is how to code the image parameter for the imagefilter() function below.


<?php

$image = imagecreatefrompng('logo.png'); 
imagefilter($image, IMG_FILTER_BRIGHTNESS, 13);  

?>

filtertype

The filtertype parameter can be one of twelve filter type values also known as constants. The filtertype parameters values are all in uppercase and are case-sensitive by default. You should also be aware that some filtertype parameter values may also need additional parameters which will very for each filtertype parameter value. If this sounds confusing it will all make sense by the end of this tutorial.

The first filtertype parameter value is the IMG_FILTER_NEGATE which reverses all the colors in the image. Also known as a negative image, which means the red areas in the image appear cyan, green areas in the image appear magenta and the blue areas appear yellow.

The second filtertype parameter value is the IMG_FILTER_GRAYSCALE which will convert the image into grayscale. Which basically means that the images colors are made up of only shades of gray.

The third value for the filtertype parameter is the IMG_FILTER_BRIGHTNESS which will change the brightness of the image. You will also need to include a third parameter to set the level of brightness, which can range from any number between -255 and 255. The numbers represent how much you want to brighten or darken the image. The number 255 will set the image to full white, which is the brightest value possible, and the number -255 will set the image to full black, which is the darkest value possible, and if you choose to set the parameters value to the number 0 the image will be unchanged. I will show you how to add the third parameter later on this tutorial.

The fourth filtertype parameter value is the IMG_FILTER_CONTRAST which will change the contrast of the image. You will also need to include a third parameter that can have a number value that can range from any number between -255 and 255, which will adjust the contrast level of the image. When using positive number values for the third parameter, will bring the images colors closer together by mixing them with the color gray, you should take note that your image will be fully gray at about 100. And when using lower number values for the third parameter, will increase the contrast of the image, essentially reducing the number of colors so that they are more separate and obvious to the eye. And if you choose to set the parameters value to the number 0 the image will be unchanged. I will show you how to add the third parameter later on this tutorial.

Now the fifth value for the filtertype parameter is the IMG_FILTER_COLORIZE which will add or subtract the RGB colors red, green or blue from every pixel of the image. You will need to include three additional parameters that can have a number value that can range from any number between -255 and 255. The first parameters value controls the color red, for example, setting the first parameters value to 255 will add the color red to every pixel. The second parameter controls the color green, if you set the second parameter value to 255 every pixel will have the color green added to it. And the third parameters value controls the color blue, for instance, if you set the third parameters value to -255 it will take all the blue out of all the pixels in the image.

When using the IMG_FILTER_COLORIZE all the three additional parameters are needed, for instance if you just want to control the second parameter for the color green and don't want to change the other colors parameter values all you have to do is give the first and third parameters the number 0 to leave the images red and blue colors unchanged.

An Important thing to remember is that the IMG_FILTER_COLORIZE also has a fourth parameter that you can use if you want to control the alpha channel. I will explain this parameter in more detail later on in this tutorial.

The sixth value for the filtertype parameter is the IMG_FILTER_EDGEDETECT which uses edge detection to highlight the edges in the image and set the other areas to gray.

The seventh value for the filtertype parameter is the IMG_FILTER_EMBOSS which embosses the image and sets the other areas to gray.

The eighth value for the filtertype parameter is the IMG_FILTER_GAUSSIAN_BLUR which blurs the image using the Gaussian blur method.

Now the ninth value for the filtertype parameter is the IMG_FILTER_SELECTIVE_BLUR which is a generic blur function that blurs the image.

The tenth value for the filtertype parameter is the IMG_FILTER_MEAN_REMOVAL which uses the mean removal to achieve a "sketchy" like effect.

The eleventh value for the filtertype parameter is the IMG_FILTER_SMOOTH which will make the image smoother. You will need to include one additional parameter that really doesn't specify how much the image should be smoothed, but instead it indicates how much weighting is needed for an image manipulation matrix. So in other words the parameters value applies the weight to the center pixel of the image. Another way of putting it is that the IMG_FILTER_SMOOTH basically applies a 9-cell convolution matrix where the center pixel out of the 9 pixels has the weight specified by the additional parameter. The additional parameter can have a value that can be any positive (13) or negative (-13) number, you may even use float numbers (13.2).

In practice when the additional parameters value is set between -6 to -8 you will get some interesting effects. When the value is set at 10 the image is about normal, but the result will not be that visible, this is because the original pixel values have more weight than the total combined sum of its neighboring pixels. It's suggested that the additional parameters value should not be outside the range of -8 to 8, because the result will not be completely visible.

Now the twelfth value for the filtertype parameter is the IMG_FILTER_PIXELATE which was added to PHP in version 5.3.0. The IMG_FILTER_PIXELATE value applies a pixelation effect to the image. You will need to include two additional parameters as well. The first parameters value can be any positive number, which will state the block size in pixels. And the second parameter doesn't have to be included but if included the value can be any boolean value you choose, for example, true or false or 0 for false or 1 for true, which sets the pixelation effect mode. The default value for the second parameter even when the parameter is not included is false.

Now here is how to code the filtertype parameter using the IMG_FILTER_EDGEDETECT value for the imagefilter() function below.


<?php

$image = imagecreatefrompng('logo.png'); 
imagefilter($image, IMG_FILTER_EDGEDETECT);  

?>

arg1

The arg1 parameter is short for argument1 and it can be used with the following filtertype parameter values which are IMG_FILTER_BRIGHTNESS, IMG_FILTER_CONTRAST, IMG_FILTER_COLORIZE, IMG_FILTER_SMOOTH and IMG_FILTER_PIXELATE. I will explain how to use the arg1 parameter with each of the allowed filtertype parameter values below.

When the arg1 parameter is used with the filtertype parameter value IMG_FILTER_BRIGHTNESS. The arg1 parameters value will set the level of brightness of the image. The arg1 parameters value can be any number between -255 and 255, the number 255 will set the image to full white, and the negative number -255 will set the image to full black. The number 0 will leave the image unchanged.

Now when the arg1 parameter is used with the filtertype parameter value IMG_FILTER_CONTRAST. The arg1 parameters value will adjust the contrast level of the image. The arg1 parameters value can be any number between -255 and 255, positive number values for the arg1 parameter will bring the images colors closer together, and lower number values for the arg1 parameter will increase the contrast of the image, which reduces the number of colors so that they are more separate and obvious to the eye. The number 0 will leave the image unchanged.

Now when the arg1 parameter is used with the filtertype parameter value IMG_FILTER_COLORIZE. The arg1 parameters value will control the color red in the image. The arg1 parameters value can be any number between -255 and 255, positive number values will increase the amount of the color red in the image and negative number values will decrease the amount of red in the image. The number 0 will leave the image unchanged.

When the arg1 parameter is used with the filtertype parameter value IMG_FILTER_SMOOTH. The arg1 parameters value really won't actually specify how much the image should be smoothed, but instead will indicate how much weighting is needed for an image manipulation matrix. The arg1 parameter can have a value that can be any positive (13) or negative (-13) number, you may even use float numbers (13.2) as well. When the arg1 parameter has a value between -6 to -8 you will get some interesting effects applied to your image. It's been suggested that the arg1 parameters value should not be outside the range of -8 to 8, because the result will not be completely visible. The image is about normal at 10, because the original pixel values have more weight than the total combined sum of its neighboring pixels.

When the arg1 parameter is used with the filtertype parameter value IMG_FILTER_PIXELATE. The arg1 parameters value can be any positive number, which will state the block size in pixels.

Here is how to code the arg1 parameter for the filtertype parameter value IMG_FILTER_CONTRAST for the imagefilter() function below.


<?php

$image = imagecreatefrompng('logo.png'); 
imagefilter($image, IMG_FILTER_CONTRAST, 40);  

?>

arg2

The arg2 parameter is short for argument2 and it can be used with the following filtertype parameter values IMG_FILTER_COLORIZE and IMG_FILTER_PIXELATE. I will explain how to use the arg2 parameter with each of the allowed filtertype parameter values below.

Now when the arg2 parameter is used with the filtertype parameter value IMG_FILTER_COLORIZE, the arg2 parameters value will control the color green in the image. The arg2 parameters value can be any number between -255 and 255, positive number values will increase the amount of the color green in the image and negative number values will decrease the amount of green in the image. The number 0 will leave the image unchanged.

When the arg2 parameter is used with the filtertype parameter value IMG_FILTER_PIXELATE, the arg2 parameters value can be any boolean value you choose which can be true, false or 0 for false and 1 for true, which sets the pixelation effect mode. You should be aware that the arg2 parameter doesn't have to be included. The default value for the arg2 parameter even when the arg2 parameter is not included is false.

Here is how to code the arg2 parameter for the filtertype parameter value IMG_FILTER_PIXELATE for the imagefilter() function below.


<?php

$image = imagecreatefrompng('logo.png'); 
imagefilter($image, IMG_FILTER_PIXELATE, 13, true);  

?>

arg3

Now the arg3 parameter is short for argument3 and it can be used with the following filtertype parameter value IMG_FILTER_COLORIZE.

Now when the arg3 parameter is used with the filtertype parameter value IMG_FILTER_COLORIZE, the arg3 parameters value will control the color blue in the image. The arg3 parameters value can be any number between -255 and 255, positive number values will increase the amount of the color blue in the image and negative number values will decrease the amount of blue in the image. The number 0 will leave the image unchanged.

Here is how to code the arg3 parameter for the filtertype parameter value IMG_FILTER_COLORIZE for the imagefilter() function below.


<?php

$image = imagecreatefrompng('logo.png'); 
imagefilter($image, IMG_FILTER_COLORIZE, 13, 0, 180);  

?>

arg4

And the last parameter is the arg4 parameter which is short for argument4 and it can be used with the following filtertype parameter value IMG_FILTER_COLORIZE.

Now when the arg4 parameter is used with the filtertype parameter value IMG_FILTER_COLORIZE, the arg4 parameters value will control the alpha channel. So basically the alpha channel controls the transparency of all the other channels, which are the RGB values specified in the arg1, arg2 and arg3 parameters. By adding the alpha channel to the image by including the arg4 parameter you control the transparency of the red channel, green channel and the blue channel. The arg4 parameter can have any number value from 0 to 127. The value 0 will indicate completely opaque while the value 127 indicates completely transparent.

Here is how to code the arg4 parameter for the filtertype parameter value IMG_FILTER_COLORIZE for the imagefilter() function below.


<?php

$image = imagecreatefrompng('logo.png'); 
imagefilter($image, IMG_FILTER_COLORIZE, 13, 0, 180, 40);  

?>

Now that you know more about the imagefilter() function and its parameters let me show you how to code each filtertype parameter value as well as show you how each filtertype parameter value affects your images.

Now here is how to code the filtertype parameter using the IMG_FILTER_NEGATE value for the imagefilter() function below.


<?php

$image = imagecreatefromjpeg('sun.jpg'); 
imagefilter($image, IMG_FILTER_NEGATE); 
imagejpeg($image, 'negate-sun.jpg'); 
imagedestroy($image); 
echo '<img src="negate-sun.jpg" alt="Image With A Negate Effect Applied" />';  

?>

The above code will display the following image below on the right, the image on the left is the original image for you to compare with the filtered image on the right that PHP generated using the filtertype parameter value IMG_FILTER_NEGATE for the imagefilter() function.

  1. Original Sun Image
  2. Image With A Negate Effect Applied

Now here is how to code the filtertype parameter using the IMG_FILTER_GRAYSCALE value for the imagefilter() function below.


<?php

$image = imagecreatefromjpeg('sun.jpg'); 
imagefilter($image, IMG_FILTER_GRAYSCALE); 
imagejpeg($image, 'grayscale-sun.jpg'); 
imagedestroy($image); 
echo '<img src="grayscale-sun.jpg" alt="Image With A Grayscale Effect Applied" />';  

?>

The above code will display the following image below on the right, the image on the left is the original image for you to compare with the filtered image on the right that PHP generated using the filtertype parameter value IMG_FILTER_GRAYSCALE for the imagefilter() function.

  1. Original Sun Image
  2. Image With A Grayscale Effect Applied

Now here is how to code the filtertype parameter using the IMG_FILTER_BRIGHTNESS value for the imagefilter() function below.


<?php

$image = imagecreatefromjpeg('sun.jpg'); 
imagefilter($image, IMG_FILTER_BRIGHTNESS, 120); 
imagejpeg($image, 'brightness-sun.jpg'); 
imagedestroy($image); 
echo '<img src="brightness-sun.jpg" alt="Image With A Brightness Effect Applied" />';  

?>

The above code will display the following image below on the right, the image on the left is the original image for you to compare with the filtered image on the right that PHP generated using the filtertype parameter value IMG_FILTER_BRIGHTNESS for the imagefilter() function.

  1. Original Sun Image
  2. Image With A Brightness Effect Applied

Now here is how to code the filtertype parameter using the IMG_FILTER_CONTRAST value for the imagefilter() function below.


<?php

$image = imagecreatefromjpeg('sun.jpg'); 
imagefilter($image, IMG_FILTER_CONTRAST, -80); 
imagejpeg($image, 'contrast-sun.jpg'); 
imagedestroy($image); 
echo '<img src="contrast-sun.jpg" alt="Image With A Contrast Effect Applied" />';  

?>

The above code will display the following image below on the right, the image on the left is the original image for you to compare with the filtered image on the right that PHP generated using the filtertype parameter value IMG_FILTER_CONTRAST for the imagefilter() function.

  1. Original Sun Image
  2. Image With A Contrast Effect Applied

Now here is how to code the filtertype parameter using the IMG_FILTER_COLORIZE value for the imagefilter() function below.


<?php

$image = imagecreatefromjpeg('sun.jpg'); 
imagefilter($image, IMG_FILTER_COLORIZE, 80, 160, -40); 
imagejpeg($image, 'colorize-sun.jpg'); 
imagedestroy($image); 
echo '<img src="colorize-sun.jpg" alt="Image With A Colorize Effect Applied" />';  

?>

The above code will display the following image below on the right, the image on the left is the original image for you to compare with the filtered image on the right that PHP generated using the filtertype parameter value IMG_FILTER_COLORIZE for the imagefilter() function.

  1. Original Sun Image
  2. Image With A Colorize Effect Applied

Now here is how to code the filtertype parameter using the IMG_FILTER_EDGEDETECT value for the imagefilter() function below.


<?php

$image = imagecreatefromjpeg('sun.jpg'); 
imagefilter($image, IMG_FILTER_EDGEDETECT); 
imagejpeg($image, 'edgedetect-sun.jpg'); 
imagedestroy($image); 
echo '<img src="edgedetect-sun.jpg" alt="Image With An Edgedetect Effect Applied" />';  

?>

The above code will display the following image below on the right, the image on the left is the original image for you to compare with the filtered image on the right that PHP generated using the filtertype parameter value IMG_FILTER_EDGEDETECT for the imagefilter() function.

  1. Original Sun Image
  2. Image With An Edgedetect Effect Applied

Now here is how to code the filtertype parameter using the IMG_FILTER_EMBOSS value for the imagefilter() function below.


<?php

$image = imagecreatefromjpeg('sun.jpg'); 
imagefilter($image, IMG_FILTER_EMBOSS); 
imagejpeg($image, 'emboss-sun.jpg'); 
imagedestroy($image); 
echo '<img src="emboss-sun.jpg" alt="Image With An Emboss Effect Applied" />';  

?>

The above code will display the following image below on the right, the image on the left is the original image for you to compare with the filtered image on the right that PHP generated using the filtertype parameter value IMG_FILTER_EMBOSS for the imagefilter() function.

  1. Original Sun Image
  2. Image With An Emboss Effect Applied

Now here is how to code the filtertype parameter using the IMG_FILTER_GAUSSIAN_BLUR value for the imagefilter() function below.


<?php

$image = imagecreatefromjpeg('sun.jpg'); 
imagefilter($image, IMG_FILTER_GAUSSIAN_BLUR); 
imagejpeg($image, 'gaussian-blur-sun.jpg'); 
imagedestroy($image); 
echo '<img src="gaussian-blur-sun.jpg" alt="Image With A Gaussian Blur Effect Applied" />';  

?>

The above code will display the following image below on the right, the image on the left is the original image for you to compare with the filtered image on the right that PHP generated using the filtertype parameter value IMG_FILTER_GAUSSIAN_BLUR for the imagefilter() function.

  1. Original Sun Image
  2. Image With A Gaussian Blur Effect Applied

Now here is how to code the filtertype parameter using the IMG_FILTER_SELECTIVE_BLUR value for the imagefilter() function below.


<?php

$image = imagecreatefromjpeg('sun.jpg'); 
imagefilter($image, IMG_FILTER_SELECTIVE_BLUR); 
imagejpeg($image, 'selective-blur-sun.jpg'); 
imagedestroy($image); 
echo '<img src="selective-blur-sun.jpg" alt="Image With A Selective Blur Effect Applied" />';  

?>

The above code will display the following image below on the right, the image on the left is the original image for you to compare with the filtered image on the right that PHP generated using the filtertype parameter value IMG_FILTER_SELECTIVE_BLUR for the imagefilter() function.

  1. Original Sun Image
  2. Image With A Selective Blur Effect Applied

Now here is how to code the filtertype parameter using the IMG_FILTER_MEAN_REMOVAL value for the imagefilter() function below.


<?php

$image = imagecreatefromjpeg('sun.jpg'); 
imagefilter($image, IMG_FILTER_MEAN_REMOVAL); 
imagejpeg($image, 'mean-removal-sun.jpg'); 
imagedestroy($image); 
echo '<img src="mean-removal-sun.jpg" alt="Image With A Mean Removal Effect Applied" />';  

?>

The above code will display the following image below on the right, the image on the left is the original image for you to compare with the filtered image on the right that PHP generated using the filtertype parameter value IMG_FILTER_MEAN_REMOVAL for the imagefilter() function.

  1. Original Sun Image
  2. Image With A Mean Removal Effect Applied

Now here is how to code the filtertype parameter using the IMG_FILTER_SMOOTH value for the imagefilter() function below.


<?php

$image = imagecreatefromjpeg('sun.jpg'); 
imagefilter($image, IMG_FILTER_SMOOTH, -8); 
imagejpeg($image, 'smooth-sun.jpg'); 
imagedestroy($image); 
echo '<img src="smooth-sun.jpg" alt="Image With A Smooth Effect Applied" />';  

?>

The above code will display the following image below on the right, the image on the left is the original image for you to compare with the filtered image on the right that PHP generated using the filtertype parameter value IMG_FILTER_SMOOTH for the imagefilter() function.

  1. Original Sun Image
  2. Image With A Smooth Effect Applied

Now here is how to code the filtertype parameter using the IMG_FILTER_PIXELATE value for the imagefilter() function below.


<?php

$image = imagecreatefromjpeg('sun.jpg'); 
imagefilter($image, IMG_FILTER_PIXELATE, 13); 
imagejpeg($image, 'pixelate-sun.jpg'); 
imagedestroy($image); 
echo '<img src="pixelate-sun.jpg" alt="Image With A Pixelate Effect Applied" />';  

?>

The above code will display the following image below on the right, the image on the left is the original image for you to compare with the filtered image on the right that PHP generated using the filtertype parameter value IMG_FILTER_PIXELATE for the imagefilter() function.

  1. Original Sun Image
  2. Image With A Pixelate Effect Applied

If you need more information about the imagefilter() function check out the PHP manual at http://www.php.net/manual/en/function.imagefilter.php

Article Tags

Tag Article (Sign in if you want to tag this article.)

Share This Article

Send To Facebook Link

Tweet This Link

Send To LinkedIn Link

Send To StumbleUpon Link

Digg This Link

Send To Google Plus One Share Link

Send To Reddit Link

Send To Tumblr Link

Send To Delicious Link

Send To FriendFeed Link

Send To Diigo Link

Email Link

AddThis Link

0 Comments

Leave Comment (Sign in and be the first to comment on this article.)

Important

This site cannot guarantee the accuracy of articles, comments, answers and other types of content or media submitted by members or anonymous users and guests, and we recommend that you use common sense when following any advice or information found on this site. Read our full Content Disclaimer Agreement for further information.