mirror of
https://github.com/lovell/sharp.git
synced 2025-12-19 07:15:08 +01:00
Add experimental, entropy-based auto-crop
Remove deprecated extract API
This commit is contained in:
@@ -5,9 +5,9 @@ var assert = require('assert');
|
||||
var sharp = require('../../index');
|
||||
var fixtures = require('../fixtures');
|
||||
|
||||
describe('Crop gravities', function() {
|
||||
describe('Crop', function() {
|
||||
|
||||
var testSettings = [
|
||||
[
|
||||
{
|
||||
name: 'North',
|
||||
width: 320,
|
||||
@@ -50,6 +50,13 @@ describe('Crop gravities', function() {
|
||||
gravity: sharp.gravity.centre,
|
||||
fixture: 'gravity-centre.jpg'
|
||||
},
|
||||
{
|
||||
name: 'Default (centre)',
|
||||
width: 80,
|
||||
height: 320,
|
||||
gravity: undefined,
|
||||
fixture: 'gravity-centre.jpg'
|
||||
},
|
||||
{
|
||||
name: 'Northeast',
|
||||
width: 320,
|
||||
@@ -106,10 +113,8 @@ describe('Crop gravities', function() {
|
||||
gravity: sharp.gravity.northwest,
|
||||
fixture: 'gravity-west.jpg'
|
||||
}
|
||||
];
|
||||
|
||||
testSettings.forEach(function(settings) {
|
||||
it(settings.name, function(done) {
|
||||
].forEach(function(settings) {
|
||||
it(settings.name + ' gravity', function(done) {
|
||||
sharp(fixtures.inputJpg)
|
||||
.resize(settings.width, settings.height)
|
||||
.crop(settings.gravity)
|
||||
@@ -122,7 +127,7 @@ describe('Crop gravities', function() {
|
||||
});
|
||||
});
|
||||
|
||||
it('allows specifying the gravity as a string', function(done) {
|
||||
it('Allows specifying the gravity as a string', function(done) {
|
||||
sharp(fixtures.inputJpg)
|
||||
.resize(80, 320)
|
||||
.crop('east')
|
||||
@@ -134,36 +139,57 @@ describe('Crop gravities', function() {
|
||||
});
|
||||
});
|
||||
|
||||
it('Invalid number', function() {
|
||||
it('Invalid values fail', function() {
|
||||
assert.throws(function() {
|
||||
sharp(fixtures.inputJpg).crop(9);
|
||||
sharp().crop(9);
|
||||
});
|
||||
assert.throws(function() {
|
||||
sharp().crop(1.1);
|
||||
});
|
||||
assert.throws(function() {
|
||||
sharp().crop(-1);
|
||||
});
|
||||
assert.throws(function() {
|
||||
sharp().crop('zoinks');
|
||||
});
|
||||
});
|
||||
|
||||
it('Invalid string', function() {
|
||||
assert.throws(function() {
|
||||
sharp(fixtures.inputJpg).crop('yadda');
|
||||
});
|
||||
});
|
||||
|
||||
it('does not throw if crop gravity is undefined', function() {
|
||||
it('Uses default value when none specified', function() {
|
||||
assert.doesNotThrow(function() {
|
||||
sharp(fixtures.inputJpg).crop();
|
||||
sharp().crop();
|
||||
});
|
||||
});
|
||||
|
||||
it('defaults crop gravity to sharp.gravity.center', function(done) {
|
||||
var centerGravitySettings = testSettings.filter(function (settings) {
|
||||
return settings.name === 'Center';
|
||||
})[0];
|
||||
sharp(fixtures.inputJpg)
|
||||
.resize(centerGravitySettings.width, centerGravitySettings.height)
|
||||
.crop()
|
||||
.toBuffer(function(err, data, info) {
|
||||
if (err) throw err;
|
||||
assert.strictEqual(centerGravitySettings.width, info.width);
|
||||
assert.strictEqual(centerGravitySettings.height, info.height);
|
||||
fixtures.assertSimilar(fixtures.expected(centerGravitySettings.fixture), data, done);
|
||||
});
|
||||
describe('Entropy-based strategy', function() {
|
||||
|
||||
it('JPEG', function(done) {
|
||||
sharp(fixtures.inputJpgWithCmykProfile)
|
||||
.resize(80, 320)
|
||||
.crop(sharp.strategy.entropy)
|
||||
.toBuffer(function(err, data, info) {
|
||||
if (err) throw err;
|
||||
assert.strictEqual('jpeg', info.format);
|
||||
assert.strictEqual(3, info.channels);
|
||||
assert.strictEqual(80, info.width);
|
||||
assert.strictEqual(320, info.height);
|
||||
fixtures.assertSimilar(fixtures.expected('crop-entropy.jpg'), data, done);
|
||||
});
|
||||
});
|
||||
|
||||
it('PNG', function(done) {
|
||||
sharp(fixtures.inputPngWithTransparency)
|
||||
.resize(320, 80)
|
||||
.crop(sharp.strategy.entropy)
|
||||
.toBuffer(function(err, data, info) {
|
||||
if (err) throw err;
|
||||
assert.strictEqual('png', info.format);
|
||||
assert.strictEqual(4, info.channels);
|
||||
assert.strictEqual(320, info.width);
|
||||
assert.strictEqual(80, info.height);
|
||||
fixtures.assertSimilar(fixtures.expected('crop-entropy.png'), data, done);
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
@@ -6,29 +6,6 @@ var sharp = require('../../index');
|
||||
var fixtures = require('../fixtures');
|
||||
|
||||
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)
|
||||
|
||||
Reference in New Issue
Block a user