Guest User

Untitled

a guest
Jun 19th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.15 KB | None | 0 0
  1.  
  2. /**
  3. * Module dependencies.
  4. */
  5.  
  6. var exec = require('child_process').exec;
  7.  
  8. var images = [
  9. 'bowl.jpeg'
  10. , 'case.jpeg'
  11. , 'guitar.jpg'
  12. ];
  13.  
  14. function flags(options) {
  15. var keys = Object.keys(options);
  16. return keys.map(function(key){
  17. var val = options[key];
  18. return true === val
  19. ? '-' + key
  20. : '-' + key + ' ' + val;
  21. }).join(' ');
  22. }
  23.  
  24. function convert(images, options, fn) {
  25. var ext = options.ext || '.png'
  26. , pending = images.length
  27. , done;
  28. delete options.ext;
  29. options = flags(options);
  30. images = images.map(function(image){
  31. var dest = image.replace(/\.(jpe?g|png|gif)$/, ext)
  32. , cmd = [
  33. 'convert'
  34. , options
  35. , image
  36. , dest
  37. ].join(' ');
  38. exec(cmd, function(err, stdout, stderr){
  39. if (done) return;
  40. if (err) return done = true, fn(err);
  41. --pending || fn(null, images);
  42. });
  43. return dest;
  44. });
  45. }
  46.  
  47. convert(images, { resize: '251x171', trim: true }, function(err, images){
  48. if (err) throw err;
  49. console.log(images);
  50. convert(images, { resize: '63x40', trim: true, ext: '.thumb.png' }, function(err, images){
  51. if (err) throw err;
  52. console.log(images);
  53. });
  54. });
Add Comment
Please, Sign In to add comment