The Free Software Media System - Server Backend & API
added percent played overlay
Luke Pulverentif380d7a092ef
diff --git a/MediaBrowser.Api/Images/ImageRequest.cs b/MediaBrowser.Api/Images/ImageRequest.csindex 294b7ae960..210ee5b64e 100644--- a/MediaBrowser.Api/Images/ImageRequest.cs+++ b/MediaBrowser.Api/Images/ImageRequest.cs@@ -58,6 +58,12 @@ namespace MediaBrowser.Api.Images[ApiMember(Name = "Indicator", Description = "Determines what overlay to render, if any. none, watched.", IsRequired = false, DataType = "bool", ParameterType = "query", Verb = "GET")]public ImageOverlay? Indicator { get; set; }++ [ApiMember(Name = "PercentPlayed", Description = "Percent to render for the percent played overlay", IsRequired = false, DataType = "bool", ParameterType = "query", Verb = "GET")]+ public int PercentPlayed { get; set; }++ [ApiMember(Name = "BackgroundColor", Description = "Optional. Apply a background color for transparent images.", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET")]+ public string BackgroundColor { get; set; }public ImageRequest(){diff --git a/MediaBrowser.Api/Images/ImageWriter.cs b/MediaBrowser.Api/Images/ImageWriter.csindex be79878aa5..190d2ba473 100644--- a/MediaBrowser.Api/Images/ImageWriter.cs+++ b/MediaBrowser.Api/Images/ImageWriter.cs@@ -90,7 +90,9 @@ namespace MediaBrowser.Api.ImagesQuality = Request.Quality,Width = Request.Width,OutputFormat = Request.Format,- Indicator = Request.Indicator+ Indicator = Request.Indicator,+ PercentPlayed = Request.PercentPlayed,+ BackgroundColor = Request.BackgroundColor};return ImageProcessor.ProcessImage(options, responseStream);diff --git a/MediaBrowser.Controller/Drawing/ImageProcessingOptions.cs b/MediaBrowser.Controller/Drawing/ImageProcessingOptions.csindex 3c2e6ab375..5910601248 100644--- a/MediaBrowser.Controller/Drawing/ImageProcessingOptions.cs+++ b/MediaBrowser.Controller/Drawing/ImageProcessingOptions.cs@@ -36,5 +36,9 @@ namespace MediaBrowser.Controller.Drawingpublic ImageOutputFormat OutputFormat { get; set; }public ImageOverlay? Indicator { get; set; }++ public int PercentPlayed { get; set; }++ public string BackgroundColor { get; set; }}}diff --git a/MediaBrowser.Server.Implementations/Drawing/ImageProcessor.cs b/MediaBrowser.Server.Implementations/Drawing/ImageProcessor.csindex 6458435baa..1f7361d2f6 100644--- a/MediaBrowser.Server.Implementations/Drawing/ImageProcessor.cs+++ b/MediaBrowser.Server.Implementations/Drawing/ImageProcessor.cs@@ -109,7 +109,7 @@ namespace MediaBrowser.Server.Implementations.Drawingvar quality = options.Quality ?? 90;- var cacheFilePath = GetCacheFilePath(originalImagePath, newSize, quality, dateModified, options.OutputFormat, options.Indicator);+ var cacheFilePath = GetCacheFilePath(originalImagePath, newSize, quality, dateModified, options.OutputFormat, options.Indicator, options.PercentPlayed, options.BackgroundColor);try{@@ -173,9 +173,11 @@ namespace MediaBrowser.Server.Implementations.DrawingthumbnailGraph.PixelOffsetMode = PixelOffsetMode.HighQuality;thumbnailGraph.CompositingMode = CompositingMode.SourceOver;+ SetBackgroundColor(thumbnailGraph, options);+thumbnailGraph.DrawImage(originalImage, 0, 0, newWidth, newHeight);- DrawIndicator(thumbnailGraph, newWidth, newHeight, options.Indicator);+ DrawIndicator(thumbnailGraph, newWidth, newHeight, options.Indicator, options.PercentPlayed);var outputFormat = GetOutputFormat(originalImage, options.OutputFormat);@@ -206,9 +208,41 @@ namespace MediaBrowser.Server.Implementations.Drawing}}- private WatchedIndicatorDrawer _watchedDrawer;+ /// <summary>+ /// Sets the color of the background.+ /// </summary>+ /// <param name="graphics">The graphics.</param>+ /// <param name="options">The options.</param>+ private void SetBackgroundColor(Graphics graphics, ImageProcessingOptions options)+ {+ var color = options.BackgroundColor;++ if (!string.IsNullOrEmpty(color))+ {+ Color drawingColor;++ try+ {+ drawingColor = ColorTranslator.FromHtml(color);+ }+ catch+ {+ drawingColor = ColorTranslator.FromHtml("#" + color);+ }- private void DrawIndicator(Graphics graphics, int imageWidth, int imageHeight, ImageOverlay? indicator)+ graphics.Clear(drawingColor);+ }+ }++ /// <summary>+ /// Draws the indicator.+ /// </summary>+ /// <param name="graphics">The graphics.</param>+ /// <param name="imageWidth">Width of the image.</param>+ /// <param name="imageHeight">Height of the image.</param>+ /// <param name="indicator">The indicator.</param>+ /// <param name="percentPlayed">The percent played.</param>+ private void DrawIndicator(Graphics graphics, int imageWidth, int imageHeight, ImageOverlay? indicator, int percentPlayed){if (!indicator.HasValue){@@ -217,13 +251,17 @@ namespace MediaBrowser.Server.Implementations.Drawingtry{- if (indicator.Value == ImageOverlay.Watched)+ if (indicator.Value == ImageOverlay.Played){- _watchedDrawer = _watchedDrawer ?? (_watchedDrawer = new WatchedIndicatorDrawer());+ var currentImageSize = new Size(imageWidth, imageHeight);+ new WatchedIndicatorDrawer().Process(graphics, currentImageSize);+ }+ if (indicator.Value == ImageOverlay.PercentPlayed)+ {var currentImageSize = new Size(imageWidth, imageHeight);- _watchedDrawer.Process(graphics, currentImageSize);+ new PercentPlayedDrawer().Process(graphics, currentImageSize, percentPlayed);}}catch (Exception ex)@@ -350,7 +388,7 @@ namespace MediaBrowser.Server.Implementations.Drawing/// <summary>/// Gets the cache file path based on a set of parameters/// </summary>- private string GetCacheFilePath(string originalPath, ImageSize outputSize, int quality, DateTime dateModified, ImageOutputFormat format, ImageOverlay? overlay)+ private string GetCacheFilePath(string originalPath, ImageSize outputSize, int quality, DateTime dateModified, ImageOutputFormat format, ImageOverlay? overlay, int percentPlayed, string backgroundColor){var filename = originalPath;@@ -364,12 +402,18 @@ namespace MediaBrowser.Server.Implementations.Drawingif (format != ImageOutputFormat.Original){- filename += "format=" + format;+ filename += "f=" + format;}if (overlay.HasValue){- filename += "overlay=" + overlay.Value;+ filename += "o=" + overlay.Value;+ filename += "p=" + percentPlayed;+ }++ if (!string.IsNullOrEmpty(backgroundColor))+ {+ filename += "b=" + backgroundColor;}return GetCachePath(_resizedImageCachePath, filename, Path.GetExtension(originalPath));diff --git a/MediaBrowser.Server.Implementations/Drawing/PercentPlayedDrawer.cs b/MediaBrowser.Server.Implementations/Drawing/PercentPlayedDrawer.csnew file mode 100644index 0000000000..3558244584--- /dev/null+++ b/MediaBrowser.Server.Implementations/Drawing/PercentPlayedDrawer.cs@@ -0,0 +1,36 @@+using System.Drawing;+using System.Globalization;++namespace MediaBrowser.Server.Implementations.Drawing+{+ public class PercentPlayedDrawer+ {+ private const int IndicatorWidth = 80;+ private const int IndicatorHeight = 50;+ private const int FontSize = 30;+ private readonly CultureInfo _usCulture = new CultureInfo("en-US");++ public void Process(Graphics graphics, Size imageSize, int percent)+ {+ var x = imageSize.Width - IndicatorWidth;++ using (var backdroundBrush = new SolidBrush(Color.FromArgb(225, 102, 192, 16)))+ {+ graphics.FillRectangle(backdroundBrush, x, 0, IndicatorWidth, IndicatorHeight);++ var text = string.Format("{0}%", percent.ToString(_usCulture));++ x = imageSize.Width - (percent < 10 ? 66 : 75);++ using (var font = new Font(FontFamily.GenericSansSerif, FontSize, FontStyle.Regular, GraphicsUnit.Pixel))+ {+ using (var fontBrush = new SolidBrush(Color.White))+ {+ graphics.DrawString(text, font, fontBrush, x, 6);+ }+ }+ }++ }+ }+}diff --git a/MediaBrowser.Server.Implementations/MediaBrowser.Server.Implementations.csproj b/MediaBrowser.Server.Implementations/MediaBrowser.Server.Implementations.csprojindex 80fee7d97a..f84c02c1f8 100644--- a/MediaBrowser.Server.Implementations/MediaBrowser.Server.Implementations.csproj+++ b/MediaBrowser.Server.Implementations/MediaBrowser.Server.Implementations.csproj@@ -114,6 +114,7 @@<Compile Include="BdInfo\BdInfoExaminer.cs" /><Compile Include="Configuration\ServerConfigurationManager.cs" /><Compile Include="Drawing\ImageHeader.cs" />+ <Compile Include="Drawing\PercentPlayedDrawer.cs" /><Compile Include="Drawing\WatchedIndicatorDrawer.cs" /><Compile Include="Dto\DtoService.cs" /><Compile Include="EntryPoints\LibraryChangedNotifier.cs" />