mirror of
https://github.com/LD-Reborn/Berufsschule_HAM.git
synced 2025-12-20 06:51:55 +00:00
Merge pull request #324 from LD-Reborn/319-feature-user-image-pre-loading
319 feature user image pre loading
This commit is contained in:
@@ -106,15 +106,15 @@ public class HomeController : Controller
|
|||||||
}
|
}
|
||||||
if (size is null)
|
if (size is null)
|
||||||
{
|
{
|
||||||
return File(Convert.FromBase64String(user.JpegPhoto), "image/jpeg");
|
return File(Convert.FromBase64String(user.JpegPhoto), "image/webp");
|
||||||
}
|
}
|
||||||
if (size is not null)
|
if (size is not null)
|
||||||
{
|
{
|
||||||
AdminSettingsModel adminSettingsModel = await adminSettingsModelTask;
|
AdminSettingsModel adminSettingsModel = await adminSettingsModelTask;
|
||||||
size = Math.Min((int)size, adminSettingsModel.MaxDownloadableUserImageSize);
|
size = Math.Min((int)size, adminSettingsModel.MaxDownloadableUserImageSize);
|
||||||
}
|
}
|
||||||
string encodedFile = ImageHelper.ResizeAndConvertToBase64(Convert.FromBase64String(user.JpegPhoto), size ?? 32);
|
byte[] encodedFile = ImageHelper.ResizeAndConvertToWebp(user.JpegPhoto, size ?? 32);
|
||||||
return File(Convert.FromBase64String(encodedFile), "image/jpeg");
|
return File(encodedFile, "image/webp");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,41 +1,38 @@
|
|||||||
using SixLabors.ImageSharp;
|
using SixLabors.ImageSharp;
|
||||||
using SixLabors.ImageSharp.Processing;
|
|
||||||
using SixLabors.ImageSharp.Formats.Jpeg;
|
|
||||||
using System.IO;
|
|
||||||
using SixLabors.ImageSharp.Formats.Gif;
|
|
||||||
using SixLabors.ImageSharp.Processing.Processors.Quantization;
|
|
||||||
using SixLabors.ImageSharp.Formats.Png;
|
|
||||||
using SixLabors.ImageSharp.Formats.Webp;
|
using SixLabors.ImageSharp.Formats.Webp;
|
||||||
|
using SixLabors.ImageSharp.Formats;
|
||||||
|
|
||||||
namespace Berufsschule_HAM.Helpers;
|
namespace Berufsschule_HAM.Helpers;
|
||||||
|
|
||||||
public static class ImageHelper
|
public static class ImageHelper
|
||||||
{
|
{
|
||||||
public static string ResizeAndConvertToBase64(byte[] imageBytes, int size = 32)
|
public static Dictionary<int, Dictionary<string, byte[]>> ImageCache = [];
|
||||||
|
public static byte[] ResizeAndConvertToWebp(string imageString, int size = 32)
|
||||||
{
|
{
|
||||||
|
if (ImageCache.TryGetValue(size, out Dictionary<string, byte[]>? sizeCache)
|
||||||
|
&& sizeCache.TryGetValue(imageString, out byte[]? result) && result.Length > 0)
|
||||||
|
{
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
if (sizeCache is null) ImageCache[size] = [];
|
||||||
|
lock (ImageCache[size])
|
||||||
|
{
|
||||||
|
byte[] imageBytes = Convert.FromBase64String(imageString);
|
||||||
using var inputStream = new MemoryStream(imageBytes);
|
using var inputStream = new MemoryStream(imageBytes);
|
||||||
using var image = Image.Load(inputStream);
|
|
||||||
|
|
||||||
// Optional: crop to square before resize
|
var decoderOptions = new DecoderOptions
|
||||||
int minDimension = Math.Min(image.Width, image.Height);
|
{
|
||||||
var cropRectangle = new Rectangle(
|
TargetSize = new Size(size, size),
|
||||||
(image.Width - minDimension) / 2,
|
SkipMetadata = true
|
||||||
(image.Height - minDimension) / 2,
|
};
|
||||||
minDimension,
|
|
||||||
minDimension);
|
|
||||||
|
|
||||||
image.Mutate(x =>
|
using var image = Image.Load(decoderOptions, inputStream);
|
||||||
{
|
|
||||||
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());
|
||||||
return Convert.ToBase64String(outputStream.ToArray());
|
result = outputStream.ToArray();
|
||||||
|
ImageCache[size][imageString] = result;
|
||||||
|
}
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -12,6 +12,7 @@ var builder = WebApplication.CreateBuilder(args);
|
|||||||
builder.Services.Configure<LdapConfig>(builder.Configuration.GetSection("Ldap"));
|
builder.Services.Configure<LdapConfig>(builder.Configuration.GetSection("Ldap"));
|
||||||
|
|
||||||
builder.Services.AddLocalization(options => options.ResourcesPath = "Resources");
|
builder.Services.AddLocalization(options => options.ResourcesPath = "Resources");
|
||||||
|
builder.Services.AddResponseCaching();
|
||||||
|
|
||||||
builder.Services.AddControllersWithViews()
|
builder.Services.AddControllersWithViews()
|
||||||
.AddViewLocalization(Microsoft.AspNetCore.Mvc.Razor.LanguageViewLocationExpanderFormat.Suffix)
|
.AddViewLocalization(Microsoft.AspNetCore.Mvc.Razor.LanguageViewLocationExpanderFormat.Suffix)
|
||||||
|
|||||||
Reference in New Issue
Block a user