diff --git a/README.md b/README.md index 8d2281f..1fc04f2 100644 --- a/README.md +++ b/README.md @@ -70,47 +70,163 @@ var fs = require('fs') var imaginary = require('imaginary') var serverUrl = 'http://imaginary.company.net' -imaginary('image.jpg') - .server(serverUrl) - .crop({ widht: 200, height: 200 }) - .on('error', function (err) { - console.error('Cannot resize the image:', err) +createImaginaryBuffer() + .then(function (buffer) { + // do stuff with buffer }) - .pipe(fs.createWriteStream('out.jpg')) + .catch(function (error) => { + // handle error +}); + +function createImaginaryBuffer() { + return new Promise(function (resolve, reject) { + var thumbnailBufferChunks = []; + var thumbnailBufferChunksLength = 0; + + imaginary('image.jpg') + .server(serverUrl) + .crop({ width: 200, height: 200 }) + .on('error', function (err) { + reject(err); + }) + .on('data', function (chunk) { + thumbnailBufferChunks.push(chunk); + thumbnailBufferChunksLength += chunk.length; + }) + .on('end', function () { + var imageBuffer = Buffer.alloc(thumbnailBufferChunksLength); + var chunkIterator = 0; + for (var chunk of thumbnailBufferChunks) { + chunk.copy(imageBuffer, chunkIterator, 0, chunk.length); + chunkIterator += chunk.length; + } + resolve(imageBuffer); + }); + } ``` Take an image from remote URL (will stream it from the client to the server): ```js -imaginary('http://myhosting.com/image.jpg') - .server('http://imaginary.server.net') - .crop({ width: 800, height: 600 }) - .on('error', function (err) { - console.error('Cannot resize the image:', err) +createImaginaryBuffer() + .then(function (buffer) { + // do stuff with buffer }) - .pipe(fs.createWriteStream('out.jpg')) + .catch(function (error) => { + // handle error +}); + +function createImaginaryBuffer() { + return new Promise(function (resolve, reject) { + var thumbnailBufferChunks = []; + var thumbnailBufferChunksLength = 0; + + imaginary('http://myhosting.com/image.jpg') + .server('http://imaginary.server.net') + .crop({ width: 800, height: 600 }) + .on('error', function (err) { + console.error('Cannot resize the image:', err) + }) + .on('error', function (err) { + reject(err); + }) + .on('data', function (chunk) { + thumbnailBufferChunks.push(chunk); + thumbnailBufferChunksLength += chunk.length; + }) + .on('end', function () { + var imageBuffer = Buffer.alloc(thumbnailBufferChunksLength); + var chunkIterator = 0; + for (var chunk of thumbnailBufferChunks) { + chunk.copy(imageBuffer, chunkIterator, 0, chunk.length); + chunkIterator += chunk.length; + } + resolve(imageBuffer); + }); + } +} ``` Take an image as readable stream: ```js -imaginary(fs.createReadStream('image.jpg')) - .server('http://imaginary.server.net') - .rotate({ rotate: 180 }) - .on('error', function (err) { - console.error('Cannot resize the image:', err) +createImaginaryBuffer() + .then(function (buffer) { + // do stuff with buffer }) - .pipe(fs.createWriteStream('out.jpg')) + .catch(function (error) => { + // handle error +}); + +function createImaginaryBuffer() { + return new Promise(function (resolve, reject) { + var thumbnailBufferChunks = []; + var thumbnailBufferChunksLength = 0; + + imaginary(fs.createReadStream('image.jpg')) + .server('http://imaginary.server.net') + .rotate({ rotate: 180 }) + .on('error', function (err) { + reject(err); + }) + .on('data', function (chunk) { + thumbnailBufferChunks.push(chunk); + thumbnailBufferChunksLength += chunk.length; + }) + .on('end', function () { + var imageBuffer = Buffer.alloc(thumbnailBufferChunksLength); + var chunkIterator = 0; + for (var chunk of thumbnailBufferChunks) { + chunk.copy(imageBuffer, chunkIterator, 0, chunk.length); + chunkIterator += chunk.length; + } + resolve(imageBuffer); + }); + } +} ``` Resize by URL without streaming it on the client first. Requires passing the `-enable-url-source` flag to `imaginary`. ```js -imaginary() - .server('http://imaginary.server.net') - .rotate({ rotate: 180, url: 'http://placehold.it/350x150' }) - .on('error', function (err) { - console.error('Cannot resize the image:', err) +createImaginaryBuffer() + .then(function (buffer) { + // do stuff with buffer }) - .pipe(fs.createWriteStream('out.jpg')) + .catch(function (error) => { + // handle error +}); + +function createImaginaryBuffer() { + return new Promise(function (resolve, reject) { + var thumbnailBufferChunks = []; + var thumbnailBufferChunksLength = 0; + + imaginary() + .server('http://imaginary.server.net') + .rotate({ rotate: 180, url: 'http://placehold.it/350x150' }) + .on('error', function (err) { + console.error('Cannot resize the image:', err) + }) + .on('error', function (err) { + console.error('Cannot resize the image:', err) + }) + .on('error', function (err) { + resolve(err); + }) + .on('data', function (chunk) { + thumbnailBufferChunks.push(chunk); + thumbnailBufferChunksLength += chunk.length; + }) + .on('end', function () { + var imageBuffer = Buffer.alloc(thumbnailBufferChunksLength); + var chunkIterator = 0; + for (var chunk of thumbnailBufferChunks) { + chunk.copy(imageBuffer, chunkIterator, 0, chunk.length); + chunkIterator += chunk.length; + } + resolve(imageBuffer); + }); + } +} ``` ### Supported params