Rails:为用户分配一个随机的默认头像(ala Twitter)?

发布于 2024-10-03 20:59:02 字数 373 浏览 4 评论 0原文

我使用 Paperclip 来处理用户的头像,目前使用以下方法设置默认图像:

:default_url => '/images/img-missing_:style.png'

但我想做的是,当用户注册时,给他们一个随机头像,直到他们上传自己的头像。

Twitter 用它的“蛋”头像来做到这一点,并且可能有六种不同的颜色被分配。

我怎样才能做到这一点?

请注意,我不想每次调用用户时都显示随机头像,而是希望他们在注册时分配一个随机头像,并一直使用该头像,直到他们上传自己的头像。

I'm using Paperclip to handle avatar's for users and currently set a default image using:

:default_url => '/images/img-missing_:style.png'

But what I'd like to do is when a user registers, give them a random avatar that they get until they upload their own.

Twitter does this with its "egg" avatar and that have probably half a dozen different colors that get assigned.

How can I accomplish this?

Note, I'm not wanting to show a random avatar every time a call to the user is made, rather I want them to be assigned a random avatar when they register and have that one stick with them until they upload their own.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

香橙ぽ 2024-10-10 20:59:02

假设您有 10 个默认头像,您可以使用其用户 ID 的模数来确定显示哪一个。这将是一致的并且实施起来很简单。

Lets say you have 10 default avatars, you could use the modulus of their user ID to determine which one is displayed. This would be consistent and trivial to implement.

别在捏我脸啦 2024-10-10 20:59:02

您还可以使用用户名长度来确定使用什么图片作为默认头像,假设我使用用户名注册:villain(7个字符),

您可以使用7作为引用图像编号7的方法(例如, 7.png) 设置为默认头像。

为了使其看起来更加一致,请添加以下子句:

 0) First, check that the username falls within a plausible character length.. (say 6-14)
 1) if number is even, divide by 2 and use the result to pick up the image.
 2) if result is odd, use the number as it is to pick up the default avatar.

因此,在上面的例子中,您可以使用 7 作为幻数,并且可以将头像 7.png 作为该用户的默认值。

You can also use the User Name length as a way to determine what picture is used as default avatar, let's say i sign up with username: villain (which is 7 characters)

you can use 7 as a means to reference image number 7 (say, 7.png) to set as the default avatar.

To make it look more consistent, add in clauses like:

 0) First, check that the username falls within a plausible character length.. (say 6-14)
 1) if number is even, divide by 2 and use the result to pick up the image.
 2) if result is odd, use the number as it is to pick up the default avatar.

So, in my case above, you can use 7 as the magic number and may be avatar 7.png as default for this user..

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文