Gravatar Implementation

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 = "".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.

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

public function validate_gravatar($email)


		//SC MFM

		// Craft a potential url and test its headers

		$hash = md5($email);

		$uri = '' . $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)




			$grav_url = "" .

			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.

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

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 = "".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

[html]<img alt="" src="<?php echo $model->user->avatar( $size ); ?>"/>[/html]

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

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