Pages

Implementing gravatars with PHP

It is quite simple. PHP provides strtolower(), md5(), and urlencode() functions, allowing us to create the gravatar URL with ease.
It can be constructed with the following php code.
$gravatar = "http://www.gravatar.com/avatar/" . md5( strtolower( trim( "email@email.com") ) ) . "?d=" . urlencode( "http://url.tld/images/default.jpg" ) . "&s= 80";
In above example s=80 is size of the image.
Once the gravatar URL is created, you can output it whenever you please:
<.img alt="" src="<?php echo $gravatar; ?>" />
Alternate method by using function :
/**
 * Get either a Gravatar URL or complete image tag for a specified email address.
 *
 * @param string $email The email address
 * @param string $s Size in pixels, defaults to 80px [ 1 - 2048 ]
 * @param string $d Default imageset to use [ 404 | mm | identicon | monsterid | wavatar ]
 * @param string $r Maximum rating (inclusive) [ g | pg | r | x ]
 * @param boole $img True to return a complete IMG tag False for just the URL
 * @param array $atts Optional, additional key/value attributes to include in the IMG tag
 * @return String containing either just a URL or a complete image tag
 * @source http://gravatar.com/site/implement/images/php/
 */
function get_gravatar( $email, $s = 80, $d = 'mm', $r = 'g', $img = false, $atts = array() ) {
    $url = 'http://www.gravatar.com/avatar/';
    $url .= md5( strtolower( trim( $email ) ) );
    $url .= "?s=$s&d=$d&r=$r";
    if ( $img ) {
        $url = ' $val )
            $url .= ' ' . $key . '="' . $val . '"';
        $url .= ' />';
    }
    return $url;
}
source : https://en.gravatar.com/site/implement/images/php/