Creating an initials avatar with Elixir

I am working on Elixir and want to make an avatar service. If the user doesn't have an avatar, you want to make one with initials on it, for example:

enter image description here

I really don't have the faintest idea where to start or how to do it.

+3


source to share


2 answers


You can use ImageMagick for this. Just call the command convert

via System.cmd

and pass parameters to it. Here's a simple example of creating an image similar to the one you posted. I'll leave the fine tuning 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 create the following image:

enter image description here

+4


source


Use Mogrify . This is the Elixir-ImageMagick integration.



+4


source







All Articles