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:
I really haven't the slightest idea where to start or how to do this.
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:
Use Mogrify. It is a Elixir-ImageMagick integration.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With