Dabrorius - WEB tutorials
Dynamic Avatar [PHP tutorials]
Learn how to create a basic dynamic avatar for a forum. This avatar counts how many times it has been viewed.
Here is the result. (reload the page to see effect)


 <?
//set width and height of your avatar
	$width = 90;
	$height = 90;
 
//Create a new image  
    $image = ImageCreate($width, $height);      
 
//Set background and text color
	$bground = ImageColorAllocate($image, 200, 200, 200);
    $textcolor = ImageColorAllocate($image, 0, 0, 0);
 
//Fill background with background color.
    ImageFill($image, 0, 0, $bground);     
 
//Add 1 to number of views
//Read data from the cookie and then save it back in cookie
//Set cookie to expire in 24 years 
//(we can't set it to never expire, but 24 years should be enough)
 
$count = $_COOKIE['count'] + 1;
setcookie(count, $count, time()+60*60*24*29.5*12*24);
 
//Write text on image
imagestring($image, 5, 5, 10, "You have", $textcolor);
imagestring($image, 5, 25, 22, "seen", $textcolor);
imagestring($image, 5, 5, 35, "my avatar", $textcolor);
imagestring($image, 8, 35, 55, $count, $textcolor);
imagestring($image, 5, 20, 70, "times!", $textcolor);
 
//tell the browser that this file act as an image
    header("Content-Type: image/gif"); 
 
//output the image in jpeg format 
    ImageGif($image);
 
//free up resources
    ImageDestroy($image);
?>