using System.IO;
using System.Threading.Tasks;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Formats.Jpeg;
using SixLabors.ImageSharp.Processing;


namespace RobotNet.IdentityServer.Services;

public class UserImageService
{
    public async Task<(byte[] ImageBytes, string ContentType)> ResizeAndConvertAsync(Stream input)
    {
        using var image = await Image.LoadAsync(input);
        image.Mutate(x => x.Resize(new ResizeOptions
        {
            Size = new Size(300, 300),
            Mode = ResizeMode.Crop 
        }));

        using var ms = new MemoryStream();
        await image.SaveAsJpegAsync(ms, new JpegEncoder { Quality = 90 });

        return (ms.ToArray(), "image/jpeg");
    }
}
