pick a package, like sharp and three lines of code https://www.npmjs.com/package/sharp
#How do I resize image before uploading to Cloudinary
1 messages · Page 1 of 1 (latest)
sharp('input.jpg')
.rotate()
.resize(200)
.jpeg({ mozjpeg: true })
.toBuffer()
.then( data => { ... })
.catch( err => { ... });
Is it possible to resize based on data Uint8Array?
I want to resize around this area
const uploadHandler = unstable_composeUploadHandlers(
// our custom upload handler
async ({ name, contentType, data, filename }) => {
// Can I resize it here?
async function uploadImageToCloudinary(data: AsyncIterable<Uint8Array>) {
let randomFolder = (Math.random() + 1).toString(36).substring(5);
const imageStream = new Readable({
objectMode: true,
read() {
this.push(data);
this.push(null);
},
});
const resizedImageStream = imageStream.pipe(
sharp().resize({ width: 800, height: 600 })
);
const resizedImageBuffer = await resizedImageStream.toBuffer();