Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Achieve a target size by combination of resize and reduce #168

Open
scottybo opened this issue Oct 12, 2018 · 1 comment
Open

Achieve a target size by combination of resize and reduce #168

scottybo opened this issue Oct 12, 2018 · 1 comment

Comments

@scottybo
Copy link

scottybo commented Oct 12, 2018

I run a social media scheduling tool and Twitter requires that images don't exceed 524kb.

Is it possible, using this library, to automatically create the "perfect" combination of scaling and quality to achieve a target size of 524kb?

For example, if an image is 10,000 pixels wide and is 10mb and optimising it to a combination 2,000 pixels wide and 90% quality would make it 523kb then we go with that.

The preference would be a minimum of 90% quality and as high a resolution as possible.

Any ideas?

@ysugimoto
Copy link
Owner

Hi @scottybo

I think it is difficult to decide the best combination of resizing and reducing by each image.

However, this processor treats an image as Buffer, so you can confirm image size easily. So probably you can try to resize/reduce with several different parameters in the same process and choose the best result from theirs.

For example of resize: https://github.com/ysugimoto/aws-lambda-image/blob/master/lib/ImageResizer.js#L31

// try to resize with three of paramters
return Promise.all([
  new ImageResizer(Object.assign({}, this.options, {quality: n1}).exec(image),
  new ImageResizer(Object.assign({}, this.options, {quality: n2}).exec(image),
  new ImageResizer(Object.assign({}, this.options, {quality: n3}).exec(image)
]).then(results => {
  // choose the best result from three of result by looking `Buffer.byteLenth(image.data)`
});

You can do it on reducing image as same as above.
But of course, Lambda will use more memory for multiple processing, so you need to consider about it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants