Fixed aspect ratio of user images wrong, added new user default image

This commit is contained in:
2025-11-22 17:11:00 +01:00
parent c7c47bff9d
commit be8257e012
5 changed files with 30 additions and 2 deletions

View File

@@ -2,6 +2,7 @@ using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Formats.Webp;
using SixLabors.ImageSharp.Formats;
using Berufsschule_HAM.Models;
using SixLabors.ImageSharp.Processing;
namespace Berufsschule_HAM.Helpers;
@@ -30,6 +31,23 @@ public static class ImageHelper
using var image = Image.Load(decoderOptions, inputStream);
int minDimension = Math.Min(image.Width, image.Height);
var cropRectangle = new Rectangle(
(image.Width - minDimension) / 2,
(image.Height - minDimension) / 2,
minDimension,
minDimension);
image.Mutate(x =>
{
x.Crop(cropRectangle);
x.Resize(new ResizeOptions
{
Size = new Size(size, size),
Mode = ResizeMode.Crop
});
});
using var outputStream = new MemoryStream();
image.Save(outputStream, new WebpEncoder());
result = outputStream.ToArray();
@@ -38,6 +56,16 @@ public static class ImageHelper
return result;
}
public static byte[] GetDefaultUserImage(int size = 48)
{
return ResizeAndConvertToWebp(GetDefaultUserImageAsBase64(), size);
}
public static string GetDefaultUserImageAsBase64()
{
return Convert.ToBase64String(File.ReadAllBytes("wwwroot/user_default.webp"));
}
public static async Task PreloadUsers(IEnumerable<UserModel> userModels, UserImagePreloadType? preloadType)
{
if (preloadType is null || preloadType == UserImagePreloadType.None) return;