AvatarMagick is a Dragonfly plugin for generating Gmail style avatars like those pictured below
The plugin is configurable and options can be set to control background color, text color, font, and size. For more information, please see this blog post and accompanying example project.
The plugin works with the latest versions (1.x) of Dragonfly with support for older versions (0.9.x) in the pipeline.
It is assumed you're already using Dragonfly. To install AvatarMagick, simply add this line to your application's Gemfile:
gem 'avatar_magick'
And then execute:
$ bundle
Once installed, you'll need to register the AvatarMagick plugin along with the ImageMagick plugin that's included with the Dragonfly gem.
If you're using Dragonfly within your Rails application, you'll already have a config/initializers/dragonfly.rb
file where your Dragonfly configuration settings are stored. Edit the file and add the plugin within the configure
block
Dragonfly.app.configure do
plugin :imagemagick
plugin :avatarmagick
# rest of settings...
end
Simply add the plugin within the configure
block
require 'dragonfly'
Dragonfly.app.configure do
plugin :imagemagick
plugin :avatarmagick
end
You can configure defaults for text color, background color, size, and font. If not specified, AvatarMagick will use the following defaults
color: FFFFFF
background_color: 000000
size: '120x120'
font: 'Arial-Regular'
To overwrite, simply provide new values for any of the above within Dragonfly's configure
block. For example, if you wanted the default background to be red (FF0000) instead of black (000000) and the default size to be 200px by 200px
Dragonfly.app.configure do
plugin :imagemagick
plugin :avatarmagick, background_color: 'FF0000', size: '200x200'
end
Or, if you wanted to change the default font
Dragonfly.app.configure do
plugin :imagemagick
plugin :avatarmagick, font: 'OpenSans'
end
To see what fonts are available, open up the terminal and type convert -list font
. You can select any font listed when configuring or when calling generate
.
Once the plugin is installed and registered, you can use it like any of the other built-in generators (text, plain, etc.)
image = Dragonfly.app.generate(:initial_avatar, "Bart Jedrocha")
will produce
The plugin will extract at most 3 initials from the passed-in string (e.g. Bill James Pheonix MacKenzie will produce an avatar with the initials BJP). This should be sufficient for the vast majority of cases.
You can pass options to control the background color, text color, size, and font used to generate the avatar
Dragonfly.app.generate(:initial_avatar, "Martin", {background_color: 'ff8f00'})
will produce
Dragonfly.app.generate(:initial_avatar, "James Hetfield", {background_color: 'f48fb1', color: '333333'})
will produce
Dragonfly.app.generate(:initial_avatar, "Amanda Smith", {background_color: '00695c', size: '200'})
will produce
Dragonfly.app.generate(:initial_avatar, "Oliver Murphy", {background_color: 'b71c1c', font: 'Georgia'})
will produce
After checking out the repo, run bash bin/setup
to install dependencies. Then, run ruby bin/console
for an interactive prompt that will allow you to experiment. Please note that before being able to use the generator, you'll need to register both the ImageMagick plugin and the AvatarMagic plugin
irb > Dragonfly.app.configure do
irb > plugin :imagemagick
irb > plugin :avatarmagick
irb > end
To install this gem onto your local machine, run bundle exec rake install
.
- Add support for older versions (0.9.x) of Dragonfly
- Fork it ( https://github.com/[my-github-username]/avatar_magick/fork )
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create a new Pull Request