PHP
  Home arrow PHP arrow Page 3 - 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? 
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 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
     

       

    PHP ARTICLES

    - 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
    - Method Chaining: Adding More Methods to the ...
    - Method Chaining in PHP 5
    - The Role of Interfaces in Applying the Depen...
    - Dependency Injection: Using a Setter Method ...
    - Using a Model Class with the Dependency Inje...
    - Injecting Objects Using Setter Methods with ...
    - Injecting Objects by Constructor with the De...
    - The Dependency Injection Design Pattern in P...
    - Performing Inferential Statistical Analysis ...
    - Performing Descriptive Statistical Analysis ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 Hosted by Hostway
    Stay green...Green IT