I am leveraging the AWS Lambda example script to resize a JPG image using Node.js and ImageMagick/GraphicsMagick libraries. I want to make a simple modification to convert the image from a JPG to a WebP format after the resize. (GraphicsMagick does not support WebP, but ImageMagick does, which is subclassed in the script). This should be possible with the following block of code, as per the example in the Buffers section here (which converts JPG to PNG).
gm('img.jpg')
.resize(100, 100)
.toBuffer('PNG',function (err, buffer) {
if (err) return handle(err);
console.log('done!');
})
When I run that block of code in my local Node.js installation (replacing PNG with WebP), it works.
When I modify the transform function (see below) of the AWS Lambda example script, however, and execute it on AWS, I receive the following error:
Unable to resize mybucket/104A0378.jpg and upload to mybucket_resized/resized-104A0378.jpg due to an error: Error: Stream yields empty buffer
Modified transform() function (see the line with 'webp'):
function tranform(response, next) {
gm(response.Body).size(function(err, size) {
// Infer the scaling factor to avoid stretching the image unnaturally.
var scalingFactor = Math.min(
MAX_WIDTH / size.width,
MAX_HEIGHT / size.height
);
var width = scalingFactor * size.width;
var height = scalingFactor * size.height;
// Transform the image buffer in memory.
this.resize(width, height)
.toBuffer('webp', function(err, buffer) {
if (err) {
next(err);
} else {
next(null, response.ContentType, buffer);
}
});
});
}
I realize that the response.ContentType is still equal to image/jpeg, but I don't think that is playing a role here. Also, I realize that I should probably convert to WebP before resizing but...baby steps!
Any ideas?
Aucun commentaire:
Enregistrer un commentaire