PHP
  Home arrow PHP arrow Page 3 - 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 - An alternative way to create image streams from scratch
    ( Page 3 of 4 )

    In accordance with the concepts that I expressed in the earlier section, the GD extension offers yet another function that is very useful for creating image streams from scratch. I'm talking about the "imagecreatetruecolor()" function, which can be used in the following way:

    // example of 'imagecreatetruecolor()', 'imagecolorallocate()',
    'imagestring()' functions
    try{
       if(!$image=imagecreatetruecolor(300,200)){
         throw new Exception('Error creating blank image');
       }
       // create text color for image
       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();
    }

    As illustrated previously, the "imagecreatetruecolor()" functions behaves very similar to its cousin "imagecreate()" covered in the prior section. However, in this case, the function in question creates a true color image stream, which can be slightly more useful if you want to generate obviously true-color graphics. Other than that, the function takes up the same input parameters, and returns an image identifier if the stream has been successfully created.

    That being explained, the image outputted to the browser by the previous example is depicted below:

    In addition, the source code of the previous example could be easily wrapped into a custom function, in the following manner:

    // example of 'imagecreatetruecolor()', 'imagecolorallocate()',
    'imagestring()' functions
    try{
      
    function displayTextOnImage($text='This is a sample text
    string'){
         if(!$text){
           throw new Exception('Invalid text string'); 
         }
         if(!$image=imagecreatetruecolor(300,200)){
           throw new Exception('Error creating blank image');
         }
         // 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); 
       }
       displayTextOnImage('This text was created with GD.');
    }
    catch(Exception $e){
       echo $e->getMessage();
       exit();
    }

    All right, at this point, after studying all of the code samples, you'll have to agree with me that building dynamic graphics from scratch by using the GD extension is actually a very intuitive process that can be performed with minor hassles.

    Therefore, provided that you have already grasped the programming logic required to create basic images with this powerful graphical extension, in the following section I'll show you some additional hands-on examples aimed at demonstrating how to build dynamic graphics from an existing GIF image, using practically the same GD functions that you learned previously. Sounds really interesting, right?

    To learn more on how to create image streams from an existing GIF image using the functionality provided by the GD extension, jump ahead and read the next few lines. I'll be there, waiting for you.



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