PHP
  Home arrow PHP arrow Page 2 - Filtering Image Streams with the GD Li...
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Actuate Whitepapers 
Moblin 
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? 
PHP

Filtering Image Streams with the GD Library in PHP
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 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:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb 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

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    Filtering Image Streams with the GD Library in PHP - Manipulating the colors of an image stream


    (Page 2 of 4 )

    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


       · Over the course of this tutorial of the series, the usage of the most important...
     

       

    PHP ARTICLES

    - Handling Attachments in MIME Email with PHP
    - Completing the Project Management Application
    - Sending MIME Email with PHP
    - Handling Files for a Project Management Appl...
    - Viewing and Editing Tasks for a Project Mana...
    - More on Private Methods with PHP 5 Member Vi...
    - Adding Tasks to a Project Management Applica...
    - Utilizing Private Methods with PHP 5 and Mem...
    - Making Changes in a Project Management Appli...
    - Defining Public and Protected Methods with M...
    - HTML for a Project Management Application
    - Using Subclasses and Accessors with Member V...
    - Implementing Internet Protocols with PHP
    - Project Management: The Application
    - Working with Private Properties to Protect P...




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway