-
Notifications
You must be signed in to change notification settings - Fork 0
/
images.js
414 lines (352 loc) · 12.5 KB
/
images.js
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
import fs from "fs";
import mkdirp from "mkdirp";
import { exec } from "child_process";
import sizeOf from "image-size";
import chalk from "chalk";
const overwrite = false; // Set this “true” to re-generate existing image files.
const forceOverwriteFiles = {
// "on-the-go-la": 1,
// "sloane-stephens-foundation": 1,
// "lynsteb-inc": 1,
// "ruckusroots": 1,,
// "crayon-collection": 1,
// "our-house-grief-support-center": 1,
// "sola-i-can-foundation": 1,
// "growing-the-table-a-project-of-tomkat-ranch-educational-foundation": 1,
// "integrated-recovery-network": 1,
// "western-center-on-law-poverty": 1,
// "rivet-school": 1,
// "the-seed-school-of-los-angeles-county": 1,
// "alliance-in-mentorship-mimentor": 1,
};
const startTime = Date.now();
const configData = JSON.parse(fs.readFileSync("./config.json", "utf8"));
const SIZES = configData.sizes;
const ASPECT_RATIO = '10/8';
const cavif = "./lib/cavif-0.6.4/mac/cavif";
const imageMagick = "magick";
// SHIM: Use the installed squoosh-cli
const squoosh = `node node_modules/.bin/squoosh-cli`;
// const squoosh = `npx @squoosh/cli@^0.6.0`;
/*
// https://developers.google.com/speed/webp/docs/api
const webp = {
quality: 75, // between 0 and 100. For lossy, 0 gives the smallest
// size and 100 the largest. For lossless, this
// parameter is the amount of effort put into the
// compression: 0 is the fastest but gives larger
// files compared to the slowest, but best, 100.
target_size: 0,
target_PSNR: 0,
method: 4, // quality/speed trade-off (0=fast, 6=slower-better).
sns_strength: 50,
filter_strength: 60,
filter_sharpness: 0,
filter_type: 1,
partitions: 0,
segments: 4,
pass: 1,
show_compressed: 0,
preprocessing: 0,
autofilter: 0,
partition_limit: 0,
alpha_compression: 1,
alpha_filtering: 1,
alpha_quality: 100,
lossless: 0, // Lossless encoding (0=lossy(default), 1=lossless).
exact: 0,
image_hint: 0,
emulate_jpeg_size: 0,
thread_level: 0,
low_memory: 0,
near_lossless: 100,
use_delta_palette: 0,
use_sharp_yuv: 0,
};
// https://research.mozilla.org/av1-media-codecs/
// https://jakearchibald.com/2020/avif-has-landed/
// https://netflixtechblog.com/avif-for-next-generation-image-coding-b1d75675fe4
const avif = {
minQuantizer: 0,
maxQuantizer: 33,
minQuantizerAlpha: 0,
maxQuantizerAlpha: 63,
tileColsLog2: 0,
tileRowsLog2: 0,
speed: 1, // 8 ==> fastest
subsample: 1,
};
const rotate = {
numRotations: 0, // degrees = (numRotations * 90) % 360
}
const quant = {
numColors: 255,
dither: 1.0,
}
*/
function doCommand(command) {
return new Promise((resolve, reject) => {
// https://stackoverflow.com/questions/20643470/execute-a-command-line-binary-with-node-js#answer-20643568
console.log(command);
exec(`${command} && echo 'finished task'`, (err, stdout, stderr) => {
if (err) {
// node couldn't execute the command
console.error(err);
reject(err);
return;
}
// the *entire* stdout and stderr (buffered)
if (stdout) {
console.log(stdout);
resolve();
}
if (stderr) {
console.log(stderr);
reject(stderr);
}
});
});
}
function stringify(options) {
// Remove quotes
return `'${JSON.stringify(options).replace(/\"/g, ``)}'`;
}
// https://web.dev/squoosh-v2/
// https://github.com/GoogleChromeLabs/squoosh/tree/dev/cli
// https://github.com/GoogleChromeLabs/squoosh/blob/dev/cli/src/codecs.js
function generateOneImage({ width, aspectRatio, sourceFile, destinationFolder }) {
return new Promise(async (resolve, reject) => {
const pathBits = sourceFile.split("/");
const fileName = pathBits[pathBits.length - 1];
const fileNameBits = fileName.split(".");
const fileNameBase = fileNameBits.slice(0, fileNameBits.length - 1).join(".");
console.log({fileNameBase});
const fileExtension = fileNameBits[fileNameBits.length - 1];
const sourceFileSize = sizeOf(sourceFile); // https://github.com/image-size/image-size
console.log("");
console.log(chalk.cyan("- - - - - - - - - - - - - - - - - - - - - - -"));
console.log("⏱️ ", chalk.cyan(`generateOneImage: sourceFileSize: `), { sourceFileSize });
console.log(chalk.cyan("- - - - - - - - - - - - - - - - - - - - - - -"));
console.log("");
console.log(`Time elapsed:\n${Date.now() - startTime} milliseconds\n`);
console.log("");
const mozjpeg = {
quality: 65,
baseline: false,
arithmetic: false,
progressive: true,
optimize_coding: true,
smoothing: 0,
color_space: 3 /*YCbCr*/,
quant_table: 3,
trellis_multipass: false,
trellis_opt_zero: false,
trellis_opt_table: false,
trellis_loops: 1,
auto_subsample: true,
chroma_subsample: 2,
separate_chroma_quality: false,
chroma_quality: 75,
};
const oxipng = {
level: 1
};
const resize = {
width: Math.min(width, sourceFileSize.width), // SHIM: Avoid scaling the image up
// height,
}
if (aspectRatio) {
const ratioWidth = Number(aspectRatio.split('/')[0]);
const ratioHeight = Number(aspectRatio.split('/')[1]);
const ratio = ratioWidth / ratioHeight;
resize.height = Math.round(resize.width / ratio);
}
const squooshOptions = [
`-d ${destinationFolder}`,
];
if (resize.width != sourceFileSize.width) {
squooshOptions.push(
`--resize ${stringify(resize)}`,
);
}
const cavifOptions = [
`--quality=${50}`,
`--speed=1`, // Encoding speed between 1 (best, but slowest) and 10 (fastest)
`--overwrite`,
// `--color=rgb`,
`-o '${destinationFolder}/${fileNameBase}.avif'`,
];
// https://imagemagick.org/script/defines.php
const imageMagickOptions = [
`'${sourceFile}'`,
// Give transparent PNGs a white background
`-fill white`,
`-opaque none`,
];
if (resize.width != sourceFileSize.width) {
const infinity = 9999;
if (aspectRatio) {
// https://legacy.imagemagick.org/discourse-server/viewtopic.php?p=71482#p71482
imageMagickOptions.push(
`-resize ${resize.width}x${resize.height}^`
);
imageMagickOptions.push(
`-gravity Center`
);
imageMagickOptions.push(
`-crop ${resize.width}x${resize.height}+0+0`
);
} else {
imageMagickOptions.push(
`-resize ${resize.width}x${infinity}`
);
}
}
// JPEG
if (fs.existsSync(`${destinationFolder}/${fileNameBase}.jpg`) && overwrite !== true && !forceOverwriteFiles[fileNameBase]) {
console.log(`${destinationFolder}/${fileNameBase}.jpg already exists. Skipping…`);
} else {
console.log(``);
console.log(`🖼 Generating JPEG`);
// await doCommand(`${squoosh} ${ [`--mozjpeg ${stringify(mozjpeg)}`,...squooshOptions].join(" ") } ${sourceFile}`);
await doCommand(`${imageMagick} ${ [
...imageMagickOptions,
`-quality ${80}`,
`'${destinationFolder}/${fileNameBase}.jpg'`,
].join(" ") }`);
}
if (width > 16) { // Skip WebP and AVIF for the smallest size (16px), since it’s only used for preview images
// WebP
if (fs.existsSync(`${destinationFolder}/${fileNameBase}.webp`) && overwrite !== true && !forceOverwriteFiles[fileNameBase]) {
console.log(`${destinationFolder}/${fileNameBase}.webp already exists. Skipping…`);
} else {
if (configData.imageFormats && configData.imageFormats.webp) {
console.log(``);
console.log(`🖼 Generating WebP`);
// SHIM: Use ImageMagick for WebP images, since it’s tricky to produce
// WebP images with Squoosh that are similar to JPEG in quality & file size
await doCommand(`${imageMagick} ${ [
...imageMagickOptions,
`-quality ${50}`,
`'${destinationFolder}/${fileNameBase}.webp'`,
].join(" ") }`);
}
}
if (fs.existsSync(`${destinationFolder}/${fileNameBase}.avif`) && overwrite !== true && !forceOverwriteFiles[fileNameBase]) {
console.log(`${destinationFolder}/${fileNameBase}.avif already exists. Skipping…`);
} else {
if (configData.imageFormats && configData.imageFormats.avif) {
// AVIF
console.log(``);
console.log(`🖼 Generating AVIF`);
// SHIM: Use Cavif for the AVIF images, since
// Squoosh seems to have an upper limit of 4500 pixels
// https://github.com/kornelski/cavif-rs
if (resize.width != sourceFileSize.width) {
// SHIM: Use a temporary TIFF file as the source image, since cavif doesn’t have a resize feature
await doCommand(`${imageMagick} ${ [
...imageMagickOptions,
`'${destinationFolder}/${fileNameBase}.tiff'`,
].join(" ") }`);
await doCommand(`${cavif} ${ cavifOptions.join(" ") } '${destinationFolder}/${fileNameBase}.tiff'`);
// Delete temporary TIFF
fs.unlinkSync(`${destinationFolder}/${fileNameBase}.tiff`);
} else {
await doCommand(`${cavif} ${ cavifOptions.join(" ") } '${sourceFile}'`);
}
}
}
}
resolve();
});
}
async function generateImages({ width, imagePath }) {
console.log("");
console.log(chalk.cyan("- - - - - - - - - - - - - - - - - - - - - - -"));
console.log("⏱️ ", chalk.cyan('generateImages: ' + width + ' :: ' + imagePath));
console.log(chalk.cyan("- - - - - - - - - - - - - - - - - - - - - - -"));
console.log("");
console.log(`Time elapsed:\n${Date.now() - startTime} milliseconds\n`);
console.log("");
const sourceFolder = `${imagePath}/original`;
const destinationFolder = `${imagePath}/${width}-wide`;
const destinationFolderAspectRatio = `${destinationFolder}-with-aspect-${String(ASPECT_RATIO).replace('/', '-')}`
const files = getAllFilesFromFolder(sourceFolder);
try {
await mkdirp(destinationFolder);
await mkdirp(destinationFolderAspectRatio);
for (let index = 0; index < files.length; index++) {
const sourceFile = files[index];
const pathBits = sourceFile.split("/");
const fileName = pathBits[pathBits.length - 1];
console.log(chalk.cyan(`${nextFolderCursor} of ${configData.albums.length} albums`));
console.log(chalk.cyan(`${nextCursor} of ${SIZES.length} sizes`));
console.log(chalk.cyan(`${index + 1} of ${files.length} pictures`));
await generateOneImage({
width,
sourceFile,
destinationFolder,
});
await generateOneImage({
width,
aspectRatio: ASPECT_RATIO,
sourceFile,
destinationFolder: destinationFolderAspectRatio,
});
}
} catch(e) {
console.error(e);
}
generateNextSize();
}
let nextCursor = 0;
let nextImagePath;
function generateNextSize() {
if (nextCursor < SIZES.length) {
console.log('generateNext: ' + nextCursor + ' :: ' + SIZES[nextCursor] + ' :: ' + nextImagePath);
generateImages({
width: SIZES[nextCursor],
imagePath: nextImagePath
});
nextCursor++;
} else {
generateNextFolder();
}
}
let nextFolderCursor = 0;
function generateNextFolder() {
if (nextFolderCursor < configData.albums.length) {
console.log('generateNextFolder: ' + nextFolderCursor + ' :: ' + configData.albums[nextFolderCursor]);
nextCursor = 0;
nextImagePath = `./${ configData.albums[nextFolderCursor] }`;
generateNextSize();
nextFolderCursor++;
} else {
console.log("");
console.log(chalk.cyan("- - - - - - - - - - - - - - - - - - - - - - -"));
console.log("🏁", chalk.cyan(`Finished creating images`));
console.log(chalk.cyan("- - - - - - - - - - - - - - - - - - - - - - -"));
console.log("");
console.log(`Total time elapsed: ${Date.now() - startTime} milliseconds`);
console.log("");
}
}
// https://stackoverflow.com/questions/20822273/best-way-to-get-folder-and-file-list-in-javascript#21459809
function getAllFilesFromFolder(dir) {
let results = []
fs.readdirSync(dir).forEach(function(file) {
file = dir+'/'+file
let stat = fs.statSync(file)
if (stat && stat.isDirectory()) {
results = results.concat(getAllFilesFromFolder(file))
} else {
if (!file.includes('DS_Store')) {
results.push(file);
}
}
})
return results
}
// Generate images
nextFolderCursor = 0;
generateNextFolder();