如何拥有一个默认的外部 Gravatar 并且能够正确调整大小?

发布于 2024-11-04 21:32:22 字数 880 浏览 0 评论 0原文

为了在我的 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

不应用大小选项,并且头像以全尺寸渲染(在本例中为 128px)。

我做错了什么,或者我怎样才能实现这种组合?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

陌上芳菲 2024-11-11 21:32:22

Gravatar 不会为您调整默认图像的大小。我假设如果它没有找到您提供的电子邮件的头像,则默认情况下它只会对 ulr 进行 302 秒。看起来 iconfinder url 中的 '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]}"

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]}"
苄①跕圉湢 2024-11-11 21:32:22

图标查找器在这里。您不想链接到下载脚本。相反,只需获取图像本身的 URL,这样您就不会获得大量标头信息。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文