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

An alternative way to create image streams from scratch - 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

In accordance with the concepts that I expressed in the earlier section, the GD extension offers yet another function that is very useful for creating image streams from scratch. I'm talking about the "imagecreatetruecolor()" function, which can be used in the following way:

// example of 'imagecreatetruecolor()', 'imagecolorallocate()',
'imagestring()' functions
try{
   if(!$image=imagecreatetruecolor(300,200)){
     throw new Exception('Error creating blank image');
   }
   // create text color for image
   if(!$textColor=imagecolorallocate($image,255,255,255)){
     throw new Exception('Error creating text color');
   }
   // include text string into image stream
   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/png");
   // display image
   imagepng($image);
   // free up memory
   imagedestroy($image);
}
catch(Exception $e){
   echo $e->getMessage();
   exit();
}

As illustrated previously, the "imagecreatetruecolor()" functions behaves very similar to its cousin "imagecreate()" covered in the prior section. However, in this case, the function in question creates a true color image stream, which can be slightly more useful if you want to generate obviously true-color graphics. Other than that, the function takes up the same input parameters, and returns an image identifier if the stream has been successfully created.

That being explained, the image outputted to the browser by the previous example is depicted below:

In addition, the source code of the previous example could be easily wrapped into a custom function, in the following manner:

// example of 'imagecreatetruecolor()', 'imagecolorallocate()',
'imagestring()' functions
try{
  
function displayTextOnImage($text='This is a sample text
string'){
     if(!$text){
       throw new Exception('Invalid text string'); 
     }
     if(!$image=imagecreatetruecolor(300,200)){
       throw new Exception('Error creating blank image');
     }
     // create text color
     if(!$textColor=imagecolorallocate($image,255,255,255)){
       throw new Exception('Error creating text color');
     }
     // include text string into image stream
     if(!imagestring($image,5,10,90,$text,$textColor)){
       throw new Exception('Error creating image text');
     }
     header("Content-type:image/png");
     // display image
     imagepng($image);
     // free up memory
     imagedestroy($image); 
   }
   displayTextOnImage('This text was created with GD.');
}
catch(Exception $e){
   echo $e->getMessage();
   exit();
}

All right, at this point, after studying all of the code samples, you'll have to agree with me that building dynamic graphics from scratch by using the GD extension is actually a very intuitive process that can be performed with minor hassles.

Therefore, provided that you have already grasped the programming logic required to create basic images with this powerful graphical extension, in the following section I'll show you some additional hands-on examples aimed at demonstrating how to build dynamic graphics from an existing GIF image, using practically the same GD functions that you learned previously. Sounds really interesting, right?

To learn more on how to create image streams from an existing GIF image using the functionality provided by the GD extension, jump ahead and read the next few lines. I'll be there, waiting for you.



 
 
>>> 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: