/// <summary> /// Generate the profile card for a given user and returns the generated image path /// </summary> /// <param name="user"></param> /// <returns></returns> public async Task <string> GenerateProfileCard(IUser user) { ulong userId = user.Id; uint xpTotal = _databaseService.GetUserXp(userId); uint xpRank = _databaseService.GetUserRank(userId); int karma = _databaseService.GetUserKarma(userId); uint level = _databaseService.GetUserLevel(userId); uint karmaRank = _databaseService.GetUserKarmaRank(userId); double xpLow = GetXpLow((int)level); double xpHigh = GetXpHigh((int)level); uint xpShown = (uint)(xpTotal - xpLow); uint maxXpShown = (uint)(xpHigh - xpLow); float percentage = (float)xpShown / maxXpShown; var u = user as IGuildUser; IRole mainRole = null; foreach (ulong id in u.RoleIds) { IRole role = u.Guild.GetRole(id); if (mainRole == null) { mainRole = u.Guild.GetRole(id); } else if (role.Position > mainRole.Position) { mainRole = role; } } using (MagickImageCollection profileCard = new MagickImageCollection()) { SkinData skin = GetSkinData(); ProfileData profile = new ProfileData { Karma = karma, KarmaRank = karmaRank, Level = level, MainRoleColor = mainRole.Color, MaxXpShown = maxXpShown, Nickname = (user as IGuildUser).Nickname, UserId = userId, Username = user.Username, XpHigh = xpHigh, XpLow = xpLow, XpPercentage = percentage, XpRank = xpRank, XpShown = xpShown, XpTotal = xpTotal }; MagickImage background = new MagickImage($"{_settings.ServerRootPath}/skins/{skin.Background}"); string avatarUrl = user.GetAvatarUrl(ImageFormat.Auto, 256); if (string.IsNullOrEmpty(avatarUrl)) { profile.Picture = new MagickImage($"{_settings.ServerRootPath}/images/default.png"); } else { try { Stream stream; using (var http = new HttpClient()) { stream = await http.GetStreamAsync(new Uri(avatarUrl)); } profile.Picture = new MagickImage(stream); } catch (Exception e) { Console.WriteLine(e); profile.Picture = new MagickImage($"{_settings.ServerRootPath}/images/default.png"); } } profile.Picture.Resize((int)skin.AvatarSize, skin.AvatarSize); profileCard.Add(background); foreach (var layer in skin.Layers) { if (layer.Image != null) { MagickImage image = layer.Image.ToLower() == "avatar" ? profile.Picture : new MagickImage($"{_settings.ServerRootPath}/skins/{layer.Image}"); background.Composite(image, (int)layer.StartX, (int)layer.StartY, CompositeOperator.Over); } MagickImage l = new MagickImage(MagickColors.Transparent, (int)layer.Width, (int)layer.Height); foreach (var module in layer.Modules) { module.GetDrawables(profile).Draw(l); } background.Composite(l, (int)layer.StartX, (int)layer.StartY, CompositeOperator.Over); } using (IMagickImage result = profileCard.Mosaic()) { result.Write($"{_settings.ServerRootPath}/images/profiles/{user.Username}-profile.png"); } } return($"{_settings.ServerRootPath}/images/profiles/{user.Username}-profile.png"); }
public async Task <string> GenerateProfileCard(IUser user) { try { var backgroundPath = SettingsHandler.LoadValueString("serverRootPath", JsonFile.Settings) + "/images/background.png"; var foregroundPath = SettingsHandler.LoadValueString("serverRootPath", JsonFile.Settings) + "/images/foreground.png"; Image <Rgba32> profileCard = ImageSharp.Image.Load(backgroundPath); Image <Rgba32> profileFg = ImageSharp.Image.Load(foregroundPath); Image <Rgba32> avatar; Stream stream; string avatarUrl = user.GetAvatarUrl(); ulong userId = user.Id; if (string.IsNullOrEmpty(avatarUrl)) { avatar = ImageSharp.Image.Load( SettingsHandler.LoadValueString("serverRootPath", JsonFile.Settings) + "/images/default.png"); } else { try { using (var http = new HttpClient()) { stream = await http.GetStreamAsync(new Uri(avatarUrl)); } avatar = ImageSharp.Image.Load(stream); } catch (Exception e) { Console.WriteLine(e); avatar = ImageSharp.Image.Load( SettingsHandler.LoadValueString("serverRootPath", JsonFile.Settings) + "/images/default.png"); } } var xpWidth = 275; var xpHeight = 17; var xpX = 131; var xpY = 42; uint xp = _databaseService.GetUserXp(userId); uint rank = _databaseService.GetUserRank(userId); int karma = _databaseService.GetUserKarma(userId); uint level = _databaseService.GetUserLevel(userId); int karmaRank = _databaseService.GetUserKarmaLevel(userId); /*uint xp = 0; * uint rank = 26; * int karma = 300; * int karmaRank = 6; * uint level = 126;*/ double xpLow = GetXpLow((int)level); double xpHigh = GetXpHigh((int)level); xp = (uint)(xpLow + ((xpHigh - xpLow) / 2)); Console.WriteLine("XP Low: " + xpLow + ", XP High: " + xpHigh); float endX = xpX + (float)((xp - xpLow) / (xpHigh - xpLow) * xpWidth); profileCard.DrawImage(profileFg, 100f, new Size(profileFg.Width, profileFg.Height), Point.Empty); var u = user as IGuildUser; IRole mainRole = null; foreach (ulong id in u.RoleIds) { IRole role = u.Guild.GetRole(id); if (mainRole == null) { mainRole = u.Guild.GetRole(id); } else if (role.Position > mainRole.Position) { mainRole = role; } } Color c = mainRole.Color; var brush = new RecolorBrush <Rgba32>(Rgba32.White, new Rgba32(c.R, c.G, c.B), .25f); // role profileCard.Fill(brush, new RectangleF(11, 11, 113, 113)); // avatar profileCard.DrawImage(avatar, 100f, new Size(111, 111), new Point(12, 12)); // name profileCard.DrawText(user.Username + '#' + user.Discriminator, _nameFont, Rgba32.FromHex("#676767"), new Point(131, 11)); // xp - back // top var rgba = Rgba32.FromHex("#c5c5c7"); profileCard.Fill(rgba, new RectangleF(xpX, xpY, xpWidth, 1)); // bottom profileCard.Fill(rgba, new RectangleF(xpX, xpY + xpHeight - 1, xpWidth, 1)); // left profileCard.Fill(rgba, new RectangleF(xpX, xpY, 1, xpHeight)); // right profileCard.Fill(rgba, new RectangleF(xpX + xpWidth - 1, xpY, 1, xpHeight)); // xp - front profileCard.Fill(rgba, new RectangleF(xpX + 2, xpY + 2, endX, xpHeight - 4)); // xp - number rgba = Rgba32.FromHex("#676767"); /*profileCard.DrawText(xp + "/" + xpHigh, _xpFont, rgba, * new Point(xpX + (xpWidth / 2), xpY - 2), new TextGraphicsOptions() * { * HorizontalAlignment = HorizontalAlignment.Center, * ApplyKerning = true * });*/ int percentage = (int)Math.Round((double)(100 * xp) / xpHigh); profileCard.DrawText(xp + "/" + xpHigh + " (" + percentage + "%)", _xpFont, rgba, new Point(xpX + (xpWidth / 2), xpY - 2), new TextGraphicsOptions() { HorizontalAlignment = HorizontalAlignment.Center, ApplyKerning = true } ); // server rank profileCard.DrawText("Level Rank", _ranksFont, rgba, new Point(235, 60)); // server rank value profileCard.DrawText("#" + rank, _ranksFont, rgba, new Point(391, 60), new TextGraphicsOptions() { HorizontalAlignment = HorizontalAlignment.Right }); // karma rank profileCard.DrawText("Karma Points", _ranksFont, rgba, new Point(235, 80)); // karma points value profileCard.DrawText(karma.ToString(), _ranksFont, rgba, new Point(391, 80), new TextGraphicsOptions() { HorizontalAlignment = HorizontalAlignment.Right }); // karma rank profileCard.DrawText("Karma Rank", _ranksFont, rgba, new Point(235, 100)); // karma points value profileCard.DrawText("#" + karmaRank, _ranksFont, rgba, new Point(391, 100), new TextGraphicsOptions() { HorizontalAlignment = HorizontalAlignment.Right }); // level text var font = new Font(_levelTextFont.Family, _levelTextFont.Size, FontStyle.Bold); profileCard.DrawText("LEVEL", font, rgba, new Point(176, 56), new TextGraphicsOptions() { HorizontalAlignment = HorizontalAlignment.Center }); // actual level font = new Font(_levelNumberFont.Family, _levelNumberFont.Size, FontStyle.Bold); profileCard.DrawText(level.ToString(), font, rgba, new Point(176, 78), new TextGraphicsOptions() { HorizontalAlignment = HorizontalAlignment.Center }); /*profileCard.Resize(400, 120);*/ profileCard.Save(SettingsHandler.LoadValueString("serverRootPath", JsonFile.Settings) + $"/images/profiles/{user.Username}-profile.png"); } catch (Exception e) { Console.WriteLine(e.Message); } return(SettingsHandler.LoadValueString("serverRootPath", JsonFile.Settings) + $"/images/profiles/{user.Username}-profile.png"); }