Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generating initials avatar with Elixir [closed]

I am working on Elixir and looking to make an avatar service. If the user doesn't have an avatar, want to make one with their initials on it, like so:

enter image description here

I really haven't the slightest idea where to start or how to do this.

like image 352
brandonhilkert Avatar asked Jan 08 '23 23:01

brandonhilkert


2 Answers

You can use ImageMagick to do this. Simply call the convert command via System.cmd and pass the options to it. Here's a simple example how to generate an image similar to the one you posted. I'll leave the fine-tuning up to you.

def generate(outfile, initials) do
  size = 512
  resolution = 72
  sampling_factor = 3
  System.cmd "convert", [
    "-density", "#{resolution * sampling_factor}",                # sample up
    "-size", "#{size*sampling_factor}x#{size*sampling_factor}",   # corrected size
    "canvas:#E0E0E0",                                             # background color
    "-fill", "#6D6D6D",                                           # text color
    "-font", "/Library/Fonts/Roboto-Bold.ttf",                    # font location
    "-pointsize", "300",                                          # font size
    "-gravity", "center",                                         # center text
    "-annotate", "+0+#{25 * sampling_factor}", initials,          # render text, move down a bit
    "-resample", "#{resolution}",                                 # sample down to reduce aliasing
    outfile
  ]
end

For example this

generate('out.png', 'JD')

will generate the following image:

enter image description here

like image 146
Patrick Oscity Avatar answered Mar 31 '23 09:03

Patrick Oscity


Use Mogrify. It is a Elixir-ImageMagick integration.

like image 28
Lenin Raj Rajasekaran Avatar answered Mar 31 '23 10:03

Lenin Raj Rajasekaran