Created
July 6, 2018 18:46
-
-
Save ASteinheiser/31fcbe960914abfca0b1387338fbc76d to your computer and use it in GitHub Desktop.
An example of how to use Jimp to read a base64 image string, resize, removeExif data, then return as an image buffer.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let imageBuffer = Buffer.from(body.profilePicture.split(',')[1], 'base64'); | |
console.log(imageBuffer); | |
Jimp.read(imageBuffer, function(err, image) { | |
if (err) { | |
context.done(err, | |
prepareErrorResponse(response, 'Unable to remove EXIF data from profile picture')); | |
} | |
console.log('before transform: ', image); | |
image = image.resize(400, 400).exifRotate((err, exifRotatedData) => { | |
if (err) { | |
console.log(err); | |
context.done(err, | |
prepareErrorResponse(response, 'Unable to rotate profile picture')); | |
} | |
console.log('data after exif: ', exifRotatedData); | |
console.log('image after exif: ', image); | |
image.getBuffer(image.getMIME(), function(err, buffer) { | |
if (err) { | |
console.log(err); | |
context.done(err, | |
prepareErrorResponse(response, 'Unable to get buffer for profile picture')); | |
} | |
console.log('after transform: ', buffer); | |
// Setup s3 bucket params to save the profile picture | |
var params = { | |
Bucket: process.env.ProviderPhotoBucket, | |
Key: 'public/' + body.fileName, | |
Body: buffer | |
}; | |
console.log(params); | |
// Set/update the Agent's profile picture in s3 bucket | |
s3.putObject(params, function(err, object) { | |
if (err) { | |
console.log(err); | |
context.done(err, | |
prepareErrorResponse(response, 'Unable to upload profile picture')); | |
} | |
else { | |
// Profile picture was set/updated successfully | |
let fileName = body.fileName; | |
let userId = event.requestContext.identity.cognitoIdentityId; | |
// Update Agent's database item to reflect the url of | |
// the newly updated profile picture in s3 | |
updateDynamoUser({ profilePictureTag: JSON.parse(object.ETag), profilePicture: fileName, userId: userId }, Table, context, response, userValidator); | |
} | |
}); | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment