Fixed high memory usage and slow image encoding times for user images

This commit is contained in:
2025-11-20 15:14:06 +01:00
parent a80c4ba8fe
commit 6be72c1f5c

View File

@@ -1,11 +1,7 @@
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;
namespace Berufsschule_HAM.Helpers;
@@ -14,9 +10,14 @@ public static class ImageHelper
public static string ResizeAndConvertToBase64(byte[] imageBytes, int size = 32)
{
using var inputStream = new MemoryStream(imageBytes);
using var image = Image.Load(inputStream);
// Optional: crop to square before resize
var decoderOptions = new DecoderOptions
{
TargetSize = new Size(size, size)
};
using var image = Image.Load(decoderOptions, inputStream);
int minDimension = Math.Min(image.Width, image.Height);
var cropRectangle = new Rectangle(
(image.Width - minDimension) / 2,
@@ -35,7 +36,7 @@ public static class ImageHelper
});
using var outputStream = new MemoryStream();
image.Save(outputStream, new WebpEncoder(){});
return Convert.ToBase64String(outputStream.ToArray());
image.Save(outputStream, new WebpEncoder());
return Convert.ToBase64String(outputStream.GetBuffer(), 0, (int)outputStream.Length);
}
}