Home arrow PHP arrow Page 4 - A Close Look at the GD Library in PHP

Generating dynamic streams from existing GIF images with the GD library - PHP

The GD library comes bundled with both PHP 4 and PHP 5. While it is a popular library that has been around for a while, many web developers do not take full advantage of its capabilities. In this five-part series, we will take a close look at what the GB library can do for you, especially in the area of dynamically-generated graphics.

TABLE OF CONTENTS:
  1. A Close Look at the GD Library in PHP
  2. Creating images from scratch with the GD library
  3. An alternative way to create image streams from scratch
  4. Generating dynamic streams from existing GIF images with the GD library
By: Alejandro Gervasio
Rating: starstarstarstarstar / 6
August 15, 2007

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

As you possibly recall from the previous section, I said that the GD extension allows you to not only create image streams from scratch, but also generate different graphics from existing images. This feature is particularly useful in those cases where you want to use a previously-built picture and create a different image from it.

To achieve this process effortlessly, the GD library comes equipped with a decent set of functions aimed specifically at generating image streams from an existing graphic. In this case, I'll show you how to use the "imagecreatefromgif()" function, but in upcoming articles of this series, I'll teach you how to utilize some others too.

Thus, assuming that the following sample GIF image has been previously created by using a graphic editing software, a scanner or a digital camera:

Then the "imagecreatefromgif()" function might be used as follows:

// 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();
}

As you can see, the above example uses an existing GIF image, and displays on top of it a sample text string by using the same functions that you learned previously. And finally, the example outputs to the browser the following GIF image:

See how easy it is to create a brand new image from an existing one using the GD library? Of course you see that! Besides, the previous "imagecreatefromgif()" function could be quickly wrapped into a custom function, as demonstrated by the example below:

// 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 might guess, the previous custom "displayGifImage()" is only a crude wrapper for the already familiar "imagecreatefromgif()" function that comes bundled with the GD library. However, this example should give you a clear idea of how to develop more complex functions or classes that use this function to generate new image streams from an existing graphic.

Lastly, as usual with many of my articles on PHP development, feel free to tweak all of the source code corresponding to the examples shown in this tutorial. In this manner you can acquire a more complete background in the GD extension.

Final thoughts

In this first tutorial of the series, I introduced some basic functions integrated with the GD extension, to demonstrate how to perform some useful things, such as creating image streams from scratch, displaying text strings and generating new GIF graphics utilizing existing ones.

However, this is merely the beginning of this journey, since the GD library comes equipped with many more useful functions. Some of them will be covered in the next part of the series. You won't want to miss it!



 
 
>>> More PHP Articles          >>> More By Alejandro Gervasio
 

blog comments powered by Disqus
   

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

 



© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap

Dev Shed Tutorial Topics: