I'm using vite-react frontend with express back end. I'm trying to resize an image using sharp and then upload it to Cloudinary. So far no matter what I try the image never resizes and is always the same size as the original. I'm sending the image via API to the backend. This is my photo.routes.js file contents
const express = require("express");
const router = express.Router();
const { upload } = require("../config/cloudinary.config");
const sharp = require("sharp");
router.post("/", upload.single("photo"), async (req, res) => {
try {
const result = req.file;
const buffer = result.buffer;
const resizedImageBuffer = await sharp(buffer)
.resize({
width: 150,
height: 150,
fit: sharp.fit.inside,
withoutEnlargement: true,
})
.toBuffer();
const response = await cloudinary.v2.uploader.upload_stream({
resource_type: 'image',
folder: "user_profiles",
public_id: result.originalname,
overwrite: true,
}, (error, result) => {
if (error) {
console.log('Error uploading resized image:', error);
res.status(500).json({
message: "Error uploading resized image",
error,
});
} else {
res.status(200).json({
message: "Photo uploaded and resized successfully",
result,
});
}
}).end(resizedImageBuffer);
res.status(200).json({
message: "Photo uploaded and resized successfully",
result,
});
} catch (error) {
console.log(error.message);
console.log(error.stack);
res.status(500).json({
message: "Error uploading and resizing the photo",
error,
});
}
});
module.exports = router;
I'm not getting any errors in the server console. The photo appears in the correct folder in cloudinary. The link to the file saves in my Firebase and displays on my webpage. I've been trying this for the past 2 days. Any help would be greatly appreciated.