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

Building an image stream from an existing JPG 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

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.



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