Advertisement
Guest User

Untitled

a guest
May 25th, 2015
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.32 KB | None | 0 0
  1. // modules
  2. var lwip = require('lwip'),
  3. path = require('path'),
  4. fs = require('fs'),
  5. rstream = require('stream').Readable;
  6.  
  7. // config
  8. var placesPath = '/Users/apx/Downloads/places';
  9.  
  10. // helper
  11. var isHiddenPath = function (fn) {
  12. return /(^|.\/)\.+[^\/\.]/g.test(fn);
  13. };
  14.  
  15. var resizePhotos = function () {
  16. var stream = new rstream,
  17. files = [];
  18.  
  19. // actual resize procedure
  20. var _resize = function () {
  21. var i = 0, n = files.length;
  22.  
  23. var _next = function () {
  24. ++i === n && stream.emit('end');
  25. }
  26.  
  27. files.forEach(function (fn) {
  28. process.nextTick(function () {
  29. var image_base = fn[0];
  30.  
  31. fs.readFile(image_base, function (err, buf) {
  32. // bubble up error if one is existent
  33. if (err) return stream.emit('error', 'Couldn\'t open image for resize: ' + image_base, err), _next();
  34.  
  35. // instanciate lwip per image
  36. lwip.open(buf, fn[0].split('.').pop() === 'png' ? 'png' : 'jpg', function(err, image) {
  37. // bubble up error if one is existent
  38. if (err) return stream.emit('error', '[lwip] Unspecific error: ' + image_base, err), _next();
  39.  
  40. image
  41. // make this call chainable,
  42. // so that it looks nicely
  43. .batch()
  44. // resize 400x400 where local center is global center
  45. .crop(400, 400)
  46. // export as buffer
  47. .toBuffer('jpg', function(err, buffer) {
  48. // bubble up error if one is existent
  49. if (err) return stream.emit('error', 'Couldn\'t export buffer: ' + image_base, err), _next();
  50.  
  51. // remove extension, add jpg
  52. var out_path = fn[1].split(".").slice(0, -1) + '.jpg';
  53.  
  54. // queue write
  55. fs.writeFile(out_path, buffer, function (err) {
  56. // emit resize event
  57. stream.emit('resize', fn[0], out_path);
  58.  
  59. _next();
  60. })
  61. })
  62. });
  63. });
  64. })
  65. })
  66. };
  67.  
  68. // aggregate files
  69. process.nextTick(function () {
  70. fs.readdir(placesPath, function(err, folders) {
  71. // remove all hidden paths
  72. folders = folders.filter(function (fn) {
  73. return !isHiddenPath(fn);
  74. });
  75.  
  76. var i = 0, n = folders.length;
  77.  
  78. var _next = function () {
  79. ++i === n && _resize();
  80. }
  81.  
  82. // iterate over folders
  83. folders.forEach(function(folder) {
  84. folder = path.join(placesPath, folder);
  85.  
  86. fs.readdir(folder, function(err, images) {
  87. // bubble up error if one is existent
  88. if (err) return stream.emit('error', 'Couldn\'t read folder: ' + folder), _next();
  89.  
  90. // remove all hidden files
  91. images = images
  92. .filter(function (fn) {
  93. // ignore hidden files and thumb_* files
  94. return !isHiddenPath(fn) && fn.indexOf('thumb_');
  95. })
  96. .map(function (fn) {
  97. return [
  98. folder + '/' + fn,
  99. folder + '/thumb_' + fn
  100. ]
  101. });
  102.  
  103. // append results to files array
  104. files.push.apply(files, images);
  105.  
  106. _next();
  107. });
  108. });
  109. });
  110. });
  111.  
  112. return stream;
  113. }
  114.  
  115. /*
  116. exports.setThumbs = function(req, res) {
  117. resizePhotos()
  118. // errors
  119. .on('error', function (msg) {
  120.  
  121. })
  122. // resizes
  123. .on('resize', function (fn) {
  124. res.write('Resized "' + fn + "\n");
  125. })
  126. .on('end', function () {
  127. res.end("Ok, resized all of them!");
  128. });
  129. }
  130. */
  131.  
  132. resizePhotos()
  133. // errors
  134. .on('error', function (msg, err) {
  135. console.log(msg, err);
  136. })
  137. // resizes
  138. .on('resize', function (fn, thumb) {
  139. console.log('Resized "' + fn + '" -> "' + thumb + '"');
  140. })
  141. .on('end', function () {
  142. console.log("Ok, resized all of them!");
  143. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement