Yii Framework Forum: Gravatar Implementation - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Gravatar Implementation Simple Gravatar Method Rate Topic: -----

#1 User is offline   queej 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 71
  • Joined: 04-February 10

Posted 09 February 2010 - 03:57 PM

I didn't see anything on gravatars here on the Yii site, so I created a simple function that I can use to display a local avatar, if it exists, or a gravatar if not.

function avatar( $uid, $email, $size ) {
  $local_exists = file_exists( "uploads/avatar_$uid.gif" );
  if ( !$local_exists ) {
	$default = "/uploads/avatar_.gif";
	$url = "http://www.gravatar.com/avatar.php?gravatar_id=".md5( strtolower($email) )."&default=".urlencode($default)."&size=".$size;
  }
  else {
	$url = "/uploads/avatar_$uid.gif";
  }
  return $url;
}


I'm sure there are better ways to do this (without hardcoding urls and paths, for example), but this is simple enough that I thought it could save others some time, perhaps. I should note that my $uid is a unique database id associated with each user.
0

#2 User is offline   smoothcoder 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 29
  • Joined: 03-December 09
  • Location:Thailand

Posted 09 February 2010 - 08:05 PM

In the same spirit of share and saving time, Just a rapid copy / paste and this is the one that I use with the blog model on DISCARDEDteenz.com.

public function validate_gravatar($email)
	{
		//SC MFM
		// Craft a potential url and test its headers
		$hash = md5($email);
		$uri = 'http://www.gravatar.com/avatar/' . $hash . '?d=404';
		$headers = @get_headers($uri);
		if (!preg_match("|200|", $headers[0])) {
			$has_valid_avatar = FALSE;
		} else {
			$has_valid_avatar = TRUE;
		}
		return $has_valid_avatar;
	}

public function get_gravatar($email)
	{
		if($this->validate_gravatar($email))
		{
			$grav_url = "http://www.gravatar.com/avatar.php?gravatar_id=" .
			md5($email) . "&default=" . urlencode($default) . "&size=" . $size;
			echo '<img class="gravatar" src="'.$grav_url.'" width="80px" height="80px"/>';
		}
		else if ($user=User::model()->findByAttributes(array('email'=>$email) ))
		{//show local avatars if the user has no gravatar
		echo '<img class="gravatar" src="'.Yii::app()->request->baseUrl.'/uploads/avatar/'.$user->avatar.'" width="80px" height="80px"/>';
		}//show default avatar
		else echo '<img class="gravatar" src="'.Yii::app()->theme->baseUrl.'/images/nogravatar.jpg" width="80px" height="80px"/>';

		// todo use params['noAvatar'] with params['avatarPath"]
	}


I'll improve with your strlower :rolleyes: .

I have more to show but I need some time for I 'm building a public repository to share the code of the blog model stuffs.
0

#3 User is offline   queej 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 71
  • Joined: 04-February 10

Posted 10 February 2010 - 01:19 PM

Cool. Thanks. I'll try to contribute more refinements as I get further along.
0

#4 User is offline   M Wotton 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 47
  • Joined: 01-November 09

Posted 01 March 2010 - 11:35 PM

Really nice work, I have implemented it as follows:
In my user model User.php:
public function avatar( $size ) {
    if ( !($local_exists = file_exists( "uploads/avatar_".$this->id.".gif" )) ) {
          $default = "/images/user_img/avatar/admin_".$size.".gif"; // absolute route to default image
          $url = "http://www.gravatar.com/avatar/".md5( strtolower($this->email) )."?d=".Yii::app()->controller->createAbsoluteUrl($default)."&s=".$size;
    }
    else {
          $url = "/uploads/avatar_".$this->id.".gif";
    }
    return $url;
  }


I then call it in one of two ways. If the current object is a user or has a user object in it's structure then it's just
<img alt="" src="<?php echo $model->user->avatar( $size ); ?>"/>


or if in a widget or some other view without a user object (eg a form) just use

<img src="<?php echo User::model()->findByPk(Yii::app()->user->id)->avatar( $size ); ?>" alt="" />

Micha Wotton
Posted Image
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users