testing using libvips
This commit is contained in:
@@ -16,6 +16,7 @@ using SixLabors.ImageSharp.Processing;
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Net.Http.Headers;
|
||||
using System.Text;
|
||||
@@ -135,9 +136,6 @@ public class ThumbnailService(IMongoDatabase db, AobaService aobaService)
|
||||
|
||||
public static async Task<Maybe<Stream>> GenerateImageThumbnailAsync(Stream stream, ThumbnailSize size, string ext, CancellationToken cancellationToken = default)
|
||||
{
|
||||
if(ext == ".avif")
|
||||
return GenerateAvifThumbnail(stream, size, cancellationToken);
|
||||
|
||||
var img = LoadImage(stream, ext);
|
||||
if (img.HasError)
|
||||
return img.Error;
|
||||
@@ -196,32 +194,29 @@ public class ThumbnailService(IMongoDatabase db, AobaService aobaService)
|
||||
{
|
||||
var w = (int)size;
|
||||
var fn = ObjectId.GenerateNewId().ToString();
|
||||
var filePath = $"/tmp/{fn}.in";
|
||||
using var source = new FileStream(filePath, FileMode.CreateNew);
|
||||
var inFilePath = $"/tmp/{fn}.avif";
|
||||
var outFilePath = $"/tmp/{fn}.webp";
|
||||
using var source = new FileStream(inFilePath, FileMode.CreateNew);
|
||||
data.CopyTo(source);
|
||||
source.Flush();
|
||||
source.Dispose();
|
||||
data.Dispose();
|
||||
try
|
||||
{
|
||||
var process = Process.Start(new ProcessStartInfo
|
||||
{
|
||||
FileName = "vips",
|
||||
Arguments = $"smartcrop \"{inFilePath}\" \"{outFilePath}\"[Q=75] {w} {w}",
|
||||
WorkingDirectory = "/tmp"
|
||||
});
|
||||
if (process == null)
|
||||
return new Error("Failed to run vips command");
|
||||
process.WaitForExit();
|
||||
if (process.ExitCode != 0)
|
||||
return new Error("Failed to convert");
|
||||
var output = new MemoryStream();
|
||||
FFMpegArguments.FromFileInput(filePath, false, opt =>
|
||||
{
|
||||
//opt.WithCustomArgument("-vf ");
|
||||
})
|
||||
.OutputToPipe(new StreamPipeSink(output), opt =>
|
||||
{
|
||||
var tonemap = ",format=gbrpf32le,zscale=primaries=bt2020:transfer=smpte2084:matrix=gbr,tonemap=hable,zscale=primaries=bt709:transfer=bt709:matrix=bt709,format=yuv420p";
|
||||
var args = $"-vf \"crop='min(in_w,in_h)':'min(in_w,in_h)',scale={w}:{w},"
|
||||
+ $"{tonemap}\""
|
||||
//+ "zscale=primaries=bt2020:transfer=smpte2084:matrix=bt2020nc,format=gbrpf32le,"
|
||||
//+ "zscale=primaries=bt709:transfer=bt709:matrix=bt709:range=tv,tonemap=hable,"
|
||||
//+ "zscale=matrix=bt709:transfer=bt709:primaries=bt709,"
|
||||
//+ "format=yuv420p\" "
|
||||
+ "-colorspace bt709";
|
||||
opt.WithCustomArgument(args)
|
||||
.ForceFormat("webp");
|
||||
}).ProcessSynchronously();
|
||||
using var oFile = File.OpenRead(outFilePath);
|
||||
oFile.CopyTo(output);
|
||||
output.Position = 0;
|
||||
return output;
|
||||
}
|
||||
@@ -231,7 +226,8 @@ public class ThumbnailService(IMongoDatabase db, AobaService aobaService)
|
||||
}
|
||||
finally
|
||||
{
|
||||
File.Delete(filePath);
|
||||
File.Delete(inFilePath);
|
||||
File.Delete(outFilePath);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user