question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

An issue piping to a readStream.

See original GitHub issue

Hi,

First of all - thanks for the amazing tool.

@lovell can you help me figure out why this doesn’t work, please?

  const imageProcessor = sharp();
  imageProcessor
    .on("info", function (metadata) {
      var width = Math.min (1080, metadata.width),
        height = (width / metadata.width) * metadata.height,
        resizeArgs = {
          width: width,
          height: height,
          fit: sharp.fit.cover,
          position: sharp.strategy.entropy
        };
      console.log (`Resizing image with opts: ${JSON.stringify (resizeArgs, null, 4)}`);

      return sharp ()
        .rotate ()
        .resize (null, null, resizeArgs)
        .jpeg();
    });

  var readableStream = fs.createReadStream(stream.path)
    .pipe (imageProcessor);

I want to

  • rotate to proper orientation (stripping the EXIF tag);
  • resizing to maximum 1080px-wide image and keep the original size if the width is smaller than that (while keeping the aspect ratio);
  • and encode as jpeg.

When doing this same thing without the metadata - it works like a charm. But since there is no max-width setting, I need to conditionally resize according to size and when I try to use the metadata somehow the image gets smaller in size, but doesn’t get rotated or resized to new dimensions…

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
lovellcommented, Oct 9, 2018

Please see #236 for a possible future enhancement that would allow this.

0reactions
nicrotocommented, Oct 10, 2018

Thanks for the help, @lovell. Cheers!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Streams, Piping, and Their Error Handling in Node.js - Medium
This simply means the streaming data from the readStream will be piped and passed through the res write stream.
Read more >
How to use stream.pipe - Node.js
pipe () is file streams. In Node.js, fs.createReadStream() and fs.createWriteStream() are used to create a stream to an open file descriptor.
Read more >
Javascript/node : Problem using pipe method from a ...
I wanted to pass a text file's content to the server via the pipe method via the following code. const {createServer}=require('http'); ...
Read more >
fs.ReadStream.pipe JavaScript and Node.js code examples
Best JavaScript code snippets using fs.ReadStream.pipe(Showing top 15 results out of 1,629).
Read more >
Use pipeline instead of pipe - DEV Community ‍ ‍
Above is a simple example shows that we use pipe to transfer the data from the read stream to the write stream. However...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found