Gravatar Logo

Gravatar Image Tag Plugin

Rails view helper for grabbing Gravatar images. The goal here is to be configurable and have those configuration points documented!

Install as a Ruby Gem

sudo gem install gravatar_image_tag

Include the following line in your Rails environment

config.gem 'gravatar_image_tag'

Then ensure the gem is installed if you did not already run the gem install as noted above.

rake gems:install

Install as a Ruby on Rails Plugin

The traditional way.

./script/plugin install git://github.com/mdeering/gravatar_image_tag.git

or the old-school but still c00l way!

piston import git://github.com/mdeering/gravatar_image_tag.git vendor/plugins/gravatar_image_tag

or for all you hip gitsters.

git submodule add git://github.com/mdeering/gravatar_image_tag.git vendor/plugins/gravatar_image_tag git submodule init

Usage

Once you have installed it as a plugin for your rails app usage is simple.

gravatar_image_tag('[email protected]'.gsub('spam', 'mdeering'), :alt => 'Michael Deering')

Boom here is my gravatar Michael Deering

Configuration Points

Setting the default image

If no default image is given in either an initializer or through the options passed to the gravatar_image_tag then the default image from the gravatar site will be displayed.

gravatar_image_tag('junk', :alt => 'Default Gravatar Image')

Splat the default gravatar image Default Gravatar Image

You can specify the default image through an initializer as follows:

# config/initializers/gravatar_defaults.rb
ActionView::Base.default_gravatar_image  = 'http://assets.yourhost.com/images/default_gravatar.png'

You can also specify the default image to fallback to or override the one set in the above initializer by passing the :gravatar => { :default => ‘http://assets.yourhost.com/images/override_gravatar.png’ } option to the gravatar_image_tag call. All other options will be forwarded onto the image_tag.

gravatar_image_tag('junk', :alt => 'Github Default Gravatar', :gravatar => { :default => 'http://github.com/images/gravatars/gravatar-80.png' })

Ka-Pow Github Default Gravatar

Other options supported besides an image url to fall back on include the following:

  • :identicon Identicon Avatar
  • :monsterid Monster Id Avatar
  • :wavatar Wavatar Avatar
  • 404: Not Found

Setting the default image size

If no default image size is given in either the plugin configuration or through the options passed to the gravatar_image_tag then the default image size from the gravatar site will be displayed (80px x 80px at the time of this writing).

You can specify your default image size through the plugin configuration by adding the following line to your rails environment.

# config/initializers/gravatar_defaults.rb
ActionView::Base.default_gravatar_size  = 120

You can also specify the image size or override the one set in the plugin configuration by passing the :gravatar => { :size => 50 } option to the gravatar_image_tag call. All other options will be forwarded on to the image_tag call.

gravatar_image_tag('[email protected]'.gsub('spam', 'mdeering'), :alt => 'Michael Deering', :class => 'some-class', :gravatar => { :size => 15 })

Mini Me! Michael Deering

Grabbing gravatars from the secure gravatar server.

You can make a request for a gravatar from the secure server at https://secure.gravatar.com by passing the :gravatar => { :secure => true } option to the gravatar_image_tag call.

gravatar_image_tag('[email protected]'.gsub('spam', 'mdeering'), :alt => 'Michael Deering', :gravatar => { :secure => true } )

Delivered by a secure url! Michael Deering

You globally set the gravatar image tag to always use the secure server by setting the following. By default this is set to false.

# config/initializers/gravatar_defaults.rb
ActionView::Base.secure_gravatar  = true

Using Gravatar’s built in rating system

By default only those gravatars that have a rating of ‘g’ are returned. You can change this by passing in the rating parameter as follows. Possible options for ratings are g, pg, r, or x.

gravatar_image_tag('[email protected]'.gsub('spam', 'mdeering'), :alt => 'Michael Deering', :gravatar => { :rating => 'pg' } )

You can globally set the gravatar image tag rating by setting the following. There is no default value, the default of ‘g’ is being set by the gravatar service itself.

# config/initializers/gravatar_defaults.rb
ActionView::Base.default_gravatar_rating  = 'r'

Specifying a filetype

By default no filetype extension is given in the produced gravatar urls but of your application requires it you can specify one to be added to the generated url.

gravatar_image_tag('[email protected]'.gsub('spam', 'mdeering'), :alt => 'Michael Deering', :gravatar => { :filetype => :gif } )

You can globally set the filetype extension as follows.

# config/initializers/gravatar_defaults.rb
ActionView::Base.default_gravatar_filetype  = :gif

Credits

The ideas an methods for this plugin are from expanding upon my original blog post Adding Gravatar To Your Website Or Blog

Copyright © 2009 Michael Deering, released under the MIT license