简体   繁体   中英

Keep rotation metadata with sharp node.js

I am working on an app that uses sharp for processing photos. Currently, when we resize and then write to buffer an image with sharp resize and toBuffer, by default the two of them wipe the EXIF data. We want to remove all metadata except for orientation (if it exists).

I've read sharp's documentation and withMetadata seems to be the candidate to achieve what I want, the problem is that withMetadata preserves all metadata and I just want the orientation of the original image.

The original line of code is

await this.sharpInstance.resize(maxDimension, maxDimension).max().toBuffer()

I think that what I want is something like

await this.sharpInstance.withMetadata().resize(maxDimension, maxDimension).max().withMetadata().toBuffer()

but only for orientation metadata.

I would really appreciated some help to solve this. Thanks very much!

Have you tried await this.sharpInstance.resize(maxDimension, maxDimension).max().withMetadata().toBuffer() as Sharp docs about withMetadata .

Edited:

I got that. So as withMetadata , first we need to save the orientation metadata and then assign to output buffer later:

// First, save the orientation for later use
const { orientation } = await this.sharpInstance.metadata();

// Then output to Buffer without metadata
// then create another Sharp instance 
// from output Buffer which doesn't have metadata
// and assign saved orientation along with it
sharp(this.sharpInstance.toBuffer())
    .withMetadata({ orientation }).toBuffer();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM