Permalink
Browse files

Let there be codez

  • Loading branch information...
0 parents commit e070228069c3346b1781974167ebc2a87ca6d5ec @honza committed Dec 30, 2011
Showing with 206 additions and 0 deletions.
  1. +1 −0 .gitignore
  2. +22 −0 LICENSE
  3. +27 −0 README.md
  4. +16 −0 bin/thumb
  5. +27 −0 package.json
  6. +113 −0 src/thumbnail.js
@@ -0,0 +1 @@
+node_modules
22 LICENSE
@@ -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.
@@ -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
@@ -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);
@@ -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"
+ }
+}
@@ -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.