- ImageService: ResizeDataUrlSmall() für 256px WebP-Thumbnails (Quality 70) - Neue DB-Spalten: agents.avatar_small, sessions.image_small - Migration generiert Thumbnails für bestehende Bilder automatisch - Listen-Endpoints (/api/agents, /api/sessions) liefern kleine Variante - Detail-Endpoints liefern weiterhin das volle 512px-Bild - Frontend unverändert (Cards nutzen automatisch die kleineren Bilder)
66 lines
2.1 KiB
C#
66 lines
2.1 KiB
C#
using System;
|
|
using System.IO;
|
|
using SixLabors.ImageSharp;
|
|
using SixLabors.ImageSharp.Formats.Webp;
|
|
using SixLabors.ImageSharp.Processing;
|
|
|
|
namespace Gott.Services;
|
|
|
|
public class ImageService
|
|
{
|
|
private const int MaxSideLength = 512;
|
|
private const int SmallSideLength = 256;
|
|
private const int WebPQuality = 80;
|
|
private const int WebPQualitySmall = 70;
|
|
|
|
/// <summary>
|
|
/// Nimmt eine Base64-Data-URL (z.B. "data:image/png;base64,..."), skaliert das Bild
|
|
/// auf max 512px Seitenlänge, konvertiert zu WebP und gibt eine neue Data-URL zurück.
|
|
/// Gibt null zurück wenn input null/leer ist.
|
|
/// </summary>
|
|
public string ResizeDataUrl(string dataUrl)
|
|
{
|
|
return ResizeToWebP(dataUrl, MaxSideLength, WebPQuality);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Erzeugt eine kleine Thumbnail-Variante (max 256px, Quality 70) für Dashboard-Listen.
|
|
/// Gibt null zurück wenn input null/leer ist.
|
|
/// </summary>
|
|
public string ResizeDataUrlSmall(string dataUrl)
|
|
{
|
|
return ResizeToWebP(dataUrl, SmallSideLength, WebPQualitySmall);
|
|
}
|
|
|
|
private static string ResizeToWebP(string dataUrl, int maxSide, int quality)
|
|
{
|
|
if (string.IsNullOrEmpty(dataUrl)) return null;
|
|
|
|
// Data-URL parsen: "data:<mediaType>;base64,<data>"
|
|
var commaIndex = dataUrl.IndexOf(',');
|
|
if (commaIndex < 0) return dataUrl;
|
|
|
|
var base64 = dataUrl.Substring(commaIndex + 1);
|
|
var bytes = Convert.FromBase64String(base64);
|
|
|
|
using var image = Image.Load(bytes);
|
|
|
|
// Skalieren wenn nötig
|
|
if (image.Width > maxSide || image.Height > maxSide)
|
|
{
|
|
image.Mutate(x => x.Resize(new ResizeOptions
|
|
{
|
|
Size = new Size(maxSide, maxSide),
|
|
Mode = ResizeMode.Max
|
|
}));
|
|
}
|
|
|
|
// Immer als WebP encodieren
|
|
using var ms = new MemoryStream();
|
|
image.Save(ms, new WebpEncoder { Quality = quality });
|
|
var webpBase64 = Convert.ToBase64String(ms.ToArray());
|
|
|
|
return "data:image/webp;base64," + webpBase64;
|
|
}
|
|
}
|