-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathImageProccessor.cs
269 lines (251 loc) · 9.9 KB
/
ImageProccessor.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
namespace Stickers.ImageFunctions;
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using Azure.Messaging.EventGrid;
using Azure.Messaging.EventGrid.SystemEvents;
using Azure.Storage.Blobs;
using Azure.Storage.Blobs.Models;
using ByteSizeLib;
using ImageMagick;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.EventGrid;
using Microsoft.Extensions.Logging;
public static class ImageProcessor
{
private const string LOG_TAG = $"{nameof(ImageProcessor)}.{nameof(Run)}";
private const string VERSION_KEY = "ipv";
private const long LATEST_IMAGE_PROCESSOR_VERSION = 1;
private const int TARGET_SIZE = 256;
private const int TARGET_COLORS = 256;
private const string CacheControl = "public, max-age=604800, immutable";
/// <summary>
/// The image type can be disable in Teams
/// https://learn.microsoft.com/en-us/microsoftteams/platform/task-modules-and-cards/cards/cards-format?tabs=adaptive-md%2Cconnector-html
/// </summary>
private static readonly HashSet<string> COMPATIBLE_CONTENT_TYPES =
new() { "image/png", "image/gif", "image/jpeg", "image/jpg", };
/// <summary>
/// IMAGE format mapping
/// </summary>
private static readonly Dictionary<MagickFormat, MagickFormat> FORMAT_MAPPING =
new()
{
// pngs
{ MagickFormat.Png, MagickFormat.Png },
{ MagickFormat.Png8, MagickFormat.Png8 },
{ MagickFormat.Png24, MagickFormat.Png24 },
{ MagickFormat.Png32, MagickFormat.Png32 },
{ MagickFormat.Png48, MagickFormat.Png48 },
{ MagickFormat.Png64, MagickFormat.Png64 },
{ MagickFormat.Png00, MagickFormat.Png00 },
{ MagickFormat.Jng, MagickFormat.Png },
{ MagickFormat.WebP, MagickFormat.Png },
{ MagickFormat.Svg, MagickFormat.Png },
// jpgs
{ MagickFormat.Bmp, MagickFormat.Jpeg },
{ MagickFormat.Bmp2, MagickFormat.Jpeg },
{ MagickFormat.Bmp3, MagickFormat.Jpeg },
{ MagickFormat.Cmyk, MagickFormat.Jpeg },
{ MagickFormat.Jpg, MagickFormat.Jpg },
{ MagickFormat.Jpeg, MagickFormat.Jpeg },
{ MagickFormat.J2c, MagickFormat.Jpeg },
{ MagickFormat.J2k, MagickFormat.Jpeg },
{ MagickFormat.Jp2, MagickFormat.Jpeg },
{ MagickFormat.Jpe, MagickFormat.Jpeg },
{ MagickFormat.Jpm, MagickFormat.Jpeg },
{ MagickFormat.Jps, MagickFormat.Jpeg },
{ MagickFormat.Jpt, MagickFormat.Jpeg },
{ MagickFormat.Tif, MagickFormat.Jpeg },
{ MagickFormat.Tiff, MagickFormat.Jpeg },
{ MagickFormat.Tiff64, MagickFormat.Jpeg },
{ MagickFormat.Rgb, MagickFormat.Jpeg },
// gifs
{ MagickFormat.Gif, MagickFormat.Gif },
{ MagickFormat.Gif87, MagickFormat.Gif87 },
{ MagickFormat.WebM, MagickFormat.Gif },
{ MagickFormat.APng, MagickFormat.Gif },
{ MagickFormat.Avif, MagickFormat.Gif },
{ MagickFormat.M2v, MagickFormat.Gif },
{ MagickFormat.M4v, MagickFormat.Gif },
{ MagickFormat.Mp4, MagickFormat.Gif },
};
private static readonly Dictionary<string, string> Metadata =
new() { { VERSION_KEY, LATEST_IMAGE_PROCESSOR_VERSION.ToString() }, };
// [FunctionName("ImageProcessor")]
// public void Run(
// [BlobTrigger("stickers/{name}", Source = BlobTriggerSource.EventGrid)] BlobClient imageBlobClient,
// string name,
// ILogger log
// )
/// <summary>
/// auto resize, compress and covert the images
/// </summary>
/// <param name="eventGridEvent"></param>
/// <param name="imageBlobClient"></param>
/// <param name="logger"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
[FunctionName("ImageProcessor")]
public static async Task Run(
[EventGridTrigger] EventGridEvent eventGridEvent,
[Blob("{data.url}", FileAccess.ReadWrite, Connection = "STICKERS_STORAGE")]
BlobClient imageBlobClient,
ILogger logger,
CancellationToken cancellationToken
)
{
if (eventGridEvent == null || imageBlobClient == null)
{
logger.LogError("Null arguments");
return;
}
var eventData = eventGridEvent.Data.ToObjectFromJson<StorageBlobCreatedEventData>();
var name = eventData.Url;
logger.LogDebug($"[{LOG_TAG}:{name}] ({eventData.ContentType}) triggered");
using var _ = logger.BeginScope($"{LOG_TAG}:{{name}}", new { name });
try
{
// check version
var properties = (
await imageBlobClient.GetPropertiesAsync(cancellationToken: cancellationToken)
);
if (
properties.Value.Metadata.TryGetValue(VERSION_KEY, out var versionString)
&& long.TryParse(versionString, out var version)
&& version >= LATEST_IMAGE_PROCESSOR_VERSION
)
{
logger.LogWarning(
$"no-ops due to detecting {VERSION_KEY} = {versionString} metadata"
);
return;
}
// read
using var inImageBlobStream = await imageBlobClient.OpenReadAsync(
cancellationToken: cancellationToken
);
var (mimeType, outBytes, originFormat) = ProcessImage(inImageBlobStream, logger);
// write
var compressionRatio = (double)outBytes.LongLength / inImageBlobStream.Length;
if (
COMPATIBLE_CONTENT_TYPES.Contains(originFormat.MimeType) && compressionRatio >= 0.99
)
{
// keep the origin data
await imageBlobClient.SetHttpHeadersAsync(
new() { CacheControl = CacheControl, ContentType = originFormat.MimeType },
cancellationToken: cancellationToken
);
await imageBlobClient.SetMetadataAsync(
Metadata,
cancellationToken: cancellationToken
);
logger.LogInformation(
$"keep the origin image, format: {originFormat.MimeType}({originFormat.Format}) for compression ratio: {compressionRatio:P2}"
);
}
else
{
await SaveBlob(imageBlobClient, outBytes, mimeType, cancellationToken);
logger.LogInformation(
$"wrote blob ({ByteSize.FromBytes(outBytes.LongLength)}, type: {mimeType} with compression ratio: {compressionRatio:P2}"
);
}
}
catch (Exception e)
{
logger.LogError(e, $"{name} got exception: {e}");
throw;
}
}
/// <summary>
/// read image form stream and covert it
/// </summary>
/// <param name="blobStream"></param>
/// <param name="logger"></param>
/// <returns></returns>
private static (string mimeType, byte[] data, IMagickFormatInfo originFormatInfo) ProcessImage(
Stream blobStream,
ILogger logger
)
{
using var images = new MagickImageCollection(blobStream);
var originFormatInfo = MagickFormatInfo.Create(images[0].Format);
images.Coalesce();
var pivot = images[0];
logger.LogInformation(
$"read image ({ByteSize.FromBytes(blobStream.Length)}, format: {pivot.Format}, size: {pivot.Width}x{pivot.Height}x{images.Count})"
);
// resize
if (pivot.Width > TARGET_SIZE || pivot.Height > TARGET_SIZE)
{
foreach (var image in images)
{
image.AdaptiveResize(TARGET_SIZE, TARGET_SIZE);
}
}
// quantize
var settings = new QuantizeSettings()
{
Colors = TARGET_COLORS,
DitherMethod = DitherMethod.FloydSteinberg,
};
images.Quantize(settings);
// optimize
images.OptimizePlus();
images.OptimizeTransparency();
// convert
pivot = images[0];
if (!FORMAT_MAPPING.TryGetValue(originFormatInfo.Format, out var outFormat))
{
if (COMPATIBLE_CONTENT_TYPES.Contains(originFormatInfo.MimeType))
{
outFormat = originFormatInfo.Format;
}
else if (originFormatInfo.IsMultiFrame)
{
outFormat = MagickFormat.Gif;
}
else
{
outFormat = MagickFormat.Png;
}
}
var outBytes = images.ToByteArray(outFormat);
logger.LogInformation(
$"covert to ({ByteSize.FromBytes(outBytes.LongLength)}, format: {outFormat}, size: {pivot.Width}x{pivot.Height}x{images.Count})"
);
var contentType = MagickFormatInfo.Create(outFormat)?.MimeType;
return (contentType, outBytes, originFormatInfo);
}
/// <summary>
/// save data to blob
/// </summary>
/// <param name="blobClient"></param>
/// <param name="data"></param>
/// <param name="contentType"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
private static async Task SaveBlob(
BlobClient blobClient,
byte[] data,
string contentType,
CancellationToken cancellationToken
)
{
var options = new BlobOpenWriteOptions()
{
HttpHeaders = new() { ContentType = contentType, CacheControl = CacheControl, },
Metadata = Metadata
};
using var outImageBlobStream = await blobClient.OpenWriteAsync(
true,
options,
cancellationToken: cancellationToken
);
await outImageBlobStream.WriteAsync(data, cancellationToken: cancellationToken);
}
}