From de7926e46f0a761fe23a132f8beca503a754a392 Mon Sep 17 00:00:00 2001 From: Thiago Pisani Date: Thu, 4 Jun 2020 13:12:08 -0300 Subject: [PATCH] Add support for image trimming --- index.js | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/index.js b/index.js index 00f8127..c0c2d21 100644 --- a/index.js +++ b/index.js @@ -21,6 +21,7 @@ function Thumbor(securityKey, thumborServerUrl) { this.valignValue = null; this.cropValues = null; this.meta = false; + this.trimValue = null; this.filtersCalls = []; } @@ -34,6 +35,9 @@ Thumbor.prototype = { CENTER: 'center', LEFT: 'left', + TOP_LEFT: 'top-left', + BOTTOM_RIGHT: 'bottom-right', + /** * Set path of image * @param {String} imagePath [description] @@ -75,6 +79,10 @@ Thumbor.prototype = { parts.push('meta'); } + if (this.trimValue) { + parts.push(this.trimValue); + } + if (this.cropValues) { parts.push( this.cropValues.left + @@ -130,6 +138,34 @@ Thumbor.prototype = { return parts; }, + /** + * Removes surrounding space in images using top-left pixel color unless specified otherwise. + * Overrides any previous call to `trim`. + * @param {String} orientation 'top-left', 'bottom-right' + * @param {Integer} tolerance between 0 and 442 + */ + trim: function(orientation, tolerance) { + var trimOpts = ['trim']; + + if (orientation !== undefined) { + if (orientation === this.TOP_LEFT || orientation === this.BOTTOM_RIGHT) { + trimOpts.push(orientation); + } else { + throw new Error('Orientation must be top-left or bottom-right.'); + } + } + + if (tolerance !== undefined) { + if (tolerance > 0 && tolerance < 442) { + trimOpts.push(tolerance); + } else { + throw new Error('Tolerance must be between 0 and 442.'); + } + } + + this.trimValue = trimOpts.join(':'); + return this; + }, /** * Resize the image to the specified dimensions. Overrides any previous call * to `fitIn` or `resize`.