jellyfin/MediaBrowser.Server.Implementations/Photos/DynamicImageHelpers.cs

123 lines
4.0 KiB
C#
Raw Normal View History

using ImageMagickSharp;
using MediaBrowser.Common.Configuration;
using MediaBrowser.Common.IO;
using System;
2014-10-29 22:01:02 +00:00
using System.Collections.Generic;
using System.IO;
2015-03-03 07:03:17 +00:00
using System.Linq;
2014-10-29 22:01:02 +00:00
using System.Threading.Tasks;
namespace MediaBrowser.Server.Implementations.Photos
{
public static class DynamicImageHelpers
{
2014-11-11 04:26:53 +00:00
public static async Task<Stream> GetThumbCollage(List<string> files,
2014-10-29 22:01:02 +00:00
IFileSystem fileSystem,
int width,
int height, IApplicationPaths appPaths)
2014-10-29 22:01:02 +00:00
{
2015-03-03 07:03:17 +00:00
if (files.Any(string.IsNullOrWhiteSpace))
{
throw new ArgumentException("Empty file found in files list");
}
2014-10-29 22:01:02 +00:00
if (files.Count < 3)
{
return await GetSingleImage(files, fileSystem).ConfigureAwait(false);
}
const int rows = 1;
const int cols = 3;
2014-11-11 04:26:53 +00:00
int cellWidth = 2 * (width / 3);
int cellHeight = height;
2014-10-29 22:01:02 +00:00
var index = 0;
2015-03-03 07:03:17 +00:00
using (var wand = new MagickWand(width, height, new PixelWand(ColorName.None, 1)))
2014-10-29 22:01:02 +00:00
{
for (var row = 0; row < rows; row++)
{
for (var col = 0; col < cols; col++)
{
var x = col * (cellWidth / 2);
var y = row * cellHeight;
if (files.Count > index)
{
using (var innerWand = new MagickWand(files[index]))
2014-10-29 22:01:02 +00:00
{
innerWand.CurrentImage.ResizeImage(cellWidth, cellHeight);
wand.CurrentImage.CompositeImage(innerWand, CompositeOperator.OverCompositeOp, x, y);
2014-10-29 22:01:02 +00:00
}
}
index++;
}
}
return GetStream(wand, appPaths);
}
2014-10-29 22:01:02 +00:00
}
2014-11-11 04:26:53 +00:00
public static async Task<Stream> GetSquareCollage(List<string> files,
2014-10-29 22:01:02 +00:00
IFileSystem fileSystem,
int size, IApplicationPaths appPaths)
2014-10-29 22:01:02 +00:00
{
2015-03-03 07:03:17 +00:00
if (files.Any(string.IsNullOrWhiteSpace))
{
throw new ArgumentException("Empty file found in files list");
}
2014-10-29 22:01:02 +00:00
if (files.Count < 4)
{
return await GetSingleImage(files, fileSystem).ConfigureAwait(false);
}
const int rows = 2;
const int cols = 2;
int singleSize = size / 2;
var index = 0;
2015-03-03 07:03:17 +00:00
using (var wand = new MagickWand(size, size, new PixelWand(ColorName.None, 1)))
2014-10-29 22:01:02 +00:00
{
for (var row = 0; row < rows; row++)
{
for (var col = 0; col < cols; col++)
{
var x = col * singleSize;
var y = row * singleSize;
using (var innerWand = new MagickWand(files[index]))
2014-10-29 22:01:02 +00:00
{
innerWand.CurrentImage.ResizeImage(singleSize, singleSize);
wand.CurrentImage.CompositeImage(innerWand, CompositeOperator.OverCompositeOp, x, y);
2014-10-29 22:01:02 +00:00
}
index++;
}
}
return GetStream(wand, appPaths);
}
2014-11-11 04:26:53 +00:00
}
private static Task<Stream> GetSingleImage(List<string> files, IFileSystem fileSystem)
{
return Task.FromResult<Stream>(fileSystem.GetFileStream(files[0], FileMode.Open, FileAccess.Read, FileShare.Read));
2014-10-29 22:01:02 +00:00
}
private static Stream GetStream(MagickWand image, IApplicationPaths appPaths)
2014-10-29 22:01:02 +00:00
{
var tempFile = Path.Combine(appPaths.TempDirectory, Guid.NewGuid().ToString("N") + ".png");
2014-11-11 04:26:53 +00:00
Directory.CreateDirectory(Path.GetDirectoryName(tempFile));
2014-11-11 04:26:53 +00:00
image.CurrentImage.CompressionQuality = 100;
image.SaveImage(tempFile);
2014-11-11 04:26:53 +00:00
return File.OpenRead(tempFile);
2014-10-29 22:01:02 +00:00
}
}
}