I have a server and it gets a lot of image uploads. The app uploading the images is based in PHP. However, I want to use Node to watch the upload folder, then run imagemin on any new images.
I was thinking I would use npm packages 'watch' and 'imagemin'. I am not sure how to configure them exactly though or how to have it run all the time.
So far I have the following which I can turn on manually:
var Imagemin = require('imagemin');
var watch = require('watch');
var imagemin = new Imagemin()
.use(Imagemin.pngquant());
watch.createMonitor('images', function (monitor) {
monitor.files['images/*.png']
monitor.on("created", function (file) {
compressImage(file);
})
monitor.on("changed", function (file) {
compressImage(file);
});
});
function compressImage(file) {
var dest, path;
path = f.split('/');
path.pop();
dest = path.join('/');
imagemin.src(file).dest(dest).run();
}
After a couple of files are added though I get a warning about a possible EventEmitter memory leak detected. 11 listeners added. Use emitter.setMaxListeners() to increase limit
and then it stops working.
Is there a better way to do this and how can I run it automatically?