Wordpress缓存 Gravatar 头像(方法一)

Function代码

//缓存 Gravatar 头像,by minirizhi.com
add_filter('get_avatar', 'minirizhi_cache_avatar');
function minirizhi_cache_avatar($avatar) {
    if(!is_dir('avatar'))//判断是否有缓存目录
        mkdir('avatar', 0755, true);//否者创建头像缓存目录
    $tmp = strpos($avatar, 'http');
    $g = substr($avatar, $tmp, strpos($avatar, "'", $tmp) - $tmp);
    $tmp = strpos($g, 'avatar/') + 7;
    $f = substr($g, $tmp, strpos($g, "?", $tmp) - $tmp);
    $w = home_url();
    //$w = '//avatar.softs.vip';
    list($width,$height,$type,$attr) = getimagesize($g);//获取头像参数
    $e = ABSPATH .'avatar/'. $f .'-'.$width.'.jpg';
    $t = 604800; //设定 7 天, 单位:秒
    if ( empty($default) ) $default = '/avatar/default.jpg';
    //if ( empty($default) ) $default = '//avatar.softs.vip/default.jpg';
    if ( !is_file($e) || (time() - filemtime($e)) > $t ){ //当头像不存在或者文件超过 7 天才更新
        copy(htmlspecialchars_decode($g), $e);
    }else{
        $avatar = strtr($avatar, array($g => $w.'/avatar/'.$f.'-'.$width.'.jpg'));
        //$avatar = strtr($avatar, array($g => $w.'/'.$f.'-'.$width.'.jpg'));
    }
    if (filesize($e) < 500) copy($default, $e);
    return $avatar;
}
文章目录