How do you have a default Gravatar that is externa

2019-04-09 11:50发布

问题:

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?

回答1:

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]}"


回答2:

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.