Last active
September 16, 2023 00:23
-
-
Save Tinitto/c81d27cc8077e40544a6f9fb17d6714d to your computer and use it in GitHub Desktop.
Generate Gravatar or default to Initials Avatar
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
This is in a Nodejs environment | |
*/ | |
const crypto = require('crypto'); | |
module.exports = ( | |
email = '', | |
name = 'Anonymous', | |
initialsAvatarBackground = '000000', | |
initialsColor = 'fff' | |
) => { | |
const processedName = name.toString().replace(/( )+/g, '+'); | |
const defaultImageUrl = encodeURIComponent( | |
`https://ui-avatars.com/api/${processedName}/80/${initialsAvatarBackground}/${initialsColor}` | |
); | |
const processedEmail = email | |
.toString() | |
.trim() | |
.toLowerCase(); | |
const hash = email | |
? crypto | |
.createHash('md5') | |
.update(processedEmail) | |
.digest('hex') | |
: ''; | |
return `https://www.gravatar.com/avatar/${hash}?d=${defaultImageUrl}`; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment