Skip to content

Instantly share code, notes, and snippets.

@Antoinebr
Last active May 21, 2018 13:11
Show Gist options
  • Save Antoinebr/0de2510a055da3ebfb99931ac6e4ed77 to your computer and use it in GitHub Desktop.
Save Antoinebr/0de2510a055da3ebfb99931ac6e4ed77 to your computer and use it in GitHub Desktop.
const glob = require("glob")
const util = require('util');
const exec = util.promisify(require('child_process').exec);
// Updte with your image path
const yourImagesPath = "/home/mobileui/app/wp-content/uploads/2018/01/*.jpg";
glob(yourImagesPath, {}, function (er, files) {
if( er ) throw new Error( er );
if( files.length === 0) throw new Error( 'No Files found' );
files.forEach( path => {
// Be sure that "cjpeg" is in your path
exec(`cjpeg -quality 60 -outfile ${path}.min.jpg -optimise ${path}`)
.then( res => console.log(`Compression OK : ${path}`))
.then( res => exec(`mv ${path}.min.jpg ${path}`))
.then( res => console.log(`Renaming OK : ${path}`))
.catch( stderr => console.log('Error ', stderr))
});
})
@Antoinebr
Copy link
Author

You have to install glob

npm install glob

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment