node.js - Tile images with imagemagick with NodeJs -


with script can tile (4096x2048) images 32 tiles(512x512). give "x, y, z" cordinate each exported image.

for keep z 0 always.

example:

  • output-1-0-0.jpg
  • output-1-1-0.jpg

    var http = require("http"); var im = require("imagemagick");   var server = http.createserver(function(req, res) {    im.convert(['img.jpg','-crop','512x512','output.jpg'], function(err) {     if(err) { throw err; }     res.end("image crop complete");   });  }).listen(8080); 

and beside that, sorry dumb question im newby nodejs, instead of giving image name script, can call request crop , image service ?

thanks in advance

i don't think imagemagick can handle requests. can save image coming request local file, , call imagemagick crop image separate tiles.

a http request library node.js request.

it has pipe() method pipe response file stream:

http.createserver(function (req, resp) {   if (req.url === '/img.jpg') {     if (req.method === 'get' || req.method === 'head') {       var r= request.get('http://localhost:8008/img.jpg')       .on('error', function(err) {         console.log(err)       }).pipe(fs.createwritestream('doodle.png')) // pipe file     }   } }) 

by assigning response variable can check if pipe operation has been completed, if can call imagemagick method cropping operations.

streams event emitters can listen events, end event.

r.on('end', function() {     im.convert(['img.jpg','-crop','512x512','output.jpg'], function(err) {     if(err) { throw err; }     res.end("image crop complete");   }); }); 

here complete code (but not tested). guidance.

http.createserver(function (req, resp) {   if (req.url === '/img.jpg') {     if (req.method === 'get' || req.method === 'head') {       var r = request.get('http://localhost:8008/img.jpg')       .on('error', function(err) {         console.log(err)       }).pipe(fs.createwritestream('doodle.png')) // pipe file        r.on('end', function() {         im.convert(['img.jpg','-crop','512x512','output.jpg'], function(err) {             if(err) { throw err; }             res.end("image crop complete");           });         });     }   } }) 

Comments

Popular posts from this blog

python - pip install -U PySide error -

arrays - C++ error: a brace-enclosed initializer is not allowed here before ‘{’ token -

cytoscape.js - How to add nodes to Dagre layout with Cytoscape -