Home arrow PHP arrow 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.

TABLE OF CONTENTS:
  1. Drawing Functions and the GD Library in PHP
  2. Taking a look at the imagearc() function
  3. Using the imagechar() and imagecharup() functions
  4. Using the imageellipse() and imagefilledellipse() functions
By: Alejandro Gervasio
Rating: starstarstarstarstar / 13
August 21, 2007

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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.



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