by Dinesh Thakur

PHP can manipulate the images for many years and for applying special effects to it, we were often forced to write many lines of code. Since PHP version 5, a new feature has appeared: "imagefilter" to achieve effects with the same quality as designs software.

This function allows you to customize the look of your site and also to achieve a different picture gallery of others.

So we'll see:

• The basics
• The ImageFilter () function
• How to play with some filters?

The basics

Before using these new effects, it is important to have the GD library enabled. If it is not active, know that it is in the php.ini file. To verify that the GD library works correctly, we will attempt to load an image and display it:

Load and display a JPG image
<?php
Header("Content-type: image/jpeg");
     $image =imagecreatefromjpeg ('guest.jpg'); // Load the image JPG
         imagejpeg ($image); // Displays the image
         imagedestroy ($image); // Frees the image
?>

Here is the result:

                                                        PHP Imagefilter (): special effects

The function ImageFilter

This function allows you to apply a filter on an image. It looks like this:

Imagefilter prototype of the function ()
imagefilter (resource $image, int $filtertype [, int $arg1 [, int $arg2 [, int $arg3]]])

Imagefilter () is divided into three criteria:

• Image: Image name
• filtertype: the type of filter of choice
• arg: the criteria, if necessary, in relation to the type of filter selected. The shade allows slice of -255 to 255

To illustrate our different examples, we will leave on the landscape we posted above.

The filter IMG_FILTER_BRIGHTNESS

This filter allows you to change the brightness of the image. We will only use the argument 1 (arg1). The possible value will be between -255 and 255 that represents:

• 255: Lighten the image with up to white (gloss effect)
• 0: Default. color unchanged
• -255: Darken the image up to the black (dark effect)

Example filter usage IMG_FILTER_BRIGHTNESS
<?php
Header("Content-type: image/jpeg");
     $filename = 'guest.jpg';
     $value = 0;
     $image =imagecreatefromjpeg ($filename);
         imagefilter ($image, IMG_FILTER_BRIGHTNESS, $value);
         imagejpeg ($image);
         imagedestroy ($image);
?>

The filter IMG_FILTER_COLORIZE

This filter allows you to change the color trends, ie subtract one color over another. We will use the three arguments, which represent the shades of Red, Green, Blue. Each of the values of its shades they are finding a range of -255 to 255.

Example filter usage IMG_FILTER_COLORIZE
<?php
Header("Content-type: image/jpeg");
     $filename = 'guest.jpg';
     $r = 100;
     $v = 0;
     $b = -50;
     $image =imagecreatefromjpeg ($filename);
         imagefilter ($image, IMG_FILTER_COLORIZE, $r, $v, $b);
         imagejpeg ($image);
         imagedestroy ($image);
?>

The filter IMG_FILTER_CONTRAST

This filter changes the contrast of the image. To determine this contrast, we use the argument 1 (arg1). Its value is in the range -255 to 255 where:

• 255: Lighten the image with up to white (gloss effect)
• 0: Default. color unchanged
• -255: Darken the image up to the black (dark effect)

Example filter usage IMG_FILTER_CONTRAST
<?php
Header("Content-type: image/jpeg");
     $filename = 'guest.jpg';
     $value = -50;
     $image =imagecreatefromjpeg ($filename);
        imagefilter ($image, IMG_FILTER_CONTRAST, $value);
        imagejpeg ($image);
        imagedestroy ($image);
?>

The filter IMG_FILTER_EDGEDETECT

This filter uses edge detection to highlight in the image. No argument is used as this filter is applied to the entire image.

Example filter usage IMG_FILTER_EDGEDETECT
<?php
Header("Content-type: image/jpeg");
     $filename = 'guest.jpg';
     $image =imagecreatefromjpeg ($filename);
         imagefilter ($image, IMG_FILTER_EDGEDETECT);
         imagejpeg ($image);
         imagedestroy ($image);
?>

The filter IMG_FILTER_EMBOSS

This filter can burn the image in relief. No argument is used as the filter is applied on the entire imag.

Example filter usage IMG_FILTER_EMBOSS
<?php
Header("Content-type: image/jpeg");
      $filename = 'guest.jpg';
      $image =imagecreatefromjpeg ($filename); 
         imagefilter ($image, IMG_FILTER_EMBOSS);
         imagejpeg ($image);
         imagedestroy ($image);
?>

The filter IMG_FILTER_GAUSSIAN_BLUR

This filter allows you to blur the image using the Gaussian method. No argument is used as this filter is applied to the entire image. It may be associated with IMG_FILTER_SELECTIVE_BLUR filter.

Example filter usage IMG_FILTER_GAUSSIAN_BLUR
<?php
Header("Content-type: image/jpeg");
     $filename = 'guest.jpg';
     $image =imagecreatefromjpeg ($filename);
         imagefilter ($image, IMG_FILTER_GAUSSIAN_BLUR);
         imagejpeg ($image);
         imagedestroy ($image);
?>

The filter IMG_FILTER_SELECTIVE_BLUR

This filter can blur an image. No argument is used as this filter is applied to the entire image. It may be associated with IMG_FILTER_GAUSSIAN_BLUR filter .

Example filter usage IMG_FILTER_SELECTIVE_BLUR
<?php
Header("Content-type: image/jpeg");
     $filename = 'guest.jpg';
     $image =imagecreatefromjpeg ($filename);
         imagefilter ($image, IMG_FILTER_SELECTIVE_BLUR);
         imagejpeg ($image);
         imagedestroy ($image);
?>

The filter IMG_FILTER_SMOOTH

This filter smoothes the image. We will use the argument 1 (arg1) to determine the degree of smoothing. The possible value will be between -255 and 255 that represents:

• 255: Lighten the image with up to white (gloss effect)
• 0: Default. color unchanged
• -255: Darken the image up to the black (dark effect)

Example filter usage IMG_FILTER_SMOOTH
<?php
Header("Content-type: image/jpeg");
      $filename = 'guest.jpg';
      $value = 50;
      $image =imagecreatefromjpeg ($filename);
          imagefilter ($image, IMG_FILTER_SMOOTH, 10);
          imagejpeg ($image);
          imagedestroy ($image);
?>

The filter IMG_FILTER_GRAYSCALE

This filter converts the image to black and white, that is, that we find the same opportunities as the IMG_FILTER_COLORIZE filter, except that here, we can not
choose the color.

Example filter usage IMG_FILTER_GRAYSCALE
<?php
Header("Content-type: image/jpeg");
     $filename = 'guest.jpg';
     $image =imagecreatefromjpeg ($filename);
         imagefilter ($image, IMG_FILTER_GRAYSCALE);
         imagejpeg ($image);
         imagedestroy ($image);
?>

The filter IMG_FILTER_NEGATE

This filter allows to reverse all the colors of the image to make it negative.

Example filter usage IMG_FILTER_NEGATE
<? php
Header("Content-type: image/jpeg");
     $filename = 'guest.jpg';
     $image =imagecreatefromjpeg ($filename);
         imagefilter ($image, IMG_FILTER_NEGATE);
         imagejpeg ($image);
         imagedestroy ($image);
?>

The filter IMG_FILTER_MEAN_REMOVAL

This filter lets you apply a noise effect to the image.

Example filter usage IMG_FILTER_MEAN_REMOVAL
<?php
Header("Content-type: image/jpeg");
     $filename = 'guest.jpg';
     $image =imagecreatefromjpeg ($filename);
         imagefilter ($image, IMG_FILTER_MEAN_REMOVAL);
         imagejpeg ($image);
         imagedestroy ($image);
?>

Now that we have seen all the possible filters for this function, we can apply multiple filters to a single image.

Let's play with some types of filters

We will, for each presented effect from a different original image. The first effect consist in the separation of RGB colors of the image. The second script we discover flattening our image. Our third test will aim to apply a blur, and then we will close this tutorial by creating a monochrome effect. Let's study the first effect:

Effect 1: Separating colors

To achieve this effect very common in software designs, we will apply two operations to the image. First, convert the black and white image with the IMG_FILTER_GRAYSCALE filter and recoloriser hue of choice with IMG_FILTER_COLORIZE filter.

RGB color separation of an image script
<?php
Header("Content-type: image/jpeg");
    // Red
    $r = 255; $v = 0; $b = 0;

   // Green
    $r = 0; $v = 255; $b = 0;

   // Blue
    $r = 0; $v = 0; $b = 255;


    $filename = 'guest.jpg';
    $image =imagecreatefromjpeg ($filename);
       imagefilter ($image, IMG_FILTER_GRAYSCALE);
       imagefilter ($image, IMG_FILTER_COLORIZE, $r, $v, $b);
       imagejpeg ($image);
       imagedestroy ($image);
?>

As you can see, you need to run the script three recovery commantant whenever two of the three lines of code number 4, 7 and 10.

Effect 2 Effect Flatten

To achieve this flattening effect that can approach an engraving effect, we must break the algorithm into two elementary operations.

• Detecting differences in the image with the filter IMG_FILTER_EDGEDETECT
• Transforming the relief image with the filter IMG_FILTER_EMBOSS

Flattening script example of an image
<?php
Header("Content-type: image/jpeg");
    $filename = 'afup.jpg';
    $image =imagecreatefromjpeg ($filename);
        imagefilter ($image, IMG_FILTER_EDGEDETECT);
        imagefilter ($image, IMG_FILTER_EMBOSS);
        imagejpeg ($image);
        imagedestroy ($image);
?>

Effect 3: Soft Snap

This effect shows how to create a blur effect. We will use two filters to achieve our goals:

• Blur the image with the filter IMG_FILTER_GAUSSIAN_BLUR
• Scrambling a little with the filter IMG_FILTER_SELECTIVE_BLUR

Script embodiment of a blur on an image
<?php
Header("Content-type: image/jpeg");
     $filename = 'phplogo.jpg';
     $image =imagecreatefromjpeg ($filename);
         imagefilter ($image, IMG_FILTER_GAUSSIAN_BLUR);
         imagefilter ($image, IMG_FILTER_SELECTIVE_BLUR);
         imagejpeg ($image);
         imagedestroy ($image);
?>

Effect 4: monochrome effect

We will perform the operation in 2 phases:

• Transform a gray image with the filter IMG_FILTER_GRAYSCALE
• Highlight color levels with the filter IMG_FILTER_NEGATE

Blur creation script on an image
<?php
Header("Content-type: image/jpeg");
     $filename = 'guest.jpg';
     $image =imagecreatefromjpeg ($filename);
         imagefilter ($image, IMG_FILTER_GRAYSCALE);
         imagefilter ($image, IMG_FILTER_NEGATE);
         imagejpeg ($image);
         imagedestroy ($image);
?>