Permalink
Browse files

Allow im.crop() to crop to arbitrary rectangles within image.

  • Loading branch information...
1 parent b830fe9 commit d33433d3a7b54310ea66a9e1ef4168ff5a11f5e9 @marcweil marcweil committed Nov 12, 2012
Showing with 11 additions and 2 deletions.
  1. +11 −2 imagemagick.js
View
@@ -319,10 +319,17 @@ exports.crop = function (options, callback) {
dDst = t.opt.width / t.opt.height,
resizeTo = (dSrc < dDst) ? ''+t.opt.width+'x' : 'x'+t.opt.height,
dGravity = options.gravity ? options.gravity : "Center";
+
+ if(!t.opt.top && !t.opt.left) {
+ // Add -resize flag if this isn't an arbitrary crop rectangle.
+ args = args.concat(['-resize', resizeTo]);
+ } else {
+ // Since this is an arbitrary crop rectange, use -crop instead of -resize.
+ args = args.concat(['-crop', ''+t.opt.width + 'x' + t.opt.height + (t.opt.top ? t.opt.top : '+0') + (t.opt.left ? t.opt.left : '+0')]);
+ }
+
args = args.concat([
- '-resize', resizeTo,
'-gravity', dGravity,
- '-crop', ''+t.opt.width + 'x' + t.opt.height + '+0+0',
'+repage'
]);
ignoreArg = false;
@@ -346,6 +353,8 @@ exports.resizeArgs = function(options) {
colorspace: null,
width: 0,
height: 0,
+ top: 0,
+ left: 0,
strip: true,
filter: 'Lagrange',
sharpening: 0.2,

0 comments on commit d33433d

Please sign in to comment.