Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // modules
- var lwip = require('lwip'),
- path = require('path'),
- fs = require('fs'),
- rstream = require('stream').Readable;
- // config
- var placesPath = '/Users/apx/Downloads/places';
- // helper
- var isHiddenPath = function (fn) {
- return /(^|.\/)\.+[^\/\.]/g.test(fn);
- };
- var resizePhotos = function () {
- var stream = new rstream,
- files = [];
- // actual resize procedure
- var _resize = function () {
- var i = 0, n = files.length;
- var _next = function () {
- ++i === n && stream.emit('end');
- }
- files.forEach(function (fn) {
- process.nextTick(function () {
- var image_base = fn[0];
- fs.readFile(image_base, function (err, buf) {
- // bubble up error if one is existent
- if (err) return stream.emit('error', 'Couldn\'t open image for resize: ' + image_base, err), _next();
- // instanciate lwip per image
- lwip.open(buf, fn[0].split('.').pop() === 'png' ? 'png' : 'jpg', function(err, image) {
- // bubble up error if one is existent
- if (err) return stream.emit('error', '[lwip] Unspecific error: ' + image_base, err), _next();
- image
- // make this call chainable,
- // so that it looks nicely
- .batch()
- // resize 400x400 where local center is global center
- .crop(400, 400)
- // export as buffer
- .toBuffer('jpg', function(err, buffer) {
- // bubble up error if one is existent
- if (err) return stream.emit('error', 'Couldn\'t export buffer: ' + image_base, err), _next();
- // remove extension, add jpg
- var out_path = fn[1].split(".").slice(0, -1) + '.jpg';
- // queue write
- fs.writeFile(out_path, buffer, function (err) {
- // emit resize event
- stream.emit('resize', fn[0], out_path);
- _next();
- })
- })
- });
- });
- })
- })
- };
- // aggregate files
- process.nextTick(function () {
- fs.readdir(placesPath, function(err, folders) {
- // remove all hidden paths
- folders = folders.filter(function (fn) {
- return !isHiddenPath(fn);
- });
- var i = 0, n = folders.length;
- var _next = function () {
- ++i === n && _resize();
- }
- // iterate over folders
- folders.forEach(function(folder) {
- folder = path.join(placesPath, folder);
- fs.readdir(folder, function(err, images) {
- // bubble up error if one is existent
- if (err) return stream.emit('error', 'Couldn\'t read folder: ' + folder), _next();
- // remove all hidden files
- images = images
- .filter(function (fn) {
- // ignore hidden files and thumb_* files
- return !isHiddenPath(fn) && fn.indexOf('thumb_');
- })
- .map(function (fn) {
- return [
- folder + '/' + fn,
- folder + '/thumb_' + fn
- ]
- });
- // append results to files array
- files.push.apply(files, images);
- _next();
- });
- });
- });
- });
- return stream;
- }
- /*
- exports.setThumbs = function(req, res) {
- resizePhotos()
- // errors
- .on('error', function (msg) {
- })
- // resizes
- .on('resize', function (fn) {
- res.write('Resized "' + fn + "\n");
- })
- .on('end', function () {
- res.end("Ok, resized all of them!");
- });
- }
- */
- resizePhotos()
- // errors
- .on('error', function (msg, err) {
- console.log(msg, err);
- })
- // resizes
- .on('resize', function (fn, thumb) {
- console.log('Resized "' + fn + '" -> "' + thumb + '"');
- })
- .on('end', function () {
- console.log("Ok, resized all of them!");
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement