mirror of
https://github.com/lovell/sharp.git
synced 2025-07-26 01:32:27 +02:00
Add appliedOrientation
obj to metadata
This commit is contained in:
parent
ab884e13cf
commit
087798d265
@ -74,13 +74,11 @@ image
|
||||
```js
|
||||
// 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
|
||||
```
|
||||
|
||||
|
||||
|
7
lib/index.d.ts
vendored
7
lib/index.d.ts
vendored
@ -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 */
|
||||
|
10
lib/input.js
10
lib/input.js
@ -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}
|
||||
|
@ -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));
|
||||
}
|
||||
|
@ -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);
|
||||
@ -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();
|
||||
@ -218,7 +222,11 @@ describe('Image metadata', function () {
|
||||
isPalette: false,
|
||||
isProgressive: false,
|
||||
space: 'b-w',
|
||||
width: 32
|
||||
width: 32,
|
||||
appliedOrientation: {
|
||||
width: 32,
|
||||
height: 32
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
@ -239,7 +247,11 @@ describe('Image metadata', function () {
|
||||
isPalette: false,
|
||||
isProgressive: false,
|
||||
space: 'grey16',
|
||||
width: 32
|
||||
width: 32,
|
||||
appliedOrientation: {
|
||||
width: 32,
|
||||
height: 32
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
@ -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
|
||||
@ -926,7 +942,11 @@ describe('Image metadata', function () {
|
||||
pagePrimary: 0,
|
||||
compression: 'av1',
|
||||
hasProfile: false,
|
||||
hasAlpha: false
|
||||
hasAlpha: false,
|
||||
appliedOrientation: {
|
||||
width: 2048,
|
||||
height: 858
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user