1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
|
using ImageMagickSharp;
using MediaBrowser.Common.Configuration;
using MediaBrowser.Common.IO;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
namespace MediaBrowser.Server.Implementations.Photos
{
public static class DynamicImageHelpers
{
public static async Task<Stream> GetThumbCollage(List<string> files,
IFileSystem fileSystem,
int width,
int height, IApplicationPaths appPaths)
{
if (files.Any(string.IsNullOrWhiteSpace))
{
throw new ArgumentException("Empty file found in files list");
}
if (files.Count == 0)
{
return null;
}
if (files.Count < 3)
{
return await GetSingleImage(files, fileSystem).ConfigureAwait(false);
}
const int rows = 1;
const int cols = 3;
int cellWidth = 2 * (width / 3);
int cellHeight = height;
var index = 0;
using (var wand = new MagickWand(width, height, new PixelWand(ColorName.None, 1)))
{
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]))
{
innerWand.CurrentImage.ResizeImage(cellWidth, cellHeight);
wand.CurrentImage.CompositeImage(innerWand, CompositeOperator.OverCompositeOp, x, y);
}
}
index++;
}
}
return GetStream(wand, appPaths);
}
}
public static async Task<Stream> GetSquareCollage(List<string> files,
IFileSystem fileSystem,
int size, IApplicationPaths appPaths)
{
if (files.Any(string.IsNullOrWhiteSpace))
{
throw new ArgumentException("Empty file found in files list");
}
if (files.Count == 0)
{
return null;
}
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;
using (var wand = new MagickWand(size, size, new PixelWand(ColorName.None, 1)))
{
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]))
{
innerWand.CurrentImage.ResizeImage(singleSize, singleSize);
wand.CurrentImage.CompositeImage(innerWand, CompositeOperator.OverCompositeOp, x, y);
}
index++;
}
}
return GetStream(wand, appPaths);
}
}
private static Task<Stream> GetSingleImage(List<string> files, IFileSystem fileSystem)
{
return Task.FromResult<Stream>(fileSystem.GetFileStream(files[0], FileMode.Open, FileAccess.Read, FileShare.Read));
}
internal static Stream GetStream(MagickWand image, IApplicationPaths appPaths)
{
var tempFile = Path.Combine(appPaths.TempDirectory, Guid.NewGuid().ToString("N") + ".png");
Directory.CreateDirectory(Path.GetDirectoryName(tempFile));
image.CurrentImage.CompressionQuality = 100;
image.SaveImage(tempFile);
return File.OpenRead(tempFile);
}
}
}
|