PHP
  Home arrow PHP arrow Page 4 - Filtering Image Streams with the GD Library in PHP
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
Google.com  
PHP

Filtering Image Streams with the GD Library in PHP
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 5
    2007-08-27


    Table of Contents:
  • Filtering Image Streams with the GD Library in PHP
  • Manipulating the colors of an image stream
  • Controlling brightness and contrast with the imagefilter() function
  • More graphic filters with the imagefilter() function

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    Filtering Image Streams with the GD Library in PHP - More graphic filters with the imagefilter() function
    ( Page 4 of 4 )

    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
     

       

    PHP ARTICLES

    - Implementing Factory Methods in PHP 5
    - Merging a File Split for FTP Upload using PHP
    - Getting Data from Yahoo Site Explorer Inboun...
    - Method Chaining: Adding More Selecting Metho...
    - How to Split a File During an FTP Upload Usi...
    - Expanding a Custom CodeIgniter Library with ...
    - Using the Yahoo Site Explorer Inbound Links ...
    - Building a CodeIgniter Custom Library with M...
    - Building an E-mini Trading System Using PHP ...
    - Completing the MySQL Class with Method Chain...
    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek