Page 1 of 1

Gravatar Capability

Posted: Sat Jul 07, 2007 4:38 pm
by romans1423
Request: If a user does not have an avatar selected, a Gravatar is automatically used instead. If no gravatar is available, a default image (or spacer.gif) is used.

In functions_display.php, there's this bit of code:

Code: Select all

if (empty($avatar) || !$avatar_type)
	{
		return '';
	}
Rather than returning nothing if no avatar is selected, a gravatar URL could be returned; something like this:

Code: Select all

if (empty($avatar) || !$avatar_type)
	{
		$grav_id = md5($user->data['user_email']);
		$grav_default = urlencode('http://fellowship-hall.com/images/spacer.gif');
		$grav_rating = 'PG';
		$grav_size = 80;

		$grav_url = "http://www.gravatar.com/avatar.php?gravatar_id=$grav_id&default=$grav_default&size=$grav_size&rating=$grav_rating";

		return '<img src="' . $grav_url . '" width="' . $grav_size . '" height="' . $grav_size . '" alt="' . ((!empty($user->lang[$alt])) ? $user->lang[$alt] : $alt) . '" />';
	}
Note: That code doesn't work, per the first point below:
  • The $grav_id line always uses *your* email address, and so anyone who does not have an avatar selected will have *your* gravatar displayed. I have no idea how to get the appropriate email address easily at that point.
  • The $grav_default line shouldn't have a hardcoded domain name.
  • $grav_rating and $grav_id should be admin-configurable via the ACP, as perhaps should $grav_default.
  • A note should be added to the avatar UCP module stating that if no avatar is selected locally, a gravatar will be used if available.
  • And anything else an interested MOD author might cook up... :P

Re: Gravatar Capability

Posted: Sun Jun 22, 2008 7:04 pm
by romans1423
Bump.

Re: Gravatar Capability

Posted: Thu Jun 26, 2008 11:10 am
by zerogreen
Hi

I was playing around with this and got it working...

The logic being that I needed to pass the email address along with the other avatar parameters:

In viewtopic.php around line 1052 I added the email var:

Code: Select all

'avatar'		=> ($user->optionget('viewavatars')) ? get_user_avatar($row['user_avatar'], $row['user_avatar_type'], $row['user_avatar_width'], $row['user_avatar_height'], $row['user_email']) : '' , 
Then in includes/functions_display.php I amended the function declaration:

Code: Select all

function get_user_avatar($avatar, $avatar_type, $avatar_width, $avatar_height, $alt = 'USER_AVATAR', $user_email='')
I then used the code you supplied in the function and first replaced

Code: Select all

$grav_id = md5($user->data['user_email']);
with

Code: Select all

$grav_id = md5($user_email);
This did not work, but I noticed that the alt tag for the image was now the relevant email address.
I then changed the line to be

Code: Select all

$grav_id = md5(trim((!empty($user->lang[$alt])) ? $user->lang[$alt] : $alt));
and it seems to be working fine...

I haven't applied this anywhere else as yet... ;)

Re: Gravatar Capability

Posted: Thu Jun 26, 2008 8:52 pm
by romans1423
You, sir, rock. I'll be trying out this code this weekend. Thanks a ton!

Re: Gravatar Capability

Posted: Sun Jul 13, 2008 1:27 pm
by Xabi
Subscribing to this thread... does this Gravatar solution works?

Re: Gravatar Capability

Posted: Sun Jul 13, 2008 6:36 pm
by romans1423
Okay, this isn't in real MOD format and it only affects the View Topic page... but check this out:

Open viewtopic.php and find:

Code: Select all

				'avatar'		=> ($user->optionget('viewavatars')) ? get_user_avatar($row['user_avatar'], $row['user_avatar_type'], $row['user_avatar_width'], $row['user_avatar_height']) : '' , 
Replace that with this:

Code: Select all

				'avatar'		=> ($user->optionget('viewavatars')) ? get_user_avatar($row['user_avatar'], $row['user_avatar_type'], $row['user_avatar_width'], $row['user_avatar_height'], 'USER_AVATAR', $row['user_email']) : '' , 
Save & close that file.

Now open includes/display_functions.php and find:

Code: Select all

function get_user_avatar($avatar, $avatar_type, $avatar_width, $avatar_height, $alt = 'USER_AVATAR')
{
	global $user, $config, $phpbb_root_path, $phpEx;

	if (empty($avatar) || !$avatar_type)
	{
		return '';
	}
Replace that with this:

Code: Select all

function get_user_avatar($avatar, $avatar_type, $avatar_width, $avatar_height, $alt = 'USER_AVATAR', $user_email = '')
{
	global $user, $config, $phpbb_root_path, $phpEx;

	if (empty($avatar) || !$avatar_type)
	{
		if ($user_email) {
			$grav_id = md5($user_email);
			$grav_default = 'identicon';
			$grav_rating = 'R';
			$grav_size = 100;
			
			$grav_url = "http://www.gravatar.com/avatar.php?gravatar_id=$grav_id&default=$grav_default&size=$grav_size&rating=$grav_rating";
			
			return '<img src="' . $grav_url . '" width="' . $grav_size . '" height="' . $grav_size . '" alt="' . ((!empty($user->lang[$alt])) ? $user->lang[$alt] : $alt) . '" />';
		}
		else {
			return '';
		}
	}
$grav_default can be either 'identicon', 'monsterid', 'wavatar', or the URL to what you want to use for your default gravatar image (leave it blank to not use a default image for users who have neither an avatar on your board or a gravatar). The differences between Identicons, MonsterIDs, and Wavatars are illustrated here.

Adjust the $grav_rating to something appropriate for your board; possible values are 'g', 'pg', 'r', and 'x'.

Adjust $grav_size to whatever you want, and you're good to go. Save the file, and enjoy gravatars in your topics. :)

Re: Gravatar Capability

Posted: Thu Dec 10, 2009 7:08 am
by richey
Does this modification still work for you?

Since updating to 3.0.6 it doesn't anymore for me ... and I don't understand why.

I preferred this tiny code modification much over the 'full-fledged MOD' here http://www.phpbb.com/community/viewtopi ... &t=1746715 because of if its small size.

kind regards,
r.