-
Notifications
You must be signed in to change notification settings - Fork 9
/
image-optimizer-service.ts
197 lines (176 loc) · 5.76 KB
/
image-optimizer-service.ts
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
import { ReadStream, createReadStream, createWriteStream } from "fs";
import { join } from "path";
import sharp, { Sharp, Metadata } from "sharp";
import { file as fileUtils } from '@strapi/utils';
// @ts-ignore - No types available
import pluginUpload from "@strapi/plugin-upload/strapi-server";
const imageManipulation = pluginUpload().services["image-manipulation"];
import {
OutputFormat,
ImageSize,
File,
SourceFile,
StrapiImageFormat,
SourceFormat,
} from "../models";
import settingsService from "./settings-service";
const defaultFormats: OutputFormat[] = ["original", "webp", "avif"];
const defaultInclude: SourceFormat[] = ["jpeg", "jpg", "png"];
const defaultQuality = 80;
async function optimizeImage(file: SourceFile): Promise<StrapiImageFormat[]> {
// Get config
const {
exclude = [],
formats = defaultFormats,
include = defaultInclude,
sizes,
additionalResolutions,
quality = defaultQuality,
} = settingsService.settings;
const sourceFileType = file.ext.replace(".", "");
if (
exclude.includes(sourceFileType.toLowerCase() as SourceFormat) ||
!include.includes(sourceFileType.toLowerCase() as SourceFormat)
) {
return Promise.all([]);
}
const imageFormatPromises: Promise<StrapiImageFormat>[] = [];
formats.forEach((format) => {
sizes.forEach((size) => {
imageFormatPromises.push(generateImage(file, format, size, quality));
if (additionalResolutions) {
additionalResolutions.forEach((resizeFactor) => {
imageFormatPromises.push(
generateImage(file, format, size, quality, resizeFactor)
);
});
}
});
});
return Promise.all(imageFormatPromises);
}
async function generateImage(
sourceFile: SourceFile,
format: OutputFormat,
size: ImageSize,
quality: number,
resizeFactor = 1
): Promise<StrapiImageFormat> {
const resizeFactorPart = resizeFactor === 1 ? "" : `_${resizeFactor}x`;
const sizeName = `${size.name}${resizeFactorPart}`;
const formatPart = format === "original" ? "" : `_${format}`;
return {
key: `${sizeName}${formatPart}`,
file: await resizeFileTo(
sourceFile,
sizeName,
format,
size,
quality,
resizeFactor
),
};
}
async function resizeFileTo(
sourceFile: SourceFile,
sizeName: string,
format: OutputFormat,
size: ImageSize,
quality: number,
resizeFactor: number
): Promise<File> {
let sharpInstance = sharp();
if (format !== "original") {
sharpInstance = sharpInstance.toFormat(format);
}
sharpInstance = sharpAddFormatSettings(sharpInstance, { quality });
sharpInstance = sharpAddResizeSettings(
sharpInstance,
size,
resizeFactor,
sourceFile
);
const imageHash = `${sizeName}_${format}_${sourceFile.hash}`;
const filePath = join(sourceFile.tmpWorkingDirectory, imageHash);
const newImageStream = sourceFile.getStream().pipe(sharpInstance);
await writeStreamToFile(newImageStream, filePath);
const metadata = await getMetadata(createReadStream(filePath));
return {
name: getFileName(sourceFile, sizeName),
hash: imageHash,
ext: getFileExtension(sourceFile, format),
mime: getFileMimeType(sourceFile, format),
path: sourceFile.path,
width: metadata.width,
height: metadata.height,
size: metadata.size && fileUtils.bytesToKbytes(metadata.size),
getStream: () => createReadStream(filePath),
};
}
function sharpAddFormatSettings(
sharpInstance: Sharp,
{ quality }: { quality?: number }
): Sharp {
// TODO: Add jxl when it's no longer experimental
return sharpInstance
.jpeg({ quality, progressive: true, force: false })
.png({
compressionLevel: Math.floor(((quality ?? 100) / 100) * 9),
progressive: true,
force: false,
})
.webp({ quality, force: false })
.avif({ quality, force: false })
.heif({ quality, force: false })
.tiff({ quality, force: false });
}
function sharpAddResizeSettings(
sharpInstance: Sharp,
size: ImageSize,
factor: number,
sourceFile: SourceFile
): Sharp {
const originalSize = !size.width && !size.height;
const { width, height } = originalSize
? { width: sourceFile.width, height: sourceFile.height }
: { width: size.width, height: size.height };
return sharpInstance.resize({
width: width ? width * factor : undefined,
height: height ? height * factor : undefined,
fit: size.fit,
// Position "center" cannot be set since it's the default (see: https://sharp.pixelplumbing.com/api-resize#resize).
position: size.position === "center" ? undefined : size.position,
withoutEnlargement: size.withoutEnlargement,
});
}
async function writeStreamToFile(sharpsStream: Sharp, path: string) {
return new Promise((resolve, reject) => {
const writeStream = createWriteStream(path);
// Reject promise if there is an error with the provided stream
sharpsStream.on("error", reject);
sharpsStream.pipe(writeStream);
writeStream.on("close", resolve);
writeStream.on("error", reject);
});
}
async function getMetadata(readStream: ReadStream): Promise<Metadata> {
return new Promise((resolve, reject) => {
const sharpInstance = sharp();
sharpInstance.metadata().then(resolve).catch(reject);
readStream.pipe(sharpInstance);
});
}
function getFileName(sourceFile: File, sizeName: string) {
const fileNameWithoutExtension = sourceFile.name.replace(/\.[^\/.]+$/, "");
return `${sizeName}_${fileNameWithoutExtension}`;
}
function getFileExtension(sourceFile: File, format: OutputFormat) {
return format === "original" ? sourceFile.ext : `.${format}`;
}
function getFileMimeType(sourceFile: File, format: OutputFormat) {
return format === "original" ? sourceFile.mime : `image/${format}`;
}
export default () => ({
...imageManipulation(),
generateResponsiveFormats: optimizeImage,
});