From 4ee98fa551b3aa632025a544f9ea8d597cf4a9a1 Mon Sep 17 00:00:00 2001 From: exsersewo Date: Sun, 29 Jan 2023 13:15:32 +0000 Subject: [PATCH] Ignore token if dev, pass through request cancellation token, tweak word wrapping --- src/Attributes/RequireTokenAttribute.cs | 2 +- src/Controllers/ImageGenerationController.cs | 2 +- .../ImageManipulationController.cs | 4 +- src/Helpers/RequestHelper.cs | 29 +++- src/Helpers/StreamResult.cs | 29 ++-- src/Kynareth.csproj | 1 + src/Managers/ImageManager.Generation.cs | 136 ++++++++++-------- src/Managers/ImageManager.Magik.cs | 30 ++-- src/Managers/ImageManager.Manipulation.cs | 56 ++++---- src/Managers/ImageManager.cs | 83 +++++++---- src/Program.cs | 1 + src/appsettings.json | 2 +- 12 files changed, 231 insertions(+), 144 deletions(-) diff --git a/src/Attributes/RequireTokenAttribute.cs b/src/Attributes/RequireTokenAttribute.cs index 15f3be7..39e4b18 100644 --- a/src/Attributes/RequireTokenAttribute.cs +++ b/src/Attributes/RequireTokenAttribute.cs @@ -20,7 +20,7 @@ public class RequireTokenAttribute : ActionFilterAttribute context.HttpContext.Response.StatusCode = (int)HttpStatusCode.Unauthorized; var invalidRequest = EventResult.FromFailure("Unauthorized request"); - await context.HttpContext.Response.WriteAsync(invalidRequest.ToJson()); + await context.HttpContext.Response.WriteAsync(invalidRequest.ToJson(), context.HttpContext.RequestAborted); return; } } \ No newline at end of file diff --git a/src/Controllers/ImageGenerationController.cs b/src/Controllers/ImageGenerationController.cs index a4e3600..9cbda5b 100644 --- a/src/Controllers/ImageGenerationController.cs +++ b/src/Controllers/ImageGenerationController.cs @@ -34,7 +34,7 @@ public class ImageGenerationController : BaseController IsRequestAuthenticatedAsync(this HttpRequest request) { - string key = request.GetToken(); + if (_webApplication.Environment.IsDevelopment()) return true; + + while (true) + { + if(request.HttpContext.RequestAborted.IsCancellationRequested) + { + return false; + } + + string key = request.GetToken(); - if (string.IsNullOrWhiteSpace(key)) return false; + if (string.IsNullOrWhiteSpace(key)) return false; - await using var database = request.HttpContext.RequestServices.GetRequiredService(); + await using var database = request.HttpContext.RequestServices.GetRequiredService(); - var tokenEntry = database.Tokens.FirstOrDefault(token => token.Token.Equals(key)); + var tokenEntry = database.Tokens.FirstOrDefault(token => token.Token.Equals(key)); - if (tokenEntry is not { IsValid: true }) return false; + if (tokenEntry is not { IsValid: true }) return false; - return true; + return true; + } } public static string GetToken(this HttpRequest request) diff --git a/src/Helpers/StreamResult.cs b/src/Helpers/StreamResult.cs index 229111f..557d3ea 100644 --- a/src/Helpers/StreamResult.cs +++ b/src/Helpers/StreamResult.cs @@ -24,24 +24,31 @@ public class StreamResult : ActionResult, IStatusCodeActionResult, IActionResult throw new ArgumentNullException(nameof(context)); } - var response = context.HttpContext.Response; + try + { + var response = context.HttpContext.Response; - response.StatusCode = StatusCode ?? 200; + response.StatusCode = StatusCode ?? 200; - response.ContentType = !string.IsNullOrEmpty(ContentType) - ? ContentType - : "image/png"; + response.ContentType = !string.IsNullOrEmpty(ContentType) + ? ContentType + : "image/png"; - Value.Position = 0; + Value.Position = 0; - await Value.CopyToAsync(response.Body); + await Value.CopyToAsync(response.Body, context.HttpContext.RequestAborted); - await Value.FlushAsync(); + await Value.FlushAsync(context.HttpContext.RequestAborted); - await Value.DisposeAsync(); + await Value.DisposeAsync(); - context.HttpContext.Response.RegisterForDisposeAsync(Value); + context.HttpContext.Response.RegisterForDisposeAsync(Value); - GC.Collect(); + GC.Collect(); + } + catch (TaskCanceledException) + { + //assume that request was cancelled if this is invoked + } } } \ No newline at end of file diff --git a/src/Kynareth.csproj b/src/Kynareth.csproj index 8d12b80..6e6eb79 100644 --- a/src/Kynareth.csproj +++ b/src/Kynareth.csproj @@ -8,6 +8,7 @@ + diff --git a/src/Managers/ImageManager.Generation.cs b/src/Managers/ImageManager.Generation.cs index a194c1a..ba3a227 100644 --- a/src/Managers/ImageManager.Generation.cs +++ b/src/Managers/ImageManager.Generation.cs @@ -16,93 +16,111 @@ public static partial class ImageManager GenerationEndpoints = configuration.GetSection("ShitpostBot:Templates").Get>(); } - public static async Task GenerateImageAsync(string template, string variant, string[] sources) + public static async Task GenerateImageAsync(string template, string variant, string[] sources, CancellationToken cancellationToken) { - ImageGenerationImage imageTemplate = null; - - var endpoint = GenerationEndpoints.FirstOrDefault(e => e.Name.ToLowerInvariant().Equals(template.ToLowerInvariant())); - - if (endpoint is null) + while (true) { - var ex = new ArgumentException($"Couldn't find endpoint named \"{template}\"", nameof(template)); + if(cancellationToken.IsCancellationRequested) + { + return null; + } - return GetResult(StatusCodes.Status404NotFound, EventResult.FromFailureException(ex.Message, ex)); - } + ImageGenerationImage imageTemplate = null; - if (endpoint.SourcesRequired > sources.Length) - { - var ex = new ArgumentException("Not enough sources provided", nameof(sources)); + var endpoint = GenerationEndpoints.FirstOrDefault(e => e.Name.ToLowerInvariant().Equals(template.ToLowerInvariant())); - return GetResult(StatusCodes.Status400BadRequest, EventResult.FromFailureException(ex.Message, ex)); - } + if (endpoint is null) + { + var ex = new ArgumentException($"Couldn't find endpoint named \"{template}\"", nameof(template)); - if (!string.IsNullOrWhiteSpace(variant)) - { - imageTemplate = endpoint.Variants? - .FirstOrDefault(v => v.Name.ToLowerInvariant().Equals(variant.ToLowerInvariant())); + return GetResult(StatusCodes.Status404NotFound, EventResult.FromFailureException(ex.Message, ex)); + } - if (imageTemplate is null) + if (endpoint.SourcesRequired > sources.Length) { - var ex = new ArgumentException( - $"Invalid variant given \"{variant}\" for endpoint \"{template}\"", - nameof(variant) - ); + var ex = new ArgumentException("Not enough sources provided", nameof(sources)); return GetResult(StatusCodes.Status400BadRequest, EventResult.FromFailureException(ex.Message, ex)); } - } - else - { - imageTemplate = endpoint; - } - string image = Path.Combine(GenerationTemplateBaseFolder, !string.IsNullOrWhiteSpace(endpoint.Folder) ? endpoint.Folder : "", imageTemplate.Image); - - using MagickImage templateImage = new(System.IO.File.ReadAllBytes(image)); - using MagickImage baseImage = new(MagickColors.Transparent, templateImage.Width, templateImage.Height); + if (!string.IsNullOrWhiteSpace(variant)) + { + imageTemplate = endpoint.Variants? + .FirstOrDefault(v => v.Name.ToLowerInvariant().Equals(variant.ToLowerInvariant())); + + if (imageTemplate is null) + { + var ex = new ArgumentException( + $"Invalid variant given \"{variant}\" for endpoint \"{template}\"", + nameof(variant) + ); + + return GetResult(StatusCodes.Status400BadRequest, EventResult.FromFailureException(ex.Message, ex)); + } + } + else + { + imageTemplate = endpoint; + } - baseImage.Format = MagickFormat.Png; + string image = Path.Combine(GenerationTemplateBaseFolder, !string.IsNullOrWhiteSpace(endpoint.Folder) ? endpoint.Folder : "", imageTemplate.Image); - if (imageTemplate.PlaceUnder) - { - await AddSourcesToImage(baseImage, imageTemplate, sources); - } - - baseImage.Composite(templateImage, Gravity.Northwest, CompositeOperator.Over); + using MagickImage templateImage = new(System.IO.File.ReadAllBytes(image)); + using MagickImage baseImage = new(MagickColors.Transparent, templateImage.Width, templateImage.Height); - if (!imageTemplate.PlaceUnder) - { - await AddSourcesToImage(baseImage, imageTemplate, sources); + baseImage.Format = MagickFormat.Png; + + if (imageTemplate.PlaceUnder) + { + await AddSourcesToImage(baseImage, imageTemplate, sources, cancellationToken); + } + + baseImage.Composite(templateImage, Gravity.Northwest, CompositeOperator.Over); + + if (!imageTemplate.PlaceUnder) + { + await AddSourcesToImage(baseImage, imageTemplate, sources, cancellationToken); + } + + return baseImage.ToByteArray(); } - return baseImage.ToByteArray(); + return null; } - static async Task AddSourcesToImage(MagickImage image, ImageGenerationImage template, IEnumerable sources) + static async Task AddSourcesToImage(MagickImage image, ImageGenerationImage template, IEnumerable sources, CancellationToken cancellationToken) { - int img = 0; - foreach (var src in sources) + while (true) { - var position = template.Positions.ElementAtOrDefault(img); + if(cancellationToken.IsCancellationRequested) + { + return; + } + + int img = 0; + foreach (var src in sources) + { + var position = template.Positions.ElementAtOrDefault(img); - using MagickImage srcImg = new MagickImage(await HttpWebClient.GetStreamAsync(new Uri(src))); + using MagickImage srcImg = new MagickImage(await HttpWebClient.GetStreamAsync(new Uri(src))); - srcImg.Resize(position.Width, position.Height); + srcImg.Resize(position.Width, position.Height); - if (position.Rotation != 0) - { - srcImg.Rotate(template.Rotate > 0 ? -position.Rotation : position.Rotation); - } + if (position.Rotation != 0) + { + srcImg.Rotate(template.Rotate > 0 ? -position.Rotation : position.Rotation); + } - string background = !string.IsNullOrWhiteSpace(position.Background) ? $"#{position.Background}" : MagickColors.Transparent.ToHexString(); + string background = !string.IsNullOrWhiteSpace(position.Background) ? $"#{position.Background}" : MagickColors.Transparent.ToHexString(); - var tmp = new MagickImage($"xc:{background}", position.Width, position.Height); + var tmp = new MagickImage($"xc:{background}", position.Width, position.Height); - tmp.Composite(srcImg, Gravity.Center, 0, 0, CompositeOperator.Over); + tmp.Composite(srcImg, Gravity.Center, 0, 0, CompositeOperator.Over); - image.Composite(tmp, Gravity.Northwest, position.X, position.Y, CompositeOperator.Over); - - img++; + image.Composite(tmp, Gravity.Northwest, position.X, position.Y, CompositeOperator.Over); + + img++; + } } } diff --git a/src/Managers/ImageManager.Magik.cs b/src/Managers/ImageManager.Magik.cs index 57ed272..717bb2b 100644 --- a/src/Managers/ImageManager.Magik.cs +++ b/src/Managers/ImageManager.Magik.cs @@ -9,27 +9,35 @@ public static partial class ImageManager readonly static Encoding s_defaultEncoding = Encoding.Unicode; const double c_defaultFontSize = 20; - public static async Task<(MemoryStream data, string contentType)> GetLiquidRescaledImageAsync(string image) + public static async Task<(MemoryStream data, string contentType)> GetLiquidRescaledImageAsync(string image, CancellationToken cancellationToken) { - var imageStream = await HttpWebClient.GetStreamAsync(new Uri(image)).ConfigureAwait(false); + while (true) + { + if (cancellationToken.IsCancellationRequested) + { + return (null, null); + } + + var imageStream = await HttpWebClient.GetStreamAsync(new Uri(image)).ConfigureAwait(false); - using var magikImage = new MagickImageCollection(imageStream); + using var magikImage = new MagickImageCollection(imageStream); - Parallel.ForEach(magikImage, LiquidResizeFrame); + Parallel.ForEach(magikImage, LiquidResizeFrame); - MemoryStream stream = new(); + MemoryStream stream = new(); - magikImage.Write(stream); + magikImage.Write(stream); - stream.Position = 0; + stream.Position = 0; - magikImage.Dispose(); + magikImage.Dispose(); - string cType = "image/png"; + string cType = "image/png"; - if (magikImage.Count > 1) cType = "image/gif"; + if (magikImage.Count > 1) cType = "image/gif"; - return (stream, cType); + return (stream, cType); + } } static void LiquidResizeFrame(IMagickImage frame) diff --git a/src/Managers/ImageManager.Manipulation.cs b/src/Managers/ImageManager.Manipulation.cs index 1aa9b95..a95911a 100644 --- a/src/Managers/ImageManager.Manipulation.cs +++ b/src/Managers/ImageManager.Manipulation.cs @@ -26,40 +26,48 @@ public static partial class ImageManager }); } - public static async Task GenerateMemeImageAsync(string template, string[] texts) + public static async Task GenerateMemeImageAsync(string template, string[] texts, CancellationToken cancellationToken) { - ImageManipulationImage imageTemplate = ManipulationEndpoints.FirstOrDefault(e => e.Name.ToLowerInvariant().Equals(template.ToLowerInvariant())); - - if (imageTemplate is null) + while (true) { - var ex = new ArgumentException($"Couldn't find endpoint named \"{template}\"", nameof(template)); + if (cancellationToken.IsCancellationRequested) + { + return null; + } + + ImageManipulationImage imageTemplate = ManipulationEndpoints.FirstOrDefault(e => e.Name.ToLowerInvariant().Equals(template.ToLowerInvariant())); + + if (imageTemplate is null) + { + var ex = new ArgumentException($"Couldn't find endpoint named \"{template}\"", nameof(template)); - return GetResult(StatusCodes.Status404NotFound, EventResult.FromFailureException(ex.Message, ex)); - } + return GetResult(StatusCodes.Status404NotFound, EventResult.FromFailureException(ex.Message, ex)); + } - if (imageTemplate.TextPositions.Count > texts.Length) - { - var ex = new ArgumentException("Not enough texts provided", nameof(texts)); + if (imageTemplate.TextPositions.Count > texts.Length) + { + var ex = new ArgumentException("Not enough texts provided", nameof(texts)); - return GetResult(StatusCodes.Status400BadRequest, EventResult.FromFailureException(ex.Message, ex)); - } + return GetResult(StatusCodes.Status400BadRequest, EventResult.FromFailureException(ex.Message, ex)); + } - string image = Path.Combine(ManipulationTemplateBaseFolder, imageTemplate.Image); + string image = Path.Combine(ManipulationTemplateBaseFolder, imageTemplate.Image); - using MagickImage templateImage = new(File.ReadAllBytes(image)); - using MagickImage baseImage = new(MagickColors.Transparent, templateImage.Width, templateImage.Height); + using MagickImage templateImage = new(File.ReadAllBytes(image)); + using MagickImage baseImage = new(MagickColors.Transparent, templateImage.Width, templateImage.Height); - baseImage.Format = MagickFormat.Png; + baseImage.Format = MagickFormat.Png; - baseImage.Composite(templateImage, CompositeOperator.Over); + baseImage.Composite(templateImage, CompositeOperator.Over); - int textPoint = 0; - foreach (var textArea in imageTemplate.TextPositions) - { - baseImage.WriteText(texts[textPoint], textArea, MagickColors.White, Encoding.Unicode); - textPoint++; - } + int textPoint = 0; + foreach (var textArea in imageTemplate.TextPositions) + { + baseImage.WriteText(texts[textPoint], textArea, MagickColors.White, Encoding.Unicode); + textPoint++; + } - return baseImage.ToByteArray(); + return baseImage.ToByteArray(); + } } } \ No newline at end of file diff --git a/src/Managers/ImageManager.cs b/src/Managers/ImageManager.cs index afa9eeb..20210ca 100644 --- a/src/Managers/ImageManager.cs +++ b/src/Managers/ImageManager.cs @@ -1,5 +1,7 @@ +using System.Globalization; using System.Numerics; using System.Text; +using ICU4N.Text; using ImageMagick; using Kynareth.Models; using Microsoft.AspNetCore.Mvc; @@ -14,38 +16,63 @@ public static partial class ImageManager ConfigureManipulation(configuration, appEnvironment); } - private static ObjectResult GetResult(int statusCode, object data) - => new(data) { StatusCode = statusCode }; + static BreakIterator lineBreakIterator = BreakIterator.GetLineInstance(CultureInfo.InvariantCulture); - public static MagickImage WriteText(this MagickImage image, string text, PositionRect rect, MagickColor fontColor, Encoding encoding, string font = "Impact", int strokeWidth = 2, MagickColor? strokeColor = null) + private static ObjectResult GetResult(int statusCode, object data) => new(data) { StatusCode = statusCode }; + + private static string GetText(string text) { - using var label = new MagickImage($"label:{text}", new MagickReadSettings() + StringBuilder newText = new(); + + // set the text to be broken + lineBreakIterator.SetText(text); + + // get the first boundary + int start = lineBreakIterator.First(); + + // iterate over all boundaries + for (int end = lineBreakIterator.Next(); end != BreakIterator.Done; start = end, end = lineBreakIterator.Next()) + { + // get the current line + string line = text.Substring(start, end - start); + + // do something with the line + newText.AppendLine(line); + Console.WriteLine(line); + } + + return newText.ToString(); + } + + public static MagickImage WriteText(this MagickImage image, string text, PositionRect rect, MagickColor fontColor, Encoding encoding, string font = "Impact", int strokeWidth = 2, MagickColor? strokeColor = null, bool wrapText = true, Gravity textGravity = Gravity.Northwest) + { + using var label = new MagickImage($"{(wrapText ? "caption" : "label")}:{text}", new MagickReadSettings() { Width = rect.Width, Height = rect.Height, BackgroundColor = MagickColors.Transparent, FillColor = fontColor, - TextGravity = Gravity.Center, + TextGravity = textGravity, Font = font, TextEncoding = encoding, StrokeColor = new MagickColor(strokeColor ?? MagickColors.Black), - StrokeWidth = strokeWidth + StrokeWidth = strokeWidth, }); - + image.Composite(label, rect.X, rect.Y, CompositeOperator.Over); return image; } - public static MagickImage WriteText(this MagickImage image, string text, PositionRect rect, MagickColor fontColor, Encoding encoding, double fontSize, string font = "Impact", int strokeWidth = 2, MagickColor? strokeColor = null) + public static MagickImage WriteText(this MagickImage image, string text, PositionRect rect, MagickColor fontColor, Encoding encoding, double fontSize, string font = "Impact", int strokeWidth = 2, MagickColor? strokeColor = null, bool wrapText = true, Gravity textGravity = Gravity.Northwest) { - using var label = new MagickImage($"caption:{text}", new MagickReadSettings() + using var label = new MagickImage($"{(wrapText ? "caption" : "label")}:{text}", new MagickReadSettings() { Width = rect.Width, Height = rect.Height, BackgroundColor = MagickColors.Transparent, FillColor = fontColor, - TextGravity = Gravity.Center, + TextGravity = textGravity, FontPointsize = fontSize, Font = font, TextEncoding = encoding, @@ -58,15 +85,15 @@ public static partial class ImageManager return image; } - public static MagickImage WriteText(this MagickImage image, string text, Vector2 position, MagickColor fontColor, Encoding encoding, string font = "Impact", int strokeWidth = 2, MagickColor? strokeColor = null) + public static MagickImage WriteText(this MagickImage image, string text, Vector2 position, MagickColor fontColor, Encoding encoding, string font = "Impact", int strokeWidth = 2, MagickColor? strokeColor = null, bool wrapText = true, Gravity textGravity = Gravity.Northwest) { - using var label = new MagickImage($"caption:{text}", new MagickReadSettings() + using var label = new MagickImage($"{(wrapText ? "caption" : "label")}:{text}", new MagickReadSettings() { Width = image.Width, Height = image.Height, BackgroundColor = MagickColors.Transparent, FillColor = fontColor, - TextGravity = Gravity.Center, + TextGravity = textGravity, Font = font, TextEncoding = encoding, StrokeColor = new MagickColor(strokeColor ?? MagickColors.Black), @@ -78,15 +105,15 @@ public static partial class ImageManager return image; } - public static MagickImage WriteText(this MagickImage image, string text, Vector2 position, MagickColor fontColor, Encoding encoding, double fontSize, string font = "Impact", int strokeWidth = 2, MagickColor? strokeColor = null) + public static MagickImage WriteText(this MagickImage image, string text, Vector2 position, MagickColor fontColor, Encoding encoding, double fontSize, string font = "Impact", int strokeWidth = 2, MagickColor? strokeColor = null, bool wrapText = true, Gravity textGravity = Gravity.Northwest) { - using var label = new MagickImage($"caption:{text}", new MagickReadSettings() + using var label = new MagickImage($"{(wrapText ? "caption" : "label")}:{text}", new MagickReadSettings() { Width = image.Width, Height = image.Height, BackgroundColor = MagickColors.Transparent, FillColor = fontColor, - TextGravity = Gravity.Center, + TextGravity = textGravity, FontPointsize = fontSize, Font = font, TextEncoding = encoding, @@ -99,15 +126,15 @@ public static partial class ImageManager return image; } - public static MagickImage WriteText(this MagickImage image, string text, PointD position, MagickColor fontColor, Encoding encoding, string font = "Impact", int strokeWidth = 2, MagickColor? strokeColor = null) + public static MagickImage WriteText(this MagickImage image, string text, PointD position, MagickColor fontColor, Encoding encoding, string font = "Impact", int strokeWidth = 2, MagickColor? strokeColor = null, bool wrapText = true, Gravity textGravity = Gravity.Northwest) { - using var label = new MagickImage($"caption:{text}", new MagickReadSettings() + using var label = new MagickImage($"{(wrapText ? "caption" : "label")}:{text}", new MagickReadSettings() { Width = image.Width, Height = image.Height, BackgroundColor = MagickColors.Transparent, FillColor = fontColor, - TextGravity = Gravity.Center, + TextGravity = textGravity, Font = font, TextEncoding = encoding, StrokeColor = new MagickColor(strokeColor ?? MagickColors.Black), @@ -119,15 +146,15 @@ public static partial class ImageManager return image; } - public static MagickImage WriteText(this MagickImage image, string text, PointD position, MagickColor fontColor, Encoding encoding, double fontSize, string font = "Impact", int strokeWidth = 2, MagickColor? strokeColor = null) + public static MagickImage WriteText(this MagickImage image, string text, PointD position, MagickColor fontColor, Encoding encoding, double fontSize, string font = "Impact", int strokeWidth = 2, MagickColor? strokeColor = null, bool wrapText = true, Gravity textGravity = Gravity.Northwest) { - using var label = new MagickImage($"caption:{text}", new MagickReadSettings() + using var label = new MagickImage($"{(wrapText ? "caption" : "label")}:{text}", new MagickReadSettings() { Width = image.Width, Height = image.Height, BackgroundColor = MagickColors.Transparent, FillColor = fontColor, - TextGravity = Gravity.Center, + TextGravity = textGravity, FontPointsize = fontSize, Font = font, TextEncoding = encoding, @@ -140,15 +167,15 @@ public static partial class ImageManager return image; } - public static MagickImage WriteText(this MagickImage image, string text, int x, int y, MagickColor fontColor, Encoding encoding, string font = "Impact", int strokeWidth = 2, MagickColor? strokeColor = null) + public static MagickImage WriteText(this MagickImage image, string text, int x, int y, MagickColor fontColor, Encoding encoding, string font = "Impact", int strokeWidth = 2, MagickColor? strokeColor = null, bool wrapText = true, Gravity textGravity = Gravity.Northwest) { - using var label = new MagickImage($"caption:{text}", new MagickReadSettings() + using var label = new MagickImage($"{(wrapText ? "caption" : "label")}:{text}", new MagickReadSettings() { Width = image.Width, Height = image.Height, BackgroundColor = MagickColors.Transparent, FillColor = fontColor, - TextGravity = Gravity.Center, + TextGravity = textGravity, Font = font, TextEncoding = encoding, StrokeColor = new MagickColor(strokeColor ?? MagickColors.Black), @@ -160,15 +187,15 @@ public static partial class ImageManager return image; } - public static MagickImage WriteText(this MagickImage image, string text, int x, int y, MagickColor fontColor, Encoding encoding, double fontSize, string font = "Impact", int strokeWidth = 2, MagickColor? strokeColor = null) + public static MagickImage WriteText(this MagickImage image, string text, int x, int y, MagickColor fontColor, Encoding encoding, double fontSize, string font = "Impact", int strokeWidth = 2, MagickColor? strokeColor = null, bool wrapText = true, Gravity textGravity = Gravity.Northwest) { - using var label = new MagickImage($"caption:{text}", new MagickReadSettings() + using var label = new MagickImage($"{(wrapText ? "caption" : "label")}:{text}", new MagickReadSettings() { Width = image.Width, Height = image.Height, BackgroundColor = MagickColors.Transparent, FillColor = fontColor, - TextGravity = Gravity.Center, + TextGravity = textGravity, FontPointsize = fontSize, Font = font, TextEncoding = encoding, diff --git a/src/Program.cs b/src/Program.cs index 2e16ad0..643996f 100644 --- a/src/Program.cs +++ b/src/Program.cs @@ -32,6 +32,7 @@ builder.Services.AddDbContext(options => var app = builder.Build(); +RequestHelper.Configure(app); ImageManager.Configure(app.Configuration, app.Environment); AmogusHelper.Configure(app.Environment); diff --git a/src/appsettings.json b/src/appsettings.json index 4d79871..6f56ff6 100644 --- a/src/appsettings.json +++ b/src/appsettings.json @@ -1702,7 +1702,7 @@ "x": 593, "y": 411, "w": 169, - "h": 75 + "h": 300 } ] },