From 703d90e6630626770da2fd9ddbb711110c4e2109 Mon Sep 17 00:00:00 2001 From: Lovell Fuller Date: Mon, 16 Dec 2019 21:15:49 +0000 Subject: [PATCH] Docs: add example of using metadata and resize to scale --- docs/api-resize.md | 9 +++++++++ lib/resize.js | 8 ++++++++ 2 files changed, 17 insertions(+) diff --git a/docs/api-resize.md b/docs/api-resize.md index 6c76a6f6..20945e97 100644 --- a/docs/api-resize.md +++ b/docs/api-resize.md @@ -114,6 +114,15 @@ sharp(input) }); ``` +```javascript +const scaleByHalf = await sharp(input) + .metadata() + .then(({ width }) => sharp(input) + .resize(Math.round(width * 0.5)) + .toBuffer() + ); +``` + - Throws **[Error][13]** Invalid parameters Returns **Sharp** diff --git a/lib/resize.js b/lib/resize.js index 0aafd5af..13e881fd 100644 --- a/lib/resize.js +++ b/lib/resize.js @@ -173,6 +173,14 @@ const mapFitToCanvas = { * // and no larger than the input image * }); * + * @example + * const scaleByHalf = await sharp(input) + * .metadata() + * .then(({ width }) => sharp(input) + * .resize(Math.round(width * 0.5)) + * .toBuffer() + * ); + * * @param {Number} [width] - pixels wide the resultant image should be. Use `null` or `undefined` to auto-scale the width to match the height. * @param {Number} [height] - pixels high the resultant image should be. Use `null` or `undefined` to auto-scale the height to match the width. * @param {Object} [options]