PHP
  Home arrow PHP arrow Page 4 - 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 
Moblin 
JMSL Numerical Library 
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


    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.


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

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

       

    PHP ARTICLES

    - Building a Database-Driven Application with ...
    - User Authentication for a Project Management...
    - Introduction to the CodeIgniter PHP Framework
    - Adding Users for a Project Management Applic...
    - Migrating Class Code for a MIME Email to PHP...
    - Login and Logout Authentication for a Projec...
    - Composing Messages in HTML for MIME Email wi...
    - Project Management: Authentication
    - A Better Way to Determine MIME Types for MIM...
    - Project Management Overview
    - 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...





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