forked from mtusi2026/mtusi2026lab5
75 lines
3.1 KiB
C#
75 lines
3.1 KiB
C#
using ImageMagick;
|
|
using Messenger.Api.Jobs;
|
|
|
|
namespace Messenger.Api.Tests;
|
|
|
|
public class ThumbnailTests
|
|
{
|
|
[Theory]
|
|
[InlineData(321, 200, 256, 159)]
|
|
[InlineData(1, 800, 1, 256)]
|
|
[InlineData(7, 9, 7, 9)]
|
|
public void FitsWithoutUpscalingAndCompositesAlphaOntoWhite(int w, int h, int expectedW, int expectedH)
|
|
{
|
|
using var source = new MagickImage(MagickColors.Transparent, (uint)w, (uint)h);
|
|
using var input = new MemoryStream(source.ToByteArray(MagickFormat.Png));
|
|
var thumbnail = ImageVariant.CreateAll(input, "image/png", 40000000, 1)[0];
|
|
using var jpeg = new MagickImage(thumbnail.Bytes);
|
|
Assert.Equal(MagickFormat.Jpeg, jpeg.Format);
|
|
Assert.Equal((expectedW, expectedH), ((int)jpeg.Width, (int)jpeg.Height));
|
|
using var pixels = jpeg.GetPixels();
|
|
var color = pixels.GetPixel(0, 0).ToColor()!;
|
|
Assert.True(color.R >= 250 && color.G >= 250 && color.B >= 250);
|
|
}
|
|
|
|
[Fact]
|
|
public void AppliesExifOrientationBeforeSizing()
|
|
{
|
|
using var source = new MagickImage(MagickColors.Red, 320, 200);
|
|
var profile = new ExifProfile();
|
|
profile.SetValue(ExifTag.Orientation, (ushort)6);
|
|
source.SetProfile(profile);
|
|
source.Orientation = Orientation.RightTop;
|
|
using var input = new MemoryStream(source.ToByteArray(MagickFormat.Jpeg));
|
|
var variants = ImageVariant.CreateAll(input, "image/jpeg", 40000000, 1);
|
|
var thumb = Assert.Single(variants, v => v.Name == "thumbnail");
|
|
Assert.Equal((160, 256), (thumb.Width, thumb.Height));
|
|
foreach (var variant in variants)
|
|
{
|
|
using var decoded = new MagickImage(variant.Bytes);
|
|
Assert.Null(decoded.GetExifProfile());
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
public void UsesCentralCropAndFitsLargeWebPWithin1280Pixels()
|
|
{
|
|
using var source = new MagickImage(MagickColors.Red, 1600, 800);
|
|
using var center = new MagickImage(MagickColors.Lime, 1000, 800);
|
|
source.Composite(center, Gravity.Center, CompositeOperator.Over);
|
|
using var input = new MemoryStream(source.ToByteArray(MagickFormat.Png));
|
|
var variants = ImageVariant.CreateAll(input, "image/png", 40000000, 1);
|
|
|
|
using var crop = new MagickImage(Assert.Single(variants, v => v.Name == "crop").Bytes);
|
|
Assert.Equal((128u, 128u), (crop.Width, crop.Height));
|
|
using var pixels = crop.GetPixels();
|
|
foreach (var x in new[] { 0, 127 })
|
|
{
|
|
var color = pixels.GetPixel(x, 64).ToColor()!;
|
|
Assert.True(color.G >= 240 && color.R <= 15);
|
|
}
|
|
|
|
using var optimized = new MagickImage(Assert.Single(variants, v => v.Name == "optimized").Bytes);
|
|
Assert.Equal(MagickFormat.WebP, optimized.Format);
|
|
Assert.Equal((1280u, 640u), (optimized.Width, optimized.Height));
|
|
}
|
|
|
|
[Fact]
|
|
public void RefusesAnImageOverThePixelLimit()
|
|
{
|
|
using var source = new MagickImage(MagickColors.Red, 11, 10);
|
|
using var input = new MemoryStream(source.ToByteArray(MagickFormat.Png));
|
|
Assert.Throws<InvalidDataException>(() => ImageVariant.CreateAll(input, "image/png", 100, 1));
|
|
}
|
|
}
|