-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathCreateThumbnail.cs
55 lines (47 loc) · 1.75 KB
/
CreateThumbnail.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
using Microsoft.Azure.Functions.Worker;
using Microsoft.Extensions.Logging;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Formats.Jpeg;
using SixLabors.ImageSharp.Processing;
namespace dotnet
{
public class CreateThumbnail
{
private readonly ILogger<CreateThumbnail> _logger;
public CreateThumbnail(ILogger<CreateThumbnail> logger)
{
_logger = logger;
}
const int thumbnailWidth = 1280;
const int thumbnailHeight = 720;
[Function(nameof(CreateThumbnail))]
[BlobOutput("images-thumbnails/thumbnail.jpg", Connection = "AzureWebJobsStorage")]
public async Task<Byte[]> Run(
[BlobTrigger("images/{name}", Connection = "AzureWebJobsStorage")] Stream stream,
string name
)
{
_logger.LogInformation($"Processing blob\n Name: {name} \n Data: {name.Length}");
using (var image = Image.Load(stream))
{
// Generate thumbnail
image.Mutate(async x =>
x.Resize(
new ResizeOptions
{
Size = new Size(thumbnailWidth, thumbnailHeight),
Mode = ResizeMode.Max,
}
)
);
var outputBlob = new MemoryStream();
image.Save(outputBlob, new JpegEncoder());
// Save the thumbnail to the output blob
_logger.LogInformation(
$"Finished processing blob\n Name:{name} and saved to output blob"
);
return outputBlob.ToArray();
}
}
}
}