From 1a631362c9a69193ec12919c770e148bdd60a461 Mon Sep 17 00:00:00 2001 From: Glax Date: Fri, 18 Oct 2024 19:30:57 +0200 Subject: [PATCH] Use Sixlabors.Imagesharp for resizing coverimages. --- README.md | 1 + Tranga/Server/Server.cs | 15 +++++++++++---- Tranga/Server/v2Manga.cs | 26 +++++++++----------------- Tranga/Tranga.csproj | 1 + 4 files changed, 22 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index 011927c..f10ff84 100644 --- a/README.md +++ b/README.md @@ -90,6 +90,7 @@ That is why I wanted to create my own project, in a language I understand, and t - [PuppeteerSharp](https://www.puppeteersharp.com/) - [Html Agility Pack (HAP)](https://html-agility-pack.net/) - [Soenneker.Utils.String.NeedlemanWunsch](https://github.com/soenneker/soenneker.utils.string.needlemanwunsch) +- [Sixlabors.ImageSharp](https://docs-v2.sixlabors.com/articles/imagesharp/index.html#license) - 💙 Blåhaj 🦈

(back to top)

diff --git a/Tranga/Server/Server.cs b/Tranga/Server/Server.cs index 6a0e057..818a6c9 100644 --- a/Tranga/Server/Server.cs +++ b/Tranga/Server/Server.cs @@ -3,6 +3,8 @@ using System.Runtime.InteropServices; using System.Text; using System.Text.RegularExpressions; using Newtonsoft.Json; +using SixLabors.ImageSharp; +using SixLabors.ImageSharp.Formats.Png; namespace Tranga.Server; @@ -26,13 +28,13 @@ public partial class Server : GlobalBase, IDisposable new ("GET", @"/v2/Mangas", GetV2Mangas), new ("GET", @"/v2/Manga/Search", GetV2MangaSearch), new ("GET", @"/v2/Manga", GetV2Manga), - new ("GET", @"/v2/Manga/([-A-Za-z0-9]*={0,3})", GetV2MangaInternalId), - new ("DELETE", @"/v2/Manga/([-A-Za-z0-9]*={0,3})", DeleteV2MangaInternalId), new ("GET", @"/v2/Manga/([-A-Za-z0-9]*={0,3})/Cover", GetV2MangaInternalIdCover), new ("GET", @"/v2/Manga/([-A-Za-z0-9]*={0,3})/Chapters", GetV2MangaInternalIdChapters), new ("GET", @"/v2/Manga/([-A-Za-z0-9]*={0,3})/Chapters/Latest", GetV2MangaInternalIdChaptersLatest), new ("POST", @"/v2/Manga/([-A-Za-z0-9]*={0,3})/ignoreChaptersBelow", PostV2MangaInternalIdIgnoreChaptersBelow), new ("POST", @"/v2/Manga/([-A-Za-z0-9]*={0,3})/moveFolder", PostV2MangaInternalIdMoveFolder), + new ("GET", @"/v2/Manga/([-A-Za-z0-9]*={0,3})", GetV2MangaInternalId), + new ("DELETE", @"/v2/Manga/([-A-Za-z0-9]*={0,3})", DeleteV2MangaInternalId), new ("GET", @"/v2/Jobs", GetV2Jobs), new ("GET", @"/v2/Jobs/Running", GetV2JobsRunning), new ("GET", @"/v2/Jobs/Waiting", GetV2JobsWaiting), @@ -205,10 +207,15 @@ public partial class Server : GlobalBase, IDisposable { if (content is Stream stream) { - response.ContentType = "image/jpeg"; - response.AddHeader("Cache-Control", "max-age=600"); + response.ContentType = "text/plain"; stream.CopyTo(response.OutputStream); stream.Close(); + }else if (content is Image image) + { + response.ContentType = image.Metadata.DecodedImageFormat?.DefaultMimeType ?? PngFormat.Instance.DefaultMimeType; + response.AddHeader("Cache-Control", "max-age=600"); + image.Save(response.OutputStream, image.Metadata.DecodedImageFormat ?? PngFormat.Instance); + image.Dispose(); } else { diff --git a/Tranga/Server/v2Manga.cs b/Tranga/Server/v2Manga.cs index 2528cba..9753907 100644 --- a/Tranga/Server/v2Manga.cs +++ b/Tranga/Server/v2Manga.cs @@ -1,5 +1,5 @@ -using System.Drawing; -using System.Drawing.Imaging; +using SixLabors.ImageSharp; +using SixLabors.ImageSharp.Processing; using System.Net; using System.Text.RegularExpressions; using Tranga.Jobs; @@ -82,7 +82,7 @@ public partial class Server if(!File.Exists(filePath)) return new ValueTuple(HttpStatusCode.NotFound, "Cover-File not found."); - Bitmap bitmap; + Image image = Image.Load(filePath); if (requestParameters.TryGetValue("dimensions", out string? dimensionsStr)) { Regex dimensionsRex = new(@"([0-9]+)x([0-9]+)"); @@ -93,23 +93,15 @@ public partial class Server int height = int.Parse(m.Groups[2].Value); double aspectRequested = (double)width / (double)height; - using Image coverImage = Image.FromFile(filePath); - double aspectCover = (double)coverImage.Width / (double)coverImage.Height; + double aspectCover = (double)image.Width / (double)image.Height; Size newSize = aspectRequested > aspectCover - ? new Size(width, (width / coverImage.Width) * coverImage.Height) - : new Size((height / coverImage.Height) * coverImage.Width, height); - - bitmap = new(coverImage, newSize); + ? new Size(width, (width / image.Width) * image.Height) + : new Size((height / image.Height) * image.Width, height); + + image.Mutate(x => x.Resize(newSize)); } - else - { - FileStream coverStream = new(filePath, FileMode.Open); - bitmap = new(coverStream); - } - using MemoryStream ret = new(); - bitmap.Save(ret, ImageFormat.Jpeg); - return new ValueTuple(HttpStatusCode.OK, ret); + return new ValueTuple(HttpStatusCode.OK, image); } private ValueTuple GetV2MangaInternalIdChapters(GroupCollection groups, Dictionary requestParameters) diff --git a/Tranga/Tranga.csproj b/Tranga/Tranga.csproj index 17a3133..70fa5ef 100644 --- a/Tranga/Tranga.csproj +++ b/Tranga/Tranga.csproj @@ -13,6 +13,7 @@ +