使用Elixir生成缩写头像

时间:2015-07-13 23:51:58

标签: elixir phoenix-framework

我正在研究Elixir,并希望提供头像服务。如果用户没有头像,想要在其上创建一个首字母,如下所示:

enter image description here

我真的没有任何想法从哪里开始或如何做到这一点。

2 个答案:

答案 0 :(得分:4)

使用Mogrify。这是一个Elixir-ImageMagick集成。

答案 1 :(得分:4)

您可以使用ImageMagick执行此操作。只需通过System.cmd调用convert命令并将选项传递给它。这是一个简单的示例,说明如何生成与您发布的图像类似的图像。我会把微调留给你。

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')

将生成以下图像:

enter image description here