Gravatar is a Fantastic service that provides a universal avatar for users. Gravatar has designed their service in such a way that the user’s avatar can be rendered on any site free of charge. Gravatar is used on millions of sites across the globe including GitHub, Stack Overflow, Word Press, Disqus, and many more sites. You can easily use gravatar without much effort. Simply add the code below to your application helper:
1 2 3 4 5 6 |
|
The first parameter, email, is the email address you wish to obtain the gravatar for. The second parameter, size, is the size of the gravatar you wish to render. To actually get the link to the gravatar, we obtain an MD5 hash of the email and then lowercase it and append it to the end of the gravatar url. The size is specified using the s parameter. To use this function, simple pass the newly generated Gravatar url to an image tag like this:
1
|
|
This will render the gravatar for that email address or a default image if the avatar doesn’t exist. If you wish to specify your default image, you can modify your helper to look like the code listed below, changing the default url to whatever you wish:
1 2 3 4 5 |
|
This will use whatever default URL you provide for the default url variable.
So far so good, That’s it!!! See ya!!! :)