PHP
  Home arrow PHP arrow Page 2 - 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 - 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
     

       

    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 6 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek