Home arrow PHP arrow Page 4 - Filtering Image Streams with the GD Library in PHP

More graphic filters with the imagefilter() function - 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 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.



 
 
>>> 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: