您如何拥有默认的Gravatar,它在外部并且实际上可以正确调整大小? [英] How do you have a default Gravatar that is external and that actually resizes properly?

查看:88
本文介绍了您如何拥有默认的Gravatar,它在外部并且实际上可以正确调整大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要在我的Rails3应用程序中实现 Gravatar ,我正在使用 gravatar_image_tag 辅助工具中的gem,但是混合2个配置选项时出现问题:

  1. 如果用户的电子邮件中没有附加图片,则会显示默认图片;但我希望它引用外部文件(例如,http://www.iconfinder.com/ajax/download/png/?id=43350&s=128而不是:identicon或其他文件)
  2. 我还希望图像调整大小,例如50px.

两个选项都可以独立运行,但是当我将它们放在一起时:

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

未应用size选项,并且图像以其全尺寸(在这种情况下为128px)呈现.

我在做错什么,或者如何实现这种结合?

解决方案

Gravatar不会为您调整默认图像的大小.我认为,如果找不到您发送给它的电子邮件的惯用图形,则默认情况下仅向ulr发送302s.看起来iconfinder网址中的's'参数与您要尝试获取的大小相同,但该图标的大小不为50px,只能使用128、256和512

示例:

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

如果您想要图标的50px和80px版本,我会将其分别保存为您的应用程序public/image目录,分别为default_gravatar_50.png和default_gravatar_80.png并像这样更改您的方法.

结束

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

或者,如果您在图标查找器中找到所需大小的图标,请更改默认选项的设置.

options[:default] = "http://www.iconfinder.com/ajax/download/png/?id=43350&s=#{options[:size]}"

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?

解决方案

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

这篇关于您如何拥有默认的Gravatar,它在外部并且实际上可以正确调整大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆