Skip to content

Instantly share code, notes, and snippets.

@marcel-ploch
Created May 3, 2019 14:15
Show Gist options
  • Save marcel-ploch/ce5f05bb9245713e765e5238a3fc2ade to your computer and use it in GitHub Desktop.
Save marcel-ploch/ce5f05bb9245713e765e5238a3fc2ade to your computer and use it in GitHub Desktop.
'use strict';
const Jimp = require('jimp');
const AWS = require('aws-sdk');
const {GifCodec, BitmapImage, GifFrame, GifUtil} = require('gifwrap');
// All Dokumentation can be found here
// https://github.com/oliver-moran/jimp/tree/master/packages/jimp
const repsonseBody = {
origUrl: null,
modifiedUrl: null
};
module.exports.imageOptimization = async(event) => {
const data = JSON.parse(event.body);
AWS.config.update({
//Your AWS Config here
});
AWS.config.apiVersions = {
s3: '2006-03-01',
// other service API versions
};
// Parse incming Data
let imageType = '', imageBuffer = null, imageBufferJimp = null,
imageData = null, image = null;
if (data && data.image) {
// Bild gefunden
imageData = data.image.slice(data.image.indexOf('base64,') + 7);
imageBuffer = Buffer.from(imageData, 'base64');
if (data.image.indexOf('png;') !== -1 || data.image.indexOf('jpeg;') !==
-1) {
imageType = data.image.indexOf('png') !== -1 ? 'png' : 'jpeg';
image = await Jimp.read(imageBuffer);
imageBufferJimp = await image.quality(25).getBufferAsync(Jimp.AUTO);
if (imageBuffer) {
// Upload Original Image
await uploadData(
{
Bucket: '',
Key: data.imageName + '_orig.' + imageType,
Body: imageBuffer,
ACL: 'public-read'
},
'origUrl');
}
if (imageBufferJimp) {
// Upload Sharpened Image
await uploadData(
{
Bucket: '',
Key: data.imageName + '.' + imageType,
Body: imageBufferJimp,
ACL: 'public-read'
},
'modifiedUrl');
}
} else if (data.image.indexOf('gif') !== -1) {
// Here do GIF optimization
imageType = 'gif';
const inputGif = await GifUtil.read(imageBuffer);
const promises = [];
inputGif.frames.forEach((frame, index) => {
const p = new Promise(async(resolve, reject) => {
let jimpFrame;
if (frame.bitmap) {
try {
jimpFrame = await Jimp.read(frame.bitmap);
} catch (e) {
console.info(e);
reject();
}
if (jimpFrame) {
// Do Gif Manipulation here
await jimpFrame.quality(10).greyscale();
const bitmapImage = new BitmapImage(jimpFrame.bitmap);
GifUtil.quantizeDekker(bitmapImage, 32);
inputGif.frames[index] = new GifFrame(bitmapImage);
resolve();
}
}
});
promises.push(p);
});
await Promise.all(promises).then(async() => {
// Upload Original Image
await uploadData(
{
Bucket: '',
Key: data.imageName + '_orig.' + imageType,
Body: imageBuffer,
ACL: 'public-read'
},
'origUrl');
// Upload Sharpened Image
const gifCodec = new GifCodec();
const gif = await gifCodec.encodeGif(inputGif.frames);
await uploadData(
{
Bucket: '',
Key: data.imageName + '.' + imageType,
Body: gif.buffer,
ACL: 'public-read'
},
'modifiedUrl');
});
}
}
return {
statusCode: 200,
headers: {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*'
},
body: JSON.stringify(repsonseBody),
};
};
/**
* Upload Files to S3 here
*/
async function uploadData(options, type) {
const s3 = new AWS.S3();
await s3.upload(options).promise().then((data) => {
repsonseBody[type] = data['Location'];
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment