4

To implement Gravatar in my Rails3 application, I'm using the gravatar_image_tag gem in a helper, but I'm having issues when mixing 2 config options:

  1. If the user doesn't have a gravatar attached to his email a default image is rendered; but I want it to reference an external file (e.g., http://www.iconfinder.com/ajax/download/png/?id=43350&s=128 instead of :identicon or others)
  2. I also want the image to be resized on the fly to, let's say 50px.

Independently, both options work as expected, but when I put them together:

def gravatar_for(user, options = { :default => 'http://www.iconfinder.com/ajax/download/png/?id=43350&s=128', :size => 50 })
  gravatar_image_tag(user.email.downcase, :alt => user.full_name,
                                          :class => 'gravatar',
                                          :gravatar => options)
end

the size option is not applied, and the gravatar gets rendered in it's full size (128px in this case).

What am I doing wrong, or how can I achieve this combination?

Marius Butuc
  • 17,781
  • 22
  • 77
  • 111

2 Answers2

2

Gravatar will not resize your default image for you. I assume that it just 302s to the ulr gave as a default if it does not find an gravatar for the email you gave it. It looks like the 's' parameter in the iconfinder url is for the size you are trying to grab but that icon does not have a size of 50px available only 128, 256, and 512

Example:

http://www.iconfinder.com/ajax/download/png/?id=43350&s=256

If you wanted a 50px and 80px versions of the icon I would save it to your applications public/image directory as default_gravatar_50.png and default_gravatar_80.png respectively and change your method like so.

end

def gravatar_for(user, options = {})
  options = { :size => 50 }.merge(options)
  options[:default] = image_tag("default_gravatar_#{options[:size]}.png
  gravatar_image_tag(user.email.downcase,
                     :alt => user.full_name,
                     :class => 'gravatar',
                     :gravatar => options)
end

Or if you find an icon on icon finder that is the size(s) you like change the setting of the default option like so.

options[:default] = "http://www.iconfinder.com/ajax/download/png/?id=43350&s=#{options[:size]}"
Michael Deering
  • 434
  • 3
  • 7
  • Take note of the way that I changed how you were setting your default options. The previous way if you only passed the size option into your call to gravatar_for you would have lost your :default option. – Michael Deering May 04 '11 at 18:46
  • I actually encountered _that_ issue, so your note is more than welcomed! :) – Marius Butuc May 04 '11 at 18:50
  • Did not want to overload you with to much at once but there is also way to configure the gem to have those defaults set globally if you checkout the readme over on github. But as of right now those global defaults do not take a lambda or proc but this is a good case for them to do so. – Michael Deering May 04 '11 at 19:17
  • hey Michael - I feel that i have encountered the issue you have said, and my gravatar_for call doesn't work.. can you add in how to properly call gravatar_for? – jay Jul 22 '11 at 05:19
0

Iconfinder here. You don't want to link to the download script. Instead just grab the URL to the image it self so you wan't get a lot of header information.

  • Hi Martin, thanks for the tip! Just to make sure, you mean that instead of `http://www.iconfinder.com/ajax/download/png/?id=43350&s=128`, it's better to use `http://cdn1.iconfinder.com/data/icons/dragon/128/User.png`? – Marius Butuc May 05 '11 at 16:16