Changed Profile Picture to be always square (will be extended using black areas)
This commit is contained in:
parent
847a543266
commit
c50fc079fd
|
@ -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;
|
||||
|
|
|
@ -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;
|
||||
|
||||
|
|
Loading…
Reference in a new issue