aboutsummaryrefslogtreecommitdiff
path: root/MediaBrowser.Server.Implementations/Photos/DynamicImageHelpers.cs
blob: c2af9cdafc517052a9790a8bf6ed7dce72ee5b0e (plain)
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
using ImageMagickSharp;
using MediaBrowser.Common.Configuration;
using MediaBrowser.Common.IO;
using System;
using System.Collections.Generic;
using System.IO;
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.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, "transparent"))
            {
                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.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, "transparent"))
            {
                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));
        }

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