Permalink
Browse files

added different resize methods

  • Loading branch information...
1 parent 51da560 commit f9eb5c727969e9f0ce61b927b10b9e6ed6161ab5 @vdemedes committed Sep 23, 2012
Showing with 52 additions and 37 deletions.
  1. +29 −25 lib/magician.coffee
  2. +23 −12 lib/magician.js
View
54 lib/magician.coffee
@@ -4,29 +4,33 @@ class Magician
constructor: (@srcPath, @destPath) ->
# making paths correct for exec, test folder/test.png => "test folder/test.png"
- @srcPath = '"' + @srcPath + '"'
- @destPath = '"' + @destPath + '"'
+ @srcPath = '"' + @srcPath + '"'
+ @destPath = '"' + @destPath + '"'
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
+ 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"
+
+ command = 'resize'
+ command = 'thumbnail' if options.thumbnail
+ command = 'sample' if options.sample
+ exec "convert #{ @srcPath } -#{ command } #{ options.width }x#{ options.height }! #{ @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
-
+ 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
+ callback new Error "x, y should be bigger than -1; width and height should be bigger than 0" if callback
+ return
+
+ 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
@@ -35,14 +39,14 @@ class Magician
callback err if callback
getDimensions: (callback) ->
- that = @
-
- 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
+ exec "identify #{ @srcPath }", (err, stdout) =>
+ if err
+ callback err if callback
+ return
+
+ [width, height] = stdout.split(" ")[2].split "x"
+ @width = parseInt width
+ @height = parseInt height
+ callback no, width: @width, height: @height if callback
module.exports = Magician
View
35 lib/magician.js
@@ -1,12 +1,10 @@
-// Generated by CoffeeScript 1.3.1
+// Generated by CoffeeScript 1.3.3
var Magician, exec;
exec = require('child_process').exec;
Magician = (function() {
- Magician.name = 'Magician';
-
function Magician(srcPath, destPath) {
this.srcPath = srcPath;
this.destPath = destPath;
@@ -15,10 +13,18 @@ Magician = (function() {
}
Magician.prototype.resize = function(options, callback) {
+ var command;
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 " + options.width + "x" + options.height + "! " + this.srcPath + " " + this.destPath, function(err) {
+ command = 'resize';
+ if (options.thumbnail) {
+ command = 'thumbnail';
+ }
+ if (options.sample) {
+ command = 'sample';
+ }
+ return exec("convert " + this.srcPath + " -" + command + " " + options.width + "x" + options.height + "! " + this.destPath, function(err) {
if (callback) {
return callback(err);
}
@@ -40,7 +46,10 @@ Magician = (function() {
options.y = 0;
}
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"));
+ if (callback) {
+ callback(new Error("x, y should be bigger than -1; width and height should be bigger than 0"));
+ }
+ return;
}
return exec("convert " + this.srcPath + " -crop " + options.width + "x" + options.height + "+" + options.x + "+" + options.y + " " + this.destPath, function(err) {
if (callback) {
@@ -67,20 +76,22 @@ Magician = (function() {
};
Magician.prototype.getDimensions = function(callback) {
- var that;
- that = this;
+ var _this = this;
return exec("identify " + this.srcPath, function(err, stdout) {
var height, width, _ref;
if (err) {
- return callback(err);
+ if (callback) {
+ callback(err);
+ }
+ return;
}
_ref = stdout.split(" ")[2].split("x"), width = _ref[0], height = _ref[1];
- that.width = parseInt(width);
- that.height = parseInt(height);
+ _this.width = parseInt(width);
+ _this.height = parseInt(height);
if (callback) {
return callback(false, {
- width: that.width,
- height: that.height
+ width: _this.width,
+ height: _this.height
});
}
});

0 comments on commit f9eb5c7

Please sign in to comment.