使用 Elixir 生成首字母头像
Generating initials avatar with Elixir
我正在研究 Elixir 并希望提供头像服务。如果用户没有头像,想制作一个上面有他们名字首字母的头像,像这样:
我真的不知道从哪里开始或如何做。
使用Mogrify。它是 Elixir-ImageMagick 集成。
您可以使用 ImageMagick to do this. Simply call the convert
command via System.cmd
并将选项传递给它。这是一个简单的示例,说明如何生成与您发布的图像相似的图像。我会把微调留给你。
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
比如这个
generate('out.png', 'JD')
将生成如下图像:
我正在研究 Elixir 并希望提供头像服务。如果用户没有头像,想制作一个上面有他们名字首字母的头像,像这样:
我真的不知道从哪里开始或如何做。
使用Mogrify。它是 Elixir-ImageMagick 集成。
您可以使用 ImageMagick to do this. Simply call the convert
command via System.cmd
并将选项传递给它。这是一个简单的示例,说明如何生成与您发布的图像相似的图像。我会把微调留给你。
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
比如这个
generate('out.png', 'JD')
将生成如下图像: