From bcf814339d8ffb0759549d9f189d2bae0f181a8f Mon Sep 17 00:00:00 2001 From: damienbod Date: Fri, 28 Jul 2023 15:49:45 +0200 Subject: [PATCH 1/2] photo format fixes --- .../IssueVerifiableEmployee.csproj | 1 + .../MicrosoftGraphDelegatedClient.cs | 16 +++++++++++++--- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/IssueVerifiableEmployee/IssueVerifiableEmployee.csproj b/IssueVerifiableEmployee/IssueVerifiableEmployee.csproj index 69c9532..1b2b866 100644 --- a/IssueVerifiableEmployee/IssueVerifiableEmployee.csproj +++ b/IssueVerifiableEmployee/IssueVerifiableEmployee.csproj @@ -8,6 +8,7 @@ + diff --git a/IssueVerifiableEmployee/Services/GraphServices/MicrosoftGraphDelegatedClient.cs b/IssueVerifiableEmployee/Services/GraphServices/MicrosoftGraphDelegatedClient.cs index 5e0226d..9ffe3ca 100644 --- a/IssueVerifiableEmployee/Services/GraphServices/MicrosoftGraphDelegatedClient.cs +++ b/IssueVerifiableEmployee/Services/GraphServices/MicrosoftGraphDelegatedClient.cs @@ -1,4 +1,5 @@ -using IssuerVerifiableEmployee.Persistence; +using ImageMagick; +using IssuerVerifiableEmployee.Persistence; using Microsoft.Graph; using Microsoft.Graph.Models; using Microsoft.IdentityModel.Tokens; @@ -118,13 +119,22 @@ public MicrosoftGraphDelegatedClient(GraphServiceClient graphServiceClient) public async Task GetGraphApiProfilePhoto(string oid) { var photo = string.Empty; + byte[] photoByte; + using (var photoStream = await _graphServiceClient.Users[oid].Photo .Content.GetAsync()) { - byte[] photoByte = ((MemoryStream)photoStream!).ToArray(); - photo = Base64UrlEncoder.Encode(photoByte); + photoByte = ((MemoryStream)photoStream!).ToArray(); } + using var imageFromFile = new MagickImage(photoByte); + // Sets the output format to jpeg + imageFromFile.Format = MagickFormat.Jpeg; + + // Create byte array that contains a jpeg file + var data = imageFromFile.ToByteArray(); + photo = Base64UrlEncoder.Encode(data); + return photo; } From 9d1d896e1e7430b86b54edab1dec3acd30e59b0e Mon Sep 17 00:00:00 2001 From: damienbod Date: Fri, 28 Jul 2023 15:53:36 +0200 Subject: [PATCH 2/2] resize image --- .../GraphServices/MicrosoftGraphDelegatedClient.cs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/IssueVerifiableEmployee/Services/GraphServices/MicrosoftGraphDelegatedClient.cs b/IssueVerifiableEmployee/Services/GraphServices/MicrosoftGraphDelegatedClient.cs index 9ffe3ca..501362b 100644 --- a/IssueVerifiableEmployee/Services/GraphServices/MicrosoftGraphDelegatedClient.cs +++ b/IssueVerifiableEmployee/Services/GraphServices/MicrosoftGraphDelegatedClient.cs @@ -130,6 +130,13 @@ public async Task GetGraphApiProfilePhoto(string oid) using var imageFromFile = new MagickImage(photoByte); // Sets the output format to jpeg imageFromFile.Format = MagickFormat.Jpeg; + var size = new MagickGeometry(400, 400); + + // This will resize the image to a fixed size without maintaining the aspect ratio. + // Normally an image will be resized to fit inside the specified size. + //size.IgnoreAspectRatio = true; + + imageFromFile.Resize(size); // Create byte array that contains a jpeg file var data = imageFromFile.ToByteArray();