PHP
  Home arrow PHP arrow Page 4 - A Close Look at 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

A Close Look at the GD Library in PHP
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 6
    2007-08-15


    Table of Contents:
  • A Close Look at the GD Library in PHP
  • Creating images from scratch with the GD library
  • An alternative way to create image streams from scratch
  • Generating dynamic streams from existing GIF images with the GD library

  • 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


    A Close Look at the GD Library in PHP - Generating dynamic streams from existing GIF images with the GD library
    ( Page 4 of 4 )

    As you possibly recall from the previous section, I said that the GD extension allows you to not only create image streams from scratch, but also generate different graphics from existing images. This feature is particularly useful in those cases where you want to use a previously-built picture and create a different image from it.

    To achieve this process effortlessly, the GD library comes equipped with a decent set of functions aimed specifically at generating image streams from an existing graphic. In this case, I'll show you how to use the "imagecreatefromgif()" function, but in upcoming articles of this series, I'll teach you how to utilize some others too.

    Thus, assuming that the following sample GIF image has been previously created by using a graphic editing software, a scanner or a digital camera:

    Then the "imagecreatefromgif()" function might be used as follows:

    // example of 'imagecreatefromgif()' function
    try{
       if(!$image=imagecreatefromgif('clouds.gif')){
         throw new Exception('Error loading image');
       }
       // create text color for gif image
       if(!$textColor=imagecolorallocate($image,0,0,0)){
         throw new Exception('Error creating text color');
       }
       // include text string into gif image
       if(!$text=imagestring($image,5,10,90,'This is a sample text
    string.',$textColor)){
         throw new Exception('Error creating image text');
       }
       header("Content-type:image/gif");
       // display image
       imagegif($image);
       // free up memory
       imagedestroy($image);
    }
    catch(Exception $e){
       echo $e->getMessage();
       exit();
    }

    As you can see, the above example uses an existing GIF image, and displays on top of it a sample text string by using the same functions that you learned previously. And finally, the example outputs to the browser the following GIF image:

    See how easy it is to create a brand new image from an existing one using the GD library? Of course you see that! Besides, the previous "imagecreatefromgif()" function could be quickly wrapped into a custom function, as demonstrated by the example below:

    // example of custom function using 'imagecreatefromgif()'
    try{
       // define 'displayGifImage()' function
       function displayGifImage($image,$text){
         if(!file_exists($image)){
           throw new Exception('Invalid image file');
         }
         if(!$text){
           throw new Exception('Invalid text for image');
         }
         if(!$image=imagecreatefromgif($image)){
           throw new Exception('Error loading image');
         }
         // create text color for gif image
         if(!$textColor=imagecolorallocate($image,0,0,0)){
           throw new Exception('Error creating text color');
         }
         // include text string into gif image
         if(!imagestring($image,5,10,90,$text,$textColor)){
           throw new Exception('Error creating image text');
         }
         header("Content-type:image/gif");
         // display image
         imagegif($image);
         // free up memory
         imagedestroy($image); 
       }
       displayGifImage('clouds.gif','This is a sample string.');
    }
    catch(Exception $e){
       echo $e->getMessage();
       exit();
    }

    As you might guess, the previous custom "displayGifImage()" is only a crude wrapper for the already familiar "imagecreatefromgif()" function that comes bundled with the GD library. However, this example should give you a clear idea of how to develop more complex functions or classes that use this function to generate new image streams from an existing graphic.

    Lastly, as usual with many of my articles on PHP development, feel free to tweak all of the source code corresponding to the examples shown in this tutorial. In this manner you can acquire a more complete background in the GD extension.

    Final thoughts

    In this first tutorial of the series, I introduced some basic functions integrated with the GD extension, to demonstrate how to perform some useful things, such as creating image streams from scratch, displaying text strings and generating new GIF graphics utilizing existing ones.

    However, this is merely the beginning of this journey, since the GD library comes equipped with many more useful functions. Some of them will be covered in the next part of the series. You won't want to miss it!



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