Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(docs): update description to use buffer instead of pipe #17

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
164 changes: 140 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down