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  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Mobile Linux 
App Generation ROI 
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

A Close Look at 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-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:
      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


    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!


    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.

       · If you're a PHP developer who wants to learn how to use the mosts important...
     

       

    PHP ARTICLES

    - Authentication Scripts for a User Management...
    - Utilizing the Use Keyword for Namespaces in ...
    - Building a User Management Application
    - Working With Different Namespaces in PHP 5
    - User Management Explained: Overview
    - Using Namespaces in PHP 5
    - Database Security: Guarding Against SQL Inje...
    - Building a Modular Exception Class in PHP 5
    - Database and Password Security for Web Appli...
    - Handling MySQL Data Set Failures in PHP 5
    - Building Site Registration for Web Applicati...
    - Intercepting Customized Exceptions in PHP 5
    - Securing Your Web Application Against Attacks
    - Sub Classing Exceptions in PHP 5
    - Authentication for Web Application Security





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway
    Stay green...Green IT