This theme snippet will supply a gravatar ("Globally Recognized Avatar"; http://gravatar.com/) for anonymous commenters, provided that you require them to leave their contact information in admin/comment/configure. You must also enable user pictures in comments in your theme settings.

Put the following php code snippet at the top of your comment.tpl.php file, assuming you are using phptemplate.


// Tested with Drupal 4.6

// check to see that comment author was not authenticated user
// and that comment author provided email address
if ((!$comment->uid) && ($comment->mail)) {
  // get the maximum allowed user picture dimensions from user.module settings
  list($maxwidth, $maxheight) = explode('x', variable_get('user_picture_dimensions', '85x85'));
  // set the maximum gravatar size equal to the maximum user picture height from settings
  $size = $maxheight;
  // build the gravatar image url with md5 hash of comment author email and size limit
  $gravatar_uri = "http://www.gravatar.com/avatar.php?gravatar_id=". md5($comment->mail) ."&size=$size";
  // get image dimensions of gravatar returned from uri
  $im = getimagesize($gravatar_uri);
  // an invalid gravatar will return 1x1 gif.
  // if a 1x1 gif is returned, don't overwrite $picture
  if (($im[0] != 1) && ($im[1] != 1)) {
    $alt = $comment->name;
    $picture = theme('image', $gravatar_uri, $alt, $alt, '', false);
    $picture = "<div class=\"picture\">$picture</div>";
  }
}

There is obviously a lot of room to improve upon this code, as it totally ignores authenticated users who have registered a gravatar, but have no local user picture uploaded.

-ba

Comments

Brian@brianpuccio.net’s picture

This is a great start. Would you conisder adding this to the phptemplate snippets section of the handbooks?

Shiny’s picture

great stuff! thanks for this.

-------------------------------------------------------
http://coffee.geek.nz