Question

Hello right know I have a small web application using Sails.JS and I'm trying to set a default image if the user doesn't have a gravatar account/profile.

This is the code that I use to hash the email in my controller:

var crypto = require('crypto');
req.session.email = 'user_email@gmail.com';
req.session.gravatar_hash = crypto.createHash('md5').update(req.session.email).digest('hex');

As you see I have in session the hash now.

So in my view I have:

<div class="gravatar" style="background-image: url('http://www.gravatar.com/avatar/<%=req.session.gravatar_hash%>?s=50');"></div>

This code works great for showing the gravatar (default or custom) image, but what If i want to do a validation so if the user doesn't have a gravatar I can use a custom image?? There is a way to validate that?

Was it helpful?

Solution

Gravatar has you covered--they allow you to specify a default image URL to display if the user doesn't have a Gravatar. From their docs:

If you'd prefer to use your own default image (perhaps your logo, a funny face, whatever), then you can easily do so by supplying the URL to an image in the d= or default= parameter. The URL should be URL-encoded to ensure that it carries across correctly, for example:

<img src="http://www.gravatar.com/avatar/00000000000000000000000000000000?d=http%3A%2F%2Fexample.com%2Fimages%2Favatar.jpg"/>

View the docs for more options and info.

OTHER TIPS

You can check if a (HEAD) request to 'http://gravatar.com/avatar/' + req.session.gravatar_hash + '.png?d=404' results in 404 or not.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top