Created
July 11, 2012 09:47
-
-
Save hzlzh/3089354 to your computer and use it in GitHub Desktop.
Mini Gavatar Cache
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* Mini Gavatar Cache */ | |
/* 注:该程序中部分代码是WordPress所用,小幅修改即可复用。 */ | |
function my_avatar( $email, $size = '50', $default = '', $alt = false ) { | |
$alt = (false === $alt) ? '' : esc_attr( $alt ); | |
$f = md5( strtolower( $email ) ); | |
$w = home_url(); //$w = get_bloginfo('url'); | |
$a = $w. '/avatar/'. $f . '.jpg'; | |
$e = preg_replace('/wordpress\//', '', ABSPATH) . 'avatar/' . $f . '.jpg'; | |
$t = 604800; //设定7天, 单位:秒 | |
if ( empty($default) ) $default = $w. '/avatar/default.jpg'; | |
if ( !is_file($e) || (time() - filemtime($e)) > $t ){ //当头像不存在或者文件超过7天才更新 | |
$r = get_option('avatar_rating'); | |
$g = sprintf( "http://www.gravatar.com", ( hexdec( $f{0} ) % 2 ) ). '/avatar/'. $f. '?s='. $size. '&d='. $default. '&r='. $r; | |
copy($g, $e); | |
} | |
if (filesize($e) < 500) copy($default, $e); | |
$avatar = "<img title='{$alt}' alt='{$alt}' src='{$a}' class='avatar avatar-{$size} photo' height='{$size}' width='{$size}' />"; | |
return $avatar; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment