Avatar restored

main
Paul Schneider 1 year ago
parent d7e2cf8804
commit a4932d08f1
8 changed files with 1145 additions and 78 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 38 KiB

After

Width:  |  Height:  |  Size: 39 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 11 KiB

After

Width:  |  Height:  |  Size: 11 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 3.1 KiB

After

Width:  |  Height:  |  Size: 3.0 KiB

@ -1,6 +1,5 @@
using System.Drawing;
using System.Drawing.Imaging;
using System.Security.Claims; using System.Security.Claims;
using Microsoft.AspNetCore.Html; using Microsoft.AspNetCore.Html;
using Microsoft.Extensions.FileProviders; using Microsoft.Extensions.FileProviders;
@ -9,6 +8,8 @@ using Yavsc.Models;
using Yavsc.Models.FileSystem; using Yavsc.Models.FileSystem;
using Yavsc.Models.Streaming; using Yavsc.Models.Streaming;
using Yavsc.ViewModels; using Yavsc.ViewModels;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Processing;
namespace Yavsc.Helpers namespace Yavsc.Helpers
{ {
@ -28,43 +29,12 @@ namespace Yavsc.Helpers
using (var org = formFile.OpenReadStream()) using (var org = formFile.OpenReadStream())
{ {
Image i = Image.FromStream(org); using Image image = Image.Load(org);
using (Bitmap source = new Bitmap(i)) image.Save(destFileName);
{
source.Save(destFileName, ImageFormat.Png);
}
} }
return item; return item;
} }
/// <summary>
/// Create avatars
/// </summary>
/// <param name="user"></param>
/// <param name="source"></param>
private static void CreateAvatars(this ApplicationUser user, Bitmap source)
{
var dir = Config.SiteSetup.Avatars;
var name = user.UserName + ".png";
var smallname = user.UserName + ".s.png";
var xsmallname = user.UserName + ".xs.png";
using (Bitmap newBMP = new Bitmap(source, 128, 128))
{
newBMP.Save(Path.Combine(
dir, name), ImageFormat.Png);
}
using (Bitmap newBMP = new Bitmap(source, 64, 64))
{
newBMP.Save(Path.Combine(
dir, smallname), ImageFormat.Png);
}
using (Bitmap newBMP = new Bitmap(source, 32, 32))
{
newBMP.Save(Path.Combine(
dir, xsmallname), ImageFormat.Png);
}
}
public static string GetAvatarUri(this ApplicationUser user) public static string GetAvatarUri(this ApplicationUser user)
{ {
return $"/{Config.SiteSetup.Avatars}/{user.UserName}.png"; return $"/{Config.SiteSetup.Avatars}/{user.UserName}.png";
@ -257,42 +227,15 @@ namespace Yavsc.Helpers
{ {
FileName = user.UserName + ".png" FileName = user.UserName + ".png"
}; };
var destFileName = Path.Combine(Config.SiteSetup.Avatars, item.FileName);
var fi = new FileInfo(destFileName);
if (fi.Exists) item.Overriden = true;
Rectangle cropRect = new Rectangle();
using (var org = formFile.OpenReadStream()) using (var org = formFile.OpenReadStream())
{ {
Image i = Image.FromStream(org); using Image image = Image.Load(org);
using (Bitmap source = new Bitmap(i)) image.Mutate(x=>x.Resize(128,128));
{ image.Save(Path.Combine(Config.SiteSetup.Avatars,item.FileName));
if (i.Width != i.Height) image.Mutate(x=>x.Resize(64,64));
{ image.Save(Path.Combine(Config.SiteSetup.Avatars,user.UserName + ".s.png"));
if (i.Width > i.Height) image.Mutate(x=>x.Resize(32,32));
{ image.Save(Path.Combine(Config.SiteSetup.Avatars,user.UserName + ".xs.png"));
cropRect.X = (i.Width - i.Height) / 2;
cropRect.Y = 0;
cropRect.Width = i.Height;
cropRect.Height = i.Height;
}
else
{
cropRect.X = 0;
cropRect.Y = (i.Height - i.Width) / 2;
cropRect.Width = i.Width;
cropRect.Height = i.Width;
}
using (var cropped = source.Clone(cropRect, source.PixelFormat))
{
CreateAvatars(user,cropped);
}
}
}
} }
item.DestDir = Config.AvatarsOptions.RequestPath.ToUriComponent(); item.DestDir = Config.AvatarsOptions.RequestPath.ToUriComponent();
user.Avatar = $"{item.DestDir}/{item.FileName}"; user.Avatar = $"{item.DestDir}/{item.FileName}";

@ -69,10 +69,6 @@
@Model.Logins.Count [<a asp-controller="Manage" asp-action="ManageLogins">Manage</a>] @Model.Logins.Count [<a asp-controller="Manage" asp-action="ManageLogins">Manage</a>]
</dd> </dd>
<dt>Full name:</dt>
<dd>@Model.FullName [<a asp-controller="Manage" asp-action="SetFullName"
>@Model.FullName==null?"Set":"Modify"</a>]</dd>
<dt>Address:</dt> <dt>Address:</dt>
<dd>@Model.PostalAddress <dd>@Model.PostalAddress
[<a asp-controller="Manage" asp-action="SetAddress" [<a asp-controller="Manage" asp-action="SetAddress"

@ -1,10 +1,10 @@
@model PerformerProfile @model PerformerProfile
@{ ViewData["Title"] = "Edit your avatar"; } @{ ViewData["Title"] = "Edit your avatar"; }
@section header{ @section header{
<link href="~/css/main/dropzone.css" rel="stylesheet"> <script src="https://unpkg.com/dropzone@5/dist/min/dropzone.min.js"></script>
<link rel="stylesheet" href="https://unpkg.com/dropzone@5/dist/min/dropzone.min.css" type="text/css" />
} }
@section scripts{ @section scripts{
<script src="~/js/dropzone.js"></script>
<script> <script>
$(document).ready(function() { $(document).ready(function() {
Dropzone.options.postavatar = { Dropzone.options.postavatar = {
@ -23,10 +23,10 @@ $(document).ready(function() {
} }
<img src="~/avatars/@(User.Identity.Name).png"> <img src="~/avatars/@(User.Identity.Name).png">
<form id="postavatar" class="dropzone" method="post" enctype="multipart/form-data"> <form id="postavatar" action="/api/setavatar" class="dropzone" method="post" enctype="multipart/form-data">
<div class="fallback"> <div class="fallback">
<input name="Avatar" type="file" id="Avatar" /> <input name="Avatar" type="file" id="Avatar" />
</div> </div>
(IconWebUploadSpecification"])
@Html.AntiForgeryToken() @Html.AntiForgeryToken()
</form> </form>

@ -43,6 +43,8 @@
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" /> <PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="8.0.2" IncludeAssets="All" /> <PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="8.0.2" IncludeAssets="All" />
<PackageReference Include="AsciiDocNet" Version="1.0.0" /> <PackageReference Include="AsciiDocNet" Version="1.0.0" />
<PackageReference Include="SixLabors.ImageSharp" Version="3.1.6" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="../Yavsc.Server/Yavsc.Server.csproj" /> <ProjectReference Include="../Yavsc.Server/Yavsc.Server.csproj" />

File diff suppressed because it is too large Load Diff

After

Width:  |  Height:  |  Size: 48 KiB

Loading…