From d3225fa193fb5b7e8e81f7cfe84bc2d05a4205f5 Mon Sep 17 00:00:00 2001 From: Lovell Fuller Date: Mon, 15 Dec 2014 13:54:19 +0000 Subject: [PATCH] Add 'size' attribute to callback's info Object #138 --- README.md | 4 +-- src/resize.cc | 13 +++++++--- test/unit/io.js | 66 ++++++++++++++++++++++++++++++++----------------- 3 files changed, 54 insertions(+), 29 deletions(-) diff --git a/README.md b/README.md index 03e115fc..c0c998a9 100755 --- a/README.md +++ b/README.md @@ -429,7 +429,7 @@ An advanced setting to disable adaptive row filtering for the lossless PNG outpu `callback`, if present, is called with two arguments `(err, info)` where: * `err` contains an error message, if any. -* `info` contains the output image `format`, `width` and `height`. +* `info` contains the output image `format`, `size` (bytes), `width` and `height`. A Promises/A+ promise is returned when `callback` is not provided. @@ -441,7 +441,7 @@ Write image data to a Buffer, the format of which will match the input image by * `err` is an error message, if any. * `buffer` is the output image data. -* `info` contains the output image `format`, `width` and `height`. +* `info` contains the output image `format`, `size` (bytes), `width` and `height`. A Promises/A+ promise is returned when `callback` is not provided. diff --git a/src/resize.cc b/src/resize.cc index dccc16ee..34ca878a 100755 --- a/src/resize.cc +++ b/src/resize.cc @@ -717,16 +717,21 @@ class ResizeWorker : public NanAsyncWorker { // Info Object Local info = NanNew(); info->Set(NanNew("format"), NanNew(baton->outputFormat)); - info->Set(NanNew("width"), NanNew(width)); - info->Set(NanNew("height"), NanNew(height)); + info->Set(NanNew("width"), NanNew(width)); + info->Set(NanNew("height"), NanNew(height)); if (baton->bufferOutLength > 0) { - // Buffer + // Copy data to new Buffer argv[1] = NanNewBufferHandle(static_cast(baton->bufferOut), baton->bufferOutLength); g_free(baton->bufferOut); + // Add buffer size to info + info->Set(NanNew("size"), NanNew(baton->bufferOutLength)); argv[2] = info; } else { - // File + // Add file size to info + struct stat st; + g_stat(baton->output.c_str(), &st); + info->Set(NanNew("size"), NanNew(st.st_size)); argv[1] = info; } } diff --git a/test/unit/io.js b/test/unit/io.js index a35ec179..75c22711 100755 --- a/test/unit/io.js +++ b/test/unit/io.js @@ -18,6 +18,7 @@ describe('Input/output', function() { sharp(fixtures.outputJpg).toBuffer(function(err, data, info) { if (err) throw err; assert.strictEqual(true, data.length > 0); + assert.strictEqual(data.length, info.size); assert.strictEqual('jpeg', info.format); assert.strictEqual(320, info.width); assert.strictEqual(240, info.height); @@ -35,6 +36,7 @@ describe('Input/output', function() { sharp(fixtures.outputJpg).toBuffer(function(err, data, info) { if (err) throw err; assert.strictEqual(true, data.length > 0); + assert.strictEqual(data.length, info.size); assert.strictEqual('jpeg', info.format); assert.strictEqual(320, info.width); assert.strictEqual(240, info.height); @@ -49,6 +51,7 @@ describe('Input/output', function() { var readable = fs.createReadStream(fixtures.inputJpg); var pipeline = sharp().resize(320, 240).toFile(fixtures.outputJpg, function(err, info) { if (err) throw err; + assert.strictEqual(true, info.size > 0); assert.strictEqual('jpeg', info.format); assert.strictEqual(320, info.width); assert.strictEqual(240, info.height); @@ -63,6 +66,7 @@ describe('Input/output', function() { var pipeline = sharp().resize(320, 240).toBuffer(function(err, data, info) { if (err) throw err; assert.strictEqual(true, data.length > 0); + assert.strictEqual(data.length, info.size); assert.strictEqual('jpeg', info.format); assert.strictEqual(320, info.width); assert.strictEqual(240, info.height); @@ -90,6 +94,7 @@ describe('Input/output', function() { sharp(fixtures.outputJpg).toBuffer(function(err, data, info) { if (err) throw err; assert.strictEqual(true, data.length > 0); + assert.strictEqual(data.length, info.size); assert.strictEqual('jpeg', info.format); assert.strictEqual(320, info.width); assert.strictEqual(240, info.height); @@ -137,6 +142,7 @@ describe('Input/output', function() { .toBuffer(function(err, data, info) { if (err) throw err; assert.strictEqual(true, data.length > 0); + assert.strictEqual(data.length, info.size); assert.strictEqual('jpeg', info.format); assert.strictEqual(320, info.width); assert.strictEqual(240, info.height); @@ -151,6 +157,7 @@ describe('Input/output', function() { .toBuffer(function(err, data, info) { if (err) throw err; assert.strictEqual(true, data.length > 0); + assert.strictEqual(data.length, info.size); assert.strictEqual('jpeg', info.format); assert.strictEqual(320, info.width); assert.strictEqual(240, info.height); @@ -221,6 +228,7 @@ describe('Input/output', function() { sharp(data).toBuffer(function(err, data, info) { if (err) throw err; assert.strictEqual(true, data.length > 0); + assert.strictEqual(data.length, info.size); assert.strictEqual('jpeg', info.format); assert.strictEqual(320, info.width); assert.strictEqual(240, info.height); @@ -262,21 +270,23 @@ describe('Input/output', function() { .resize(320, 240) .png() .progressive(false) - .toBuffer(function(err, nonProgressive, info) { + .toBuffer(function(err, nonProgressiveData, nonProgressiveInfo) { if (err) throw err; - assert.strictEqual(true, nonProgressive.length > 0); - assert.strictEqual('png', info.format); - assert.strictEqual(320, info.width); - assert.strictEqual(240, info.height); - sharp(nonProgressive) + assert.strictEqual(true, nonProgressiveData.length > 0); + assert.strictEqual(nonProgressiveData.length, nonProgressiveInfo.size); + assert.strictEqual('png', nonProgressiveInfo.format); + assert.strictEqual(320, nonProgressiveInfo.width); + assert.strictEqual(240, nonProgressiveInfo.height); + sharp(nonProgressiveData) .progressive() - .toBuffer(function(err, progressive, info) { + .toBuffer(function(err, progressiveData, progressiveInfo) { if (err) throw err; - assert.strictEqual(true, progressive.length > 0); - assert.strictEqual(true, progressive.length > nonProgressive.length); - assert.strictEqual('png', info.format); - assert.strictEqual(320, info.width); - assert.strictEqual(240, info.height); + assert.strictEqual(true, progressiveData.length > 0); + assert.strictEqual(progressiveData.length, progressiveInfo.size); + assert.strictEqual(true, progressiveData.length > nonProgressiveData.length); + assert.strictEqual('png', progressiveInfo.format); + assert.strictEqual(320, progressiveInfo.width); + assert.strictEqual(240, progressiveInfo.height); done(); }); }); @@ -287,6 +297,7 @@ describe('Input/output', function() { it('JPEG', function(done) { sharp(fixtures.inputJpg).resize(320, 80).toFile(fixtures.outputZoinks, function(err, info) { if (err) throw err; + assert.strictEqual(true, info.size > 0); assert.strictEqual('jpeg', info.format); assert.strictEqual(320, info.width); assert.strictEqual(80, info.height); @@ -298,6 +309,7 @@ describe('Input/output', function() { it('PNG', function(done) { sharp(fixtures.inputPng).resize(320, 80).toFile(fixtures.outputZoinks, function(err, info) { if (err) throw err; + assert.strictEqual(true, info.size > 0); assert.strictEqual('png', info.format); assert.strictEqual(320, info.width); assert.strictEqual(80, info.height); @@ -309,6 +321,7 @@ describe('Input/output', function() { it('Transparent PNG', function(done) { sharp(fixtures.inputPngWithTransparency).resize(320, 80).toFile(fixtures.outputZoinks, function(err, info) { if (err) throw err; + assert.strictEqual(true, info.size > 0); assert.strictEqual('png', info.format); assert.strictEqual(320, info.width); assert.strictEqual(80, info.height); @@ -319,6 +332,7 @@ describe('Input/output', function() { it('WebP', function(done) { sharp(fixtures.inputWebP).resize(320, 80).toFile(fixtures.outputZoinks, function(err, info) { if (err) throw err; + assert.strictEqual(true, info.size > 0); assert.strictEqual('webp', info.format); assert.strictEqual(320, info.width); assert.strictEqual(80, info.height); @@ -330,6 +344,7 @@ describe('Input/output', function() { it('TIFF', function(done) { sharp(fixtures.inputTiff).resize(320, 80).toFile(fixtures.outputZoinks, function(err, info) { if (err) throw err; + assert.strictEqual(true, info.size > 0); assert.strictEqual('tiff', info.format); assert.strictEqual(320, info.width); assert.strictEqual(80, info.height); @@ -375,23 +390,25 @@ describe('Input/output', function() { sharp(fixtures.inputPng) .resize(320, 240) .withoutAdaptiveFiltering(false) - .toBuffer(function(err, dataAdaptive, info) { + .toBuffer(function(err, adaptiveData, adaptiveInfo) { if (err) throw err; - assert.strictEqual(true, dataAdaptive.length > 0); - assert.strictEqual('png', info.format); - assert.strictEqual(320, info.width); - assert.strictEqual(240, info.height); + assert.strictEqual(true, adaptiveData.length > 0); + assert.strictEqual(adaptiveData.length, adaptiveInfo.size); + assert.strictEqual('png', adaptiveInfo.format); + assert.strictEqual(320, adaptiveInfo.width); + assert.strictEqual(240, adaptiveInfo.height); // Then generate without sharp(fixtures.inputPng) .resize(320, 240) .withoutAdaptiveFiltering() - .toBuffer(function(err, dataWithoutAdaptive, info) { + .toBuffer(function(err, withoutAdaptiveData, withoutAdaptiveInfo) { if (err) throw err; - assert.strictEqual(true, dataWithoutAdaptive.length > 0); - assert.strictEqual('png', info.format); - assert.strictEqual(320, info.width); - assert.strictEqual(240, info.height); - assert.strictEqual(true, dataWithoutAdaptive.length < dataAdaptive.length); + assert.strictEqual(true, withoutAdaptiveData.length > 0); + assert.strictEqual(withoutAdaptiveData.length, withoutAdaptiveInfo.size); + assert.strictEqual('png', withoutAdaptiveInfo.format); + assert.strictEqual(320, withoutAdaptiveInfo.width); + assert.strictEqual(240, withoutAdaptiveInfo.height); + assert.strictEqual(true, withoutAdaptiveData.length < adaptiveData.length); done(); }); }); @@ -406,6 +423,7 @@ describe('Input/output', function() { .png() .toFile(fixtures.path('output.svg.png'), function(err, info) { if (err) throw err; + assert.strictEqual(true, info.size > 0); assert.strictEqual('png', info.format); assert.strictEqual(100, info.width); assert.strictEqual(100, info.height); @@ -419,6 +437,7 @@ describe('Input/output', function() { .png() .toFile(fixtures.path('output.psd.png'), function(err, info) { if (err) throw err; + assert.strictEqual(true, info.size > 0); assert.strictEqual('png', info.format); assert.strictEqual(320, info.width); assert.strictEqual(240, info.height); @@ -435,6 +454,7 @@ describe('Input/output', function() { .toBuffer(function(err, data, info) { if (err) throw err; assert.strictEqual(true, data.length > 0); + assert.strictEqual(data.length, info.size); assert.strictEqual('jpeg', info.format); assert.strictEqual(320, info.width); assert.strictEqual(240, info.height);