PHP
  Home arrow PHP arrow Page 4 - 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 an existing PNG graphic
    ( Page 4 of 4 )

    In according with the concepts that I deployed in the previous section, the GD library comes bundled with yet another powerful image preprocessing function. This function is helpful for creating a new stream from an existing PNG graphic. I'm talking about the "imagecreatefrompng()" function, which works in a way that is closely similar to all the other preprocessing functions that I showed you in the earlier sections of this tutorial.

    Now that I have explained how this brand new GD function works, please take a look at the following hands-on example, which precisely illustrates its functioning:

    // example of 'imagecreatefrompng()' function

    try{
       if(!$image=imagecreatefrompng('clouds.png')){
          throw new Exception('Error loading image');
       }
       // create text color for png image
       if(!$textColor=imagecolorallocate($image,0,0,0)){
          throw new Exception('Error creating text color');
       }
       // include text string into png 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/png");
       // display image
       imagepng($image);
       // free up memory
       imagedestroy($image);
       
    }
    catch(Exception $e){
       echo $e->getMessage();
       exit();
    }

    As illustrated above, the "imagecreatefrompng()" function is nearly identical to the previous ones in the way it works. As you can see, the function takes a PNG image as its input argument, which is quite useful for building a new graphic using this popular image format.

    Also, provided that the existing PNG image inputted into the function is the following:

    The image stream outputted to the browser after running the previous example would be similar to this:

    And finally, by following the same approach that I utilized with all the prior GD functions discussed here, below I coded a basic wrapper for the already familiar "imagecreatefrompng()" function. It looks like this:

    // example of 'imagecreatefrompng()' function

    try{
       function displayPngImage($image,$text){
          if(!file_exists($image)){
             throw new Exception('Invalid image file');
          }
          if(!$text){
             throw new Exception('Invalid text for image');
          }
          if(!$image=imagecreatefrompng($image)){
             throw new Exception('Error loading image');
          }
          // create text color for jpg image
          $textColor=imagecolorallocate($image,0,0,0);
          // 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/png");
          // display image
          imagepng($image);
          // free up memory
          imagedestroy($image); 
       }
       // display image stream on the browser
       displayPngImage('clouds.png','This is a sample string.');
    }
    catch(Exception $e){
       echo $e->getMessage();
       exit();
    }

    Quite simple to grasp, right? After studying the signature of the above example, and naturally, of all the others developed previously, you'll certainly agree with me that the GD library makes creating different image streams from existing graphics a truly painless process. Besides, it's fair to mention that the GD extension also supports seamlessly the generation of dynamic image streams from other graphic formats, like WBMP, etc., but in this tutorial I only covered the most popular ones.

    Final thoughts

    In this second part of the series you learned how to use the capacity provided by some functions integrated with the GD extension to create dynamic image streams from existing graphics. As you saw here, this process was quite easy to follow, so I believe that you won't have any serious problems using these functions in your own PHP scripts.

    Now, focusing specifically on the topics that will be discussed in the next part of the series, you'll learn how to create new images from data strings. You'll also learn how to utilize the drawing functions that come with the GD library. I think that 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 6 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek