Skip to content

Commit

Permalink
First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
hacksparrow committed Feb 28, 2012
0 parents commit 56a21bf
Show file tree
Hide file tree
Showing 5 changed files with 166 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
@@ -0,0 +1,2 @@
sample-images/
NOTES
3 changes: 3 additions & 0 deletions README.md
@@ -0,0 +1,3 @@

See test.js for usage examples.
Does not work under Windows as of now.
99 changes: 99 additions & 0 deletions easyimage.js
@@ -0,0 +1,99 @@
var exec = require('child_process').exec;
var child;
var imcmd; // short for ImageMagick Command, how ingenious (y)

// treasure of error messages
var error_messages = {
'path': 'MIssing image paths.\nMake sure both source and destination files are specified.',
'dim': 'Missing dimensions.\nSpecify the width atleast.',
'restricted': 'The command you are trying to execute is prohibited.'
};

// function to throw errors at unsuspecting and potentially innocent developers
var throw_err = function(type) { throw(error_messages[type]); };

// get basic information about an image file
exports.info = function(file, callback) {
imcmd = 'identify ' + file;
child = exec(imcmd, function(err, stdout, stderr) {
var info = {};
var temp = stdout.split(' ');
info.name = temp[0];
info.type = temp[1];
var dim = temp[2].split('x');
info.width = dim[0];
info.height = dim[1];
info.depth = temp[4];
info.size = temp[6];
callback(err, info, stderr);
});
};

// convert a file type to another
exports.convert = function(options, callback) {
if (options.src === undefined || options.dst === undefined) throw_err('path');

if (options.quality === undefined) imcmd = 'convert ' + options.src + ' ' + options.dst;
else imcmd = 'convert ' + options.src + ' -quality ' + options.quality + ' ' + options.dst;
child = exec(imcmd, function(err, stdout, stderr) {
callback(err, stdout, stderr);
});
};

// resize an image
exports.resize = function(options, callback) {
if (options.src === undefined || options.dst === undefined) throw_err('path');
if (options.width === undefined) throw_err('dim');
options.height = options.height || options.width;

if (options.quality === undefined) imcmd = 'convert ' + options.src + ' -resize '+options.width + 'x' + options.height + ' ' + options.dst;
else imcmd = 'convert ' + options.src + ' -resize '+options.width + 'x' + options.height + ' -quality ' + options.quality + ' ' + options.dst;
child = exec(imcmd, function(err, stdout, stderr) {
callback(err, stdout, stderr);
});
};

// crop an image
exports.crop = function(options, callback) {
if (options.src === undefined || options.dst === undefined) throw_err('path');
if (options.cropwidth === undefined) throw_err('dim');
options.cropheight = options.cropheight || options.cropwidth;
options.gravity = options.gravity || 'Center';
options.x = options.x || 0;
options.y = options.y || 0;

if (options.quality === undefined) imcmd = 'convert ' + options.src + ' -gravity ' + options.gravity + ' -crop '+ options.cropwidth + 'x'+ options.cropheight + '+' + options.x + '+' + options.y + ' ' + options.dst;
else imcmd = 'convert ' + options.src + ' -gravity ' + options.gravity + ' -crop '+ options.cropwidth + 'x'+ options.cropheight + '+' + options.x + '+' + options.y + ' -quality ' + options.quality + ' ' + options.dst;
child = exec(imcmd, function(err, stdout, stderr) {
callback(err, stdout, stderr);
});
};

// resize and crop in one shot!
exports.rescrop = function(options, callback) {
if (options.src === undefined || options.dst === undefined) throw_err('path');
if (options.width === undefined) throw_err('dim');
options.height = options.height || options.width;
if (options.cropwidth === undefined) throw_err('dim');
options.cropheight = options.cropheight || options.cropwidth;
options.gravity = options.gravity || 'Center';
options.x = options.x || 0;
options.y = options.y || 0;

if (options.quality === undefined) imcmd = 'convert ' + options.src + ' -resize ' + options.width + 'x' + options.height + ' -gravity ' + options.gravity + ' -crop '+ options.cropwidth + 'x'+ options.cropheight + '+' + options.x + '+' + options.y + ' ' + options.dst;
else imcmd = 'convert ' + options.src + ' -resize ' + options.width + 'x' + options.height + ' -gravity ' + options.gravity + ' -crop '+ options.cropwidth + 'x'+ options.cropheight + '+' + options.x + '+' + options.y + ' -quality ' + options.quality + ' ' + options.dst;
child = exec(imcmd, function(err, stdout, stderr) {
callback(err, stdout, stderr);
});
};


// for the hardcore types - issue your own ImageMagick command
exports.exec = function(command, callback) {
var _command = command.split(' ')[0];
// as a security measure, we will allow only 'convert' commands
if (_command != 'convert') throw_error('restricted');

child = exec(command, function(err, stdout, stderr) { callback(err, stdout, stderr); });
};

13 changes: 13 additions & 0 deletions package.json
@@ -0,0 +1,13 @@
{
"name": "easyimage",
"version": "0.0.1",
"description": "A friendly module for processing images.",
"main": "easyimage.js",
"author": "Hack Sparrow <captain@hacksparrow.com>",
"keywords": ["image", "graphics", "process", "convert", "resize", "crop"],
"repository" : {
"type": "git",
"url": "https://git@github.com:hacksparrow/node-easyimage.git"
},
"engines": { "node": "0.6.*" }
}
49 changes: 49 additions & 0 deletions test.js
@@ -0,0 +1,49 @@
var easyim = require('./easyimage');

easyim.info('sample-images/kitten.jpg', function(err, stdout, stderr) {
if (err) throw err;
console.log(stdout);
});


easyim.convert({src:'sample-images/kitten.jpg', dst:'kitten.png', quality:10}, function(err, stdout, stderr) {
if (err) throw err;
console.log('Converted');
});


easyim.resize({src:'sample-images/kitten.jpg', dst:'kitten-small.jpg', width:640, height:480}, function(err, stdout, stderr) {
if (err) throw err;
console.log('Resized');
});

easyim.crop(
{
src:'sample-images/kitten.jpg', dst:'kitten.jpg',
cropwidth:128, cropheight:128,
gravity:'North',
x:0, y:0
},
function(err, stdout, stderr) {
if (err) throw err;
console.log('Cropped');
}
);

easyim.rescrop(
{
src:'sample-images/kitten.jpg', dst:'kitten-thumbnail.jpg',
width:500, height:500,
cropwidth:128, cropheight:128,
x:0, y:0
},
function(err, stdout, stderr) {
if (err) throw err;
console.log('Resized and cropped');
}
);

easyim.exec('convert sample-images/kitten.jpg kitten.gif', function(err, stdout, stderr) {
if (err) throw err;
console.log('Command executed');
});

0 comments on commit 56a21bf

Please sign in to comment.