用Elixir生成首字母缩写 [英] Generating initials avatar with Elixir

查看:96
本文介绍了用Elixir生成首字母缩写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究Elixir,并希望提供头像服务.如果用户没有头像,则要在头像上写一个头像,如下所示:

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.

推荐答案

您可以使用 ImageMagick 为此.只需通过 System.cmd convert命令>并将选项传递给它.这是一个简单的示例,该示例如何生成与您发布的图像相似的图像.我将把微调留给您.

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

例如这个

generate('out.png', 'JD')

将生成以下图像:

这篇关于用Elixir生成首字母缩写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆