sharp icon indicating copy to clipboard operation
sharp copied to clipboard

Can't process data from inside metadata call-back

Open jobe451 opened this issue 11 years ago • 6 comments

I would like make some processing dependend on image metadata (i.e. based on the current image size). However, currently I fail at doing so:

This code does what one would expect. It receives a jpeg-stream (mstream) and returns that stream to a web-browser by piping the processing result to self.res. However, this code does not allow me to be dependend on metadata, since the transformations get triggered, before the metadata is ready.

            var pipeline = sharp();
            pipeline.metadata(function(metadata){
                console.log(metadata);
            }).resize(400, 300).pipe(self.res);
            mstream.pipe(pipeline);

Based on the few examples I saw I would have expected that the snippet below will do the trick. The code executes with no errors. However, the code does not start to stream anything to the web-response.

            var pipeline = sharp();
            pipeline.metadata(function(metadata){
                console.log(metadata);
                pipeline.resize(400, 300).pipe(self.res);
            });
            mstream.pipe(pipeline);

How can I get this to work?

jobe451 avatar Nov 25 '15 02:11 jobe451

I believe you could do this:

        var pipeline = sharp();
        pipeline.clone().metadata(function(metadata){
            console.log(metadata);
            pipeline.resize(400, 300).pipe(self.res);
        });
        mstream.pipe(pipeline);

On Tue, Nov 24, 2015 at 6:06 PM, jobe451 [email protected] wrote:

I would like make some processing dependend on image metadata (i.e. based on the current image size). However, currently I fail at doing so:

This code does what one would expect. It receives a jpeg-stream (mstream) and returns that stream to a web-browser by piping the processing result to self.res. However, this code does not allow me to be dependend on metadata, since the transformations get triggered, before the metadata is ready.

        var pipeline = sharp();
        pipeline.metadata(function(metadata){
            console.log(metadata);
        }).resize(400, 300).pipe(self.res);
        mstream.pipe(pipeline);

Based on the few examples I saw I would have expected that the snippet below will do the trick. The code executes with no errors. However, the code does not start to stream anything to the web-response.

        var pipeline = sharp();
        pipeline.metadata(function(metadata){
            console.log(metadata);
            pipeline.resize(400, 300).pipe(self.res);
        });
        mstream.pipe(pipeline);

How can I get this to work?

— Reply to this email directly or view it on GitHub https://github.com/lovell/sharp/issues/314.

dacarley avatar Nov 25 '15 04:11 dacarley

In addition, #236 should reduce the amount of "boilerplate" code needed to achieve this.

lovell avatar Nov 25 '15 10:11 lovell

@dacarley Thanks for the hint. Unfortunately it didn't work. I also tried a variation with two clones, that also did not work:

            var pipeline = sharp();
            pipeline.clone().metadata(function(err, metadata){
                console.log(metadata);
                pipeline.clone().resize(400, 300).pipe(self.res);
            });
            mstream.pipe(pipeline);

@lovell #236 looks very nice. I guess this would do the trick in the future.

For now I will fall back to something less elegant to get my project progressing.

jobe451 avatar Nov 26 '15 00:11 jobe451

@jobe451 Quick thought: perhaps take the clone() outside the metadata callback and cork() the pipeline that will be used to resize, then uncork() it within the callback after updating options.

lovell avatar Nov 26 '15 09:11 lovell

Thanks for the hint, but still no luck

I tried and failed with this (using two variations the one below and one that has switched positions for resize() and uncork()):

            var pipeline = sharp();
            var pipeline_c1 = pipeline.clone();
            var pipeline_c2 = pipeline.clone();
            pipeline_c2.cork();
            pipeline_c1.metadata(function(err, metadata){
                console.log(metadata);
                pipeline_c2.resize(400, 300).pipe(self.res);
                pipeline_c2.uncork();
            });
            mstream.pipe(pipeline);

I did a variation of this, just to proof that without the metadata callback it would stil work. This did work, is missing the metadata though:

            var pipeline = sharp();
            var pipeline_c2 = pipeline.clone();
            pipeline_c2.cork();
            pipeline_c2.resize(400, 300).pipe(self.res);
            pipeline_c2.uncork();
            mstream.pipe(pipeline);

For now I did get my code working by starting two dedicated read-streams and using two dedicated pipelines (one outside the metadata cb, one inside). Probably a bit less efficient but working.

jobe451 avatar Nov 27 '15 00:11 jobe451

Thanks for the update. I'll close this as #236 should provide a more elegant solution.

lovell avatar Dec 12 '15 11:12 lovell