Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

letter_avatar_simple

Package Overview
Dependencies
Maintainers
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

letter_avatar_simple

  • 1.0.0
  • Rubygems
  • Socket score

Version published
Maintainers
5
Created
Source

LetterAvatarSimple

Generate letter image avatars based on user initials.

Usage

LetterAvatarSimple.generate("foobar")
# => #<StringIO:0x000055b3804948a8>
Image of letter "F"

We can do multiple letters:

LetterAvatarSimple.generate("Dallas Smith")
# => #<StringIO:0x000055b380e6a058>
Image of letters "DS"

We can save to a file:

LetterAvatarSimple.generate_file("Dallas Smith")
# => #<File:/tmp/x20190527-19344-79m629.png>

LetterAvatarSimple.generate_file("Dallas Smith", filename: "/tmp/dallas.png")
# => #<File:/tmp/dallas.png>

We can specify the initials using LetterAvatarSimple::Identity:

i = LetterAvatarSimple::Identity.new("ZZ", "Dallas Smith")
LetterAvatarSimple.generate(i)
Image of letters "ZZ" in green

Image color is chosen from a palette based on username, so the same initials will (most likely) not share the same color:

i = LetterAvatarSimple::Identity.new("ZZ", "foobar")
LetterAvatarSimple.generate(i)
Image of letters "ZZ" in pink

The default palette is based on Google Inbox. You can change palettes:

LetterAvatarSimple.generate("foobar", palette: :i_want_hue)
Image of letter "F"

Or skip the palette feature and provide the desired color directly, in RGB tuple format:

LetterAvatarSimple.generate("foobar", color: [255,0,0])
Image of letter "F"

Other options that can be provided to customize image generation:

LetterAvatarSimple.generate(
  "foobar",
  size: 256,                            # => default 1024
  palette: :i_want_hue,                 # => default :google
  # warning: this bypasses the palette's color selection
  color: [255, 0, 0]                    # => default nil
  pointsize: 150,                       # => default 600
  font: "/tmp/path/to/font/file",       # => default is path to included Roboto font
  weight: 500,                          # => default 300
  fill_color: "rgba(255, 255, 255, 1)", # => default "rgba(255, 255, 255, 0.65)"
  annotate_position: "-0+10",           # => default "-0+5"
  filename: "/tmp/foo.png",             # => default is randomly generated tempfile path
)

About

Forked from letter_avatar, which was in turn extracted from Discourse.

Compared to letter_avatar, this gem:

  • Outputs StringIO binary data by default (but can write to files too!) instead of always writing files to the public directory. This way you can use image upload gems like shrine much easier
  • Supports keyword arguments for generating each image (don't need to edit global config or constants)
  • Simplifies custom palette loading and supports multiple custom palettes
  • Uses minimagick instead of homegrown ImageMagick shell execution
  • Does not come with model, view, or controller helpers (you should be using shrine)
  • Does not do caching (you should be using shrine)

Installation

gem "letter_avatar_simple"

System requirements

ImageMagick or GraphicsMagick - see MiniMagick requirements

Color palettes

Two color palettes are provided by default: :google and :i_want_hue

Both palettes use an MD5 digest of the username to select the color, so it's likely that two different usernames that share the same initial(s) will render with different colors.

If you need the same initials to always render with the same color, simply provide a custom palette using a customized letter_color method. See below for an example.

:google - Google Inbox palette

Image of letter "A" Image of letter "B" Image of letter "C" Image of letter "D" Image of letter "E" Image of letter "F" Image of letter "G" Image of letter "H" Image of letter "I" Image of letter "J" Image of letter "K" Image of letter "L" Image of letter "M" Image of letter "N" Image of letter "O" Image of letter "P" Image of letter "Q" Image of letter "R" Image of letter "S" Image of letter "T" Image of letter "U" Image of letter "V" Image of letter "W" Image of letter "X" Image of letter "Y" Image of letter "Z"

:i_want_hue - iWantHue palette

Custom palettes

You can add your own custom palette:

LetterAvatarSimple.palettes[:my_palette] = LetterAvatarSimple::Palette.new([
  [120, 132, 205],
  [91, 149, 249],
  [72, 194, 249],
  [69, 208, 226],
])
# The default method of selecting the color is by MD5 digest of the username. You
# can change this behavior by providing a letter_color method.
LetterAvatarSimple.palettes[:my_palette].tap do |p|
  def p.letter_color(identity)
    if identity.id == "admin"
      [255, 0, 0] # red
    elsif identity.id == "bozo"
      @palette.sample # random
    else
      # same initials = same color
      digest = Digest::MD5.hexdigest(identity.letters.to_s)
      @palette[digest[0...15].to_i(16) % @palette.length]
    end
  end
end

LetterAvatarSimple.generate_file("foobar", palette: :my_palette)

Configuration

The same options that can be passed to generate can be set as global defaults:

LetterAvatarSimple.config do |config|
  config.size              = 256
  config.palette           = :i_want_hue
  config.color             = [255, 0, 0]
  config.pointsize         = 150
  config.font              = "/tmp/path/to/font/file"
  config.weight            = 500
  config.fill_color        = "rgba(255, 255, 255, 1)"
  config.annotate_position = "-0+10"
end

FAQs

Package last updated on 28 May 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc