Skip to content
Browse files

resize and crop methods with better params, handling path with spaces…

…, correct error handling
  • Loading branch information...
1 parent fad4b14 commit e5934dd9f23680ecbde4e79f64a6d03556c7fa41 @vdemedes committed May 16, 2012
Showing with 165 additions and 102 deletions.
  1. +13 −8 Readme.md
  2. +36 −25 lib/magician.coffee
  3. +58 −18 lib/magician.js
  4. +14 −8 package.json
  5. +44 −43 test/magician.test.coffee
View
21 Readme.md
@@ -1,7 +1,7 @@
# magician
- Library for image manipulation. Requires imagemagick.
+ Library for **easy** image manipulation. Requires ImageMagick.
# Here it is!
@@ -70,14 +70,14 @@ This is very early release, so Magician does not have a lot of features.
# Usage
```
-magician = require 'magician'
+Magician = require 'magician'
-image = new magician __dirname + '/source.jpg', __dirname + '/target.jpg'
+image = new Magician "#{ __dirname }/source.jpg", "#{ __dirname }/target.jpg"
-image.resizeTo 100, 100, (err) ->
+image.resize width: 100, height: 100, (err) ->
// done!
-image.cropFrom 0, 0, 200, 100, (err) ->
+image.crop x: 0, y: 0, width: 200, height: 100, (err) ->
// done!
image.convert (err) ->
@@ -86,15 +86,20 @@ image.convert (err) ->
image.getDimensions (err, dimensions) ->
width = dimensions.width
height = dimensions.height
-
```
# Tests
-You can run tests using zap:
+You can run tests using mocha:
+
+```
+mocha
+```
+
+Don't forget to install development dependencies first:
```
-zap
+npm install --development
```
# TODO List
View
61 lib/magician.coffee
@@ -1,37 +1,48 @@
-exec = require("child_process").exec
+{exec} = require 'child_process'
class Magician
- constructor: (@sourcePath, @destPath) ->
+ constructor: (@srcPath, @destPath) ->
+ # making paths correct for exec, test folder/test.png => "test folder/test.png"
+ @srcPath = '"' + @srcPath + '"'
+ @destPath = '"' + @destPath + '"'
- resizeTo: (width, height, callback) ->
- if not width or width < 0 or not height or height < 0
- return callback new Error "width and height should be bigger than 0"
-
- exec "convert -resize #{ width }x#{ height } #{ @sourcePath } #{ @destPath }", (err) ->
- callback err if callback
+ resize: (options, callback) ->
+ if not options.width or options.width < 0 or not options.height or options.height < 0
+ return callback new Error "width and height should be bigger than 0"
+
+ exec "convert -resize #{ options.width }x#{ options.height }! #{ @srcPath } #{ @destPath }", (err) ->
+ callback err if callback
- cropFrom: (x, y, width, height, callback) ->
- if x < 0 or y < 0 or width < 0 or height < 0
- return callback new Error "x, y, width and height should be bigger than 0"
- exec "convert #{ @sourcePath } -crop #{ width }x#{ height }+#{ x }+#{ y } #{ @destPath }", (err) ->
- callback err if callback
+ resizeTo: (width, height, callback) -> # backwards compatability, to be removed
+ @resize width: width, height: height, callback
+
+ crop: (options, callback) ->
+ options.x = 0 if not options.x
+ options.y = 0 if not options.y
+
+ if options.x < 0 or options.y or options.width < 0 or options.height < 0
+ return callback new Error "x, y should be bigger than -1; width and height should be bigger than 0"
+
+ exec "convert #{ @srcPath } -crop #{ options.width }x#{ options.height }+#{ options.x }+#{ options.y } #{ @destPath }", (err) ->
+ callback err if callback
+
+ cropFrom: (x, y, width, height, callback) -> # backwards compatability, to be removed
+ @crop x: x, y: y, width: width, height: height, callback
convert: (callback) ->
- exec "convert #{ @sourcePath } #{ @destPath }", (err) ->
+ exec "convert #{ @srcPath } #{ @destPath }", (err) ->
callback err if callback
-
getDimensions: (callback) ->
that = @
- exec "identify #{ @sourcePath }", (err, stdout) ->
- dimensions = stdout.split(" ")[2].split "x"
- that.width = dimensions[0]
- that.height = dimensions[1]
- callback err, {
- width: that.width,
- height: that.height
- } if callback
-
-
+
+ exec "identify #{ @srcPath }", (err, stdout) ->
+ return callback err if err
+
+ [width, height] = stdout.split(" ")[2].split "x"
+ that.width = parseInt width
+ that.height = parseInt height
+ callback no, width: that.width, height: that.height if callback
+
module.exports = Magician
View
76 lib/magician.js
@@ -1,53 +1,93 @@
+// Generated by CoffeeScript 1.3.1
var Magician, exec;
-exec = require("child_process").exec;
+
+exec = require('child_process').exec;
+
Magician = (function() {
- function Magician(sourcePath, destPath) {
- this.sourcePath = sourcePath;
+
+ Magician.name = 'Magician';
+
+ function Magician(srcPath, destPath) {
+ this.srcPath = srcPath;
this.destPath = destPath;
+ this.srcPath = '"' + this.srcPath + '"';
+ this.destPath = '"' + this.destPath + '"';
}
- Magician.prototype.resizeTo = function(width, height, callback) {
- if (!width || width < 0 || !height || height < 0) {
+
+ Magician.prototype.resize = function(options, callback) {
+ if (!options.width || options.width < 0 || !options.height || options.height < 0) {
return callback(new Error("width and height should be bigger than 0"));
}
- return exec("convert -resize " + width + "x" + height + " " + this.sourcePath + " " + this.destPath, function(err) {
+ return exec("convert -resize " + options.width + "x" + options.height + "! " + this.srcPath + " " + this.destPath, function(err) {
if (callback) {
return callback(err);
}
});
};
- Magician.prototype.cropFrom = function(x, y, width, height, callback) {
- if (x < 0 || y < 0 || width < 0 || height < 0) {
- return callback(new Error("x, y, width and height should be bigger than 0"));
+
+ Magician.prototype.resizeTo = function(width, height, callback) {
+ return this.resize({
+ width: width,
+ height: height
+ }, callback);
+ };
+
+ Magician.prototype.crop = function(options, callback) {
+ if (!options.x) {
+ options.x = 0;
+ }
+ if (!options.y) {
+ options.y = 0;
}
- return exec("convert " + this.sourcePath + " -crop " + width + "x" + height + "+" + x + "+" + y + " " + this.destPath, function(err) {
+ if (options.x < 0 || options.y || options.width < 0 || options.height < 0) {
+ return callback(new Error("x, y should be bigger than -1; width and height should be bigger than 0"));
+ }
+ return exec("convert " + this.srcPath + " -crop " + options.width + "x" + options.height + "+" + options.x + "+" + options.y + " " + this.destPath, function(err) {
if (callback) {
return callback(err);
}
});
};
+
+ Magician.prototype.cropFrom = function(x, y, width, height, callback) {
+ return this.crop({
+ x: x,
+ y: y,
+ width: width,
+ height: height
+ }, callback);
+ };
+
Magician.prototype.convert = function(callback) {
- return exec("convert " + this.sourcePath + " " + this.destPath, function(err) {
+ return exec("convert " + this.srcPath + " " + this.destPath, function(err) {
if (callback) {
return callback(err);
}
});
};
+
Magician.prototype.getDimensions = function(callback) {
var that;
that = this;
- return exec("identify " + this.sourcePath, function(err, stdout) {
- var dimensions;
- dimensions = stdout.split(" ")[2].split("x");
- that.width = dimensions[0];
- that.height = dimensions[1];
+ return exec("identify " + this.srcPath, function(err, stdout) {
+ var height, width, _ref;
+ if (err) {
+ return callback(err);
+ }
+ _ref = stdout.split(" ")[2].split("x"), width = _ref[0], height = _ref[1];
+ that.width = parseInt(width);
+ that.height = parseInt(height);
if (callback) {
- return callback(err, {
+ return callback(false, {
width: that.width,
height: that.height
});
}
});
};
+
return Magician;
+
})();
-module.exports = Magician;
+
+module.exports = Magician;
View
22 package.json
@@ -1,10 +1,16 @@
{
- "name": "magician"
- , "version": "0.0.5"
- , "description": "Library for image manipulation. Requires imagemagick."
- , "keywords": ["image", "imagemagick", "magick", "resize", "crop"]
- , "author": "Vadim Demedes <sbioko@gmail.com>"
- , "dependencies": {}
- , "main": "index"
- , "engines": { "node": ">= 0.4" }
+ "name": "magician",
+ "version": "0.1.0",
+ "description": "Library for cute image manipulation. Requires ImageMagick.",
+ "keywords": ["image", "imagemagick", "magick", "resize", "crop"],
+ "author": "Vadim Demedes <sbioko@gmail.com>",
+ "dependencies": {},
+ "devDependencies": {
+ "should": "*"
+ },
+ "main": "index",
+ "engine": {
+ "node": ">= 0.4"
+ },
+ "license": "MIT"
}
View
87 test/magician.test.coffee
@@ -1,45 +1,46 @@
-assert = require 'assert'
fs = require 'fs'
+{exec} = require 'child_process'
+should = require 'should'
+Magician = require '../lib/magician'
-module.exports=
- setup: (test) ->
- test.magician = require '../lib/magician'
-
- 'test resizing image': (test) ->
- image = new test.magician __dirname + '/image.jpg', __dirname + '/image_resized.jpg'
- image.resizeTo 100, 100, (err) ->
- resizedImage = new test.magician __dirname + '/image_resized.jpg'
- resizedImage.getDimensions (err, dimensions) ->
- assert.equal(dimensions.width, 100) and assert.equal(dimensions.height, 100)
- test.done()
- fs.unlinkSync __dirname + '/image_resized.jpg'
-
- 'test cropping image': (test) ->
- image = new test.magician __dirname + '/image.jpg', __dirname + '/image_cropped.jpg'
- image.cropFrom 0, 0, 200, 100, (err) ->
- croppedImage = new test.magician __dirname + '/image_cropped.jpg'
- croppedImage.getDimensions (err, dimensions) ->
- assert.equal(dimensions.width, 200) and assert.equal(dimensions.height, 100)
- test.done()
- fs.unlinkSync __dirname + '/image_cropped.jpg'
-
- 'test converting image': (test) ->
- image = new test.magician __dirname + '/image.jpg', __dirname + '/image.png'
- image.convert ->
- assert.ok(true)
- test.done()
- fs.unlinkSync __dirname + '/image.png'
-
- 'test getting dimensions of an image': (test) ->
- image = new test.magician __dirname + '/image.jpg'
- image.getDimensions (err, dimensions) ->
- assert.equal(dimensions.width, 500) and assert.equal(dimensions.height, 250)
- test.done()
-
- 'test writing to folder with spaces in name': (test) ->
- fs.mkdirSync __dirname + '/test folder'
- image = new test.magician __dirname + '/image.jpg', __dirname + '/test folder/image.png'
- image.convert ->
- fs.statSync __dirname + '/test folder/image.png'
- assert.ok(true)
- test.done()
+describe 'Magician', ->
+ it 'should resize image', (done) ->
+ image = new Magician "#{ __dirname }/image.jpg", "#{ __dirname }/image_resized.jpg"
+ image.resize width: 100, height: 100, (err) ->
+ resizedImage = new Magician "#{ __dirname }/image_resized.jpg"
+ resizedImage.getDimensions (err, dimensions) ->
+ fs.unlinkSync "#{ __dirname }/image_resized.jpg"
+ dimensions.width.should.equal(100) and dimensions.height.should.equal(100)
+ do done
+
+ it 'should crop image', (done) ->
+ image = new Magician "#{ __dirname }/image.jpg", "#{ __dirname }/image_cropped.jpg"
+ image.crop x: 0, y: 0, width: 200, height: 100, (err) ->
+ croppedImage = new Magician "#{ __dirname }/image_cropped.jpg"
+ croppedImage.getDimensions (err, dimensions) ->
+ fs.unlinkSync "#{ __dirname }/image_cropped.jpg"
+ dimensions.width.should.equal(200) and dimensions.height.should.equal(100)
+ do done
+
+ it 'should convert image', (done) ->
+ image = new Magician "#{ __dirname }/image.jpg", "#{ __dirname }/image.png"
+ image.convert ->
+ fs.stat "#{ __dirname }/image.png", (err, stat) ->
+ fs.unlinkSync "#{ __dirname }/image.png"
+ should.exist(stat)
+ do done
+
+ it 'should get dimensions of an image', (done) ->
+ image = new Magician "#{ __dirname }/image.jpg"
+ image.getDimensions (err, dimensions) ->
+ dimensions.width.should.equal(500) and dimensions.height.should.equal(250)
+ do done
+
+ it 'should write to folder with spaces in path', (done) ->
+ fs.mkdirSync "#{ __dirname }/test folder"
+ image = new Magician "#{ __dirname }/image.jpg", "#{ __dirname }/test folder/image.png"
+ image.convert ->
+ fs.stat "#{ __dirname }/test folder/image.png", (err, stat) ->
+ exec "rm -rf #{ __dirname }/test\\ folder", ->
+ should.exist(stat)
+ do done

0 comments on commit e5934dd

Please sign in to comment.
Something went wrong with that request. Please try again.