diff --git a/docs/changelog.md b/docs/changelog.md index 23c1b34a..5c3c3f3c 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -14,6 +14,9 @@ Requires libvips v8.14.3 [#3755](https://github.com/lovell/sharp/pull/3755) [@kleisauke](https://github.com/kleisauke) +* Ensure resize with a `fit` of `inside` respects 90/270 degree rotation. + [#3756](https://github.com/lovell/sharp/issues/3756) + * TypeScript: Ensure `minSize` property of `WebpOptions` is boolean. [#3758](https://github.com/lovell/sharp/pull/3758) [@sho-xizz](https://github.com/sho-xizz) diff --git a/src/common.cc b/src/common.cc index 075c6008..54354edb 100644 --- a/src/common.cc +++ b/src/common.cc @@ -968,6 +968,9 @@ namespace sharp { if (swap && canvas != Canvas::IGNORE_ASPECT) { // Swap input width and height when requested. std::swap(width, height); + if (canvas == Canvas::MAX) { + std::swap(targetWidth, targetHeight); + } } double hshrink = 1.0; diff --git a/test/unit/rotate.js b/test/unit/rotate.js index 86e9a8a6..b5758082 100644 --- a/test/unit/rotate.js +++ b/test/unit/rotate.js @@ -489,4 +489,28 @@ describe('Rotation', function () { .timeout({ seconds: 5 }) .toBuffer() ); + + it('Rotate 90 then resize with inside fit', async () => { + const data = await sharp({ create: { width: 16, height: 8, channels: 3, background: 'red' } }) + .rotate(90) + .resize({ width: 6, fit: 'inside' }) + .png({ compressionLevel: 0 }) + .toBuffer(); + + const { width, height } = await sharp(data).metadata(); + assert.strictEqual(width, 6); + assert.strictEqual(height, 12); + }); + + it('Resize with inside fit then rotate 90', async () => { + const data = await sharp({ create: { width: 16, height: 8, channels: 3, background: 'red' } }) + .resize({ width: 6, fit: 'inside' }) + .rotate(90) + .png({ compressionLevel: 0 }) + .toBuffer(); + + const { width, height } = await sharp(data).metadata(); + assert.strictEqual(width, 3); + assert.strictEqual(height, 6); + }); });