3

This is basic imagemagick setup for node.js that i came across lately. I understand that question i'm asking is very newbie but i'm new to node js and imagemagick and i wanted to try to make it dynamic. Current script bellow is cropping one specific file, is there a way to make it to apply conversion to all the files from directory /before and then output it to directory /after ? Also the operation i want to do for all the images in directory is to apply watermark on each (crop operation in code is just from example).

var http = require("http"); var im = require("imagemagick"); var args = [ "image.png", "-crop", "120x80+30+15", "output.png" ]; var server = http.createServer(function(req, res) { im.convert(args, function(err) { if(err) { throw err; } res.end("Image crop complete"); }); }).listen(8080); 

1 Answer 1

2

Yes, you can do it by implementing foreach by all files itself. Also you will need to install async module:

npm install async 

Example code:

var async = require('async'), fs = require('fs'), im = require('imagemagick'), maxworkers = require('os').cpus().length, path = require('path'); module.exports = resize; function resize(params) { var queue = async.queue(resizeimg, maxworkers); fs.readdir(params.src, function(err, files) { files.forEach(function(file) { queue.push({ src: path.join(params.src, '/', file), dest: path.join(params.dest, '/', file), width: params.width, height: params.height }) }); }); } function resizeimg(params, cb) { var imoptions = { srcPath: params.src, dstPath: params.dest }; if (params.width !== undefined) imoptions.width = params.width; if (params.height !== undefined) imoptions.height = params.height im.resize(imoptions, cb); } 

Then in you code:

var http = require("http"); var resize = require("./resize"); var server = http.createServer(function(req, res) { resize({ src: '/source/folder', dest: '/destination/folder', width: 300 }); }).listen(8080); 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.