Skip to content

Instantly share code, notes, and snippets.

@thgiang
Created August 10, 2017 17:09
Show Gist options
  • Save thgiang/9aa1f5d787ef8ff21f1bff9a1ad16e19 to your computer and use it in GitHub Desktop.
Save thgiang/9aa1f5d787ef8ff21f1bff9a1ad16e19 to your computer and use it in GitHub Desktop.
Kunena auto avatar by UserName
(function($) {
var backgroundColors = ['#00AAF7','#FF6C00','#243341','#FF0D3E','#64BB21'];
$('.profilebox li img').each(function(){
if ($(this).attr('src').indexOf('nophoto.png') !== -1)
{
var userName = $(this).attr('alt').replace('\'s Avatar','');;
console.log(userName);
var matches = userName.match(/\b(\w)/g);
console.log(matches);
if (matches)
{
if (matches.length > 2)
{
matches = matches.slice(0, 2);
}
var firstLetter = matches.join('').toUpperCase();
var background = backgroundColors[hashCode(firstLetter)%backgroundColors.length];
$(this).parent().html('<span style="background: '+background+'; line-height: 100px; display: block; width: 100px; height: 100px; border-radius: 50%; text-align: center; color: #FFF;">'+firstLetter+'</span>');
}
}
});
function hashCode(str) { // java String#hashCode
var hash = 0;
for (var i = 0; i < str.length; i++) {
hash = str.charCodeAt(i) + ((hash << 5) - hash);
}
return hash;
}
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment