mirror of
https://github.com/LD-Reborn/Berufsschule_HAM.git
synced 2025-12-20 06:51:55 +00:00
Added user photo resizing
This commit is contained in:
42
src/Helpers/ImageHelper.cs
Normal file
42
src/Helpers/ImageHelper.cs
Normal 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());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user