Skip to content

Commit 8ee8d27

Browse files
committed
Provide XMP as a string, as well as a Buffer, where possible
1 parent 4e3f379 commit 8ee8d27

File tree

5 files changed

+12
-0
lines changed

5 files changed

+12
-0
lines changed

docs/src/content/docs/changelog.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ Requires libvips v8.17.0
2020

2121
* Add support for RAW digital camera image input. Requires libvips compiled with libraw support.
2222

23+
* Provide XMP metadata as a string, as well as a Buffer, where possible.
24+
2325
* Add `pageHeight` option to `create` and `raw` input for animated images.
2426
[#3236](https://github.com/lovell/sharp/issues/3236)
2527

lib/index.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1254,6 +1254,8 @@ declare namespace sharp {
12541254
iptc?: Buffer | undefined;
12551255
/** Buffer containing raw XMP data, if present */
12561256
xmp?: Buffer | undefined;
1257+
/** String containing XMP data, if valid UTF-8 */
1258+
xmpAsString?: string | undefined;
12571259
/** Buffer containing raw TIFFTAG_PHOTOSHOP data, if present */
12581260
tifftagPhotoshop?: Buffer | undefined;
12591261
/** The encoder used to compress an HEIF file, `av1` (AVIF) or `hevc` (HEIC) */

lib/input.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -605,6 +605,7 @@ function _isStreamInput () {
605605
* - `icc`: Buffer containing raw [ICC](https://www.npmjs.com/package/icc) profile data, if present
606606
* - `iptc`: Buffer containing raw IPTC data, if present
607607
* - `xmp`: Buffer containing raw XMP data, if present
608+
* - `xmpAsString`: String containing XMP data, if valid UTF-8.
608609
* - `tifftagPhotoshop`: Buffer containing raw TIFFTAG_PHOTOSHOP data, if present
609610
* - `formatMagick`: String containing format for images loaded via *magick
610611
* - `comments`: Array of keyword/text pairs representing PNG text blocks, if present.

src/metadata.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,10 @@ class MetadataWorker : public Napi::AsyncWorker {
262262
}
263263
if (baton->xmpLength > 0) {
264264
info.Set("xmp", Napi::Buffer<char>::NewOrCopy(env, baton->xmp, baton->xmpLength, sharp::FreeCallback));
265+
if (g_utf8_validate(static_cast<char const *>(baton->xmp), baton->xmpLength, nullptr)) {
266+
info.Set("xmpAsString",
267+
Napi::String::New(env, static_cast<char const *>(baton->xmp), baton->xmpLength));
268+
}
265269
}
266270
if (baton->tifftagPhotoshopLength > 0) {
267271
info.Set("tifftagPhotoshop",

test/unit/metadata.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ describe('Image metadata', function () {
8282
assert.strictEqual(true, metadata.xmp instanceof Buffer);
8383
assert.strictEqual(12466, metadata.xmp.byteLength);
8484
assert.strictEqual(metadata.xmp.indexOf(Buffer.from('<?xpacket begin="')), 0);
85+
assert(metadata.xmpAsString.startsWith('<?xpacket begin="" id="W5M0MpCehiHzreSzNTczkc9d"?>'));
8586
done();
8687
});
8788
});
@@ -106,6 +107,8 @@ describe('Image metadata', function () {
106107
assert.strictEqual(3248, metadata.autoOrient.height);
107108
assert.strictEqual('undefined', typeof metadata.exif);
108109
assert.strictEqual('undefined', typeof metadata.icc);
110+
assert.strictEqual('undefined', typeof metadata.xmp);
111+
assert.strictEqual('undefined', typeof metadata.xmpAsString);
109112
assert.strictEqual('inch', metadata.resolutionUnit);
110113
done();
111114
});

0 commit comments

Comments
 (0)