HomePHP Page 3 - Drawing Functions and the GD Library in PHP
Using the imagechar() and imagecharup() functions - PHP
If you're a PHP developer looking for an approachable guide on how to use the popular GD extension available with PHP 4 and PHP 5, then look no further, because this series might be what you need. Welcome to the third article of the series that began with "A Close Look at the GD Library in PHP." In this group of tutorials, you'll find distilled material on using the most important functions that come integrated with the GD library.
As I stated in the previous section, the GD library also provides PHP developers with a couple of intuitive functions for displaying single characters on a specified image stream. In this case, I'm talking about the "imagechar()" and "imagecharup()" functions respectively, whose basic usage is clearly demonstrated by the following code samples:
// example of 'imagechar()' function
try{ if(!$img=imagecreatetruecolor(300,200)){ throw new Exception('Error creating image'); } // allocate some colors $whiteColor=imagecolorallocate($img,255,255,255); $blackColor=imagecolorallocate($img,0,0,0); // display character imagechar($img,5,150,90,'X',$whiteColor); header('Content-type: image/png'); imagepng($img); // free memory imagedestroy($img);
As illustrated previously by the above pair of hands-on examples, displaying different characters on a specified image stream is actually a no-brainer process that can be performed with minor hassles. In the first case, a simple "X" character is included into a basic image stream via the corresponding "imagechar()" function. Quite simple, right?
In the second example, the "imagecharup()" function is properly utilized to display another basic character in front of a black-colored image stream. In this case, the character has been rotated 90 degrees clockwise, since this function allows you to specify the inclination angle used to show the character in question.
So far, so good. At this point I showed you how to display a few basic characters in front of a previously-created image stream, by using the pair of simple GD functions that you learned previously. As you may have noticed, the aforementioned functions aren't actually as useful as the powerful "imagestring()" that you saw in the first tutorial of this series, but they did deserve a close look, since they could be helpful in certain circumstances.
Now, going back to covering the functions provided by the GD library to draw basic shapes on specific image streams, in the following section I'll teach you how to display on the browser some primitive ellipses, something that can be quite helpful if your PHP application requires you to work with this type of shape.
To learn how to display some elemental ellipses with the GD library, please click on the link below and keep reading.