SunQuest
 
       PHP
  Home arrow PHP arrow Page 4 - Creating Image Streams from Existing G...
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 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Actuate Whitepapers 
VeriSign Whitepapers 
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

Creating Image Streams from Existing Graphics with the GD Library in PHP
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 3 stars3 stars3 stars3 stars3 stars / 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:
      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

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    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!


    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.

       · Over the course of this second part of the series, you'll learn how to use the GD...
     

       

    PHP ARTICLES

    - Viewing and Editing Tasks for a Project Mana...
    - More on Private Methods with PHP 5 Member Vi...
    - Adding Tasks to a Project Management Applica...
    - Utilizing Private Methods with PHP 5 and Mem...
    - Making Changes in a Project Management Appli...
    - Defining Public and Protected Methods with M...
    - HTML for a Project Management Application
    - Using Subclasses and Accessors with Member V...
    - Implementing Internet Protocols with PHP
    - Project Management: The Application
    - Working with Private Properties to Protect P...
    - Protecting PHP 5 Class Data with Member Visi...
    - Setting Up a Web-based Image Hosting Service
    - Comparing Files and Databases with PHP Bench...
    - Setting Up a Web-Based Image Gallery





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway