lib/output.js
function toFile (fileOut, callback) {
  if (!fileOut || fileOut.length === 0) {
    const errOutputInvalid = new Error('Missing output file path');
    if (is.fn(callback)) {
      callback(errOutputInvalid);
    } else {
      return Promise.reject(errOutputInvalid);
    }
  } else {
    if (this.options.input.file === fileOut) {
      const errOutputIsInput = new Error('Cannot use same file for input and output');
      if (is.fn(callback)) {
        callback(errOutputIsInput);
      } else {
        return Promise.reject(errOutputIsInput);
      }
    } else {
      this.options.fileOut = fileOut;
      return this._pipeline(callback);
    }
  }
  return this;
}
function toFile (fileOut, callback) {
  if (!fileOut || fileOut.length === 0) {
    const errOutputInvalid = new Error('Missing output file path');
    if (is.fn(callback)) {
      callback(errOutputInvalid);
    } else {
      return Promise.reject(errOutputInvalid);
    }
  } else {
    if (this.options.input.file === fileOut) {
      const errOutputIsInput = new Error('Cannot use same file for input and output');
      if (is.fn(callback)) {
        callback(errOutputIsInput);
      } else {
        return Promise.reject(errOutputIsInput);
      }
    } else {
      this.options.fileOut = fileOut;
      return this._pipeline(callback);
    }
  }
  return this;
}
function toFile ( fileOut , callback ) { if ( ! fileOut || fileOut . length === 0 ) { const errOutputInvalid = new Error ( 'Missing output file path' ) ; if ( is . fn ( callback ) ) { callback ( errOutputInvalid ) ; } else { return Promise . reject ( errOutputInvalid ) ; } } else { if ( this . options . input . file === fileOut ) { const errOutputIsInput = new Error ( 'Cannot use same file for input and output' ) ; if ( is . fn ( callback ) ) { callback ( errOutputIsInput ) ; } else { return Promise . reject ( errOutputIsInput ) ; } } else { this . options . fileOut = fileOut ; return this . _pipeline ( callback ) ; } } return this ; }
Write output image data to a file.

If an explicit output format is not selected, it will be inferred from the extension,
with JPEG, PNG, WebP, TIFF, DZI, and libvips' V format supported.
Note that raw pixel data is only supported for buffer output.

A `Promise` is returned when `callback` is not provided.

@example
sharp(input)
.toFile('output.png', (err, info) => { ... });

@example
sharp(input)
.toFile('output.png')
.then(info => { ... })
.catch(err => { ... });

@param {String} fileOut - the path to write the image data to.
@param {Function} [callback] - called on completion with two arguments `(err, info)`.
`info` contains the output image `format`, `size` (bytes), `width`, `height`,
`channels` and `premultiplied` (indicating if premultiplication was used).
When using a crop strategy also contains `cropOffsetLeft` and `cropOffsetTop`.
@returns {Promise<Object>} - when no callback is provided
@throws {Error} Invalid parameters
Write output image data to a file .