Replace write() with toFile() to allow streams in the future #30

This commit is contained in:
Lovell Fuller 2014-05-29 19:54:43 +01:00
parent 4de9a2435f
commit 906311d403
4 changed files with 37 additions and 31 deletions

View File

@ -75,7 +75,7 @@ var sharp = require('sharp');
```
```javascript
sharp('input.jpg').resize(300, 200).write('output.jpg', function(err) {
sharp('input.jpg').resize(300, 200).toFile('output.jpg', function(err) {
if (err) {
throw err;
}
@ -103,7 +103,7 @@ sharp('input.png').rotate(180).resize(300).sharpen().quality(90).webp(function(e
```
```javascript
sharp(inputBuffer).resize(200, 300).embedWhite().write('output.tiff', function(err) {
sharp(inputBuffer).resize(200, 300).embedWhite().toFile('output.tiff', function(err) {
if (err) {
throw err;
}
@ -199,7 +199,7 @@ An advanced setting for the _zlib_ compression level of the lossless PNG output
An advanced setting that switches the libvips access method to `VIPS_ACCESS_SEQUENTIAL`. This will reduce memory usage and can improve performance on some systems.
### write(filename, callback)
### toFile(filename, callback)
`filename` is a String containing the filename to write the image data to. The format is inferred from the extension, with JPEG, PNG, WebP and TIFF supported.

View File

@ -120,7 +120,10 @@ Sharp.prototype.resize = function(width, height) {
return this;
};
Sharp.prototype.write = function(output, callback) {
/*
Write output image data to a file
*/
Sharp.prototype.toFile = function(output, callback) {
if (!output || output.length === 0) {
callback('Invalid output');
} else {
@ -133,6 +136,9 @@ Sharp.prototype.write = function(output, callback) {
return this;
};
// Deprecated to make way for future stream support - remove in v0.6.0
Sharp.prototype.write = Sharp.prototype.toFile;
Sharp.prototype.toBuffer = function(callback) {
return this._sharp('__input', callback);
};

View File

@ -111,7 +111,7 @@ async.series({
}).add("sharp-buffer-file", {
defer: true,
fn: function(deferred) {
sharp(inputJpgBuffer).resize(width, height).write(outputJpg, function(err) {
sharp(inputJpgBuffer).resize(width, height).toFile(outputJpg, function(err) {
if (err) {
throw err;
} else {
@ -134,7 +134,7 @@ async.series({
}).add("sharp-file-file", {
defer: true,
fn: function(deferred) {
sharp(inputJpg).resize(width, height).write(outputJpg, function(err) {
sharp(inputJpg).resize(width, height).toFile(outputJpg, function(err) {
if (err) {
throw err;
} else {
@ -263,7 +263,7 @@ async.series({
}).add("sharp-buffer-file", {
defer: true,
fn: function(deferred) {
sharp(inputPngBuffer).resize(width, height).write(outputPng, function(err) {
sharp(inputPngBuffer).resize(width, height).toFile(outputPng, function(err) {
if (err) {
throw err;
} else {
@ -286,7 +286,7 @@ async.series({
}).add("sharp-file-file", {
defer: true,
fn: function(deferred) {
sharp(inputPng).resize(width, height).write(outputPng, function(err) {
sharp(inputPng).resize(width, height).toFile(outputPng, function(err) {
if (err) {
throw err;
} else {
@ -353,7 +353,7 @@ async.series({
(new Benchmark.Suite("webp")).add("sharp-buffer-file", {
defer: true,
fn: function(deferred) {
sharp(inputWebpBuffer).resize(width, height).write(outputWebp, function(err) {
sharp(inputWebpBuffer).resize(width, height).toFile(outputWebp, function(err) {
if (err) {
throw err;
} else {
@ -376,7 +376,7 @@ async.series({
}).add("sharp-file-file", {
defer: true,
fn: function(deferred) {
sharp(inputWebp).resize(width, height).write(outputWebp, function(err) {
sharp(inputWebp).resize(width, height).toFile(outputWebp, function(err) {
if (err) {
throw err;
} else {
@ -430,7 +430,7 @@ async.series({
(new Benchmark.Suite("tiff")).add("sharp-file-file", {
defer: true,
fn: function(deferred) {
sharp(inputTiff).resize(width, height).write(outputTiff, function(err) {
sharp(inputTiff).resize(width, height).toFile(outputTiff, function(err) {
if (err) {
throw err;
} else {
@ -441,7 +441,7 @@ async.series({
}).add("sharp-file-file-sharpen", {
defer: true,
fn: function(deferred) {
sharp(inputTiff).resize(width, height).sharpen().write(outputTiff, function(err) {
sharp(inputTiff).resize(width, height).sharpen().toFile(outputTiff, function(err) {
if (err) {
throw err;
} else {
@ -452,7 +452,7 @@ async.series({
}).add("sharp-file-file-sequentialRead", {
defer: true,
fn: function(deferred) {
sharp(inputTiff).sequentialRead().resize(width, height).write(outputTiff, function(err) {
sharp(inputTiff).sequentialRead().resize(width, height).toFile(outputTiff, function(err) {
if (err) {
throw err;
} else {
@ -470,7 +470,7 @@ async.series({
(new Benchmark.Suite("gif")).add("sharp-file-file", {
defer: true,
fn: function(deferred) {
sharp(inputGif).resize(width, height).write(outputTiff, function(err) {
sharp(inputGif).resize(width, height).toFile(outputTiff, function(err) {
if (err) {
throw err;
} else {
@ -481,7 +481,7 @@ async.series({
}).add("sharp-file-file-sharpen", {
defer: true,
fn: function(deferred) {
sharp(inputGif).resize(width, height).sharpen().write(outputTiff, function(err) {
sharp(inputGif).resize(width, height).sharpen().toFile(outputTiff, function(err) {
if (err) {
throw err;
} else {
@ -492,7 +492,7 @@ async.series({
}).add("sharp-file-file-sequentialRead", {
defer: true,
fn: function(deferred) {
sharp(inputGif).sequentialRead().resize(width, height).write(outputTiff, function(err) {
sharp(inputGif).sequentialRead().resize(width, height).toFile(outputTiff, function(err) {
if (err) {
throw err;
} else {

View File

@ -17,7 +17,7 @@ var inputJpgWithExif = path.join(fixturesPath, "Landscape_8.jpg"); // https://gi
async.series([
// Resize with exact crop
function(done) {
sharp(inputJpg).resize(320, 240).write(outputJpg, function(err) {
sharp(inputJpg).resize(320, 240).toFile(outputJpg, function(err) {
if (err) throw err;
imagemagick.identify(outputJpg, function(err, features) {
if (err) throw err;
@ -29,7 +29,7 @@ async.series([
},
// Resize to fixed width
function(done) {
sharp(inputJpg).resize(320).write(outputJpg, function(err) {
sharp(inputJpg).resize(320).toFile(outputJpg, function(err) {
if (err) throw err;
imagemagick.identify(outputJpg, function(err, features) {
if (err) throw err;
@ -41,7 +41,7 @@ async.series([
},
// Resize to fixed height
function(done) {
sharp(inputJpg).resize(null, 320).write(outputJpg, function(err) {
sharp(inputJpg).resize(null, 320).toFile(outputJpg, function(err) {
if (err) throw err;
imagemagick.identify(outputJpg, function(err, features) {
if (err) throw err;
@ -53,7 +53,7 @@ async.series([
},
// Identity transform
function(done) {
sharp(inputJpg).write(outputJpg, function(err) {
sharp(inputJpg).toFile(outputJpg, function(err) {
if (err) throw err;
imagemagick.identify(outputJpg, function(err, features) {
if (err) throw err;
@ -65,7 +65,7 @@ async.series([
},
// Upscale
function(done) {
sharp(inputJpg).resize(3000).write(outputJpg, function(err) {
sharp(inputJpg).resize(3000).toFile(outputJpg, function(err) {
if (err) throw err;
imagemagick.identify(outputJpg, function(err, features) {
if (err) throw err;
@ -91,7 +91,7 @@ async.series([
},
// TIFF with dimensions known to cause rounding errors
function(done) {
sharp(inputTiff).resize(240, 320).embedBlack().write(outputJpg, function(err) {
sharp(inputTiff).resize(240, 320).embedBlack().toFile(outputJpg, function(err) {
if (err) throw err;
imagemagick.identify(outputJpg, function(err, features) {
if (err) throw err;
@ -102,7 +102,7 @@ async.series([
});
},
function(done) {
sharp(inputTiff).resize(240, 320).write(outputJpg, function(err) {
sharp(inputTiff).resize(240, 320).toFile(outputJpg, function(err) {
if (err) throw err;
imagemagick.identify(outputJpg, function(err, features) {
if (err) throw err;
@ -114,7 +114,7 @@ async.series([
},
// Resize to max width or height considering ratio (landscape)
function(done) {
sharp(inputJpg).resize(320, 320).max().write(outputJpg, function(err) {
sharp(inputJpg).resize(320, 320).max().toFile(outputJpg, function(err) {
if (err) throw err;
imagemagick.identify(outputJpg, function(err, features) {
if (err) throw err;
@ -126,7 +126,7 @@ async.series([
},
// Resize to max width or height considering ratio (portrait)
function(done) {
sharp(inputTiff).resize(320, 320).max().write(outputJpg, function(err) {
sharp(inputTiff).resize(320, 320).max().toFile(outputJpg, function(err) {
if (err) throw err;
imagemagick.identify(outputJpg, function(err, features) {
if (err) throw err;
@ -138,7 +138,7 @@ async.series([
},
// Attempt to resize to max but only provide one dimension, so should default to crop
function(done) {
sharp(inputJpg).resize(320).max().write(outputJpg, function(err) {
sharp(inputJpg).resize(320).max().toFile(outputJpg, function(err) {
if (err) throw err;
imagemagick.identify(outputJpg, function(err, features) {
if (err) throw err;
@ -150,14 +150,14 @@ async.series([
},
// Attempt to output to input, should fail
function(done) {
sharp(inputJpg).write(inputJpg, function(err) {
sharp(inputJpg).toFile(inputJpg, function(err) {
assert(!!err);
done();
});
},
// Rotate by 90 degrees, respecting output input size
function(done) {
sharp(inputJpg).rotate(90).resize(320, 240).write(outputJpg, function(err) {
sharp(inputJpg).rotate(90).resize(320, 240).toFile(outputJpg, function(err) {
if (err) throw err;
imagemagick.identify(outputJpg, function(err, features) {
if (err) throw err;
@ -169,7 +169,7 @@ async.series([
},
// Input image has Orientation EXIF tag but do not rotate output
function(done) {
sharp(inputJpgWithExif).resize(320).write(outputJpg, function(err) {
sharp(inputJpgWithExif).resize(320).toFile(outputJpg, function(err) {
if (err) throw err;
imagemagick.identify(outputJpg, function(err, features) {
if (err) throw err;
@ -181,7 +181,7 @@ async.series([
},
// Input image has Orientation EXIF tag value of 8 (270 degrees), auto-rotate
function(done) {
sharp(inputJpgWithExif).rotate().resize(320).write(outputJpg, function(err) {
sharp(inputJpgWithExif).rotate().resize(320).toFile(outputJpg, function(err) {
if (err) throw err;
imagemagick.identify(outputJpg, function(err, features) {
if (err) throw err;
@ -193,7 +193,7 @@ async.series([
},
// Attempt to auto-rotate using image that has no EXIF
function(done) {
sharp(inputJpg).rotate().resize(320).write(outputJpg, function(err) {
sharp(inputJpg).rotate().resize(320).toFile(outputJpg, function(err) {
if (err) throw err;
imagemagick.identify(outputJpg, function(err, features) {
if (err) throw err;