Allow withMetadata to set density #967

This commit is contained in:
Lovell Fuller
2021-04-17 13:46:54 +01:00
parent 8c0c01c702
commit 4237f5520f
9 changed files with 85 additions and 6 deletions

View File

@@ -231,6 +231,7 @@ const Sharp = function (input, options) {
streamOut: false,
withMetadata: false,
withMetadataOrientation: -1,
withMetadataDensity: 0,
withMetadataIcc: '',
withMetadataStrs: {},
resolveWithObject: false,

View File

@@ -150,7 +150,7 @@ function toBuffer (options, callback) {
*
* @example
* // Set "IFD0-Copyright" in output EXIF metadata
* await sharp(input)
* const data = await sharp(input)
* .withMetadata({
* exif: {
* IFD0: {
@@ -160,10 +160,17 @@ function toBuffer (options, callback) {
* })
* .toBuffer();
*
* * @example
* // Set output metadata to 96 DPI
* const data = await sharp(input)
* .withMetadata({ density: 96 })
* .toBuffer();
*
* @param {Object} [options]
* @param {number} [options.orientation] value between 1 and 8, used to update the EXIF `Orientation` tag.
* @param {string} [options.icc] filesystem path to output ICC profile, defaults to sRGB.
* @param {Object<Object>} [options.exif={}] Object keyed by IFD0, IFD1 etc. of key/value string pairs to write as EXIF data.
* @param {number} [options.density] Number of pixels per inch (DPI).
* @returns {Sharp}
* @throws {Error} Invalid parameters
*/
@@ -177,6 +184,13 @@ function withMetadata (options) {
throw is.invalidParameterError('orientation', 'integer between 1 and 8', options.orientation);
}
}
if (is.defined(options.density)) {
if (is.number(options.density) && options.density > 0) {
this.options.withMetadataDensity = options.density;
} else {
throw is.invalidParameterError('density', 'positive number', options.density);
}
}
if (is.defined(options.icc)) {
if (is.string(options.icc)) {
this.options.withMetadataIcc = options.icc;