Add appliedOrientation obj to metadata

This commit is contained in:
Don Denton 2024-12-08 21:00:19 -05:00
parent ab884e13cf
commit 087798d265
5 changed files with 47 additions and 15 deletions

View File

@ -74,13 +74,11 @@ image
```js ```js
// Based on EXIF rotation metadata, get the right-side-up width and height: // 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 }) { // These will match metadata.width and metadata.height, but with EXIF
return (orientation || 0) >= 5 // orientation applied if necessary.
? { width: height, height: width } const {width, height} = metadata.appliedOrientation
: { width, height };
}
``` ```

7
lib/index.d.ts vendored
View File

@ -1119,6 +1119,13 @@ declare namespace sharp {
width?: number | undefined; width?: number | undefined;
/** Number of pixels high (EXIF orientation is not taken into consideration) */ /** Number of pixels high (EXIF orientation is not taken into consideration) */
height?: number | undefined; 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 */ /** Name of colour space interpretation */
space?: keyof ColourspaceEnum | undefined; space?: keyof ColourspaceEnum | undefined;
/** Number of bands e.g. 3 for sRGB, 4 for CMYK */ /** Number of bands e.g. 3 for sRGB, 4 for CMYK */

View File

@ -486,13 +486,11 @@ function _isStreamInput () {
* @example * @example
* // Based on EXIF rotation metadata, get the right-side-up width and height: * // 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 }) { * // These will match metadata.width and metadata.height, but with EXIF
* return (orientation || 0) >= 5 * // orientation applied if necessary.
* ? { width: height, height: width } * const {width, height} = metadata.appliedOrientation
* : { width, height };
* }
* *
* @param {Function} [callback] - called with the arguments `(err, metadata)` * @param {Function} [callback] - called with the arguments `(err, metadata)`
* @returns {Promise<Object>|Sharp} * @returns {Promise<Object>|Sharp}

View File

@ -242,6 +242,15 @@ class MetadataWorker : public Napi::AsyncWorker {
if (baton->orientation > 0) { if (baton->orientation > 0) {
info.Set("orientation", baton->orientation); 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) { if (baton->exifLength > 0) {
info.Set("exif", Napi::Buffer<char>::NewOrCopy(env, baton->exif, baton->exifLength, sharp::FreeCallback)); info.Set("exif", Napi::Buffer<char>::NewOrCopy(env, baton->exif, baton->exifLength, sharp::FreeCallback));
} }

View File

@ -102,6 +102,8 @@ describe('Image metadata', function () {
assert.strictEqual(false, metadata.hasProfile); assert.strictEqual(false, metadata.hasProfile);
assert.strictEqual(false, metadata.hasAlpha); assert.strictEqual(false, metadata.hasAlpha);
assert.strictEqual(1, metadata.orientation); 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.exif);
assert.strictEqual('undefined', typeof metadata.icc); assert.strictEqual('undefined', typeof metadata.icc);
assert.strictEqual('inch', metadata.resolutionUnit); assert.strictEqual('inch', metadata.resolutionUnit);
@ -148,6 +150,8 @@ describe('Image metadata', function () {
assert.strictEqual(false, metadata.hasProfile); assert.strictEqual(false, metadata.hasProfile);
assert.strictEqual(false, metadata.hasAlpha); assert.strictEqual(false, metadata.hasAlpha);
assert.strictEqual('undefined', typeof metadata.orientation); 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.exif);
assert.strictEqual('undefined', typeof metadata.icc); assert.strictEqual('undefined', typeof metadata.icc);
done(); done();
@ -218,7 +222,11 @@ describe('Image metadata', function () {
isPalette: false, isPalette: false,
isProgressive: false, isProgressive: false,
space: 'b-w', space: 'b-w',
width: 32 width: 32,
appliedOrientation: {
width: 32,
height: 32
}
}); });
}); });
@ -239,7 +247,11 @@ describe('Image metadata', function () {
isPalette: false, isPalette: false,
isProgressive: false, isProgressive: false,
space: 'grey16', space: 'grey16',
width: 32 width: 32,
appliedOrientation: {
width: 32,
height: 32
}
}); });
}); });
@ -601,6 +613,10 @@ describe('Image metadata', function () {
if (err) throw err; if (err) throw err;
assert.strictEqual(true, metadata.hasProfile); assert.strictEqual(true, metadata.hasProfile);
assert.strictEqual(8, metadata.orientation); 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('object', typeof metadata.exif);
assert.strictEqual(true, metadata.exif instanceof Buffer); assert.strictEqual(true, metadata.exif instanceof Buffer);
// EXIF // EXIF
@ -926,7 +942,11 @@ describe('Image metadata', function () {
pagePrimary: 0, pagePrimary: 0,
compression: 'av1', compression: 'av1',
hasProfile: false, hasProfile: false,
hasAlpha: false hasAlpha: false,
appliedOrientation: {
width: 2048,
height: 858
}
}); });
}); });