-
Notifications
You must be signed in to change notification settings - Fork 0
/
img-opt-multi-route.ts
138 lines (117 loc) · 3.71 KB
/
img-opt-multi-route.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
import { Image } from "https://deno.land/x/[email protected]/mod.ts";
import { join } from "https://deno.land/[email protected]/path/mod.ts";
import { expandGlob } from "https://deno.land/[email protected]/fs/mod.ts";
import { Img } from "./types.ts";
const imgQuality = 80;
const thumbWidth = 600;
const inputDir = join(Deno.cwd(), "static", "gallery");
const outputDirFull = join(Deno.cwd(), "static", "gallery");
const outputDir = join(Deno.cwd(), "static", "gallery-opt");
const inputDirs = [];
for await (const file of expandGlob("{routes}/**/gallery")) {
if (file.isDirectory) {
inputDirs.push(file.path);
}
}
console.log(inputDirs);
/**
* Optimizes the images in the inputDir directory and saves them to the outputDir directory
* writes the optimized images metadata to a images.ts file in the outputDir directory
*/
async function optimizeImages(galleryDir: string) {
const galleryDirOpt = galleryDir + "-opt";
// const galleryDirOpt = '/static/gallery-opt';
const staticGalleryDir = "/static/gallery/";
const staticGalleryDirOptimized = "/static/gallery-opt/";
const files = [];
try {
Deno.removeSync(galleryDirOpt, { recursive: true });
Deno.mkdirSync(galleryDirOpt);
} catch {
Deno.mkdirSync(galleryDirOpt);
}
/**
* Remove and recreate the /static/gallery directory
*/
// join(Deno.cwd(), "static", "gallery-opt");
// try {
// Deno.removeSync(staticGalleryDirOptimized, { recursive: true });
// Deno.mkdirSync(staticGalleryDirOptimized);
// } catch {
// Deno.mkdirSync(staticGalleryDirOptimized);
// }
for await (const dirEntry of Deno.readDir(galleryDir)) {
if (dirEntry.isFile) {
files.push(dirEntry.name);
}
}
const images = files.filter((file) =>
/\.(jpg|jpeg|png|webp|gif)$/i.test(file)
);
const imagesMetaDataArr: Img[] = [];
for (const img of images) {
const inputBuffer = await Deno.readFile(`${galleryDir}/${img}`);
const image = await Image.decode(inputBuffer);
const outputImg = await image.encodeJPEG(70);
await Deno.writeFile(`${outputDirFull}/${img}`, outputImg);
const imgData = await optimizeImgForWeb(
`${galleryDir}/${img}`,
`${outputDir}/${img}`,
70,
img,
);
imagesMetaDataArr.push(imgData);
}
const content = `export const images = ${JSON.stringify(imagesMetaDataArr)}`;
await Deno.writeTextFile(galleryDirOpt + "/images.ts", content);
}
/**
* Optimizes an image for the web in jpg format
* @param {*} inputPath
* @param {*} outputPath
* @param {*} quality
* @param {*} fileName
* @returns JSON object with image metadata
*/
async function optimizeImgForWeb(
inputPath: string,
outputPath: string,
quality = imgQuality,
fileName: string,
): Promise<Img> {
// Read the input file
const inputBuffer = await Deno.readFile(inputPath);
// Decode the image
const image = await Image.decode(inputBuffer);
const widthFull = image.width;
const heightFull = image.height;
image.resize(thumbWidth, Image.RESIZE_AUTO);
const outputImg = await image.encodeJPEG(quality);
await Deno.writeFile(outputPath, outputImg);
const obj = {
name: fileName,
widthFull,
heightFull,
width: image.width,
height: image.height,
};
return obj;
// } catch (error) {
// console.error(`Error converting ${inputPath} to Optimized file:`, error);
// }
}
if (import.meta.main) {
console.log(`Optimizing images please wait...`);
const t0 = performance.now();
for (let dir of inputDirs) {
await optimizeImages(dir);
}
// await optimizeImages();
const t1 = performance.now();
// console.log(
// `Optimized ${imagesObjsArr.length} images in ${t1 - t0} milliseconds.`,
// );
console.log(
`Optimized images in ${t1 - t0} milliseconds.`,
);
}