Show a gravatar in Twig

In twig

<!-- gravatar (left) -->
<div class="post-info-left d-inline-block">
     <div class="post-info-author-avatar">
          <img class="post-gravatar post-avatar" src="{{ gravatar(post.user.email, 64) }}"
               title="{{ rtGetAuthorName(post) }}" alt="{{ rtGetAuthorName(post) }}"/>
     </div>
</div>

In component

public function registerMarkupTags() {
    return [
        'functions' => [
            'gravatar'     => [ $this, 'getAvatar' ],
        ]
    ];
}

/**
 * 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 | mp | 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 https://gravatar.com/site/implement/images/php/
 */
public function getGravatar( $email, $s = 80, $d = 'mp', $r = 'g', $img = false, $atts = [] ) {
    $url = 'https://www.gravatar.com/avatar/';
    $url .= md5( strtolower( trim( $email ) ) );
    $url .= "?s=$s&d=$d&r=$r";
    if ( $img ) {
        $url = '<img src="' . $url . '"';
        foreach ( $atts as $key => $val ) {
            $url .= ' ' . $key . '="' . $val . '"';
        }
        $url .= ' />';
    }

    return $url;
}
Last update: November 15th at 11:49am