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

Building image streams from an existing PNG graphic - 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

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!



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