Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

First commit

  • Loading branch information...
commit 56a21bf107058806aac659a43cc0939e5f53837f 0 parents
@hacksparrow authored
Showing with 166 additions and 0 deletions.
  1. +2 −0  .gitignore
  2. +3 −0  README.md
  3. +99 −0 easyimage.js
  4. +13 −0 package.json
  5. +49 −0 test.js
2  .gitignore
@@ -0,0 +1,2 @@
+sample-images/
+NOTES
3  README.md
@@ -0,0 +1,3 @@
+
+See test.js for usage examples.
+Does not work under Windows as of now.
99 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 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 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');
+});
Please sign in to comment.
Something went wrong with that request. Please try again.