Skip to content

Instantly share code, notes, and snippets.

@RickardAhlstedt
Created August 13, 2018 19:40
Show Gist options
  • Save RickardAhlstedt/20a75187a4d78fa8d3730e2bf4083830 to your computer and use it in GitHub Desktop.
Save RickardAhlstedt/20a75187a4d78fa8d3730e2bf4083830 to your computer and use it in GitHub Desktop.
<?php
/**
* Get either a gravatar URL or a complete image tag for a specified email
*
* @param string $sEmail The email address
* @param string $sSize The size on pixels, defaults to 80px [ 1 - 2048 ]
* @param string $sDefault Default imageset to use [ 404 | mp | identicon | monsterid | wavatar ]
* @param string $sRating Maximum rating (inclusive) [ g | pg | r | x ]
* @param boolean $bImg True to return the complete img-tag, false for just the URL
* @param array $aParams Optional key/values to the img-tag
* @return string Containing either URL or complete img-tag
*/
function getGravatarImage( $sEmail, $sSize = 80, $sDefault = 'mp', $sRating = 'g', $bImg = false, $aParams = array() ) {
$sUrl = 'https://www.gravatar.com/avatar/';
$sUrl .= md5( strtolower( $sEmail ) );
$sUrl .= "?s=$sSize&d=$sDefault&r=$sRating";
if( $bImg ) {
$sUrl = '<img src="' . $sUrl . '"';
foreach( $aParams as $sKey => $sValue ) {
$sUrl .= ' ' . $sKey . '="' . $sValue . '"';
}
$sUrl . '/>';
}
return $sUrl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment