Add support for .extract({left:...,top:...,width:...,height:...}).

This commit is contained in:
Andreas Lind
2015-11-18 11:09:24 +01:00
parent 5842da22d8
commit 60b8b92630
2 changed files with 78 additions and 23 deletions

View File

@@ -8,10 +8,33 @@ var fixtures = require('../fixtures');
sharp.cache(0);
describe('Partial image extraction', function() {
describe('using the legacy extract(top,left,width,height) syntax', function () {
it('JPEG', function(done) {
sharp(fixtures.inputJpg)
.extract(2, 2, 20, 20)
.toBuffer(function(err, data, info) {
if (err) throw err;
assert.strictEqual(20, info.width);
assert.strictEqual(20, info.height);
fixtures.assertSimilar(fixtures.expected('extract.jpg'), data, done);
});
});
it('PNG', function(done) {
sharp(fixtures.inputPng)
.extract(300, 200, 400, 200)
.toBuffer(function(err, data, info) {
if (err) throw err;
assert.strictEqual(400, info.width);
assert.strictEqual(200, info.height);
fixtures.assertSimilar(fixtures.expected('extract.png'), data, done);
});
});
});
it('JPEG', function(done) {
sharp(fixtures.inputJpg)
.extract(2, 2, 20, 20)
.extract({ left: 2, top: 2, width: 20, height: 20 })
.toBuffer(function(err, data, info) {
if (err) throw err;
assert.strictEqual(20, info.width);
@@ -22,7 +45,7 @@ describe('Partial image extraction', function() {
it('PNG', function(done) {
sharp(fixtures.inputPng)
.extract(300, 200, 400, 200)
.extract({ left: 200, top: 300, width: 400, height: 200 })
.toBuffer(function(err, data, info) {
if (err) throw err;
assert.strictEqual(400, info.width);
@@ -34,7 +57,7 @@ describe('Partial image extraction', function() {
if (sharp.format.webp.output.file) {
it('WebP', function(done) {
sharp(fixtures.inputWebP)
.extract(50, 100, 125, 200)
.extract({ left: 100, top: 50, width: 125, height: 200 })
.toBuffer(function(err, data, info) {
if (err) throw err;
assert.strictEqual(125, info.width);
@@ -46,7 +69,7 @@ describe('Partial image extraction', function() {
it('TIFF', function(done) {
sharp(fixtures.inputTiff)
.extract(63, 34, 341, 529)
.extract({ left: 34, top: 63, width: 341, height: 529 })
.jpeg()
.toBuffer(function(err, data, info) {
if (err) throw err;
@@ -58,7 +81,7 @@ describe('Partial image extraction', function() {
it('Before resize', function(done) {
sharp(fixtures.inputJpg)
.extract(10, 10, 10, 500, 500)
.extract({ left: 10, top: 10, width: 10, height: 500 })
.resize(100, 100)
.toBuffer(function(err, data, info) {
if (err) throw err;
@@ -72,7 +95,7 @@ describe('Partial image extraction', function() {
sharp(fixtures.inputJpg)
.resize(500, 500)
.crop(sharp.gravity.north)
.extract(10, 10, 100, 100)
.extract({ left: 10, top: 10, width: 100, height: 100 })
.toBuffer(function(err, data, info) {
if (err) throw err;
assert.strictEqual(100, info.width);
@@ -83,10 +106,10 @@ describe('Partial image extraction', function() {
it('Before and after resize and crop', function(done) {
sharp(fixtures.inputJpg)
.extract(0, 0, 700, 700)
.extract({ left: 0, top: 0, width: 700, height: 700 })
.resize(500, 500)
.crop(sharp.gravity.north)
.extract(10, 10, 100, 100)
.extract({ left: 10, top: 10, width: 100, height: 100 })
.toBuffer(function(err, data, info) {
if (err) throw err;
assert.strictEqual(100, info.width);
@@ -97,7 +120,7 @@ describe('Partial image extraction', function() {
it('Extract then rotate', function(done) {
sharp(fixtures.inputPngWithGreyAlpha)
.extract(10, 20, 380, 280)
.extract({ left: 20, top: 10, width: 380, height: 280 })
.rotate(90)
.toBuffer(function(err, data, info) {
if (err) throw err;
@@ -110,7 +133,7 @@ describe('Partial image extraction', function() {
it('Rotate then extract', function(done) {
sharp(fixtures.inputPngWithGreyAlpha)
.rotate(90)
.extract(10, 20, 280, 380)
.extract({ left: 20, top: 10, width: 280, height: 380 })
.toBuffer(function(err, data, info) {
if (err) throw err;
assert.strictEqual(280, info.width);
@@ -120,6 +143,31 @@ describe('Partial image extraction', function() {
});
describe('Invalid parameters', function() {
describe('using the legacy extract(top,left,width,height) syntax', function () {
it('String top', function() {
assert.throws(function() {
sharp(fixtures.inputJpg).extract('spoons', 10, 10, 10);
});
});
it('Non-integral left', function() {
assert.throws(function() {
sharp(fixtures.inputJpg).extract(10, 10.2, 10, 10);
});
});
it('Negative width - negative', function() {
assert.throws(function() {
sharp(fixtures.inputJpg).extract(10, 10, -10, 10);
});
});
it('Null height', function() {
assert.throws(function() {
sharp(fixtures.inputJpg).extract(10, 10, 10, null);
});
});
});
it('Undefined', function() {
assert.throws(function() {
@@ -129,27 +177,26 @@ describe('Partial image extraction', function() {
it('String top', function() {
assert.throws(function() {
sharp(fixtures.inputJpg).extract('spoons', 10, 10, 10);
sharp(fixtures.inputJpg).extract({ left: 10, top: 'spoons', width: 10, height: 10 });
});
});
it('Non-integral left', function() {
assert.throws(function() {
sharp(fixtures.inputJpg).extract(10, 10.2, 10, 10);
sharp(fixtures.inputJpg).extract({ left: 10.2, top: 10, width: 10, height: 10 });
});
});
it('Negative width - negative', function() {
assert.throws(function() {
sharp(fixtures.inputJpg).extract(10, 10, -10, 10);
sharp(fixtures.inputJpg).extract({ left: 10, top: 10, width: -10, height: 10 });
});
});
it('Null height', function() {
assert.throws(function() {
sharp(fixtures.inputJpg).extract(10, 10, 10, null);
sharp(fixtures.inputJpg).extract({ left: 10, top: 10, width: 10, height: null });
});
});
});
});