Meme Generation API
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Kynareth/src/Managers/ImageMagiker.cs

62 lines
1.3 KiB

using Exsersewo.Common.Utilities;
using ImageMagick;
namespace Kynareth.Managers;
public class ImageMagiker : ImageModifier
{
public async Task<(MemoryStream data, string contentType)> GetLiquidRescaledImageAsync(string image, CancellationToken cancellationToken)
{
while (true)
{
if (cancellationToken.IsCancellationRequested)
{
return (null, null);
}
var imageStream = await HttpWebClient.GetStreamAsync(new Uri(image)).ConfigureAwait(false);
using var magikImage = new MagickImageCollection(imageStream);
Parallel.ForEach(magikImage, LiquidResizeFrame);
MemoryStream stream = new();
magikImage.Write(stream);
stream.Position = 0;
magikImage.Dispose();
string cType = "image/png";
if (magikImage.Count > 1) cType = "image/gif";
return (stream, cType);
}
}
static void LiquidResizeFrame(IMagickImage<byte> frame)
{
int originalWidth = frame.Width;
int originalHeight = frame.Height;
frame.Resize(800, 600);
frame.LiquidRescale(new MagickGeometry
{
X = 1,
Width = Convert.ToInt32(frame.Width * 0.5),
Height = Convert.ToInt32(frame.Height * 0.5)
});
frame.LiquidRescale(new MagickGeometry
{
X = 2,
Width = Convert.ToInt32(frame.Width * 1.5),
Height = Convert.ToInt32(frame.Height * 1.5)
});
frame.Resize(originalWidth, originalHeight);
}
}