# Generate Letter Icon from Name instead of Username?

**URL:** https://meta.discourse.org/t/generate-letter-icon-from-name-instead-of-username/64167
**Category:** Development
**Created:** [6월 8, 2017, 4:30오후 UTC](https://meta.discourse.org/t/generate-letter-icon-from-name-instead-of-username/64167 "2017-06-08T16:30:33Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [6월 8, 2017, 4:30오후 UTC](https://meta.discourse.org/t/generate-letter-icon-from-name-instead-of-username/64167/1 "2017-06-08T16:30:33Z")

</div>

I’m doing an import and all of the usernames are integers. Most users have avatars, but those that don’t end up with something like ![](https://global.discourse-cdn.com/meta/original/3X/c/0/c0448394c86bf9de74b789ee08649c76bf299af2.png) as their avatar.

Is there some way to have them generated by their name rather than username?

Perhaps the importer can call `LetterAvatar::Identity.generate(name, SOME_SIZE)`?

---

<div class="post-metadata">

### Author: ![Falco](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/falco/32/179432_2.png) [@Falco](https://meta.discourse.org/u/Falco)
#### Post date: [6월 8, 2017, 4:47오후 UTC](https://meta.discourse.org/t/generate-letter-icon-from-name-instead-of-username/64167/2 "2017-06-08T16:47:05Z")

</div>

You can write a plugin (maybe a pr?) that will allow you to use the first name char on the setting `external system avatars url`.

Currently it accepts: “Allowed substitutions are {username} {first\_letter} {color} {size}”

Adding {name\_first \_letter} to that is the way to go.

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [6월 8, 2017, 4:51오후 UTC](https://meta.discourse.org/t/generate-letter-icon-from-name-instead-of-username/64167/3 "2017-06-08T16:51:44Z")

</div>

> [@Falco](#):
>
> external system avatars url.

Oh. So you’re saying that what I want is to be able to change `external system avatars url` to

```
/letter_avatar_proxy/v2/letter/{name_first_letter}/{color}/{size}.png

```

And that “all” I would have to do is make a PR that defined `name_first_letter`?

That seems “easy” if I knew where to find the file that defines those substitutions.

---

<div class="post-metadata">

### Author: ![Falco](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/falco/32/179432_2.png) [@Falco](https://meta.discourse.org/u/Falco)
#### Post date: [6월 8, 2017, 4:53오후 UTC](https://meta.discourse.org/t/generate-letter-icon-from-name-instead-of-username/64167/4 "2017-06-08T16:53:15Z")

</div>

> [@pfaffman](#):
>
> That seems “easy” if I knew where to find the file that defines those substitutions.

> <https://github.com/discourse/discourse/blob/038454bde2dc5f43aa1fff78f493f3b3245aba03/app/models/user.rb#L573-L585>

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [6월 8, 2017, 4:54오후 UTC](https://meta.discourse.org/t/generate-letter-icon-from-name-instead-of-username/64167/5 "2017-06-08T16:54:35Z")

</div>

OMG. I might actually submit a PR that doesn’t involve shell scripts.

---

<div class="post-metadata">

### Author: ![Falco](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/falco/32/179432_2.png) [@Falco](https://meta.discourse.org/u/Falco)
#### Post date: [6월 8, 2017, 4:57오후 UTC](https://meta.discourse.org/t/generate-letter-icon-from-name-instead-of-username/64167/6 "2017-06-08T16:57:07Z")

</div>

Well I would first start with a plugin, so you can get it immediately working.

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [6월 8, 2017, 5:02오후 UTC](https://meta.discourse.org/t/generate-letter-icon-from-name-instead-of-username/64167/7 "2017-06-08T17:02:14Z")

</div>

But it seems like I could make a PR that involved about 2 lines of code, something like

```plaintext
u=User.find_by_username(username)
name = some stuff if that search fails or there's no name
...
url.gsub! "{name_first_letter", name[0].downcase
...

```

And it’d take me a Long Time to figure out how to make a plugin do that.

And it’d need to be a bit more complicated not not needlessly do a username lookup every time, so it might not be as easy a PR as I’d thought, so maybe I"ll have to figure out the plugin after all. ☹

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [6월 8, 2017, 5:54오후 UTC](https://meta.discourse.org/t/generate-letter-icon-from-name-instead-of-username/64167/9 "2017-06-08T17:54:48Z")

</div>

I wouldn’t have thought it possible, but with your help, I did it!

Thanks, @falco!

> [@Discourse Avatar name\_first\_letter](https://meta.discourse.org/t/discourse-avatar-name-first-letter/64180):
>
> This plugin adds the ability to use name\_first\_letter in site setting external system avatars url. The default value for external system avatars url is /letter\_avatar\_proxy/v2/letter/{first\_letter}/{color}/{size}.png. This plugin allows you to use /letter\_avatar\_proxy/v2/letter/{name\_first\_letter}/{color}/{size}.png to have the avatar be the first letter of the name rather than username. If there is no name, the username is used. You must change the site setting for the plugin to have any …
