Merge pull request #3954 from Ullmie02/library-pictures
Use backdrop with library name as library thumbnail
This commit is contained in:
commit
1f5ac19346
|
@ -36,7 +36,7 @@ namespace Emby.Drawing
|
||||||
private readonly IImageEncoder _imageEncoder;
|
private readonly IImageEncoder _imageEncoder;
|
||||||
private readonly IMediaEncoder _mediaEncoder;
|
private readonly IMediaEncoder _mediaEncoder;
|
||||||
|
|
||||||
private bool _disposed = false;
|
private bool _disposed;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Initializes a new instance of the <see cref="ImageProcessor"/> class.
|
/// Initializes a new instance of the <see cref="ImageProcessor"/> class.
|
||||||
|
@ -466,11 +466,11 @@ namespace Emby.Drawing
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public void CreateImageCollage(ImageCollageOptions options)
|
public void CreateImageCollage(ImageCollageOptions options, string? libraryName)
|
||||||
{
|
{
|
||||||
_logger.LogInformation("Creating image collage and saving to {Path}", options.OutputPath);
|
_logger.LogInformation("Creating image collage and saving to {Path}", options.OutputPath);
|
||||||
|
|
||||||
_imageEncoder.CreateImageCollage(options);
|
_imageEncoder.CreateImageCollage(options, libraryName);
|
||||||
|
|
||||||
_logger.LogInformation("Completed creation of image collage and saved to {Path}", options.OutputPath);
|
_logger.LogInformation("Completed creation of image collage and saved to {Path}", options.OutputPath);
|
||||||
}
|
}
|
||||||
|
|
|
@ -38,7 +38,7 @@ namespace Emby.Drawing
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public void CreateImageCollage(ImageCollageOptions options)
|
public void CreateImageCollage(ImageCollageOptions options, string? libraryName)
|
||||||
{
|
{
|
||||||
throw new NotImplementedException();
|
throw new NotImplementedException();
|
||||||
}
|
}
|
||||||
|
|
|
@ -133,9 +133,20 @@ namespace Emby.Server.Implementations.Images
|
||||||
|
|
||||||
protected virtual IEnumerable<string> GetStripCollageImagePaths(BaseItem primaryItem, IEnumerable<BaseItem> items)
|
protected virtual IEnumerable<string> GetStripCollageImagePaths(BaseItem primaryItem, IEnumerable<BaseItem> items)
|
||||||
{
|
{
|
||||||
|
var useBackdrop = primaryItem is CollectionFolder;
|
||||||
return items
|
return items
|
||||||
.Select(i =>
|
.Select(i =>
|
||||||
{
|
{
|
||||||
|
// Use Backdrop instead of Primary image for Library images.
|
||||||
|
if (useBackdrop)
|
||||||
|
{
|
||||||
|
var backdrop = i.GetImageInfo(ImageType.Backdrop, 0);
|
||||||
|
if (backdrop != null && backdrop.IsLocalFile)
|
||||||
|
{
|
||||||
|
return backdrop.Path;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
var image = i.GetImageInfo(ImageType.Primary, 0);
|
var image = i.GetImageInfo(ImageType.Primary, 0);
|
||||||
if (image != null && image.IsLocalFile)
|
if (image != null && image.IsLocalFile)
|
||||||
{
|
{
|
||||||
|
@ -190,7 +201,7 @@ namespace Emby.Server.Implementations.Images
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
ImageProcessor.CreateImageCollage(options);
|
ImageProcessor.CreateImageCollage(options, primaryItem.Name);
|
||||||
return outputPath;
|
return outputPath;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -553,13 +553,13 @@ namespace Jellyfin.Drawing.Skia
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc/>
|
/// <inheritdoc/>
|
||||||
public void CreateImageCollage(ImageCollageOptions options)
|
public void CreateImageCollage(ImageCollageOptions options, string? libraryName)
|
||||||
{
|
{
|
||||||
double ratio = (double)options.Width / options.Height;
|
double ratio = (double)options.Width / options.Height;
|
||||||
|
|
||||||
if (ratio >= 1.4)
|
if (ratio >= 1.4)
|
||||||
{
|
{
|
||||||
new StripCollageBuilder(this).BuildThumbCollage(options.InputPaths, options.OutputPath, options.Width, options.Height);
|
new StripCollageBuilder(this).BuildThumbCollage(options.InputPaths, options.OutputPath, options.Width, options.Height, libraryName);
|
||||||
}
|
}
|
||||||
else if (ratio >= .9)
|
else if (ratio >= .9)
|
||||||
{
|
{
|
||||||
|
|
|
@ -82,48 +82,62 @@ namespace Jellyfin.Drawing.Skia
|
||||||
/// <param name="outputPath">The path at which to place the resulting image.</param>
|
/// <param name="outputPath">The path at which to place the resulting image.</param>
|
||||||
/// <param name="width">The desired width of the collage.</param>
|
/// <param name="width">The desired width of the collage.</param>
|
||||||
/// <param name="height">The desired height of the collage.</param>
|
/// <param name="height">The desired height of the collage.</param>
|
||||||
public void BuildThumbCollage(string[] paths, string outputPath, int width, int height)
|
/// <param name="libraryName">The name of the library to draw on the collage.</param>
|
||||||
|
public void BuildThumbCollage(string[] paths, string outputPath, int width, int height, string? libraryName)
|
||||||
{
|
{
|
||||||
using var bitmap = BuildThumbCollageBitmap(paths, width, height);
|
using var bitmap = BuildThumbCollageBitmap(paths, width, height, libraryName);
|
||||||
using var outputStream = new SKFileWStream(outputPath);
|
using var outputStream = new SKFileWStream(outputPath);
|
||||||
using var pixmap = new SKPixmap(new SKImageInfo(width, height), bitmap.GetPixels());
|
using var pixmap = new SKPixmap(new SKImageInfo(width, height), bitmap.GetPixels());
|
||||||
pixmap.Encode(outputStream, GetEncodedFormat(outputPath), 90);
|
pixmap.Encode(outputStream, GetEncodedFormat(outputPath), 90);
|
||||||
}
|
}
|
||||||
|
|
||||||
private SKBitmap BuildThumbCollageBitmap(string[] paths, int width, int height)
|
private SKBitmap BuildThumbCollageBitmap(string[] paths, int width, int height, string? libraryName)
|
||||||
{
|
{
|
||||||
var bitmap = new SKBitmap(width, height);
|
var bitmap = new SKBitmap(width, height);
|
||||||
|
|
||||||
using var canvas = new SKCanvas(bitmap);
|
using var canvas = new SKCanvas(bitmap);
|
||||||
canvas.Clear(SKColors.Black);
|
canvas.Clear(SKColors.Black);
|
||||||
|
|
||||||
// number of images used in the thumbnail
|
using var backdrop = GetNextValidImage(paths, 0, out _);
|
||||||
var iCount = 3;
|
if (backdrop == null)
|
||||||
|
|
||||||
// determine sizes for each image that will composited into the final image
|
|
||||||
var iSlice = Convert.ToInt32(width / iCount);
|
|
||||||
int iHeight = Convert.ToInt32(height * 1.00);
|
|
||||||
int imageIndex = 0;
|
|
||||||
for (int i = 0; i < iCount; i++)
|
|
||||||
{
|
{
|
||||||
using var currentBitmap = GetNextValidImage(paths, imageIndex, out int newIndex);
|
return bitmap;
|
||||||
imageIndex = newIndex;
|
|
||||||
if (currentBitmap == null)
|
|
||||||
{
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
// resize to the same aspect as the original
|
|
||||||
int iWidth = Math.Abs(iHeight * currentBitmap.Width / currentBitmap.Height);
|
|
||||||
using var resizedImage = SkiaEncoder.ResizeImage(currentBitmap, new SKImageInfo(iWidth, iHeight, currentBitmap.ColorType, currentBitmap.AlphaType, currentBitmap.ColorSpace));
|
|
||||||
|
|
||||||
// crop image
|
|
||||||
int ix = Math.Abs((iWidth - iSlice) / 2);
|
|
||||||
using var subset = resizedImage.Subset(SKRectI.Create(ix, 0, iSlice, iHeight));
|
|
||||||
// draw image onto canvas
|
|
||||||
canvas.DrawImage(subset ?? resizedImage, iSlice * i, 0);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// resize to the same aspect as the original
|
||||||
|
var backdropHeight = Math.Abs(width * backdrop.Height / backdrop.Width);
|
||||||
|
using var residedBackdrop = SkiaEncoder.ResizeImage(backdrop, new SKImageInfo(width, backdropHeight, backdrop.ColorType, backdrop.AlphaType, backdrop.ColorSpace));
|
||||||
|
// draw the backdrop
|
||||||
|
canvas.DrawImage(residedBackdrop, 0, 0);
|
||||||
|
|
||||||
|
// draw shadow rectangle
|
||||||
|
var paintColor = new SKPaint
|
||||||
|
{
|
||||||
|
Color = SKColors.Black.WithAlpha(0x78),
|
||||||
|
Style = SKPaintStyle.Fill
|
||||||
|
};
|
||||||
|
canvas.DrawRect(0, 0, width, height, paintColor);
|
||||||
|
|
||||||
|
// draw library name
|
||||||
|
var textPaint = new SKPaint
|
||||||
|
{
|
||||||
|
Color = SKColors.White,
|
||||||
|
Style = SKPaintStyle.Fill,
|
||||||
|
TextSize = 112,
|
||||||
|
TextAlign = SKTextAlign.Center,
|
||||||
|
Typeface = SKTypeface.FromFamilyName("sans-serif", SKFontStyleWeight.Bold, SKFontStyleWidth.Normal, SKFontStyleSlant.Upright),
|
||||||
|
IsAntialias = true
|
||||||
|
};
|
||||||
|
|
||||||
|
// scale down text to 90% of the width if text is larger than 95% of the width
|
||||||
|
var textWidth = textPaint.MeasureText(libraryName);
|
||||||
|
if (textWidth > width * 0.95)
|
||||||
|
{
|
||||||
|
textPaint.TextSize = 0.9f * width * textPaint.TextSize / textWidth;
|
||||||
|
}
|
||||||
|
|
||||||
|
canvas.DrawText(libraryName, width / 2f, (height / 2f) + (textPaint.FontMetrics.XHeight / 2), textPaint);
|
||||||
|
|
||||||
return bitmap;
|
return bitmap;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
#pragma warning disable CS1591
|
#pragma warning disable CS1591
|
||||||
|
#nullable enable
|
||||||
|
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
@ -63,6 +64,7 @@ namespace MediaBrowser.Controller.Drawing
|
||||||
/// Create an image collage.
|
/// Create an image collage.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="options">The options to use when creating the collage.</param>
|
/// <param name="options">The options to use when creating the collage.</param>
|
||||||
void CreateImageCollage(ImageCollageOptions options);
|
/// <param name="libraryName">Optional. </param>
|
||||||
|
void CreateImageCollage(ImageCollageOptions options, string? libraryName);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
#pragma warning disable CS1591
|
#pragma warning disable CS1591
|
||||||
|
#nullable enable
|
||||||
|
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
@ -75,7 +76,7 @@ namespace MediaBrowser.Controller.Drawing
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="options">The options.</param>
|
/// <param name="options">The options.</param>
|
||||||
/// <returns>Task.</returns>
|
/// <returns>Task.</returns>
|
||||||
Task<(string path, string mimeType, DateTime dateModified)> ProcessImage(ImageProcessingOptions options);
|
Task<(string path, string? mimeType, DateTime dateModified)> ProcessImage(ImageProcessingOptions options);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets the supported image output formats.
|
/// Gets the supported image output formats.
|
||||||
|
@ -87,7 +88,8 @@ namespace MediaBrowser.Controller.Drawing
|
||||||
/// Creates the image collage.
|
/// Creates the image collage.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="options">The options.</param>
|
/// <param name="options">The options.</param>
|
||||||
void CreateImageCollage(ImageCollageOptions options);
|
/// <param name="libraryName">The library name to draw onto the collage.</param>
|
||||||
|
void CreateImageCollage(ImageCollageOptions options, string? libraryName);
|
||||||
|
|
||||||
bool SupportsTransparency(string path);
|
bool SupportsTransparency(string path);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user