Weborum Webmaster Forum > Custom Dynamic Avatar
Help - Search - Members - Calendar
Full Version: Custom Dynamic Avatar
Weborum Webmaster Forum > TUTORIAL ARCHIVE - tutorials & scripts to save you scouring the internet. Please feel free to add your own. > PHP Tutorials & scripts
Erikina
My signature is an example (it's a random background color).

If you would like to try it out, look at the pattern then try yourself:

http://www.thesecondbestsite.com/random-Erikina.gif
http://www.thesecondbestsite.com/blue-Billy.gif
http://www.thesecondbestsite.com/red-devil.gif
http://www.thesecondbestsite.com/pink-girl.gif
http://www.thesecondbestsite.com/pink-Username.gif
http://www.thesecondbestsite.com/sblue-fool.gif
http://www.thesecondbestsite.com/green-ih8green.gif
etc.

Well commented.
PHP
<?php
//Made by Eric Springer for http://www.fantastictutorials.com
//$font should be true type
$font = 'http://www.thesecondbestsite.com/avatars/trebucbd.ttf';
// This is the average pixel per character width for the font (you can always guess)
$pixels_a_character = 6.5;
//This is the background image width, if you are using lots of backgrounds
//use getimagesize()
$image_width = 80;
//

$name = stripslashes($_GET['name']); //So that we don't see annoying slashes
$string_size = ceil(strlen($name)*$pixels_a_character);

if(
$string_size > $image_width){
$size = 6; //This a font size
$padding = 5; //Padding from the left of the image
} else {
$padding = ($image_width - $string_size) / 2;
$size = 10; //This is a font size
}

if(
$_GET['color'] ){

if(
$_GET['color'] == 'random'){
$num = rand(1,6);
if(
$num == 1){ $bgpic = 'http://www.thesecondbestsite.com/avatars/blue.gif'; }
elseif(
$num == 1){ $bgpic = 'http://www.thesecondbestsite.com/avatars/gray.gif'; }
elseif(
$num == 2){ $bgpic = 'http://www.thesecondbestsite.com/avatars/green.gif'; }
elseif(
$num == 3){ $bgpic = 'http://www.thesecondbestsite.com/avatars/pink.gif'; }
elseif(
$num == 4){ $bgpic = 'http://www.thesecondbestsite.com/avatars/purple.gif'; }
elseif(
$num == 5){ $bgpic = 'http://www.thesecondbestsite.com/avatars/red.gif'; }
elseif(
$num == 6){ $bgpic = 'http://www.thesecondbestsite.com/avatars/sblue.gif'; }
else {
$bgpic = 'http://www.thesecondbestsite.com/avatars/blank.gif'; }

}
elseif(
$_GET['color'] == 'blue' ){ $bgpic = 'http://www.thesecondbestsite.com/avatars/blue.gif'; }
elseif(
$_GET['color'] == 'gray'){ $bgpic = 'http://www.thesecondbestsite.com/avatars/gray.gif'; }
elseif(
$_GET['color'] == 'green'){ $bgpic = 'http://www.thesecondbestsite.com/avatars/green.gif'; }
elseif(
$_GET['color'] == 'pink'){ $bgpic = 'http://www.thesecondbestsite.com/avatars/pink.gif'; }
elseif(
$_GET['color'] == 'purple'){ $bgpic = 'http://www.thesecondbestsite.com/avatars/purple.gif'; }
elseif(
$_GET['color'] == 'red'){ $bgpic = 'http://www.thesecondbestsite.com/avatars/red.gif'; }
elseif(
$_GET['color'] == 'sblue'){ $bgpic = 'http://www.thesecondbestsite.com/avatars/sblue.gif'; }
else {
$bgpic = 'http://www.thesecondbestsite.com/avatars/blank.gif'; }

} else {
$bgpic = 'http://www.thesecondbestsite.com/avatars/blank.gif';
}

//blank.gif is the default image if errors occurr.

$im = imagecreatefromgif($bgpic);
$textcolor = imagecolorat($im, 0, 0);
imagettftext($im, $size, 0, $padding, 75, $textcolor, $font, $name);
header("Content-type: image/gif"); //important headers
imagegif($im); //create a gif
?>


What do you think?
leo
hey that's pretty cool, i was wondering how your avatar kept changing color!

welcome to weborum by the way smile.gif
exiang
For people that wish to use dynamic avatar on forum, Davatar.net is providing FREE service on this. Using it on the forum is just as simple as an Offsite Avatar Linking.
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2010 Invision Power Services, Inc.