Creating Image Streams from Existing Graphics with the GD Library in 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.

Introduction

Welcome to the second installment of the series that began with  "A Close Look at the GD Library in PHP." This series steps you through creating and processing dynamic images with this powerful PHP extension, and it shows you how to use its most relevant functions with numerous practical examples.

Now, reviewing the group of topics discussed in the previous article, hopefully you’ll recall that the GD extension can be remarkably useful when it’s necessary to generate image streams from scratch. With regard to this process, this extension provides PHP developers with the intuitive "imagecreate()", "imagecreatetruecolor()" and "imagecolorallocate()" functions. When used in conjunction, these functions make building dynamic graphics a truly painless procedure.

The GD library presents some other useful functions as well, like the popular "createimagefromgif()" for building image streams from existing GIF graphics. This function can be quite helpful if your PHP applications demand some kind of preprocessing on certain background images.

So far, so good right? At this stage, hopefully all of the GD functions that I mentioned above should be quite familiar to you, since they’re very easy to grasp and use in the context of any PHP application. However, as I stated at the end of the previous article of this series, the GD extension comes packaged with many other useful functions, which indeed deserve a close and detailed look.

Thus, in this second tutorial I’ll show you how to create different types of image streams from existing graphics, but in this case limiting the process to building GIF, JPG and PNG images.

Now, having introduced the topics that I plan to discuss, let’s continue exploring the functions that come bundled with the GD library. Let’s get going!

{mospagebreak title=Building image streams from existing GIF graphics}

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.

{mospagebreak title=Building an image stream from an existing JPG graphic}

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.

{mospagebreak title=Building image streams from an existing PNG graphic}

In according with the concepts that I deployed in the previous section, the GD library comes bundled with yet another powerful image preprocessing function. This function is helpful for creating a new stream from an existing PNG graphic. I’m talking about the "imagecreatefrompng()" function, which works in a way that is closely similar to all the other preprocessing functions that I showed you in the earlier sections of this tutorial.

Now that I have explained how this brand new GD function works, please take a look at the following hands-on example, which precisely illustrates its functioning:

// example of ‘imagecreatefrompng()’ function

try{
   if(!$image=imagecreatefrompng(‘clouds.png’)){
      throw new Exception(‘Error loading image’);
   }
   // create text color for png image
   if(!$textColor=imagecolorallocate($image,0,0,0)){
      throw new Exception(‘Error creating text color’);
   }
   // include text string into png 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/png");
   // display image
   imagepng($image);
   // free up memory
   imagedestroy($image);
   
}
catch(Exception $e){
   echo $e->getMessage();
   exit();
}

As illustrated above, the "imagecreatefrompng()" function is nearly identical to the previous ones in the way it works. As you can see, the function takes a PNG image as its input argument, which is quite useful for building a new graphic using this popular image format.

Also, provided that the existing PNG image inputted into the function is the following:

The image stream outputted to the browser after running the previous example would be similar to this:

And finally, by following the same approach that I utilized with all the prior GD functions discussed here, below I coded a basic wrapper for the already familiar "imagecreatefrompng()" function. It looks like this:

// example of ‘imagecreatefrompng()’ function

try{
   function displayPngImage($image,$text){
      if(!file_exists($image)){
         throw new Exception(‘Invalid image file’);
      }
      if(!$text){
         throw new Exception(‘Invalid text for image’);
      }
      if(!$image=imagecreatefrompng($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/png");
      // display image
      imagepng($image);
      // free up memory
      imagedestroy($image); 
   }
   // display image stream on the browser
   displayPngImage(‘clouds.png’,'This is a sample string.’);
}
catch(Exception $e){
   echo $e->getMessage();
   exit();
}

Quite simple to grasp, right? After studying the signature of the above example, and naturally, of all the others developed previously, you’ll certainly agree with me that the GD library makes creating different image streams from existing graphics a truly painless process. Besides, it’s fair to mention that the GD extension also supports seamlessly the generation of dynamic image streams from other graphic formats, like WBMP, etc., but in this tutorial I only covered the most popular ones.

Final thoughts

In this second part of the series you learned how to use the capacity provided by some functions integrated with the GD extension to create dynamic image streams from existing graphics. As you saw here, this process was quite easy to follow, so I believe that you won’t have any serious problems using these functions in your own PHP scripts.

Now, focusing specifically on the topics that will be discussed in the next part of the series, you’ll learn how to create new images from data strings. You’ll also learn how to utilize the drawing functions that come with the GD library. I think that you won’t want to miss it!

[gp-comments width="770" linklove="off" ]
antalya escort bayan antalya escort bayan