HomePHP Page 2 - Filtering Image Streams with the GD Library in PHP
Manipulating the colors of an image stream - 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!
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);
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.