-
-
Save titouancreach/eb097a5e509109fbf033f89f2e6be9fa to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Text; | |
using Microsoft.AspNetCore.WebUtilities; | |
namespace LiveBoat.Web.Services | |
{ | |
public class ImageProxyOptions | |
{ | |
public int? Width { get; set; } | |
public int? Height { get; set; } | |
public string Format { get; set; } | |
public string ResizeType { get; set; } = "fill"; | |
public bool Enlarge { get; set; } = false; | |
public int Dpr { get; set; } = 2; | |
} | |
public class ImageProxyService | |
{ | |
public string GetUrl(string originalUrl, ImageProxyOptions options = null) | |
{ | |
if (string.IsNullOrEmpty(originalUrl)) | |
{ | |
return originalUrl; | |
} | |
if (options == null) | |
{ | |
options = new ImageProxyOptions(); | |
} | |
var baseUrl = "https://img.unitee.io"; | |
var b64 = WebEncoders.Base64UrlEncode(Encoding.UTF8.GetBytes(originalUrl)); | |
var processingOptions = new StringBuilder(); | |
if (options.Width.HasValue && !options.Height.HasValue) | |
{ | |
options.Height = 0; // valeur 0 veut dire de garder l'aspect ratio | |
} | |
if (options.Height.HasValue && !options.Width.HasValue) | |
{ | |
options.Width = 0; | |
} | |
if (options.Width.HasValue && options.Height.HasValue) | |
{ | |
processingOptions.Append($"resize:{options.ResizeType}:{options.Width}:{options.Height}:{(options.Enlarge ? 1 : 0)}:0/"); | |
// doubler la densité de pixel pour les écrans rétina (= dpr:2) | |
processingOptions.Append($"dpr:{options.Dpr}/"); | |
} | |
if (options.Format != null) | |
{ | |
processingOptions.Append($"format:{options.Format}/"); | |
} | |
return $"{baseUrl}/insecure/{processingOptions}{b64}"; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment