Setting different width and height for gravatar

I’ve been trying to set a different width and height using the get_avatar function. I need to set the author’s avatar in single.php to 60×40 size.

So let’s say the gravatar looks like this:

Read More

enter image description here

when set to 60×40, it would look like this (resized and cropped):

enter image description here

However, the default get_avatar does not seem to allow different values for width and height, since

<?php echo get_avatar( $comment, '60' ); ?>

would simply result in 60×60-sized gravatar.

I’m not sure if this is a nice way for doing this, but I tried adding this to functions.php, by facilitating the TimThumb image resizer (I renamed the timthumb.php to display.php):

add_filter('get_avatar','change_avatar_url');
function change_avatar_url($urel) {
    $urel = str_replace("src='", "src='". bloginfo( 'template_directory' ) ."/script/display.php?src=", $urel);
    $urel = str_replace("' class", "&w=60&h=40&zc=1' class", $urel);
    return $urel;
}

but it (seem obviously) does not work.

Is there any way to achieve this?

Related posts

Leave a Reply

3 comments

  1. As informed by @Rarst, apparently currently Gravatar only accepts one value for size. It is really unfortunate. However I managed to work-around this by facilitating timthumb.php and a function I found from here: How to get gravatar url alone

    I’m not sure if this is the best way to do this (it looks messy), however this works for me for creating 60×40 px size. It doesn’t seem to work well when I tried with other sizes, though. Not sure why.

    Well, here goes.

    First I add gravatar.com into the list of allowed sites in timthumb.php (I renamed the file to display.php). The list is under the $ALLOWED_SITES.

    Then in functions.php I put this (notice that I rename the timthumb.php to display.php):

    // Get the gravatar URL
    // source: https://wordpress.stackexchange.com/questions/46904/how-to-get-gravatar-url-alone
    function get_gravatar_url( $email ) {
        $hash = md5( strtolower( trim ( $email ) ) );
        return 'http://gravatar.com/avatar/' . $hash;
    }
    
    // Function to display the custom-sized gravatar
    function custom_gravatar_timthumb($width, $height, $class) {
        $custom = get_template_directory_uri() . "/script/display.php?src=". get_gravatar_url(get_the_author_meta('email')) ."w=". $width ."&h=". $height ."&zc=1&a=c";
        echo "<img src='" . $custom . "' class='". $class ."' alt='avatar' />";
    }
    

    Then in single.php (where I display the gravatar) I put this:

    <?php
    custom_gravatar_timthumb(60, 40, "author-avatar avatar photo");
    ?>
    
  2. apply_filters('get_avatar', $avatar, $id_or_email, $size, $default, $alt);
    

    So you can filter the output:

    function wpse69318_avatar_sizes( $avatar, $id_or_email, $size, $default, $alt )
    {
        if ( is_single() )
            return preg_replace( '/width="[0-9]{1,3}"/i', 'width="40px"', $avatar );
    
        // return default for other pages
        return $avatar
    }
    add_filter( 'get_avatar', 'wpse69318_avatar_sizes', 10, 5 );