1

I am building an app in flutter where user can upload image of max-size 20mb and want image to be compressed before storing it on server side. Basically compression will be done at server site.

Ive tried sharp but it couldn't compressed even a single byte. Above is the code

module.exports = function resize(path, format, width, height) {
  const readStream = fs.createReadStream(path);
  let transform = sharp(path).png({
    compressionLevel: 9,
    adaptiveFiltering: true,
    force: true,
    quality: 80,
  }).withMetadata().toFile('output.png',function(err){
      console.log("error :- ",err);
  });

  return readStream;
};

Is image compression is possible using sharp?

Omatt
  • 8,564
  • 2
  • 42
  • 144
Uttam Pandey
  • 31
  • 2
  • 8
  • 1
    I've never seen an image library better or faster than Sharp. Not sure what you're expecting, considering you're still outputting a lossless PNG. – Brad May 01 '20 at 18:20
  • I want to compress an image. For eg uploaded image size is 8mb. I want to compress it to some extent let say to 6mb or less – Uttam Pandey May 01 '20 at 18:22
  • FYI, be careful with this question. Your subject title asking for a package that does image compression is off-topic here on stackoverflow (you can't specifically ask for software/library recommendations here). The content of your question asking about specific features of the library you're using is on-topic. I'd suggest you edit your subject title. – jfriend00 May 01 '20 at 18:24
  • 3
    @UttamPandey I think your expectations are not good. You can't just magically make something smaller without choosing a tradeoff. If your image content is a photo, you should use a codec designed for photos, like JPEG, which will compress the content much better. If you are compressing graphics, PNG tends to be better as it can encode contiguous blocks of the same color very well. If you require lossless compression, PNG is the way to go. If you need alpha channel, also PNG. If you're already choosing the right format, and the file is too big, all you can do is scale the image. – Brad May 01 '20 at 18:36
  • So what do you suggest which library should I use @brad – Uttam Pandey May 01 '20 at 18:56
  • 1
    @UttamPandey I already told you, Sharp is the best for these sorts of tasks. I'm telling you that you can't fix your assumptions about the physics of compressing an image by installing a JavaScript library. Follow my previous comment to ensure you're using the right format for your use case, whatever that use case is. And if you already are, then all you can do is resize your image. – Brad May 01 '20 at 18:58
  • Thank you @brad noted your point. – Uttam Pandey May 01 '20 at 19:03

0 Answers0