Skip to content

Commit

Permalink
Let there be codez
Browse files Browse the repository at this point in the history
  • Loading branch information
honza committed Dec 30, 2011
0 parents commit e070228
Show file tree
Hide file tree
Showing 6 changed files with 206 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
22 changes: 22 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
Copyright (c) 2011, Honza Pokorny
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL HONZA POKORNY BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
node-thumbnail
==============

thumbnail all the things

usage:

thumb [options] source/path dest/path

options:

-h, --help
-v, --version

-s SUFFIX, --suffix SUFFIX
-d, --digest
-t TYPE, --hashing-type TYPE

-w, --width

-c NUM, --concurency NUM

installation
------------

$ brew install imagemagick
$ npm install node-thumbnail
16 changes: 16 additions & 0 deletions bin/thumb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/usr/bin/env node

var program = require('commander');

program
.version('0.0.1')
.usage('[options] source/dir dest/dir')
.option('-s, --suffix [suffix]', 'Add [suffix] to thumb filename', '_thumb')
.option('-d, --digest', 'Use digest for thumb filename')
.option('-t, --hashing-type [type]', 'Hashing type sha1, md5. Must be used with -d')
.option('-w, --width [width]', 'Thumbnail width in pixels. Default: 800', 800)
.option('-c, --concurency [num]', 'Number of workers', 2)
.parse(process.argv);

var main = require('../src/thumbnail.js');
main(program);
27 changes: 27 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"name": "node-thumbnail",
"description": "thumbnail all the things",
"keywords": ["thumbnail", "images"],
"author": "Honza Pokorny",
"version": "0.0.1",
"licenses": [{
"type": "BSD"
}],
"engines": {
"node": ">=0.4.0"
},
"bin": {
"thumb": "./bin/thumb"
},
"homepage": "https://github.com/honza/node-thumbnail",
"repository": {
"type": "git",
"url": "git://github.com/honza/node-thumbnail.git"
},
"dependencies": {
"async": ">= 0.1.15",
"imagemagick": ">= 0.1.2",
"commander": ">= 0.5.1",
"underscore": "latest"
}
}
113 changes: 113 additions & 0 deletions src/thumbnail.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
// node-thumbnail
// (c) 2011 Honza Pokorny
// Licensed under BSD
// https://github.com/honza/node-thumbnail

var fs = require('fs');
var path = require('path');
var crypto = require('crypto');

var im = require('imagemagick');
var async = require('async');
var _ = require('underscore');

var options, settings, queue;

var extensions = [
'.jpg',
'.jpeg',
'.JPG',
'.JPEG',
'.png',
'.PNG'
];


var createQueue = function() {

queue = async.queue(function (task, callback) {

if (settings.digest) {

var hash = crypto.createHash(settings.hashingType);
var stream = fs.ReadStream(task.options.srcPath);

stream.on('data', function(d) {
hash.update(d);
});

stream.on('end', function() {
var d = hash.digest('hex');

task.options.dstPath = settings.destination + '/' + d + '_' +
settings.width + path.extname(task.options.srcPath);

im.resize(task.options, function(err, stdout, stderr) {
callback();
});

});

} else {
var name = task.options.srcPath;
var ext = path.extname(name);
var base = path.basename(name, ext);

task.options.dstPath = settings.destination + '/' + base +
settings.suffix + ext;

im.resize(task.options, function(err, stdout, stderr) {
callback();
});
}

}, settings.concurency);

queue.drain = function() {
console.log('all items have been processed');
};
};


var run = function() {
var images = fs.readdirSync(settings.source);
images = _.reject(images, function(file) {
return _.indexOf(extensions, path.extname(file)) === -1;
});

createQueue();

_.each(images, function(image) {

options = {
srcPath: settings.source + '/' + image,
width: settings.width
};

queue.push({options: options}, function() {
console.log(image);
});

});
};


module.exports = function(options) {
if (options.args.length != 2) {
console.log('Please provide a source and destination directories.');
return;
}

if (path.existsSync(options.args[0]) && path.existsSync(options.args[1])) {
options.source = options.args[0];
options.destination = options.args[1];
} else {
console.log('Source or destination doesn\'t exist.');
return;
}

settings = options;

run();

};

0 comments on commit e070228

Please sign in to comment.