HomePHP 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.
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.