少於 1 分鐘閱讀

Gravatar 介紹

gravatar是「Globally Recognized Avatar」的縮寫,中文翻譯就是全球認證的大頭貼,這個網站致力於提供大頭貼對應的服務。

當你在註冊一些使用此服務的網站的時候,例如GitHub、Slack、Quip等,你會發現你還沒上傳頭像,但你的頭像在註冊完成後,就自動出現在該網站中,這是因為你在gravatar註冊的頭像可以直接被存取來使用。每當你註冊帳號或登錄,只要填上Email就可以自動顯示大頭貼。

還記得當初是因為註冊了worldpress才接觸到這個網站,後來發現幾乎每個國外網站都會自動出現我的頭像,據說有50~60%的worldpress使用者擁有gravatar。

如果你的網站中,想要放上使用者頭像,就不能缺少這個功能

安裝gem

gravatar_image_tag

gem 'gravatar_image_tag'

記得重開rails s

使用方式

簡單輸出image tag

<%= gravatar_image_tag('xxx@mail.com') %>
<%= gravatar_image_tag('xxx@mail.com', :alt => 'Github Default Gravatar', :gravatar => { :size => 20, :default => 'https://assets.github.com/images/gravatars/gravatar-140.png' }) %>

直接輸出url

<%= gravatar_image_url('xxx@mail.com', filetype: :png, rating: 'pg', size: 15, secure:false ) %>

可以調整的初始值

## [path] config/initializers/gravatar_image_tag.rb
GravatarImageTag.configure do |config|
  config.default_image           = nil   ## 預設頭像,你可以自訂,否則會以此當預設值: [ 'http://example.com/images/default_gravitar.jpg', :identicon, :monsterid, :wavatar, 404 ].
  config.filetype                = nil   ## 可自訂檔案的格式 ['gif', 'jpg' or 'png']. 預設是png
  config.include_size_attributes = true  ## 寬高是否由gravatar定義(就是下方的size)
  config.rating                  = nil   ## 設定等級['G', 'PG', 'R', 'X']. 預設是 G
  config.size                    = nil   ## 圖片大小 (長寬像素:1..512). 預設是80, 就是 80px * 80px
  config.secure                  = false ## Set this to true if you require secure images on your pages.
end

效果

沒使用 使用(預設圖像) 使用 (依照email讀取頭像) 未命名.png

參考

  1. https://github.com/mdeering/gravatar_image_tag
  2. http://euri.ca/2013/how-many-people-use-gravatar/

更新時間:

留言