Created
July 24, 2022 03:06
-
-
Save aredden/ff59598592bab1b053a19ad614c7619f to your computer and use it in GitHub Desktop.
Functions for reading or writing png exif metadata
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { ExifTool, Tags } = require('exiftool-vendored'); | |
/** | |
* Function to write makernote and exif data to a PNG file. | |
* @param {string} filePath - Path to the PNG file. | |
* @param {string} makerNote - Makernote exif key data to write. | |
* @param {string} comment - Comment exif key data to write. | |
* @return {Promise<boolean>} - True if successful, false otherwise. | |
*/ | |
export const writeMetadata = async (filePath, makerNote, comment) => { | |
const tool = new ExifTool(); | |
let success = false; | |
try { | |
// Open the file with ExifTool and write the metadata. | |
await tool.write(filePath, { | |
MakerNoteUnknownText: makerNote, | |
UserComment: comment, | |
}); | |
success = true; | |
} catch (err) { | |
console.error(err); | |
success = false; | |
} finally { | |
await tool.end(true); | |
return success; | |
} | |
}; | |
/** | |
* Function to read makernote and exif data from a PNG file. | |
* @param {string} filePath - Path to the PNG file. | |
* @returns {Promise<Tags> | null} key value object containing exif data or null if error occurs. | |
*/ | |
export const readMetadata = async (filePath) => { | |
const tool = new ExifTool(); | |
try { | |
// Read the metadata from the file. | |
const metadata = await tool.read(filePath); | |
// Gracefully end the exiftool process. | |
await tool.end(true); | |
return metadata; | |
} catch (err) { | |
console.error(`Failed to read exif data: ${err}`); | |
// Gracefully end the exiftool process. | |
await tool.end(true); | |
return null; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment