Creating Image Streams from Existing Graphics with the GD Library in PHP - Building image streams from existing GIF graphics
(Page 2 of 4 )
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.
Next: Building an image stream from an existing JPG graphic >>
More PHP Articles
More By Alejandro Gervasio