Files
Kynareth/src/Managers/ImageMagiker.cs
exsersewo e52c1be361 Tweaks
Add DogstatsD,
Move to inheritance for manipulators,
Add Metrics counting for endpoint
2023-03-26 13:21:19 +01:00

62 lines
1.3 KiB
C#

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);
}
}