mirror of
https://github.com/LD-Reborn/Berufsschule_HAM.git
synced 2025-12-20 15:01:56 +00:00
Fixed aspect ratio of user images wrong, added new user default image
This commit is contained in:
@@ -102,7 +102,7 @@ public class HomeController : Controller
|
|||||||
UserModel? user = await _ldap.GetUserByUidAsync(uid, _ldap.UsersAttributes);
|
UserModel? user = await _ldap.GetUserByUidAsync(uid, _ldap.UsersAttributes);
|
||||||
if (user is null || user.JpegPhoto is null || user.JpegPhoto == "")
|
if (user is null || user.JpegPhoto is null || user.JpegPhoto == "")
|
||||||
{
|
{
|
||||||
return File("user_default.jpeg", "image/jpeg");
|
return File(ImageHelper.GetDefaultUserImage(size ?? 48), "image/webp");
|
||||||
}
|
}
|
||||||
if (size is null)
|
if (size is null)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -75,7 +75,7 @@ public class UsersController : Controller
|
|||||||
string? title = requestModel.Title;
|
string? title = requestModel.Title;
|
||||||
string userPassword = requestModel.UserPassword ?? "";
|
string userPassword = requestModel.UserPassword ?? "";
|
||||||
UserDescription? description = requestModel.Description;
|
UserDescription? description = requestModel.Description;
|
||||||
jpegPhoto ??= Convert.ToBase64String(System.IO.File.ReadAllBytes("wwwroot/user_default.jpeg")); // TODO: cleanup - make this a config setting
|
jpegPhoto ??= ImageHelper.GetDefaultUserImageAsBase64();
|
||||||
string uid = UsersHelper.CreateUsername(requestModel.Cn ?? "", requestModel.Sn ?? "");
|
string uid = UsersHelper.CreateUsername(requestModel.Cn ?? "", requestModel.Sn ?? "");
|
||||||
title ??= "";
|
title ??= "";
|
||||||
description ??= new() {Address = new(), BirthDate = "", Workplace = "", Groups = []};
|
description ??= new() {Address = new(), BirthDate = "", Workplace = "", Groups = []};
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ using SixLabors.ImageSharp;
|
|||||||
using SixLabors.ImageSharp.Formats.Webp;
|
using SixLabors.ImageSharp.Formats.Webp;
|
||||||
using SixLabors.ImageSharp.Formats;
|
using SixLabors.ImageSharp.Formats;
|
||||||
using Berufsschule_HAM.Models;
|
using Berufsschule_HAM.Models;
|
||||||
|
using SixLabors.ImageSharp.Processing;
|
||||||
|
|
||||||
namespace Berufsschule_HAM.Helpers;
|
namespace Berufsschule_HAM.Helpers;
|
||||||
|
|
||||||
@@ -30,6 +31,23 @@ public static class ImageHelper
|
|||||||
|
|
||||||
using var image = Image.Load(decoderOptions, inputStream);
|
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();
|
using var outputStream = new MemoryStream();
|
||||||
image.Save(outputStream, new WebpEncoder());
|
image.Save(outputStream, new WebpEncoder());
|
||||||
result = outputStream.ToArray();
|
result = outputStream.ToArray();
|
||||||
@@ -38,6 +56,16 @@ public static class ImageHelper
|
|||||||
return result;
|
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)
|
public static async Task PreloadUsers(IEnumerable<UserModel> userModels, UserImagePreloadType? preloadType)
|
||||||
{
|
{
|
||||||
if (preloadType is null || preloadType == UserImagePreloadType.None) return;
|
if (preloadType is null || preloadType == UserImagePreloadType.None) return;
|
||||||
|
|||||||
Binary file not shown.
|
Before Width: | Height: | Size: 1.2 KiB |
BIN
src/wwwroot/user_default.webp
Normal file
BIN
src/wwwroot/user_default.webp
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 12 KiB |
Reference in New Issue
Block a user