create square collages

This commit is contained in:
Luke Pulverenti 2015-04-02 12:58:52 -04:00
parent 74adff0d8d
commit 285805d84a
3 changed files with 80 additions and 21 deletions

View File

@ -13,6 +13,7 @@ using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using MediaBrowser.Server.Implementations.UserViews;
namespace MediaBrowser.Server.Implementations.Photos
{
@ -134,30 +135,25 @@ namespace MediaBrowser.Server.Implementations.Photos
return parts.GetMD5().ToString("N");
}
protected Task<Stream> GetThumbCollage(List<BaseItem> items)
protected Task<Stream> GetThumbCollage(IHasImages primaryItem, List<BaseItem> items)
{
var files = items
.Select(i => i.GetImagePath(ImageType.Primary) ?? i.GetImagePath(ImageType.Thumb))
.Where(i => !string.IsNullOrWhiteSpace(i))
.ToList();
var stream = new StripCollageBuilder(ApplicationPaths).BuildThumbCollage(GetStripCollageImagePaths(items), primaryItem.Name, 960, 540);
return DynamicImageHelpers.GetThumbCollage(files,
FileSystem,
1600,
900,
ApplicationPaths);
return Task.FromResult(stream);
}
protected Task<Stream> GetSquareCollage(List<BaseItem> items)
private IEnumerable<String> GetStripCollageImagePaths(IEnumerable<BaseItem> items)
{
var files = items
return items
.Select(i => i.GetImagePath(ImageType.Primary) ?? i.GetImagePath(ImageType.Thumb))
.Where(i => !string.IsNullOrWhiteSpace(i))
.ToList();
.Where(i => !string.IsNullOrWhiteSpace(i));
}
return DynamicImageHelpers.GetSquareCollage(files,
FileSystem,
800, ApplicationPaths);
protected Task<Stream> GetSquareCollage(IHasImages primaryItem, List<BaseItem> items)
{
var stream = new StripCollageBuilder(ApplicationPaths).BuildSquareCollage(GetStripCollageImagePaths(items), primaryItem.Name, 800, 800);
return Task.FromResult(stream);
}
public string Name
@ -176,8 +172,8 @@ namespace MediaBrowser.Server.Implementations.Photos
}
return imageType == ImageType.Thumb ?
await GetThumbCollage(itemsWithImages).ConfigureAwait(false) :
await GetSquareCollage(itemsWithImages).ConfigureAwait(false);
await GetThumbCollage(item, itemsWithImages).ConfigureAwait(false) :
await GetSquareCollage(item, itemsWithImages).ConfigureAwait(false);
}
public bool HasChanged(IHasMetadata item, IDirectoryService directoryService, DateTime date)

View File

@ -248,8 +248,7 @@ namespace MediaBrowser.Server.Implementations.UserViews
return null;
}
var stream = new StripCollageBuilder(ApplicationPaths).BuildThumbCollage(GetStripCollageImagePaths(itemsWithImages, view.ViewType), item.Name, 960, 540);
return stream;
return new StripCollageBuilder(ApplicationPaths).BuildThumbCollage(GetStripCollageImagePaths(itemsWithImages, view.ViewType), item.Name, 960, 540);
}
return await base.CreateImageAsync(item, itemsWithImages, imageType, imageIndex);

View File

@ -18,6 +18,14 @@ namespace MediaBrowser.Server.Implementations.UserViews
_appPaths = appPaths;
}
public Stream BuildSquareCollage(IEnumerable<string> paths, string text, int width, int height)
{
using (var wand = BuildSquareCollageWand(paths, width, height))
{
return DynamicImageHelpers.GetStream(wand, _appPaths);
}
}
public Stream BuildThumbCollage(IEnumerable<string> paths, string text, int width, int height)
{
using (var wand = BuildThumbCollageWand(paths, width, height))
@ -172,6 +180,62 @@ namespace MediaBrowser.Server.Implementations.UserViews
}
}
private MagickWand BuildSquareCollageWand(IEnumerable<string> paths, int width, int height)
{
var inputPaths = ProjectPaths(paths, 4);
using (var wandImages = new MagickWand(inputPaths))
{
var wand = new MagickWand(width, height);
wand.OpenImage("gradient:#111111-#111111");
using (var draw = new DrawingWand())
{
var iSlice = Convert.ToInt32(width * 0.2333333334);
int iTrans = Convert.ToInt32(height * .25);
int iHeight = Convert.ToInt32(height * .65);
var horizontalImagePadding = Convert.ToInt32(width * 0.0125);
foreach (var element in wandImages.ImageList)
{
int iWidth = (int)Math.Abs(iHeight * element.Width / element.Height);
element.Gravity = GravityType.CenterGravity;
element.BackgroundColor = ColorName.Black;
element.ResizeImage(iWidth, iHeight, FilterTypes.LanczosFilter);
int ix = (int)Math.Abs((iWidth - iSlice) / 2);
element.CropImage(iSlice, iHeight, ix, 0);
element.ExtentImage(iSlice, iHeight, 0 - horizontalImagePadding, 0);
}
wandImages.SetFirstIterator();
using (var wandList = wandImages.AppendImages())
{
wandList.CurrentImage.TrimImage(1);
using (var mwr = wandList.CloneMagickWand())
{
mwr.CurrentImage.ResizeImage(wandList.CurrentImage.Width, (wandList.CurrentImage.Height / 2), FilterTypes.LanczosFilter, 1);
mwr.CurrentImage.FlipImage();
mwr.CurrentImage.AlphaChannel = AlphaChannelType.DeactivateAlphaChannel;
mwr.CurrentImage.ColorizeImage(ColorName.Black, ColorName.Grey70);
using (var mwg = new MagickWand(wandList.CurrentImage.Width, iTrans))
{
mwg.OpenImage("gradient:black-none");
var verticalSpacing = Convert.ToInt32(height * 0.01111111111111111111111111111111);
mwr.CurrentImage.CompositeImage(mwg, CompositeOperator.CopyOpacityCompositeOp, 0, verticalSpacing);
wandList.AddImage(mwr);
int ex = (int)(wand.CurrentImage.Width - mwg.CurrentImage.Width) / 2;
wand.CurrentImage.CompositeImage(wandList.AppendImages(true), CompositeOperator.AtopCompositeOp, ex, Convert.ToInt32(height * .05));
}
}
}
}
return wand;
}
}
private string MontserratLightFont
{
get { return PlayedIndicatorDrawer.ExtractFont("MontserratLight.otf", _appPaths); }