39 lines
1.0 KiB
JavaScript
39 lines
1.0 KiB
JavaScript
const {Jimp} = require('jimp');
|
|
const fs = require("fs");
|
|
const path = require("path");
|
|
require.resolve('node_modules/@jimp');
|
|
|
|
async function compressImages(inputPath) {
|
|
const images = fs.readdirSync(inputPath).filter(f => /\.(jpe?g|png)$/i.test(f));
|
|
const bufferMap = {};
|
|
|
|
await Promise.all(images.map(async (image) => {
|
|
const inputImagePath = path.join(inputPath, image);
|
|
const img = await Jimp.read(inputImagePath);
|
|
img.resize(700, Jimp.AUTO).quality(70); // Resize and compress
|
|
|
|
const buffer = await img.getBufferAsync(Jimp.MIME_JPEG);
|
|
bufferMap[image] = buffer;
|
|
}));
|
|
|
|
return bufferMap;
|
|
}
|
|
|
|
async function bufferImages(inputPath) {
|
|
const images = fs.readdirSync(inputPath).filter(f => /\.(jpe?g|png)$/i.test(f));
|
|
const bufferMap = {};
|
|
|
|
await Promise.all(images.map(async (image) => {
|
|
const inputImagePath = path.join(inputPath, image);
|
|
const buffer = await fs.promises.readFile(inputImagePath);
|
|
bufferMap[image] = buffer;
|
|
}));
|
|
|
|
return bufferMap;
|
|
}
|
|
|
|
module.exports = {
|
|
compressImages,
|
|
bufferImages,
|
|
};
|