As you possibly recall from the previous section, I said that the GD extension allows you to not only create image streams from scratch, but also generate different graphics from existing images. This feature is particularly useful in those cases where you want to use a previously-built picture and create a different image from it. To achieve this process effortlessly, the GD library comes equipped with a decent set of functions aimed specifically at generating image streams from an existing graphic. In this case, I'll show you how to use the "imagecreatefromgif()" function, but in upcoming articles of this series, I'll teach you how to utilize some others too. Thus, assuming that the following sample GIF image has been previously created by using a graphic editing software, a scanner or a digital camera:
Then the "imagecreatefromgif()" function might be used as follows: // example of 'imagecreatefromgif()' function As you can see, the above example uses an existing GIF image, and displays on top of it a sample text string by using the same functions that you learned previously. And finally, the example outputs to the browser the following GIF image:
See how easy it is to create a brand new image from an existing one using the GD library? Of course you see that! Besides, the previous "imagecreatefromgif()" function could be quickly wrapped into a custom function, as demonstrated by the example below: // example of custom function using 'imagecreatefromgif()' As you might guess, the previous custom "displayGifImage()" is only a crude wrapper for the already familiar "imagecreatefromgif()" function that comes bundled with the GD library. However, this example should give you a clear idea of how to develop more complex functions or classes that use this function to generate new image streams from an existing graphic. Lastly, as usual with many of my articles on PHP development, feel free to tweak all of the source code corresponding to the examples shown in this tutorial. In this manner you can acquire a more complete background in the GD extension. Final thoughts In this first tutorial of the series, I introduced some basic functions integrated with the GD extension, to demonstrate how to perform some useful things, such as creating image streams from scratch, displaying text strings and generating new GIF graphics utilizing existing ones. However, this is merely the beginning of this journey, since the GD library comes equipped with many more useful functions. Some of them will be covered in the next part of the series. You won't want to miss it!
blog comments powered by Disqus |