Giter VIP home page Giter VIP logo

Comments (17)

EyalAr avatar EyalAr commented on August 15, 2024

@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.

gkaimakas avatar gkaimakas commented on August 15, 2024

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.

 avatar commented on August 15, 2024

+1, I would really like to see a promises implementation

from lwip.

gkaimakas avatar gkaimakas commented on August 15, 2024

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.

raine avatar raine commented on August 15, 2024

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.

EyalAr avatar EyalAr commented on August 15, 2024

@raine great, looks good!

from lwip.

raine avatar raine commented on August 15, 2024

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.

cookch10 avatar cookch10 commented on August 15, 2024

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.

nkt avatar nkt commented on August 15, 2024

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.

felixfbecker avatar felixfbecker commented on August 15, 2024

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.

felixfbecker avatar felixfbecker commented on August 15, 2024

Maybe we can add @raine's working example to readme?

from lwip.

RobertHerhold avatar RobertHerhold commented on August 15, 2024

@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.

felixfbecker avatar felixfbecker commented on August 15, 2024

@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.

RobertHerhold avatar RobertHerhold commented on August 15, 2024

@felixfbecker Thanks for the quick response. Turns out my issue was more of a poor understanding of LWIP's API than anything.

from lwip.

RobertHerhold avatar RobertHerhold commented on August 15, 2024

@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.

felixfbecker avatar felixfbecker commented on August 15, 2024

@RobertHerhold that has nothing to do with promises, please ask this on stackoverflow

from lwip.

jpdevries avatar jpdevries commented on August 15, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.