Comments (17)
@gkaimakas Promises support would indeed make a nice feature.
However, I'm not sure I want to incorporate it as part of the core module, as it would add much overhead.
Maybe a new lwip-promise
module is in order, which will wrap the original lwip
module with support for promises.
Also worth checking Bluebird's PromisifyAll
.
Something like this would be nice:
lwip.open('image.jpg')
.then(function(image){
return image.scale(0.5);
})
.then(function(image){
return image.rotate(45);
})
.catch(function(err){
// ...
});
I'm open for discussion on what's the best approach.
If anybody else would like support for promises please let me know here.
Thanks.
from lwip.
I was thinking exactly the same thing as the code you posted!
On Nov 15, 2014 12:27 PM, "Eyal Arubas" [email protected] wrote:
@gkaimakas https://github.com/gkaimakas Promises support would indeed
make a nice feature.
However, I'm not sure I want to incorporate it as part of the core module,
as it would add much overhead.Maybe a new lwip-promise module is in order, which will wrap the original
lwip module with support for promises.
Also worth checking Bluebird's PromisifyAll
https://github.com/petkaantonov/bluebird/blob/master/API.md#promisepromisifyallobject-target--object-options---object
.Something like this would be nice:
lwip.open('image.jpg')
.then(function(image){
return image.scale(0.5);
})
.then(function(image){
return image.rotate(45);
})
.catch(function(err){
// ...
});I'm open for discussion on what's the best approach.
If anybody else would like support for promises please let me know here.Thanks.
—
Reply to this email directly or view it on GitHub
#63 (comment).
from lwip.
+1, I would really like to see a promises implementation
from lwip.
I have tried to use promisifyAll from Bluebird to use promises on lwip but unfortunately it throughs error and I can't use it.
from lwip.
I just tried the following but can't get further.
var lwip = Promise.promisifyAll(require('lwip'));
Promise.promisifyAll(require('lwip/lib/Image').prototype);
lwip.openAsync(buf, 'png').then(function(image) {
image.toBufferAsync('gif').then(function(buffer) {
console.log(buffer);
});
})
Edit: updated with fixed code, I had the arguments wrong!
bluebird 2.4.2
lwip 0.0.6 branch
from lwip.
@raine great, looks good!
from lwip.
Working example:
var lwip = Promise.promisifyAll(require('lwip'));
Promise.promisifyAll(require('lwip/lib/Image').prototype);
Promise.promisifyAll(require('lwip/lib/Batch').prototype);
lwip.openAsync('image.jpg')
.then(function(image) {
return image.batch()
.rotate(45, 'white')
.scale(0.5)
.blur(5)
.writeFileAsync('output.jpg');
})
.catch(function(err) {
console.log('something went wrong', err);
});
Another example without batch()
:
lwip.openAsync('image.jpg')
.call('rotateAsync', 45)
.call('scaleAsync', 0.5)
.call('writeFileAsync', 'output.jpg')
.catch(function(err) {
console.log('something went wrong', err);
});
Hope this helps.
from lwip.
Without modifying the core library, I've used https://github.com/then/promise successfully to promisify multiple lwip image processing steps. Would be happy to create a Gist if there's any interest.
from lwip.
I've created package that implements this issue: https://github.com/nkt/node-lwip-promise.
It's successfully works in production right now. Thanks!
from lwip.
That would indeed be a very nice feature. The future is definitely moving towards promises, as they drive ES7 async/await functions, which you can use today with babel or co. I'm against wrapper modules because they might not be always up-to-date, so promisifyAll for now, but that Async suffix and the boilerplate is annoying.
This is an example taken from a tutorial from http://thejackalofjavascript.com/image-manipulation-node-js/ for a simple image manipulation. Look at the callback hell:
var lwip = require('lwip');
var lefty, righty;
lwip.open('images/portrait.jpg', function(err, image) {
var imgWidth = image.width(),
imgHeight = image.height();
image.crop(0, 0, (imgWidth / 2) - 1, imgHeight - 1, function(err, cropped) {
lefty = cropped;
lefty.writeFile('images/processed/portrait_lefty.jpg', function(err) {
if (err) throw err;
// re-opening the same image, coz the `image` is == `cropped`
// If we do not get a fresh copy, we will be modifing the cropped image :(
lwip.open('images/portrait.jpg', function(err, image) {
if (err) throw err;
var imgWidth = image.width(),
imgHeight = image.height();
image.crop(imgWidth / 2, 0, imgWidth - 1, imgHeight - 1, function(err, cropped) {
if (err) throw err;
righty = cropped;
righty.writeFile('images/processed/portrait_righty.jpg', function(err) {
if (err) throw err;
lwip.create(imgWidth, imgHeight, 'white', function(err, newImg) {
if (err) throw err;
newImg.paste(0, 0, righty, function(err, newImg) {
if (err) throw err;
newImg.paste(imgWidth / 2, 0, lefty, function(err, newImg) {
if (err) throw err;
newImg.border(18, 'gray', function(err, newImg) {
if (err) throw err;
newImg.writeFile('images/processed/portrait_new.jpg', function(err) {
if (err) throw err;
}); // write the final output
}); // add a border - for fun \O/
}); // paste lefty to the right part of the new image
}); // paste righty to the left part of the new image
}); // create a blank image
}); // write righty to file for reference
}); // righty creation
}); // re-open the same image
}); // write lefty to file for reference
}); // lefty creation
});
With promises or async/await that would be so much better.
from lwip.
Maybe we can add @raine's working example to readme?
from lwip.
@EyalAr
I seem to be unable to do any kind of promisification of LWIP due to decree. While using a promisified form of the open
function, I get this error:
Error: Unknown arguments configuration,images/myimg.png
Is there any way I can still use promises with LWIP?
from lwip.
@RobertHerhold this works for me:
const lwip = require('lwip')
Promise.promisifyAll(lwip)
Promise.promisifyAll(require('lwip/lib/Image').prototype)
Promise.promisifyAll(require('lwip/lib/Batch').prototype)
from lwip.
@felixfbecker Thanks for the quick response. Turns out my issue was more of a poor understanding of LWIP's API than anything.
from lwip.
@felixfbecker Ran into another issue. I have the following code:
return lwip.openAsync(inname)
.then(image => image.batch().resize(size, size, "nearest-neighbor").toBufferAsync('png'));
and I get this error:
Unhandled rejection Error: Unknown arguments configuration,200,200,nearest-neighbor
from lwip.
@RobertHerhold that has nothing to do with promises, please ask this on stackoverflow
from lwip.
setPixel
seems to be asynchronous. I want to draw a PNG from scratch and know when it is done. Would be great to be able to use Promise.all()
for this.
from lwip.
Related Issues (20)
- I cannot install css-sprite because lwip has a bug
- TIFF support
- Failed to install on Redhat7 HOT 1
- Installation Failed - Build issue HOT 18
- CPU Usage
- npm install --save @dadi/cdn is failing with error on windows 7 HOT 2
- Project dead? HOT 4
- browser version broken in this release
- Problem with build on node 8 HOT 3
- node-gyp rebuild HOT 7
- LWIP installation error HOT 6
- Installation failing on Cloud 9 Web IDE
- Installation error HOT 7
- Installation Error HOT 3
- can it classify image
- Virtual routing and Forwarding(VRF) integration in lwIP HOT 1
- Failed install HOT 3
- Cmake vs2019 build error
- dns HOT 1
- lwip ssh Remote port forwarding
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from lwip.