Home arrow PHP arrow Page 2 - Creating Image Streams from Existing Graphics with the GD Library in PHP

Building image streams from existing GIF graphics - PHP

Within the huge range of applications that can be developed with PHP, creating dynamic images may be among the most useful and interesting. This process can be easily tackled by using the GD library. If you're curious about how to use the functions that come bundled with this extension, this series of articles might be what you're looking for.

TABLE OF CONTENTS:
  1. Creating Image Streams from Existing Graphics with the GD Library in PHP
  2. Building image streams from existing GIF graphics
  3. Building an image stream from an existing JPG graphic
  4. Building image streams from an existing PNG graphic
By: Alejandro Gervasio
Rating: starstarstarstarstar / 2
August 20, 2007

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: