@maioradv/nestjs-core 2.0.1 → 2.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -22,13 +22,19 @@ let ImageCompressionPipe = class ImageCompressionPipe {
|
|
|
22
22
|
}
|
|
23
23
|
async transform(file, metadata) {
|
|
24
24
|
try {
|
|
25
|
-
const
|
|
25
|
+
const picture = (0, sharp_1.default)(file.buffer, { failOn: 'error' });
|
|
26
|
+
const meta = await picture.metadata();
|
|
27
|
+
const stats = await picture.stats();
|
|
28
|
+
const dev = stats.channels.map(c => c.stdev);
|
|
29
|
+
const avg = dev.reduce((a, b) => a + b) / dev.length;
|
|
30
|
+
const max = Math.max(...dev);
|
|
31
|
+
const isFlat = avg < 20 && max < 30;
|
|
26
32
|
const defaultQuality = this.defaultQuality(meta.width, meta.height);
|
|
27
33
|
const [resolutionCap, quality] = [
|
|
28
34
|
this.options.resolutionCap ?? 2560,
|
|
29
35
|
this.options.quality ?? defaultQuality
|
|
30
36
|
];
|
|
31
|
-
const compressedBuffer = await
|
|
37
|
+
const compressedBuffer = await picture
|
|
32
38
|
.resize({
|
|
33
39
|
width: resolutionCap,
|
|
34
40
|
height: resolutionCap,
|
|
@@ -36,7 +42,7 @@ let ImageCompressionPipe = class ImageCompressionPipe {
|
|
|
36
42
|
withoutEnlargement: true
|
|
37
43
|
})
|
|
38
44
|
.withMetadata()
|
|
39
|
-
.webp({ quality: quality })
|
|
45
|
+
.webp(isFlat ? { lossless: true } : { quality: quality })
|
|
40
46
|
.rotate()
|
|
41
47
|
.toBuffer();
|
|
42
48
|
file.buffer = compressedBuffer;
|