queej
(Dqj)
February 9, 2010, 8:57pm
1
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.
smoothcoder
(Smooth Traveller)
February 10, 2010, 1:05am
2
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 .
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.
queej
(Dqj)
February 10, 2010, 6:19pm
3
Cool. Thanks. I’ll try to contribute more refinements as I get further along.
mwotton
(Micha Wotton)
March 2, 2010, 4:35am
4
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
[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]