Filtering Image Streams with the GD Library in PHP

Building and processing dynamic images with PHP is a procedure that can be easily tackled with the GD extension. If you want to learn how to put its main functions to work for you, then you should start reading this tutorial right now!

Introduction

Welcome to the final installment of the series that began with "A Close Look at the GD Library in PHP." Comprised of five articles, this series introduces the most common operations that you can perform with this graphic-processing package. These range from creating basic image streams from scratch to performing more complex operations such as drawing shapes and filtering graphics.

As usual with many of my articles on web development, before I move on and continue exploring some additional functions integrated with the GD extension, I’d like to refresh the topics treated in the previous article of the series. This will give you a better idea of how the respective functions discussed in that tutorial can be linked with the ones that I plan to cover in the next few sentences.

Having said that, you’ll surely recall that in the preceding article of the series I showed you how to display on the browser some basic filled shapes, including simple rectangles, ellipses and more complex polygons. In addition, I explained how to utilize the handy "imagex()", "imagey()" and "getimagesize()" functions to retrieve the width and height values for a given image stream, as well as determining its MIME type. As you can see, the GD extension has plenty of power when it comes to fetching useful data about a particular graphic, whether that graphic was created dynamically or generated from an existing picture.

All right, having summarized the concepts that I covered so far, in this article I’m going to demonstrate how the GD library can be used to apply some basic graphic filters (yes, like Photoshop, but much simpler) by utilizing the versatility of a brand new function called "imagefilter()".

As you’ll see shortly, with this function it is possible to reverse, blur and highlight the colors of a determined image, as well as convert it to its grayscale version, among other things. Sounds pretty interesting, right?

So, are you ready to find out how to apply some basic graphic filters to a given image stream using the GD extension? Okay, let’s get started!

{mospagebreak title=Manipulating the colors of an image stream}

As I stated in the beginning of this article, the GD extension provides PHP developers with a powerful function named "imagefilter()" for applying some primitive filters to a specified image stream.

The function in question supports a variety of graphic filters, but I’m only going to cover the most useful of them. Now that I have outlined basically how this function works, please have a look at the following pair of hands-on examples. They illustrate how to reverse the colors of a specified image, and how to convert it to grayscale.

Assuming that the following input image is used by the pertinent examples:

Then here are respective code samples that apply some basic filters to the image in question:

// example of ‘imagefilter()’ function – Reverses all colors of
the image

try{
   if(!$image=imagecreatefromgif(‘clouds.gif’)){
     throw new Exception(‘Error creating image’);
   }
   // apply filter to image
   if(!imagefilter($image,IMG_FILTER_NEGATE)){
     throw new Exception(‘Error applying filter to image’);
   }
   // display image to the browser
   header("Content-type: image/gif");
   imagegif($image);
   // free memory
   imagedestroy($image);

}
catch(Exception $e){
   echo $e->getMessage();
   exit();
}

// example of ‘imagefilter()’ function – Converts the image into
grayscale

try{
   if(!$image=imagecreatefromgif(‘clouds.gif’)){
     throw new Exception(‘Error creating image’);
   }
   // apply filter to image
   if(!imagefilter($image,IMG_FILTER_GRAYSCALE)){
     throw new Exception(‘Error applying filter to image’);
   }
   // display image to the browser
   header("Content-type: image/gif");
   imagegif($image);
   // free memory
   imagedestroy($image);
   
}
catch(Exception $e){
   echo $e->getMessage();
   exit();
}

As you can see, the processes of reversing the colors of a given image stream and converting an image to its grayscale version are very simple, and can performed with minor hassles utilizing the neat "imagefilter()" function. As shown above, this function takes the type of filter to be applied to the image stream as an input argument. When it performs a successful operation, it returns to client code the corresponding filtered version. Quite simple, right?

Now that you have grasped the way that this handy function works, it’s time to develop more practical examples that show how to use the same function for applying some additional filters to the sample image.

To see these brand new filters in action, please click on the link below and keep reading.

{mospagebreak title=Controlling brightness and contrast with the imagefilter() function}

In the section that you just read, I explained how to use the imagefilter() function to apply some basic graphic filters to an existing image stream. However, the versatility exposed by this function allows you to implement many other popular filters as well. 

Below I coded another pair of practical examples. They show how to use this helpful function to control the brightness and contrast of a specified image stream. In these examples I used the same sample image that you saw in the previous section.

Given that, here are the respective signatures for these code samples:

// example of ‘imagefilter()’ function – Changes the brightness
of the image

try{
   if(!$image=imagecreatefromgif(‘clouds.gif’)){
     throw new Exception(‘Error creating image’);
   }
   // apply filter to image
   if(!imagefilter($image,IMG_FILTER_BRIGHTNESS,30)){
     throw new Exception(‘Error applying filter to image’);
   }
   // display image to the browser
   header("Content-type: image/gif");
   imagegif($image);
   // free memory
   imagedestroy($image);

}
catch(Exception $e){
    echo $e->getMessage();
    exit();
}

// example of ‘imagefilter()’ function – Changes the contrast of
the image

try{
   if(!$image=imagecreatefromgif(‘clouds.gif’)){
     throw new Exception(‘Error creating image’);
   }
   // apply filter to image
   if(!imagefilter($image,IMG_FILTER_CONTRAST,30)){
     throw new Exception(‘Error applying filter to image’);
   }
   // display image to the browser
   header("Content-type: image/gif");
   imagegif($image);
   // free memory
   imagedestroy($image);
   
}
catch(Exception $e){
    echo $e->getMessage();
    exit();
}

Definitely, after studying the signature for the above examples, you’ll have to admit that the "imagefilter()" function that comes with the GD library is extremely useful for controlling some aspects of a selected image stream, such as its brightness and contrast. In this case you can see that the function accepts the type of filter to be applied, along with its intensity, as an input argument.

So far, so good, right? I hope you see the great potential of the "imagefilter()" function. It will let you apply a bunch of popular graphic filters to a selected image stream, which can be a real time saver for those PHP applications that fetch multiple images from one or more database tables.

Nevertheless, if you’re actually thinking that the capacity of this function is only limited to applying the set of filters that you saw in the previous examples, I’m afraid that you’re wrong, since it has many others that can be really handy.

If you’re interested in learning how to use the remaining filters that can be applied with the "imagefilter()" function, click the link below and read the next section. You won’t be disappointed, trust me.

{mospagebreak title=More graphic filters with the imagefilter() function}

As I mentioned in the previous section, the "imagefilter()" function provides PHP developers with a number of other graphic filters for controlling certain characteristics of a selected image stream. Of course, as you might guess, the application of these additional graphic filters can be easily performed by specifying the type of effect that must be implemented when calling the function.

Below I coded some illustrative examples which demonstrate how to apply different graphic filters to the same sample image that you saw in the previous section. You should notice that each code sample is accompanied by the respective resulting image after applying the appropriate filter.

This being said, here are the aforementioned examples, so take some time and have a look at them, please:

// example of ‘imagefilter()’ function – Colorizes the image

try{
   if(!$image=imagecreatefromgif(‘clouds.gif’)){
     throw new Exception(‘Error creating image’);
   }
   // apply filter to image
   if(!imagefilter($image,IMG_FILTER_COLORIZE,255,0,0)){
     throw new Exception(‘Error applying filter to image’);
   }
   // display image to the browser
   header("Content-type: image/gif");
   imagegif($image);
   // free memory
   imagedestroy($image);

}
catch(Exception $e){
   echo $e->getMessage();
   exit();
}

// example of ‘imagefilter()’ function – Uses edge detection to
highlight the edges in the image

try{
   if(!$image=imagecreatefromgif(‘clouds.gif’)){
     throw new Exception(‘Error creating image’);
   }
   // apply filter to image
   if(!imagefilter($image,IMG_FILTER_EDGEDETECT)){
     throw new Exception(‘Error applying filter to image’);
   }
   // display image to the browser
   header("Content-type: image/gif");
   imagegif($image);
   // free memory
   imagedestroy($image);

}
catch(Exception $e){
   echo $e->getMessage();
   exit();
}

// example of ‘imagefilter()’ function – Embosses the image

try{
   if(!$image=imagecreatefromgif(‘clouds.gif’)){
     throw new Exception(‘Error creating image’);
   }
   // apply filter to image
   if(!imagefilter($image,IMG_FILTER_EMBOSS)){
     throw new Exception(‘Error applying filter to image’);
   }
   // display image to the browser
   header("Content-type: image/gif");
   imagegif($image);
   // free memory
   imagedestroy($image);

}
catch(Exception $e){
   echo $e->getMessage();
   exit();
}

// example of ‘imagefilter()’ function – Blurs the image using
the Gaussian method

try{
   if(!$image=imagecreatefromgif(‘clouds.gif’)){
     throw new Exception(‘Error creating image’);
   }
   // apply filter to image
   if(!imagefilter($image,IMG_FILTER_GAUSSIAN_BLUR)){
     throw new Exception(‘Error applying filter to image’);
   }
   // display image to the browser
   header("Content-type: image/gif");
   imagegif($image);
   // free memory
   imagedestroy($image);

}
catch(Exception $e){
   echo $e->getMessage();
   exit();
}

// example of ‘imagefilter()’ function – Blurs the image

try{
   if(!$image=imagecreatefromgif(‘clouds.gif’)){
     throw new Exception(‘Error creating image’);
   }
   // apply filter to image
   if(!imagefilter($image,IMG_FILTER_SELECTIVE_BLUR)){
     throw new Exception(‘Error applying filter to image’);
   }
   // display image to the browser
   header("Content-type: image/gif");
   imagegif($image);
   // free memory
   imagedestroy($image);

}
catch(Exception $e){
   echo $e->getMessage();
   exit();
}

// example of ‘imagefilter()’ function – Uses mean removal to
achieve a "sketchy" effect

try{
   if(!$image=imagecreatefromgif(‘clouds.gif’)){
     throw new Exception(‘Error creating image’);
   }
   // apply filter to image
   if(!imagefilter($image,IMG_FILTER_MEAN_REMOVAL)){
     throw new Exception(‘Error applying filter to image’);
   }
   // display image to the browser
   header("Content-type: image/gif");
   imagegif($image);
   // free memory
   imagedestroy($image);
   
}
catch(Exception $e){
   echo $e->getMessage();
   exit();
}

// example of ‘imagefilter()’ function – Makes the image smoother

try{
   if(!$image=imagecreatefromgif(‘clouds.gif’)){
     throw new Exception(‘Error creating image’);
   }
   // apply filter to image
   if(!imagefilter($image,IMG_FILTER_SMOOTH,40)){
     throw new Exception(‘Error applying filter to image’);
   }
   // display image to the browser
   header("Content-type: image/gif");
   imagegif($image);
   // free memory
   imagedestroy($image);
   
}
catch(Exception $e){
   echo $e->getMessage();
   exit();
}

All right, I believe that the hands-on examples coded above should be explanatory enough in themselves to demonstrate the capacity offered by the "imagefilter()" function for applying different graphic filters to a specified image stream.

As with the majority of my articles on PHP development, feel free to modify all the code samples included in this tutorial. This will help you to improve your existing background in using the GD extension within your own PHP applications.

Final thoughts

Finally, this educational journey has come to an end. Hopefully, this series of articles has served as an approachable introduction to using the main functions that come packaged with the GD library. However, if you’re looking for a complete reference on this PHP extension, the best place to go is the official PHP site.

[gp-comments width="770" linklove="off" ]

antalya escort bayan antalya escort bayan Antalya escort diyarbakir escort