Drawing Functions and the GD Library in PHP - Using the imagechar() and imagecharup() functions
(Page 3 of 4 )
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);
}
catch(Exception $e){
echo $e->getMessage();
exit();
}

// example of 'imagecharup()' 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
imagecharup($img,5,150,90,'A',$whiteColor);
header('Content-type: image/gif');
imagegif($img);
// free memory
imagedestroy($img);
}
catch(Exception $e){
echo $e->getMessage();
exit();
}

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.
Next: Using the imageellipse() and imagefilledellipse() functions >>
More PHP Articles
More By Alejandro Gervasio