DynamicImageHelpers.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. using ImageMagickSharp;
  2. using MediaBrowser.Common.Configuration;
  3. using MediaBrowser.Common.IO;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.IO;
  7. using System.Threading.Tasks;
  8. namespace MediaBrowser.Server.Implementations.Photos
  9. {
  10. public static class DynamicImageHelpers
  11. {
  12. public static async Task<Stream> GetThumbCollage(List<string> files,
  13. IFileSystem fileSystem,
  14. int width,
  15. int height, IApplicationPaths appPaths)
  16. {
  17. if (files.Count < 3)
  18. {
  19. return await GetSingleImage(files, fileSystem).ConfigureAwait(false);
  20. }
  21. const int rows = 1;
  22. const int cols = 3;
  23. int cellWidth = 2 * (width / 3);
  24. int cellHeight = height;
  25. var index = 0;
  26. using (var wand = new MagickWand(width, height, "transparent"))
  27. {
  28. for (var row = 0; row < rows; row++)
  29. {
  30. for (var col = 0; col < cols; col++)
  31. {
  32. var x = col * (cellWidth / 2);
  33. var y = row * cellHeight;
  34. if (files.Count > index)
  35. {
  36. using (var innerWand = new MagickWand(files[index]))
  37. {
  38. innerWand.CurrentImage.ResizeImage(cellWidth, cellHeight);
  39. wand.CurrentImage.CompositeImage(innerWand, CompositeOperator.OverCompositeOp, x, y);
  40. }
  41. }
  42. index++;
  43. }
  44. }
  45. return GetStream(wand, appPaths);
  46. }
  47. }
  48. public static async Task<Stream> GetSquareCollage(List<string> files,
  49. IFileSystem fileSystem,
  50. int size, IApplicationPaths appPaths)
  51. {
  52. if (files.Count < 4)
  53. {
  54. return await GetSingleImage(files, fileSystem).ConfigureAwait(false);
  55. }
  56. const int rows = 2;
  57. const int cols = 2;
  58. int singleSize = size / 2;
  59. var index = 0;
  60. using (var wand = new MagickWand(size, size, "transparent"))
  61. {
  62. for (var row = 0; row < rows; row++)
  63. {
  64. for (var col = 0; col < cols; col++)
  65. {
  66. var x = col * singleSize;
  67. var y = row * singleSize;
  68. using (var innerWand = new MagickWand(files[index]))
  69. {
  70. innerWand.CurrentImage.ResizeImage(singleSize, singleSize);
  71. wand.CurrentImage.CompositeImage(innerWand, CompositeOperator.OverCompositeOp, x, y);
  72. }
  73. index++;
  74. }
  75. }
  76. return GetStream(wand, appPaths);
  77. }
  78. }
  79. private static Task<Stream> GetSingleImage(List<string> files, IFileSystem fileSystem)
  80. {
  81. return Task.FromResult<Stream>(fileSystem.GetFileStream(files[0], FileMode.Open, FileAccess.Read, FileShare.Read));
  82. }
  83. private static Stream GetStream(MagickWand image, IApplicationPaths appPaths)
  84. {
  85. var tempFile = Path.Combine(appPaths.TempDirectory, Guid.NewGuid().ToString("N") + ".png");
  86. Directory.CreateDirectory(Path.GetDirectoryName(tempFile));
  87. image.CurrentImage.CompressionQuality = 100;
  88. image.SaveImage(tempFile);
  89. return File.OpenRead(tempFile);
  90. }
  91. }
  92. }