PHP
  Home arrow PHP arrow Page 3 - 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 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Mobile Linux 
App Generation ROI 
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


    Creating Image Streams from Existing Graphics with the GD Library in PHP - Building an image stream from an existing JPG graphic


    (Page 3 of 4 )

    As I stated previously, the GD library doesn't run short when it comes to generating brand new image streams from existing graphics. As you saw in the prior section, it's easy to create dynamic images by utilizing an initial GIF picture. This capacity can be extended to build images of different formats.

    That's precisely the case with the "imagecreatefromjpeg()" function. As you can probably guess from the name, it allows you to build a new image stream from an existing JPEG graphic. Its use is very intuitive, as is adequately shown by the following code sample:

    // example of 'imagecreatefromjpeg()' function

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

    As shown above, the brand new "imagecreatefromjpeg()" function behaves in a fashion that is nearly identical to the "imagecreatefromgif()" function that you learned in the previous section. Of course, the only difference to spot here is that in this case, the function in question takes a JPEG image as its input argument. Quite simple, right?

    To complement the correct implementation of the previous example, suppose that the JPEG image inputted into the function is the following:

    Then, based upon this input parameter, the prior example would output to the browser the below image stream:

    Also, as you might guess, it's extremely easy to define a simple wrapper for the  "imagecreatefromjpeg()" function, to facilitate it within a determined PHP application. Having said that, this condition is clearly reflected by the following code sample:

    // example of 'imagecreatefromjpeg()' function

    try{
       // define 'displayJpgImage()' function
       function displayJpgImage($image,$text){
          if(!file_exists($image)){
             throw new Exception('Invalid image file');
          }
          if(!$text){
             throw new Exception('Invalid text for image');
          }
          if(!$image=imagecreatefromjpeg($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/jpeg");
          // display image
          imagejpeg($image);
          // free up memory
          imagedestroy($image); 
       }
       // display image on the browser
       displayJpgImage('bw_clouds.jpg','This is a sample string.');
    }
    catch(Exception $e){
       echo $e->getMessage();
       exit();
    }

    So far, so good. At this time, I'm pretty certain that you understand how the previous "imagecreatefromjpeg()" function works. And since it's very simple to follow, I won't spend more time discussing how it functions. 

    So what's the next step? Well, as I said earlier, the GD library comes equipped with yet another image preprocessing function, which is quite handy for creating new image streams from an existing PNG image. In other words, it behaves very similarly to the "imagecreatefromgif()" and "imagecreatefromjpeg()" functions that you have already learned.

    Therefore, in the next section I'll show you how to perform this task via the brand new "imagecreatefrompng()" function included in the GD extension.

    Click on the link below and keep reading.

    More PHP Articles
    More By Alejandro Gervasio


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

       

    PHP ARTICLES

    - Authentication Scripts for a User Management...
    - Utilizing the Use Keyword for Namespaces in ...
    - Building a User Management Application
    - Working With Different Namespaces in PHP 5
    - User Management Explained: Overview
    - Using Namespaces in PHP 5
    - Database Security: Guarding Against SQL Inje...
    - Building a Modular Exception Class in PHP 5
    - Database and Password Security for Web Appli...
    - Handling MySQL Data Set Failures in PHP 5
    - Building Site Registration for Web Applicati...
    - Intercepting Customized Exceptions in PHP 5
    - Securing Your Web Application Against Attacks
    - Sub Classing Exceptions in PHP 5
    - Authentication for Web Application Security





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
    Stay green...Green IT