diff --git a/test/unit/rotate.js b/test/unit/rotate.js index f7fe2f1d..dc178409 100644 --- a/test/unit/rotate.js +++ b/test/unit/rotate.js @@ -9,72 +9,20 @@ const sharp = require('../../'); const fixtures = require('../fixtures'); describe('Rotation', function () { - ['Landscape', 'Portrait'].forEach(function (orientation) { - [1, 2, 3, 4, 5, 6, 7, 8].forEach(function (exifTag) { - const input = fixtures[`inputJpgWith${orientation}Exif${exifTag}`]; - const expectedOutput = fixtures.expected(`${orientation}_${exifTag}-out.jpg`); - it(`${orientation} image with EXIF Orientation ${exifTag}: Auto-rotate`, function (done) { - const [expectedWidth, expectedHeight] = orientation === 'Landscape' ? [600, 450] : [450, 600]; - sharp(input) - .rotate() - .toBuffer(function (err, data, info) { - if (err) throw err; - assert.strictEqual(info.width, expectedWidth); - assert.strictEqual(info.height, expectedHeight); - fixtures.assertSimilar(expectedOutput, data, done); - }); - }); - it(`${orientation} image with EXIF Orientation ${exifTag}: Auto-rotate then resize`, function (done) { - const [expectedWidth, expectedHeight] = orientation === 'Landscape' ? [320, 240] : [320, 427]; - sharp(input) - .rotate() - .resize({ width: 320 }) - .toBuffer(function (err, data, info) { - if (err) throw err; - assert.strictEqual(info.width, expectedWidth); - assert.strictEqual(info.height, expectedHeight); - fixtures.assertSimilar(expectedOutput, data, done); - }); - }); - it(`${orientation} image with EXIF Orientation ${exifTag}: Resize then auto-rotate`, function (done) { - const [expectedWidth, expectedHeight] = orientation === 'Landscape' - ? (exifTag < 5) ? [320, 240] : [320, 240] - : [320, 427]; - sharp(input) - .resize({ width: 320 }) - .rotate() - .toBuffer(function (err, data, info) { - if (err) throw err; - assert.strictEqual(info.width, expectedWidth); - assert.strictEqual(info.height, expectedHeight); - fixtures.assertSimilar(expectedOutput, data, done); - }); - }); + ['rotate', 'autoOrient', 'constructor'].forEach(function (rotateMethod) { + describe(`Auto orientation via ${rotateMethod}:`, () => { + const options = rotateMethod === 'constructor' ? { autoOrient: true } : {}; - it(`${orientation} image with EXIF Orientation ${exifTag}: Auto-orient (alias of .rotate())`, function (done) { - const [expectedWidth, expectedHeight] = orientation === 'Landscape' ? [600, 450] : [450, 600]; - sharp(input) - .autoOrient() - .toBuffer(function (err, data, info) { - if (err) throw err; - assert.strictEqual(info.width, expectedWidth); - assert.strictEqual(info.height, expectedHeight); - fixtures.assertSimilar(expectedOutput, data, done); - }); - }); + ['Landscape', 'Portrait'].forEach(function (orientation) { + [1, 2, 3, 4, 5, 6, 7, 8].forEach(function (exifTag) { + const input = fixtures[`inputJpgWith${orientation}Exif${exifTag}`]; + const expectedOutput = fixtures.expected(`${orientation}_${exifTag}-out.jpg`); + it(`${orientation} image with EXIF Orientation ${exifTag}: Auto-rotate`, function (done) { + const [expectedWidth, expectedHeight] = orientation === 'Landscape' ? [600, 450] : [450, 600]; - [true, false].forEach((doResize) => { - [90, 180, 270, 45].forEach(function (angle) { - const [inputWidth, inputHeight] = orientation === 'Landscape' ? [600, 450] : [450, 600]; - const expectedOutput = fixtures.expected(`${orientation}_${exifTag}_rotate${angle}-out.jpg`); - it(`${orientation} image with EXIF Orientation ${exifTag}: Auto-rotate then rotate ${angle} ${doResize ? 'and resize' : ''}`, function (done) { - const [width, height] = (angle === 45 ? [742, 742] : [inputWidth, inputHeight]).map((x) => doResize ? Math.floor(x / 1.875) : x); - const [expectedWidth, expectedHeight] = angle % 180 === 0 ? [width, height] : [height, width]; - - const img = sharp(input) - .rotate() - .rotate(angle); - doResize && img.resize(expectedWidth); + const img = sharp(input, options); + rotateMethod === 'rotate' && img.rotate(); + rotateMethod === 'autoOrient' && img.autoOrient(); img.toBuffer(function (err, data, info) { if (err) throw err; @@ -83,25 +31,90 @@ describe('Rotation', function () { fixtures.assertSimilar(expectedOutput, data, done); }); }); - }); - [[true, true], [true, false], [false, true]].forEach(function ([flip, flop]) { - const [inputWidth, inputHeight] = orientation === 'Landscape' ? [600, 450] : [450, 600]; - const flipFlopFileName = [flip && 'flip', flop && 'flop'].filter(Boolean).join('_'); - const flipFlopTestName = [flip && 'flip', flop && 'flop'].filter(Boolean).join(' & '); - it(`${orientation} image with EXIF Orientation ${exifTag}: Auto-rotate then ${flipFlopTestName} ${doResize ? 'and resize' : ''}`, function (done) { - const expectedOutput = fixtures.expected(`${orientation}_${exifTag}_${flipFlopFileName}-out.jpg`); + it(`${orientation} image with EXIF Orientation ${exifTag}: Auto-rotate then resize`, function (done) { + const [expectedWidth, expectedHeight] = orientation === 'Landscape' ? [320, 240] : [320, 427]; - const img = sharp(input).rotate(); - flip && img.flip(); - flop && img.flop(); - doResize && img.resize(orientation === 'Landscape' ? 320 : 240); + const img = sharp(input, options); + rotateMethod === 'rotate' && img.rotate(); + rotateMethod === 'autoOrient' && img.autoOrient(); - img.toBuffer(function (err, data, info) { - if (err) throw err; - assert.strictEqual(info.width, inputWidth / (doResize ? 1.875 : 1)); - assert.strictEqual(info.height, inputHeight / (doResize ? 1.875 : 1)); - fixtures.assertSimilar(expectedOutput, data, done); + img.resize({ width: 320 }) + .toBuffer(function (err, data, info) { + if (err) throw err; + assert.strictEqual(info.width, expectedWidth); + assert.strictEqual(info.height, expectedHeight); + fixtures.assertSimilar(expectedOutput, data, done); + }); + }); + + if (rotateMethod !== 'constructor') { + it(`${orientation} image with EXIF Orientation ${exifTag}: Resize then auto-rotate`, function (done) { + const [expectedWidth, expectedHeight] = orientation === 'Landscape' + ? (exifTag < 5) ? [320, 240] : [320, 240] + : [320, 427]; + + const img = sharp(input, options) + .resize({ width: 320 }); + + rotateMethod === 'rotate' && img.rotate(); + rotateMethod === 'autoOrient' && img.autoOrient(); + img.toBuffer(function (err, data, info) { + if (err) throw err; + assert.strictEqual(info.width, expectedWidth); + assert.strictEqual(info.height, expectedHeight); + fixtures.assertSimilar(expectedOutput, data, done); + }); + }); + } + + [true, false].forEach((doResize) => { + [90, 180, 270, 45].forEach(function (angle) { + const [inputWidth, inputHeight] = orientation === 'Landscape' ? [600, 450] : [450, 600]; + const expectedOutput = fixtures.expected(`${orientation}_${exifTag}_rotate${angle}-out.jpg`); + it(`${orientation} image with EXIF Orientation ${exifTag}: Auto-rotate then rotate ${angle} ${doResize ? 'and resize' : ''}`, function (done) { + const [width, height] = (angle === 45 ? [742, 742] : [inputWidth, inputHeight]).map((x) => doResize ? Math.floor(x / 1.875) : x); + const [expectedWidth, expectedHeight] = angle % 180 === 0 ? [width, height] : [height, width]; + + const img = sharp(input, options); + rotateMethod === 'rotate' && img.rotate(); + rotateMethod === 'autoOrient' && img.autoOrient(); + + img.rotate(angle); + doResize && img.resize(expectedWidth); + + img.toBuffer(function (err, data, info) { + if (err) throw err; + assert.strictEqual(info.width, expectedWidth); + assert.strictEqual(info.height, expectedHeight); + fixtures.assertSimilar(expectedOutput, data, done); + }); + }); + }); + + [[true, true], [true, false], [false, true]].forEach(function ([flip, flop]) { + const [inputWidth, inputHeight] = orientation === 'Landscape' ? [600, 450] : [450, 600]; + const flipFlopFileName = [flip && 'flip', flop && 'flop'].filter(Boolean).join('_'); + const flipFlopTestName = [flip && 'flip', flop && 'flop'].filter(Boolean).join(' & '); + it(`${orientation} image with EXIF Orientation ${exifTag}: Auto-rotate then ${flipFlopTestName} ${doResize ? 'and resize' : ''}`, function (done) { + const expectedOutput = fixtures.expected(`${orientation}_${exifTag}_${flipFlopFileName}-out.jpg`); + + const img = sharp(input, options); + + rotateMethod === 'rotate' && img.rotate(); + rotateMethod === 'autoOrient' && img.autoOrient(); + + flip && img.flip(); + flop && img.flop(); + doResize && img.resize(orientation === 'Landscape' ? 320 : 240); + + img.toBuffer(function (err, data, info) { + if (err) throw err; + assert.strictEqual(info.width, inputWidth / (doResize ? 1.875 : 1)); + assert.strictEqual(info.height, inputHeight / (doResize ? 1.875 : 1)); + fixtures.assertSimilar(expectedOutput, data, done); + }); + }); }); }); });