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

Custom crop sx/sy/sw/sh #9

Merged
merged 1 commit into from
May 8, 2016
Merged
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
4 changes: 3 additions & 1 deletion app.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ app.get('/', function(req, res) {
"resize=500x100",
"resize=100x100&crop=16x9",
"crop=1x1",
"quality=50"
"quality=50",
"sx=100&sy=100&sw=100&sh=100",
"sx=0&sy=0&sw=200&sh=200",
]
};
res.render(__dirname + '/public/index.ejs', template);
Expand Down
47 changes: 33 additions & 14 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,13 @@ module.exports = function (_config) {
return function (_req, _res, _next) {
var dimensions = _req.query.resize || '0x0',
crop = _req.query.crop || '0x0',
quality = _req.query.quality;
quality = _req.query.quality,
customCrop = {
x: _req.query.sx,
y: _req.query.sy,
w: _req.query.sw,
h: _req.query.sh,
};

var gmOptions = {};
if(config.imageMagick) {
Expand Down Expand Up @@ -126,39 +132,52 @@ module.exports = function (_config) {

async.waterfall([

//Get original size
//Get original content-type
function (_callback) {
gmImage.size({
gmImage.format({
bufferStream: true
}, function (_error, _size) {
}, function (_error, _format) {
if(_error) {
console.log(_error);
return _callback(_error);
}
image.size = _size;
image.format = _format;
if(!_res.headersSent) {
_res.header('Content-Type', 'image/' + _format.toLowerCase());
}

_callback(null);
});
},

//Get original content-type
// Custom crop as per request
function (_callback) {
gmImage.format({
// All params should exist and be numeric.
var hasCrop = _.every(customCrop, function(param){
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Slick

var num = Number(param);
return !isNaN(num) && num >= 0;
});
if(hasCrop){
gmImage.crop( customCrop.w, customCrop.h, customCrop.x, customCrop.y );
}
_callback(null);
},

//Get original size (after initial crop)
function (_callback) {
gmImage.size({
bufferStream: true
}, function (_error, _format) {
}, function (_error, _size) {
if(_error) {
console.log(_error);
return _callback(_error);
}
image.format = _format;
if(!_res.headersSent) {
_res.header('Content-Type', 'image/' + _format.toLowerCase());
}
image.size = _size;

_callback(null);
});
},

// Crop
// Aspect ratio
function (_callback) {
// If a crop ratio has been specified
if(!(!image.crop.width && !image.crop.height)) {
Expand Down
79 changes: 79 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,85 @@ describe('immp', function () {

});

it('should do a custom crop', function (_done) {
this.slow(5000);
this.timeout(10000);

http.get(server + '/im/?image=/images/robot.jpg&sx=0&sy=0&sw=100&sh=111', function (_httpResponse) {

_httpResponse.headers['content-type'].should.eql('image/jpeg');

gm(_httpResponse)
.options(gmOptions)
.size(function (_error, _size) {
if(_error) return _done(_error);

_size.width.should.equal(100);
_size.height.should.equal(111);

_done();
});

});

});

it('should do a custom crop with an offset', function (_done) {
this.slow(5000);
this.timeout(10000);

http.get(server + '/im/?image=/images/robot.jpg&sx=100&sy=100&sw=222&sh=111', function (_httpResponse) {

_httpResponse.headers['content-type'].should.eql('image/jpeg');

gm(_httpResponse)
.options(gmOptions)
.size(function (_error, _size) {
if(_error) return _done(_error);

_size.width.should.equal(222);
_size.height.should.equal(111);

_done();
});

});

});

describe('with invalid operators', function(){
[
[0,0,0,0],
['a',0,0,0],
[-1,0,0,0],
['',0,0,0],
].forEach(function(params){
it('should not do a custom crop with ' + params, function (_done) {
this.slow(5000);
this.timeout(10000);

http.get(server + '/im/?image=/images/robot.jpg&sx='+params[0]+'&sy='+params[1]+'&sw='+params[2]+'&sh='+params[3]+'', function (_httpResponse) {

_httpResponse.headers['content-type'].should.eql('image/jpeg');

gm(_httpResponse)
.options(gmOptions)
.size(function (_error, _size) {
if(_error) return _done(_error);

// Default image size
_size.width.should.equal(1920);
_size.height.should.equal(1080);

_done();
});

});

});
});
});

it('should crop to a short width big height ratio', function (_done) {
this.slow(5000);
this.timeout(10000);
Expand Down