mirror of
https://github.com/LD-Reborn/Berufsschule_HAM.git
synced 2025-12-20 06:51:55 +00:00
Fixed high memory usage and slow image encoding times for user images
This commit is contained in:
@@ -1,11 +1,7 @@
|
|||||||
using SixLabors.ImageSharp;
|
using SixLabors.ImageSharp;
|
||||||
using SixLabors.ImageSharp.Processing;
|
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;
|
||||||
|
|
||||||
@@ -14,9 +10,14 @@ public static class ImageHelper
|
|||||||
public static string ResizeAndConvertToBase64(byte[] imageBytes, int size = 32)
|
public static string ResizeAndConvertToBase64(byte[] imageBytes, int size = 32)
|
||||||
{
|
{
|
||||||
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
|
||||||
|
{
|
||||||
|
TargetSize = new Size(size, size)
|
||||||
|
};
|
||||||
|
|
||||||
|
using var image = Image.Load(decoderOptions, inputStream);
|
||||||
|
|
||||||
int minDimension = Math.Min(image.Width, image.Height);
|
int minDimension = Math.Min(image.Width, image.Height);
|
||||||
var cropRectangle = new Rectangle(
|
var cropRectangle = new Rectangle(
|
||||||
(image.Width - minDimension) / 2,
|
(image.Width - minDimension) / 2,
|
||||||
@@ -35,7 +36,7 @@ public static class ImageHelper
|
|||||||
});
|
});
|
||||||
|
|
||||||
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());
|
return Convert.ToBase64String(outputStream.GetBuffer(), 0, (int)outputStream.Length);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user