Changed Profile Picture to be always square (will be extended using black areas)
Some checks failed
Docker Release / build (push) Has been cancelled
GitHub Release / Generate Release (push) Has been cancelled

This commit is contained in:
Mia Rose Winter 2024-04-19 21:48:07 +02:00
parent 847a543266
commit c50fc079fd
Signed by: miawinter
GPG key ID: 4B6F6A83178F595E
2 changed files with 6 additions and 4 deletions

View file

@ -40,7 +40,7 @@
private async Task ProfilePictureChanged(string tempFilePath) {
if (User is null) return;
var guid = await ImageService.StoreImageAsync(tempFilePath);
var guid = await ImageService.StoreImageAsync(tempFilePath, enforceSize:true);
if (!guid.HasValue) throw new ApplicationException("Processing Image failed.");
Guid? imageToDelete = null;

View file

@ -13,15 +13,16 @@ public class ImageService(ILogger<ImageService> logger) {
return File.Exists(path) ? path : null;
}
public async Task<byte[]> GetResized(string path, int size) {
public async Task<byte[]> GetResized(string path, int size, bool enforceSize = false, CancellationToken cancellation = default) {
var image = new MagickImage(path);
image.Resize(new MagickGeometry(size));
if (enforceSize) image.Extent(new MagickGeometry(size), Gravity.Center, MagickColors.Black);
using var memory = new MemoryStream();
await image.WriteAsync(memory);
await image.WriteAsync(memory, cancellation);
return memory.ToArray();
}
public async ValueTask<Guid?> StoreImageAsync(string temporaryPath, int size = 800,
public async ValueTask<Guid?> StoreImageAsync(string temporaryPath, int size = 800, bool enforceSize = false,
CancellationToken cancellation = default) {
if (File.Exists(temporaryPath) is not true) return null;
@ -33,6 +34,7 @@ public class ImageService(ILogger<ImageService> logger) {
// Jpeg with 90% compression should look decent
image.Resize(new MagickGeometry(size)); // this preserves aspect ratio
if (enforceSize) image.Extent(new MagickGeometry(size), Gravity.Center, MagickColors.Black);
image.Format = MagickFormat.Jpeg;
image.Quality = 90;