Here is a simple PHP function you could use to display the Gravatar image for a user based on their email address. It allows setting a css class for the image tag as well as specifying the size of the image to display.
function displayGravatarImage($email_address, $class = '', $size = 32) {
if(!filter_var($email_address, FILTER_VALIDATE_EMAIL)) {
throw new Exception("Invalid email address");
return FALSE;
}
$gravatar_link = 'http://www.gravatar.com/avatar/' . md5($email_address) . '?s='.$size;
echo '<img class="' . $class . '" src="' . $gravatar_link . '" />';
}
As you can see it is pretty simple and straightforward. We validate the email address and then generate the appropriate gravatar image url before outputting.
You could easily extend this function to include extra markup to suit your purpose, or even just to return the link rather than output it.
No comments:
Post a Comment