using Netina.Common.Models.Api;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Processing;

namespace Netina.Core.Utilities;

public static class ImageConvertor
{
    public static async Task<Stream> ImageResize(this FileUploadRequest fileUpload, Stream input, Stream output, int newWidth)
    {
        using var image = await Image.LoadAsync(input);
        if (image.Width > newWidth)
        {
            double ratio = (double)newWidth / image.Width;
            int new_Height = (int)Math.Round(image.Height * ratio);
            image.Mutate(x => x.Resize(newWidth, new_Height));
            image.Mutate(x => x.Resize(newWidth, new_Height));
        }
        await image.SaveAsJpegAsync(output);
        return output;
    }
}