PHP
  Home arrow PHP arrow Page 2 - Creating Image Streams from Existing Graphics with 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

Creating Image Streams from Existing Graphics with the GD Library in PHP
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 2
    2007-08-20


    Table of Contents:
  • Creating Image Streams from Existing Graphics with the GD Library in PHP
  • Building image streams from existing GIF graphics
  • Building an image stream from an existing JPG graphic
  • Building image streams from an existing PNG graphic

  • 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


    Creating Image Streams from Existing Graphics with the GD Library in PHP - Building image streams from existing GIF graphics
    ( Page 2 of 4 )

    As I explained at the beginning of this article, the GD library provides PHP developers with a decent set of functions for generating different image streams from existing graphics. This can be helpful when a particular web application needs to preprocess a determined number of background pictures, although there may be other cases where these functions can be pretty effective too.

    Concerning the implementation of these image preprocessing functions, in the preceding article of the series I showed you how to create a brand new image stream from an existing GIF picture using the "imagecreatefromgif()" function. Let me refresh your memory with the following two code samples:

    // 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();
    }

    // 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 can see, the practical examples shown above demonstrate in a clear way how to create a dynamic image stream from an existing GIF graphic. Of course, once the original picture is built via the corresponding "imagecreatefromgif()" function, it can be later modified and displayed in any other format supported by the browser, such as JPG and PNG respectively.

    And speaking of these popular image formats, in the following section I'll teach you how to use another handy function included into the neat GD extension to build several image streams from an existing JPG graphic.

    Want to see how this function looks? Click on the link that appears 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 2 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek