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:
when set to 60×40, it would look like this (resized and cropped):
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?
Unfortunately at moment Gravatar service itself only accepts single number for size and only serves square images.
So you have to achieve it with CSS or download, modify and cache images.
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 aloneI’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 intimthumb.php
(I renamed the file todisplay.php
). The list is under the$ALLOWED_SITES
.Then in
functions.php
I put this (notice that I rename thetimthumb.php
todisplay.php
):Then in
single.php
(where I display the gravatar) I put this:So you can filter the output: