Skip to content

Commit

Permalink
Add appliedOrientation obj to metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
happycollision committed Dec 9, 2024
1 parent ab884e1 commit 4acc3a7
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 9 deletions.
7 changes: 7 additions & 0 deletions lib/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1119,6 +1119,13 @@ declare namespace sharp {
width?: number | undefined;
/** Number of pixels high (EXIF orientation is not taken into consideration) */
height?: number | undefined;
/** Any changed metadata after the image orientation is applied. */
appliedOrientation: {
/** Number of pixels wide (EXIF orientation is taken into consideration) */
width: number;
/** Number of pixels high (EXIF orientation is taken into consideration) */
height: number;
};
/** Name of colour space interpretation */
space?: keyof ColourspaceEnum | undefined;
/** Number of bands e.g. 3 for sRGB, 4 for CMYK */
Expand Down
10 changes: 4 additions & 6 deletions lib/input.js
Original file line number Diff line number Diff line change
Expand Up @@ -486,13 +486,11 @@ function _isStreamInput () {
* @example
* // Based on EXIF rotation metadata, get the right-side-up width and height:
*
* const size = getNormalSize(await sharp(input).metadata());
* const metadata = await sharp(input).metadata()
*
* function getNormalSize({ width, height, orientation }) {
* return (orientation || 0) >= 5
* ? { width: height, height: width }
* : { width, height };
* }
* // These will match metadata.width and metadata.height, but with EXIF
* // orientation applied if necessary.
* const {width, height} = metadata.appliedOrientation
*
* @param {Function} [callback] - called with the arguments `(err, metadata)`
* @returns {Promise<Object>|Sharp}
Expand Down
9 changes: 9 additions & 0 deletions src/metadata.cc
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,15 @@ class MetadataWorker : public Napi::AsyncWorker {
if (baton->orientation > 0) {
info.Set("orientation", baton->orientation);
}
Napi::Object appliedOrientation = Napi::Object::New(env);
info.Set("appliedOrientation", appliedOrientation);
if (baton->orientation >= 5) {
appliedOrientation.Set("width", baton->height);
appliedOrientation.Set("height", baton->width);
} else {
appliedOrientation.Set("width", baton->width);
appliedOrientation.Set("height", baton->height);
}
if (baton->exifLength > 0) {
info.Set("exif", Napi::Buffer<char>::NewOrCopy(env, baton->exif, baton->exifLength, sharp::FreeCallback));
}
Expand Down
26 changes: 23 additions & 3 deletions test/unit/metadata.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ describe('Image metadata', function () {
assert.strictEqual(false, metadata.hasProfile);
assert.strictEqual(false, metadata.hasAlpha);
assert.strictEqual(1, metadata.orientation);
assert.strictEqual(2464, metadata.appliedOrientation.width);
assert.strictEqual(3248, metadata.appliedOrientation.height);
assert.strictEqual('undefined', typeof metadata.exif);
assert.strictEqual('undefined', typeof metadata.icc);
assert.strictEqual('inch', metadata.resolutionUnit);
Expand Down Expand Up @@ -148,6 +150,8 @@ describe('Image metadata', function () {
assert.strictEqual(false, metadata.hasProfile);
assert.strictEqual(false, metadata.hasAlpha);
assert.strictEqual('undefined', typeof metadata.orientation);
assert.strictEqual(2809, metadata.appliedOrientation.width);
assert.strictEqual(2074, metadata.appliedOrientation.height);
assert.strictEqual('undefined', typeof metadata.exif);
assert.strictEqual('undefined', typeof metadata.icc);
done();
Expand Down Expand Up @@ -218,7 +222,11 @@ describe('Image metadata', function () {
isPalette: false,
isProgressive: false,
space: 'b-w',
width: 32
width: 32,
appliedOrientation: {
width: 32,
height: 32
}
});
});

Expand All @@ -239,7 +247,11 @@ describe('Image metadata', function () {
isPalette: false,
isProgressive: false,
space: 'grey16',
width: 32
width: 32,
appliedOrientation: {
width: 32,
height: 32
}
});
});

Expand Down Expand Up @@ -601,6 +613,10 @@ describe('Image metadata', function () {
if (err) throw err;
assert.strictEqual(true, metadata.hasProfile);
assert.strictEqual(8, metadata.orientation);
assert.strictEqual(320, metadata.width);
assert.strictEqual(240, metadata.height);
assert.strictEqual(240, metadata.appliedOrientation.width);
assert.strictEqual(320, metadata.appliedOrientation.height);
assert.strictEqual('object', typeof metadata.exif);
assert.strictEqual(true, metadata.exif instanceof Buffer);
// EXIF
Expand Down Expand Up @@ -926,7 +942,11 @@ describe('Image metadata', function () {
pagePrimary: 0,
compression: 'av1',
hasProfile: false,
hasAlpha: false
hasAlpha: false,
appliedOrientation: {
width: 2048,
height: 858
}
});
});

Expand Down

0 comments on commit 4acc3a7

Please sign in to comment.