import { run, success, error, Task } from '@taskdaemon/handler';
import sharp from 'sharp';
import axios from 'axios';
async function resizeImage(task: Task) {
try {
const { image_url, width, height } = task.task_data as {
image_url: string;
width: number;
height: number;
};
// Download image
const response = await axios.get(image_url, { responseType: 'arraybuffer' });
// Resize
const resized = await sharp(response.data)
.resize(width, height)
.png()
.toBuffer();
// Encode
const b64 = resized.toString('base64');
return success({
data: b64,
size: [width, height],
format: 'png'
});
} catch (e) {
return error(String(e));
}
}
run(resizeImage);