Created
October 10, 2019 12:15
-
-
Save nadimtuhin/c7f2bea3f08b7630ac9bddf8166835d6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const fs = require('fs'); | |
| const path = require('path'); | |
| const sharp = require('sharp'); | |
| const { promisify } = require('util'); | |
| const sizeOf = promisify(require('image-size')); | |
| const imageOptim = require('imageoptim'); | |
| /** | |
| * Find all files inside a dir, recursively. | |
| * @function getAllFiles | |
| * @param {string} dir Dir path string. | |
| * @return {string[]} Array with all file names that are inside the directory. | |
| */ | |
| const getAllFiles = dir => | |
| fs.readdirSync(dir).reduce((files, file) => { | |
| const name = path.join(dir, file); | |
| const isDirectory = fs.statSync(name).isDirectory(); | |
| return isDirectory ? [...files, ...getAllFiles(name)] : [...files, process.cwd()+"/"+name]; | |
| }, []); | |
| function updateExt(file, ext) { | |
| return file.split('.').slice(0, -1).join("") + "." + ext; | |
| } | |
| async function resizeImage(file, size, folder) { | |
| let width = size, height = size; | |
| const fileResized = file.replace('seller/original-backup/', `seller/${folder}/`); | |
| const fileWebp = updateExt(fileResized, 'webp'); | |
| fs.mkdirSync(fileResized.split("/").slice(0, -1).join("/"), { recursive: true }); | |
| await sharp(file) | |
| .resize({width, height, fit: sharp.fit.inside, withoutEnlargement: true }) | |
| .webp({quality: 85}) | |
| .toFile(fileWebp); | |
| console.log(fileWebp); | |
| if (fileResized.endsWith('jpg')) { | |
| await sharp(file) | |
| .resize({width, height, fit: sharp.fit.inside, withoutEnlargement: true }) | |
| .jpeg({ | |
| quality: 90, | |
| chromaSubsampling: '4:4:4', | |
| progressive: true, | |
| optimiseScans: true, | |
| overshootDeringing: true, | |
| trellisQuantisation: true | |
| }) | |
| .toFile(fileResized); | |
| console.log("jpeg 90"); | |
| } else if (fileResized.endsWith('png')) { | |
| await sharp(file) | |
| .resize({width, height, fit: sharp.fit.inside, withoutEnlargement: true }) | |
| .png({ | |
| quality: 80, | |
| adaptiveFiltering: true, | |
| progressive: true, | |
| dither: true | |
| }) | |
| .toFile(fileResized); | |
| console.log("png 85"); | |
| } else { | |
| await sharp(file) | |
| .resize({width, height, fit: sharp.fit.inside, withoutEnlargement: true }) | |
| .toFile(fileResized); | |
| console.log("optimzing file not jpeg"); | |
| } | |
| console.log(fileResized); | |
| } | |
| const files = getAllFiles('seller/original-backup') | |
| .filter(file => !file.endsWith('webp')) | |
| // files.forEach(async file => await resizeImage(file, 1200, 'original')); | |
| files.forEach(async file => await resizeImage(file, 200, 'thumbnail')); | |
| files.forEach(async file => await resizeImage(file, 400, '400X400')); | |
| files.forEach(async file => await resizeImage(file, 800, '800X800')); | |
| const optimFiles = [ | |
| ...getAllFiles('seller/original'), | |
| // ...getAllFiles('seller/thumbnail'), | |
| // ...getAllFiles('seller/400X400'), | |
| // ...getAllFiles('seller/800X800'), | |
| ]; | |
| // console.log(optimFiles); | |
| // imageOptim | |
| // .optim(optimFiles, { reporters: ['flat', 'html'] }) | |
| // .then(function (res) { | |
| // console.log(res); | |
| // }) | |
| // .done(); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment