PHP
  Home arrow PHP arrow Page 2 - 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 - Creating images from scratch with the GD library
    ( Page 2 of 4 )

    We will start exploring the numerous functions that come bundled with the GD extension by showing how to create a dynamic image from scratch. In this case, this basic task can be performed by using three core GD functions, called "imagecreate()", "imagecolorallocate()" and "imagestring()" respectively.

    Concerning the usage of the aforementioned functions, below I coded a simple example, which shows how to utilize these functions:

    // example of 'imagecreate()', 'imagecolorallocate()',
    'imagestring()' functions
    try{
       if(!$image=imagecreate(300,200)){
         throw new Exception('Error creating blank image');
       }
       // create background color for image
       $backColor=imagecolorallocate($image,0,0,255);
       // create text color
       if(!$textColor=imagecolorallocate($image,255,255,255)){
         throw new Exception('Error creating text color');
       }
       // include text string into image stream
       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/png");
       // display image
       imagepng($image);
       // free up memory
       imagedestroy($image);
    }
    catch(Exception $e){
       echo $e->getMessage();
       exit();
    }

    Basically, all the previous examples does is create a 300px X 200px image stream via the "imagecreate()" function, then allocate a white color into the stream by the means of "imagecolorallocate()", and finally include a sample string by using the "imagestring()" function. Two additional functions are used in the proper sequence, that is "imagepng()" and imagedestroy()" to output the image in PNG format to the browser, and free up the memory occupied by the respective image.

    The PNG image generated by the previous example is shown below:

    As you saw, the above example not only demonstrates how easy it is to create an image from scratch with the GD extension, but how intuitive its main functions are, right?

    Also, in a similar fashion, the example can be quickly rewritten, this time using a simple procedural function. This approach is illustrated by the code sample below:

    try{
      
    // define procedural function
      
    function displayTextOnImage($text='This is a sample text
    string'){
         if(!$text){
           throw new Exception('Invalid text string'); 
         }
         if(!$image=imagecreate(300,200)){
           throw new Exception('Error creating blank image');
         }
         // create background color for image
         $backColor=imagecolorallocate($image,0,0,255);
         // create text color
         if(!$textColor=imagecolorallocate($image,255,255,255)){
           throw new Exception('Error creating text color');
         }
         // include text string into image stream
         if(!imagestring($image,5,10,90,$text,$textColor)){
           throw new Exception('Error creating image text');
         }
         header("Content-type:image/png");
         // display image
         imagepng($image);
         // free up memory
         imagedestroy($image);
       }
       // display image on the browser
       displayTextOnImage('This text was created with GD.');
    }
    catch(Exception $e){
       echo $e->getMessage();
       exit();
    }

    Of course, as you might have guessed, the custom "displayTextOnImage()" function that I defined previously displays a new PNG image on the browser, similar to the one shown below:

    All right, now that you hopefully learned how to create an image from scratch by using a few intuitive functions bundled with the GD library, it's a good time to move forward and continue analyzing other useful functions. Thus, in the following section I'll show you how to utilize an alternative GD function to generate basic image streams.

    To learn how this brand new function will be used, 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 3 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek