lib/resize.js
function extract (options) {
  const suffix = this.options.width === -1 && this.options.height === -1 ? 'Pre' : 'Post';
  ['left', 'top', 'width', 'height'].forEach(function (name) {
    const value = options[name];
    if (is.integer(value) && value >= 0) {
      this.options[name + (name === 'left' || name === 'top' ? 'Offset' : '') + suffix] = value;
    } else {
      throw new Error('Non-integer value for ' + name + ' of ' + value);
    }
  }, this);
  // Ensure existing rotation occurs before pre-resize extraction
  if (suffix === 'Pre' && ((this.options.angle % 360) !== 0 || this.options.useExifOrientation === true)) {
    this.options.rotateBeforePreExtract = true;
  }
  return this;
}
function extract (options) {
  const suffix = this.options.width === -1 && this.options.height === -1 ? 'Pre' : 'Post';
  ['left', 'top', 'width', 'height'].forEach(function (name) {
    const value = options[name];
    if (is.integer(value) && value >= 0) {
      this.options[name + (name === 'left' || name === 'top' ? 'Offset' : '') + suffix] = value;
    } else {
      throw new Error('Non-integer value for ' + name + ' of ' + value);
    }
  }, this);
  // Ensure existing rotation occurs before pre-resize extraction
  if (suffix === 'Pre' && ((this.options.angle % 360) !== 0 || this.options.useExifOrientation === true)) {
    this.options.rotateBeforePreExtract = true;
  }
  return this;
}
function extract ( options ) { const suffix = this . options . width === - 1 && this . options . height === - 1 ? 'Pre' : 'Post' ; [ 'left' , 'top' , 'width' , 'height' ] . forEach ( function ( name ) { const value = options [ name ] ; if ( is . integer ( value ) && value >= 0 ) { this . options [ name + ( name === 'left' || name === 'top' ? 'Offset' : '' ) + suffix ] = value ; } else { throw new Error ( 'Non-integer value for ' + name + ' of ' + value ) ; } } , this ) ; // Ensure existing rotation occurs before pre-resize extraction if ( suffix === 'Pre' && ( ( this . options . angle % 360 ) !== 0 || this . options . useExifOrientation === true ) ) { this . options . rotateBeforePreExtract = true ; } return this ; }
Extract a region of the image.

- Use `extract` before `resize` for pre-resize extraction.
- Use `extract` after `resize` for post-resize extraction.
- Use `extract` before and after for both.

@example
sharp(input)
.extract({ left: left, top: top, width: width, height: height })
.toFile(output, function(err) {
// Extract a region of the input image, saving in the same format.
});
@example
sharp(input)
.extract({ left: leftOffsetPre, top: topOffsetPre, width: widthPre, height: heightPre })
.resize(width, height)
.extract({ left: leftOffsetPost, top: topOffsetPost, width: widthPost, height: heightPost })
.toFile(output, function(err) {
// Extract a region, resize, then extract from the resized image
});

@param {Object} options - describes the region to extract using integral pixel values
@param {Number} options.left - zero-indexed offset from left edge
@param {Number} options.top - zero-indexed offset from top edge
@param {Number} options.width - width of region to extract
@param {Number} options.height - height of region to extract
@returns {Sharp}
@throws {Error} Invalid parameters
Extract a region of the image .