Achieve a target size by combination of resize and reduce
scottybo opened this issue · 1 comments
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?
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.