Home arrow PHP arrow 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!

TABLE OF CONTENTS:
  1. Filtering Image Streams with the GD Library in PHP
  2. Manipulating the colors of an image stream
  3. Controlling brightness and contrast with the imagefilter() function
  4. More graphic filters with the imagefilter() function
By: Alejandro Gervasio
Rating: starstarstarstarstar / 5
August 27, 2007

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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.



 
 
>>> More PHP Articles          >>> More By Alejandro Gervasio
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: