Using PHP's imagefileter() Function

This is mostly an article for reference. It details many of the effects that can be achieved with the imagefilter function available since PHP 5. Here is the code that was used for all of the examples below.

  1. <?php
  2.   header("content-type: image/png");
  3.   $im = imagecreatefromjpeg("photo.jpg");
  4.   imagefilter($im, IMG_FILTER_TYPE, $args);
  5.   imagepng($im);
  6.   imagedestroy($im);
  7. ?>

I have found this wonderful picture at Stock Vault and used it for all of the manipulations. In order to speed up the loading of the page, I am not regenerating all of the images on-the-fly, every time the page is loaded, but rather have stored them on the server. Although you certainly could load them every single time if you wanted. Another option with this helpful function is manipulation is to save the file on the server, instead of or along with outputting it. This method is helpful when uploading files, for example. Again, here is the original photograph.

Original Photograph

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

Negative Effect

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

Grayscale Effect

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.

Grayscale & Negative Effect

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.

Brightness (Magnitute 100) Effect

Brightness (Magnitute 10) Effect

Brightness (Magnitute -10) Effect

Brightness (Magnitute -100) Effect

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.

Contrast (Magnitude 100) Effect

Contrast (Magnitude -50) Effect

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.

Grayscale & Contrast (Magnitude 15) Effect

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.

Colorize (Magnitudes: 200 red, 0 green, 0 blue, no alpha) Effect

Colorize (Magnitudes: 0 red, 200 green, 0 blue, 50 alpha) Effect

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

Edge Detect Effect

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

Emboss Effect

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

Gaussian Blur Effect

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

Selective Blur Effect

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

Mean Removal Effect

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.

Smoothing (Magnitude 50) Effect

Smoothing (Magnitude -50) Effect

I hope that this helps visualize what all of the different filter constants do with the imagefilter function.

TAGS: PHP, Image Manipulation