Optimize Image Size With JavaScript in Node.js code example
Example: Optimize Image Size With JavaScript in Node.js
/*
This code comes from Vincent Lab
And it has a video version linked here: https://www.youtube.com/watch?v=bmSOapQ5I78
*/
// Import dependencies
const imagemin = require("imagemin");
const imageminJpegtran = require("imagemin-jpegtran");
const imageminPngquant = require("imagemin-pngquant");
(async () => {
// Optimize the image size
const files = await imagemin(["original-images/*.{jpg,png}"], {
destination: "optimised-images",
plugins: [
imageminJpegtran(),
imageminPngquant()
]
});
// Show amount of images optimize
console.log(`Optimized ${files.length} images`);
})();