我正在研究 Elixir 并希望提供头像服务。如果用户没有头像,想要制作一个带有姓名首字母的头像,如下所示:
我真的不知道从哪里开始或如何做到这一点。
我正在研究 Elixir 并希望提供头像服务。如果用户没有头像,想要制作一个带有姓名首字母的头像,如下所示:
我真的不知道从哪里开始或如何做到这一点。
您可以使用ImageMagick来执行此操作。只需convert
通过调用命令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')
将生成以下图像:
使用Mogrify。这是一个 Elixir-ImageMagick 集成。