Skip to content

Commit 4c4596d

Browse files
Add appliedOrientation obj to metadata
1 parent 9212282 commit 4c4596d

File tree

4 files changed

+43
-9
lines changed

4 files changed

+43
-9
lines changed

lib/index.d.ts

+7
Original file line numberDiff line numberDiff line change
@@ -1119,6 +1119,13 @@ declare namespace sharp {
11191119
width?: number | undefined;
11201120
/** Number of pixels high (EXIF orientation is not taken into consideration) */
11211121
height?: number | undefined;
1122+
/** Any changed metadata after the image orientation is applied. */
1123+
appliedOrientation: {
1124+
/** Number of pixels wide (EXIF orientation is taken into consideration) */
1125+
width: number;
1126+
/** Number of pixels high (EXIF orientation is taken into consideration) */
1127+
height: number;
1128+
};
11221129
/** Name of colour space interpretation */
11231130
space?: keyof ColourspaceEnum | undefined;
11241131
/** Number of bands e.g. 3 for sRGB, 4 for CMYK */

lib/input.js

+4-6
Original file line numberDiff line numberDiff line change
@@ -486,13 +486,11 @@ function _isStreamInput () {
486486
* @example
487487
* // Based on EXIF rotation metadata, get the right-side-up width and height:
488488
*
489-
* const size = getNormalSize(await sharp(input).metadata());
489+
* const metadata = await sharp(input).metadata()
490490
*
491-
* function getNormalSize({ width, height, orientation }) {
492-
* return (orientation || 0) >= 5
493-
* ? { width: height, height: width }
494-
* : { width, height };
495-
* }
491+
* // These will match metadata.width and metadata.height, but with EXIF
492+
* // orientation applied if necessary.
493+
* const {width, height} = metadata.appliedOrientation
496494
*
497495
* @param {Function} [callback] - called with the arguments `(err, metadata)`
498496
* @returns {Promise<Object>|Sharp}

src/metadata.cc

+9
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,15 @@ class MetadataWorker : public Napi::AsyncWorker {
242242
if (baton->orientation > 0) {
243243
info.Set("orientation", baton->orientation);
244244
}
245+
Napi::Object appliedOrientation = Napi::Object::New(env);
246+
info.Set("appliedOrientation", appliedOrientation);
247+
if (baton->orientation >= 5) {
248+
appliedOrientation.Set("width", baton->height);
249+
appliedOrientation.Set("height", baton->width);
250+
} else {
251+
appliedOrientation.Set("width", baton->width);
252+
appliedOrientation.Set("height", baton->height);
253+
}
245254
if (baton->exifLength > 0) {
246255
info.Set("exif", Napi::Buffer<char>::NewOrCopy(env, baton->exif, baton->exifLength, sharp::FreeCallback));
247256
}

test/unit/metadata.js

+23-3
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,8 @@ describe('Image metadata', function () {
102102
assert.strictEqual(false, metadata.hasProfile);
103103
assert.strictEqual(false, metadata.hasAlpha);
104104
assert.strictEqual(1, metadata.orientation);
105+
assert.strictEqual(2464, metadata.appliedOrientation.width);
106+
assert.strictEqual(3248, metadata.appliedOrientation.height);
105107
assert.strictEqual('undefined', typeof metadata.exif);
106108
assert.strictEqual('undefined', typeof metadata.icc);
107109
assert.strictEqual('inch', metadata.resolutionUnit);
@@ -148,6 +150,8 @@ describe('Image metadata', function () {
148150
assert.strictEqual(false, metadata.hasProfile);
149151
assert.strictEqual(false, metadata.hasAlpha);
150152
assert.strictEqual('undefined', typeof metadata.orientation);
153+
assert.strictEqual(2809, metadata.appliedOrientation.width);
154+
assert.strictEqual(2074, metadata.appliedOrientation.height);
151155
assert.strictEqual('undefined', typeof metadata.exif);
152156
assert.strictEqual('undefined', typeof metadata.icc);
153157
done();
@@ -218,7 +222,11 @@ describe('Image metadata', function () {
218222
isPalette: false,
219223
isProgressive: false,
220224
space: 'b-w',
221-
width: 32
225+
width: 32,
226+
appliedOrientation: {
227+
width: 32,
228+
height: 32
229+
}
222230
});
223231
});
224232

@@ -239,7 +247,11 @@ describe('Image metadata', function () {
239247
isPalette: false,
240248
isProgressive: false,
241249
space: 'grey16',
242-
width: 32
250+
width: 32,
251+
appliedOrientation: {
252+
width: 32,
253+
height: 32
254+
}
243255
});
244256
});
245257

@@ -601,6 +613,10 @@ describe('Image metadata', function () {
601613
if (err) throw err;
602614
assert.strictEqual(true, metadata.hasProfile);
603615
assert.strictEqual(8, metadata.orientation);
616+
assert.strictEqual(320, metadata.width);
617+
assert.strictEqual(240, metadata.height);
618+
assert.strictEqual(240, metadata.appliedOrientation.width);
619+
assert.strictEqual(320, metadata.appliedOrientation.height);
604620
assert.strictEqual('object', typeof metadata.exif);
605621
assert.strictEqual(true, metadata.exif instanceof Buffer);
606622
// EXIF
@@ -926,7 +942,11 @@ describe('Image metadata', function () {
926942
pagePrimary: 0,
927943
compression: 'av1',
928944
hasProfile: false,
929-
hasAlpha: false
945+
hasAlpha: false,
946+
appliedOrientation: {
947+
width: 2048,
948+
height: 858
949+
}
930950
});
931951
});
932952

0 commit comments

Comments
 (0)