Added user photo resizing

This commit is contained in:
2025-10-26 16:26:20 +01:00
parent 7226ee962c
commit 6c42ff500b
4 changed files with 66 additions and 9 deletions

View File

@@ -0,0 +1,42 @@
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;
namespace Berufsschule_HAM.Helpers;
public static class ImageHelper
{
public static string ResizeAndConvertToBase64(byte[] imageBytes, int size = 32)
{
size = Math.Min(size, 256);
using var inputStream = new MemoryStream(imageBytes);
using var image = Image.Load(inputStream);
// Optional: crop to square before resize
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(){});
return Convert.ToBase64String(outputStream.ToArray());
}
}