Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding support to -distort command #745

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,22 @@ gm(200, 400, "#ddff99f3")
.write("/path/to/brandNewImg.jpg", function (err) {
// ...
});

// distorting an image
// array of arrays with from -> to objects
// fx,fy -> tx,ty

// It will only works with imageMagick subclass
gm("image.jpg")
.distort([
[{x:99,y:71}, {x:0,y:0}],
[{x:299,y:71}, {x:479,y:0}],
[{x:100,y:270}, {x:0,y:477}],
[{x:299,y:270}, {x:479,y:476}],
])
.write("/path/to/brandNewImg.jpg", function (err) {
// ...
});
```

## Streams
Expand Down
22 changes: 22 additions & 0 deletions lib/args.js
Original file line number Diff line number Diff line change
Expand Up @@ -1103,6 +1103,28 @@ module.exports = function (proto) {
proto.background = function background (color) {
return this.in("-background", color);
}

//http://www.imagemagick.org/Usage/distorts/
proto.distort = function distort(pers, method){
if(!method){
method = "BilinearForward";
}
if(!this._options.imageMagick){
console.warn('Graphics Magick appears not to support the -distort option. Please use {imageMagick: true}\nDistort command was ignored!');
return this;
}
var perspectiveFrom = "";
pers.forEach(function(coordinate){
perspectiveFrom += coordinate[0].x.toString();
perspectiveFrom += ","
perspectiveFrom += coordinate[0].y.toString() +" ";
perspectiveFrom += coordinate[1].x.toString();
perspectiveFrom += ","
perspectiveFrom += coordinate[1].y.toString() +" ";
});

return this.out("-distort",method, perspectiveFrom.trim() );
}
};

/**
Expand Down