Enable Gravatar/Avatar at Your Website – Gravatar Image PHP Script

by Hiroshi on October 5, 2009

in Scripts, Useful Scripts

gravatarYou might have already created an account at gravatar (Globally Recognized Avatars) and enjoying your display picture or avatar at forums and blogs when you just post your comment with your email and there you are — it displays your avatar along with your comment. If you are a developer and you create websites – PHP websites – and wondering how you can enable gravatars / avatars in your next project then here is very easy to use working solution – PHP script to enable gravatar at your PHP website / blog / forum / project.

Copy the following code. Paste it in a file, save it in PHP extension, upload it or use it at localhost – just change the usersEmail value to your email address, if you are already at gravatars website and hit it. Other instructions for customization of script – you can find in the script. e.g. size of gravatar, default image, rating etc.

Gravatars PHP Script for Your Website

<?php
// The users email, passed to Gravatar
$usersEmail = "myemail@live.com";
 
// default avatar to load if Gravatar doesn't find one
$defaultImage = "https://domain.com/default.png";
 
// The size of the image
$avatarSize = "50";
 
// Minimum rating for your site
// Possible values (G, PG, R, X)
$avatarRating = "G";
 
// Border around the image
$avatarBorder = "000000";
 
// URL for Gravatar
$gravatarURL = "https://www.gravatar.com/avatar.php?gravatar_id=%s&default=%s&size=%s&border=%s&rating=%s";
 
$avatarURL = sprintf
(
	$gravatarURL,
	md5($usersEmail),
	$defaultImage,
	$avatarSize,
	$avatarBorder,
	$avatarRating
);
 
print "<img src="" .
	$avatarURL . "" width="" .
	$avatarSize . "" height="" .
	$avatarSize . "" />";
?>

Related Posts

Previous post:

Next post: